1 /* $Id: d1_marc.c,v 1.6.2.6 2006-08-14 10:38:51 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003
5 This file is part of the Zebra server.
7 Zebra is free software; you can redistribute it and/or modify it under
8 the terms of the GNU General Public License as published by the Free
9 Software Foundation; either version 2, or (at your option) any later
12 Zebra is distributed in the hope that it will be useful, but WITHOUT ANY
13 WARRANTY; without even the implied warranty of MERCHANTABILITY or
14 FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License
17 You should have received a copy of the GNU General Public License
18 along with this program; if not, write to the Free Software
19 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
23 /* converts data1 tree to ISO2709/MARC record */
31 #include <yaz/marcdisp.h>
32 #include <yaz/readconf.h>
33 #include <yaz/xmalloc.h>
34 #include <yaz/tpath.h>
37 data1_marctab *data1_read_marctab (data1_handle dh, const char *file)
40 NMEM mem = data1_nmem_get (dh);
41 data1_marctab *res = (data1_marctab *)nmem_malloc(mem, sizeof(*res));
42 char line[512], *argv[50];
46 if (!(f = data1_path_fopen(dh, file, "r")))
48 yaz_log(LOG_WARN|LOG_ERRNO, "%s", file);
53 res->reference = VAL_NONE;
55 res->length_data_entry = 4;
56 res->length_starting = 5;
57 res->length_implementation = 0;
58 strcpy(res->future_use, "4");
60 strcpy(res->record_status, "n");
61 strcpy(res->implementation_codes, " ");
62 res->indicator_length = 2;
63 res->identifier_length = 2;
64 res->force_indicator_length = -1;
65 res->force_identifier_length = -1;
66 strcpy(res->user_systems, "z ");
68 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
69 if (!strcmp(*argv, "name"))
73 yaz_log(LOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
77 res->name = nmem_strdup(mem, argv[1]);
79 else if (!strcmp(*argv, "reference"))
83 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
87 if ((res->reference = oid_getvalbyname(argv[1])) == VAL_NONE)
89 yaz_log(LOG_WARN, "%s:%d: Unknown tagset reference '%s'",
90 file, lineno, argv[1]);
94 else if (!strcmp(*argv, "length-data-entry"))
98 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
102 res->length_data_entry = atoi(argv[1]);
104 else if (!strcmp(*argv, "length-starting"))
108 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
112 res->length_starting = atoi(argv[1]);
114 else if (!strcmp(*argv, "length-implementation"))
118 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
122 res->length_implementation = atoi(argv[1]);
124 else if (!strcmp(*argv, "future-use"))
128 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
132 strncpy(res->future_use, argv[1], 2);
134 else if (!strcmp(*argv, "force-indicator-length"))
138 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
142 res->force_indicator_length = atoi(argv[1]);
144 else if (!strcmp(*argv, "force-identifier-length"))
148 yaz_log(LOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
152 res->force_identifier_length = atoi(argv[1]);
155 yaz_log(LOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
164 * Locate some data under this node. This routine should handle variants
167 static char *get_data(data1_node *n, int *len, int chop)
173 if (n->which == DATA1N_data)
176 *len = n->u.data.len;
181 for (i = 0; i<*len; i++)
182 if (!d1_isspace(n->u.data.data[i]))
184 while (*len && d1_isspace(n->u.data.data[*len - 1]))
188 return n->u.data.data + i;
192 return n->u.data.data;
194 if (n->which == DATA1N_tag)
196 else if (n->which == DATA1N_data)
206 static void memint (char *p, int val, int len)
214 sprintf (buf, "%08d", val);
215 memcpy (p, buf+8-len, len);
219 /* check for indicator. non MARCXML only */
220 static int is_indicator (data1_marctab *p, data1_node *subf)
222 if (p->indicator_length != 2 ||
223 (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
228 static int nodetomarc(data1_handle dh,
229 data1_marctab *p, data1_node *n, int selected,
230 char **buf, int *size)
236 int base_address = 25;
239 data1_node *field, *subf;
242 data1_pr_tree(dh, n, stdout);
244 yaz_log (LOG_DEBUG, "nodetomarc");
246 memcpy (leader+5, p->record_status, 1);
247 memcpy (leader+6, p->implementation_codes, 4);
248 memint (leader+10, p->indicator_length, 1);
249 memint (leader+11, p->identifier_length, 1);
250 memcpy (leader+17, p->user_systems, 3);
251 memint (leader+20, p->length_data_entry, 1);
252 memint (leader+21, p->length_starting, 1);
253 memint (leader+22, p->length_implementation, 1);
254 memcpy (leader+23, p->future_use, 1);
256 for (field = n->child; field; field = field->next)
258 int control_field = 0; /* 00X fields - usually! */
261 if (field->which != DATA1N_tag)
263 if (selected && !field->u.tag.node_selected)
270 if (!yaz_matchstr(field->u.tag.tag, "mc?"))
272 else if (!strcmp(field->u.tag.tag, "leader"))
275 char *dbuf = get_data(subf, &dlen, 0);
278 if (dbuf && dlen > 0)
279 memcpy (leader, dbuf, dlen);
282 else if (!strcmp(field->u.tag.tag, "controlfield"))
287 else if (!strcmp(field->u.tag.tag, "datafield"))
292 else if (subf->which == DATA1N_data)
303 len += 4 + p->length_data_entry + p->length_starting
304 + p->length_implementation;
305 base_address += 3 + p->length_data_entry + p->length_starting
306 + p->length_implementation;
309 len += p->indicator_length;
311 /* we'll allow no indicator if length is not 2 */
312 /* select when old XML format, since indicator is an element */
313 if (marc_xml == 0 && is_indicator (p, subf))
316 for (; subf; subf = subf->next)
320 if (marc_xml && subf->which != DATA1N_tag)
321 continue; /* we skip comments, cdata .. */
322 len += p->identifier_length;
324 get_data(subf, &dlen, control_field ? 0 : 1);
330 *buf = (char *)xmalloc(*size = len);
331 else if (*size <= len)
332 *buf = (char *)xrealloc(*buf, *size = len);
336 /* we know the base address now */
337 memint (leader+12, base_address, 5);
339 /* copy temp leader to real output buf op */
340 memcpy (op, leader, 24);
344 data_p = base_address;
346 for (field = n->child; field; field = field->next)
348 int control_field = 0;
353 char indicator_data[6];
355 memset (indicator_data, ' ', sizeof(indicator_data)-1);
356 indicator_data[sizeof(indicator_data)-1] = '\0';
358 if (field->which != DATA1N_tag)
361 if (selected && !field->u.tag.node_selected)
368 if (!yaz_matchstr(field->u.tag.tag, "mc?"))
370 else if (!strcmp(field->u.tag.tag, "leader"))
372 else if (!strcmp(field->u.tag.tag, "controlfield"))
377 else if (!strcmp(field->u.tag.tag, "datafield"))
382 else if (subf->which == DATA1N_data)
392 if (marc_xml == 0 && is_indicator (p, subf))
394 strncpy(indicator_data, subf->u.tag.tag, sizeof(indicator_data)-1);
397 else if (marc_xml == 1 && !control_field)
400 for (xa = field->u.tag.attributes; xa; xa = xa->next)
402 if (!strcmp(xa->name, "ind1"))
403 indicator_data[0] = xa->value[0];
404 if (!strcmp(xa->name, "ind2"))
405 indicator_data[1] = xa->value[0];
406 if (!strcmp(xa->name, "ind3"))
407 indicator_data[2] = xa->value[0];
412 memcpy (op + data_p, indicator_data, p->indicator_length);
413 data_p += p->indicator_length;
415 for (; subf; subf = subf->next)
421 const char *identifier = "a";
425 if (subf->which != DATA1N_tag)
427 if (strcmp(subf->u.tag.tag, "subfield"))
428 yaz_log(LOG_WARN, "Unhandled tag %s", subf->u.tag.tag);
430 for (xa = subf->u.tag.attributes; xa; xa = xa->next)
431 if (!strcmp(xa->name, "code"))
432 identifier = xa->value;
434 else if (subf->which != DATA1N_tag)
435 yaz_log(LOG_WARN, "Malformed fields for marc output.");
437 identifier = subf->u.tag.tag;
438 op[data_p] = ISO2709_IDFS;
439 memcpy (op + data_p+1, identifier, p->identifier_length-1);
440 data_p += p->identifier_length;
442 data = get_data(subf, &dlen, control_field ? 0 : 1);
443 memcpy (op + data_p, data, dlen);
446 op[data_p++] = ISO2709_FS;
451 for (xa = field->u.tag.attributes; xa; xa = xa->next)
452 if (!strcmp(xa->name, "tag"))
456 tag = field->u.tag.tag;
458 if (!tag || strlen(tag) != 3)
460 memcpy (op + entry_p, tag, 3);
463 memint (op + entry_p, data_p - data_0, p->length_data_entry);
464 entry_p += p->length_data_entry;
465 memint (op + entry_p, data_0 - base_address, p->length_starting);
466 entry_p += p->length_starting;
467 entry_p += p->length_implementation;
469 op[entry_p++] = ISO2709_FS;
470 assert (entry_p == base_address);
471 op[data_p++] = ISO2709_RS;
472 assert (data_p == len);
476 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
477 int selected, int *len)
480 char **buf = data1_get_map_buf (dh, &size);
482 n = data1_get_root_tag (dh, n);
485 *len = nodetomarc(dh, p, n, selected, buf, size);