2 * Copyright (c) 1995-1999, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
7 * Revision 1.17 2002-04-04 20:49:46 adam
8 * New functions yaz_is_abspath, yaz_path_fopen_base
10 * Revision 1.16 1999/11/30 13:47:12 adam
11 * Improved installation. Moved header files to include/yaz.
13 * Revision 1.15 1999/10/21 12:06:29 adam
14 * Retrieval module no longer uses ctype.h - functions.
16 * Revision 1.14 1999/08/27 09:40:32 adam
17 * Renamed logf function to yaz_log. Removed VC++ project files.
19 * Revision 1.13 1998/10/13 16:09:52 adam
20 * Added support for arbitrary OID's for tagsets, schemas and attribute sets.
21 * Added support for multiple attribute set references and tagset references
22 * from an abstract syntax file.
23 * Fixed many bad logs-calls in routines that read the various
24 * specifications regarding data1 (*.abs,*.att,...) and made the messages
25 * consistent whenever possible.
26 * Added extra 'lineno' argument to function readconf_line.
28 * Revision 1.12 1998/02/23 10:57:09 adam
29 * Take care of integer data nodes as well in conversion.
31 * Revision 1.11 1998/02/11 11:53:35 adam
32 * Changed code so that it compiles as C++.
34 * Revision 1.10 1997/09/30 11:50:04 adam
35 * Added handler data1_get_map_buf that is used by data1_nodetomarc.
37 * Revision 1.9 1997/09/24 13:35:45 adam
38 * Added two members to data1_marctab to ease reading of weird MARC records.
40 * Revision 1.8 1997/09/17 12:10:37 adam
43 * Revision 1.7 1997/09/05 09:50:57 adam
44 * Removed global data1_tabpath - uses data1_get_tabpath() instead.
46 * Revision 1.6 1997/09/04 13:51:58 adam
47 * Added data1 to marc conversion with indicators.
49 * Revision 1.5 1997/09/04 13:48:04 adam
50 * Added data1 to marc conversion.
52 * Revision 1.4 1996/03/25 10:18:03 quinn
53 * Removed trailing whitespace from data elements
55 * Revision 1.3 1995/11/01 16:34:57 quinn
56 * Making data1 look for tables in data1_tabpath
58 * Revision 1.2 1995/11/01 13:54:48 quinn
61 * Revision 1.1 1995/11/01 11:56:08 quinn
62 * Added Retrieval (data management) functions en masse.
74 #include <yaz/marcdisp.h>
75 #include <yaz/readconf.h>
76 #include <yaz/xmalloc.h>
77 #include <yaz/data1.h>
78 #include <yaz/tpath.h>
80 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
83 NMEM mem = data1_nmem_get (dh);
84 data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
85 char line[512], *argv[50];
89 if (!(f = data1_path_fopen(dh, file, "r")))
91 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
96 res->reference = VAL_NONE;
98 res->length_data_entry = 4;
99 res->length_starting = 5;
100 res->length_implementation = 0;
101 strcpy(res->future_use, "4");
103 strcpy(res->record_status, "n");
104 strcpy(res->implementation_codes, " ");
105 res->indicator_length = 2;
106 res->identifier_length = 2;
107 res->force_indicator_length = -1;
108 res->force_identifier_length = -1;
109 strcpy(res->user_systems, "z ");
111 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
112 if (!strcmp(*argv, "name"))
116 yaz_log(LOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
120 res->name = nmem_strdup(mem, argv[1]);
122 else if (!strcmp(*argv, "reference"))
126 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
130 if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
132 yaz_log(LOG_WARN, "%s:%d: Unknown tagset reference '%s'",
133 file, lineno, argv[1]);
137 else if (!strcmp(*argv, "length-data-entry"))
141 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
145 res->length_data_entry = atoi(argv[1]);
147 else if (!strcmp(*argv, "length-starting"))
151 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
155 res->length_starting = atoi(argv[1]);
157 else if (!strcmp(*argv, "length-implementation"))
161 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
165 res->length_implementation = atoi(argv[1]);
167 else if (!strcmp(*argv, "future-use"))
171 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
175 strncpy(res->future_use, argv[1], 2);
177 else if (!strcmp(*argv, "force-indicator-length"))
181 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
185 res->force_indicator_length = atoi(argv[1]);
187 else if (!strcmp(*argv, "force-identifier-length"))
191 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
195 res->force_identifier_length = atoi(argv[1]);
198 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
206 * Locate some data under this node. This routine should handle variants
209 static char *get_data(data1_node *n, int *len)
213 while (n->which != DATA1N_data && n->child)
215 if (n->which != DATA1N_data ||
216 (n->u.data.what != DATA1I_text && n->u.data.what != DATA1I_num))
218 r = "[Structured/included data]";
223 *len = n->u.data.len;
224 while (*len && d1_isspace(n->u.data.data[*len - 1]))
226 return n->u.data.data;
229 static void memint (char *p, int val, int len)
237 sprintf (buf, "%08d", val);
238 memcpy (p, buf+8-len, len);
242 static int is_indicator (data1_marctab *p, data1_node *subf)
245 if (p->indicator_length != 2 ||
246 (subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
249 if (subf->which == DATA1N_tag && subf->child->which == DATA1N_tag)
255 static int nodetomarc(data1_marctab *p, data1_node *n, int selected,
256 char **buf, int *size)
260 int base_address = 25;
263 data1_node *field, *subf;
265 yaz_log (LOG_DEBUG, "nodetomarc");
266 for (field = n->child; field; field = field->next)
268 if (field->which != DATA1N_tag)
270 yaz_log(LOG_WARN, "Malformed field composition for marc output.");
273 if (selected && !field->u.tag.node_selected)
275 len += 4 + p->length_data_entry + p->length_starting
276 + p->length_implementation;
277 base_address += 3 + p->length_data_entry + p->length_starting
278 + p->length_implementation;
279 if (strncmp(field->u.tag.tag, "00", 2))
280 len += p->indicator_length; /* this is fairly bogus */
283 /* we'll allow no indicator if length is not 2 */
284 if (is_indicator (p, subf))
287 for (; subf; subf = subf->next)
289 if (subf->which != DATA1N_tag)
292 "Malformed subfield composition for marc output.");
295 if (strncmp(field->u.tag.tag, "00", 2))
296 len += p->identifier_length;
297 get_data(subf, &dlen);
303 *buf = (char *)xmalloc(*size = len);
304 else if (*size <= len)
305 *buf = (char *)xrealloc(*buf, *size = len);
309 memcpy (op+5, p->record_status, 1);
310 memcpy (op+6, p->implementation_codes, 4);
311 memint (op+10, p->indicator_length, 1);
312 memint (op+11, p->identifier_length, 1);
313 memint (op+12, base_address, 5);
314 memcpy (op+17, p->user_systems, 3);
315 memint (op+20, p->length_data_entry, 1);
316 memint (op+21, p->length_starting, 1);
317 memint (op+22, p->length_implementation, 1);
318 memcpy (op+23, p->future_use, 1);
321 data_p = base_address;
323 for (field = n->child; field; field = field->next)
326 char *indicator_data = " ";
327 if (selected && !field->u.tag.node_selected)
332 if (is_indicator (p, subf))
334 indicator_data = subf->u.tag.tag;
337 if (strncmp(field->u.tag.tag, "00", 2)) /* bogus */
339 memcpy (op + data_p, indicator_data, p->indicator_length);
340 data_p += p->indicator_length;
342 for (; subf; subf = subf->next)
346 if (strncmp(field->u.tag.tag, "00", 2))
348 op[data_p] = ISO2709_IDFS;
349 memcpy (op + data_p+1, subf->u.tag.tag, p->identifier_length-1);
350 data_p += p->identifier_length;
352 data = get_data(subf, &dlen);
353 memcpy (op + data_p, data, dlen);
356 op[data_p++] = ISO2709_FS;
358 memcpy (op + entry_p, field->u.tag.tag, 3);
360 memint (op + entry_p, data_p - data_0, p->length_data_entry);
361 entry_p += p->length_data_entry;
362 memint (op + entry_p, data_0 - base_address, p->length_starting);
363 entry_p += p->length_starting;
364 entry_p += p->length_implementation;
366 op[entry_p++] = ISO2709_FS;
367 assert (entry_p == base_address);
368 op[data_p++] = ISO2709_RS;
369 assert (data_p == len);
373 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
374 int selected, int *len)
377 char **buf = data1_get_map_buf (dh, &size);
379 *len = nodetomarc(p, n, selected, buf, size);