2 * Copyright (c) 1995-2002, Index Data.
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: d1_marc.c,v 1.23 2002-08-19 21:09:10 adam Exp $
16 #include <yaz/marcdisp.h>
17 #include <yaz/readconf.h>
18 #include <yaz/xmalloc.h>
19 #include <yaz/data1.h>
20 #include <yaz/tpath.h>
22 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
25 NMEM mem = data1_nmem_get (dh);
26 data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
27 char line[512], *argv[50];
31 if (!(f = data1_path_fopen(dh, file, "r")))
33 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
38 res->reference = VAL_NONE;
40 res->length_data_entry = 4;
41 res->length_starting = 5;
42 res->length_implementation = 0;
43 strcpy(res->future_use, "4");
45 strcpy(res->record_status, "n");
46 strcpy(res->implementation_codes, " ");
47 res->indicator_length = 2;
48 res->identifier_length = 2;
49 res->force_indicator_length = -1;
50 res->force_identifier_length = -1;
51 strcpy(res->user_systems, "z ");
53 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
54 if (!strcmp(*argv, "name"))
58 yaz_log(LOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
62 res->name = nmem_strdup(mem, argv[1]);
64 else if (!strcmp(*argv, "reference"))
68 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
72 if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
74 yaz_log(LOG_WARN, "%s:%d: Unknown tagset reference '%s'",
75 file, lineno, argv[1]);
79 else if (!strcmp(*argv, "length-data-entry"))
83 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
87 res->length_data_entry = atoi(argv[1]);
89 else if (!strcmp(*argv, "length-starting"))
93 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
97 res->length_starting = atoi(argv[1]);
99 else if (!strcmp(*argv, "length-implementation"))
103 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
107 res->length_implementation = atoi(argv[1]);
109 else if (!strcmp(*argv, "future-use"))
113 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
117 strncpy(res->future_use, argv[1], 2);
119 else if (!strcmp(*argv, "force-indicator-length"))
123 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
127 res->force_indicator_length = atoi(argv[1]);
129 else if (!strcmp(*argv, "force-identifier-length"))
133 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
137 res->force_identifier_length = atoi(argv[1]);
140 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
149 * Locate some data under this node. This routine should handle variants
152 static char *get_data(data1_node *n, int *len)
159 if (n->which == DATA1N_data)
161 *len = n->u.data.len;
162 while (*len && d1_isspace(n->u.data.data[*len - 1]))
165 return n->u.data.data;
167 if (n->which == DATA1N_tag)
181 static void memint (char *p, int val, int len)
189 sprintf (buf, "%08d", val);
190 memcpy (p, buf+8-len, len);
194 static int is_indicator (data1_marctab *p, data1_node *subf)
197 if (p->indicator_length != 2 ||
198 (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
201 if (subf && subf->which == DATA1N_tag && subf->child->which == DATA1N_tag)
207 static int nodetomarc(data1_handle dh,
208 data1_marctab *p, data1_node *n, int selected,
209 char **buf, int *size)
213 int base_address = 25;
216 data1_node *field, *subf;
218 yaz_log (LOG_DEBUG, "nodetomarc");
220 for (field = n->child; field; field = field->next)
224 if (field->which != DATA1N_tag)
226 yaz_log(LOG_WARN, "Malformed field composition for marc output.");
229 if (selected && !field->u.tag.node_selected)
236 len += 4 + p->length_data_entry + p->length_starting
237 + p->length_implementation;
238 base_address += 3 + p->length_data_entry + p->length_starting
239 + p->length_implementation;
241 if (subf->which == DATA1N_data)
243 if (!data1_is_xmlmode(dh))
245 if (subf->which == DATA1N_tag && !strcmp(subf->u.tag.tag, "@"))
251 len += p->indicator_length;
252 /* we'll allow no indicator if length is not 2 */
253 if (is_indicator (p, subf))
256 for (; subf; subf = subf->next)
259 len += p->identifier_length;
260 get_data(subf, &dlen);
266 *buf = (char *)xmalloc(*size = len);
267 else if (*size <= len)
268 *buf = (char *)xrealloc(*buf, *size = len);
272 memcpy (op+5, p->record_status, 1);
273 memcpy (op+6, p->implementation_codes, 4);
274 memint (op+10, p->indicator_length, 1);
275 memint (op+11, p->identifier_length, 1);
276 memint (op+12, base_address, 5);
277 memcpy (op+17, p->user_systems, 3);
278 memint (op+20, p->length_data_entry, 1);
279 memint (op+21, p->length_starting, 1);
280 memint (op+22, p->length_implementation, 1);
281 memcpy (op+23, p->future_use, 1);
284 data_p = base_address;
286 for (field = n->child; field; field = field->next)
291 char *indicator_data = " ";
292 if (selected && !field->u.tag.node_selected)
299 if (subf->which == DATA1N_data)
301 if (!data1_is_xmlmode(dh))
303 if (subf->which == DATA1N_tag && !strcmp(subf->u.tag.tag, "@"))
307 if (is_indicator (p, subf))
309 indicator_data = subf->u.tag.tag;
314 memcpy (op + data_p, indicator_data, p->indicator_length);
315 data_p += p->indicator_length;
317 for (; subf; subf = subf->next)
323 const char *identifier = "a";
324 if (subf->which != DATA1N_tag)
325 yaz_log(LOG_WARN, "Malformed fields for marc output.");
327 identifier = subf->u.tag.tag;
328 op[data_p] = ISO2709_IDFS;
329 memcpy (op + data_p+1, identifier, p->identifier_length-1);
330 data_p += p->identifier_length;
332 data = get_data(subf, &dlen);
333 memcpy (op + data_p, data, dlen);
336 op[data_p++] = ISO2709_FS;
338 memcpy (op + entry_p, field->u.tag.tag, 3);
340 memint (op + entry_p, data_p - data_0, p->length_data_entry);
341 entry_p += p->length_data_entry;
342 memint (op + entry_p, data_0 - base_address, p->length_starting);
343 entry_p += p->length_starting;
344 entry_p += p->length_implementation;
346 op[entry_p++] = ISO2709_FS;
347 assert (entry_p == base_address);
348 op[data_p++] = ISO2709_RS;
349 assert (data_p == len);
353 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
354 int selected, int *len)
357 char **buf = data1_get_map_buf (dh, &size);
359 n = data1_get_root_tag (dh, n);
362 *len = nodetomarc(dh, p, n, selected, buf, size);