1 /* $Id: d1_marc.c,v 1.19 2007-06-27 22:17:20 adam Exp $
2 Copyright (C) 1995-2007
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 */
30 #include <yaz/oid_db.h>
31 #include <yaz/marcdisp.h>
32 #include <yaz/readconf.h>
33 #include <yaz/xmalloc.h>
34 #include <yaz/tpath.h>
35 #include <idzebra/data1.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")))
52 res->length_data_entry = 4;
53 res->length_starting = 5;
54 res->length_implementation = 0;
55 strcpy(res->future_use, "4");
57 strcpy(res->record_status, "n");
58 strcpy(res->implementation_codes, " ");
59 res->indicator_length = 2;
60 res->identifier_length = 2;
61 res->force_indicator_length = -1;
62 res->force_identifier_length = -1;
63 strcpy(res->user_systems, "z ");
65 while ((argc = readconf_line(f, &lineno, line, 512, argv, 50)))
66 if (!strcmp(*argv, "name"))
70 yaz_log(YLOG_WARN, "%s:%d:Missing arg for %s", file, lineno,
74 res->name = nmem_strdup(mem, argv[1]);
76 else if (!strcmp(*argv, "reference"))
80 yaz_log(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
84 res->oid = yaz_string_to_oid_nmem(yaz_oid_std(),
85 CLASS_TAGSET, argv[1],
89 yaz_log(YLOG_WARN, "%s:%d: Unknown tagset reference '%s'",
90 file, lineno, argv[1]);
94 else if (!strcmp(*argv, "length-data-entry"))
98 yaz_log(YLOG_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(YLOG_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(YLOG_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(YLOG_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(YLOG_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(YLOG_WARN, "%s:%d: Missing arg for %s", file, lineno,
152 res->force_identifier_length = atoi(argv[1]);
155 yaz_log(YLOG_WARN, "%s:%d: Unknown directive '%s'", file, lineno,
163 static void get_data2(data1_node *n, int *len, char *dst, size_t max)
169 if (n->which == DATA1N_data)
171 if (dst && *len < max)
173 size_t copy_len = max - *len;
174 if (copy_len > n->u.data.len)
175 copy_len = n->u.data.len;
176 memcpy(dst + *len, n->u.data.data, copy_len);
178 *len += n->u.data.len;
180 if (n->which == DATA1N_tag)
182 else if (n->which == DATA1N_data)
189 static void memint (char *p, int val, int len)
197 sprintf (buf, "%08d", val);
198 memcpy (p, buf+8-len, len);
202 /* check for indicator. non MARCXML only */
203 static int is_indicator (data1_marctab *p, data1_node *subf)
205 if (p->indicator_length != 2 ||
206 (subf && subf->which == DATA1N_tag && strlen(subf->u.tag.tag) == 2))
211 static int nodetomarc(data1_handle dh,
212 data1_marctab *p, data1_node *n, int selected,
213 char **buf, int *size)
219 int base_address = 25;
222 data1_node *field, *subf;
225 data1_pr_tree(dh, n, stdout);
227 yaz_log (YLOG_DEBUG, "nodetomarc");
229 memcpy (leader+5, p->record_status, 1);
230 memcpy (leader+6, p->implementation_codes, 4);
231 memint (leader+10, p->indicator_length, 1);
232 memint (leader+11, p->identifier_length, 1);
233 memcpy (leader+17, p->user_systems, 3);
234 memint (leader+20, p->length_data_entry, 1);
235 memint (leader+21, p->length_starting, 1);
236 memint (leader+22, p->length_implementation, 1);
237 memcpy (leader+23, p->future_use, 1);
239 for (field = n->child; field; field = field->next)
241 int control_field = 0; /* 00X fields - usually! */
244 if (field->which != DATA1N_tag)
246 if (selected && !field->u.tag.node_selected)
253 if (!yaz_matchstr(field->u.tag.tag, "mc?"))
255 else if (!strcmp(field->u.tag.tag, "leader"))
258 get_data2(subf, &dlen, leader, 24);
261 else if (!strcmp(field->u.tag.tag, "controlfield"))
266 else if (!strcmp(field->u.tag.tag, "datafield"))
271 else if (subf->which == DATA1N_data)
282 len += 4 + p->length_data_entry + p->length_starting
283 + p->length_implementation;
284 base_address += 3 + p->length_data_entry + p->length_starting
285 + p->length_implementation;
288 len += p->indicator_length;
290 /* we'll allow no indicator if length is not 2 */
291 /* select when old XML format, since indicator is an element */
292 if (marc_xml == 0 && is_indicator (p, subf))
295 for (; subf; subf = subf->next)
299 if (marc_xml && subf->which != DATA1N_tag)
300 continue; /* we skip comments, cdata .. */
301 len += p->identifier_length;
303 get_data2(subf, &dlen, 0, 0);
309 *buf = (char *)xmalloc(*size = len);
310 else if (*size <= len)
311 *buf = (char *)xrealloc(*buf, *size = len);
315 /* we know the base address now */
316 memint (leader+12, base_address, 5);
318 /* copy temp leader to real output buf op */
319 memcpy (op, leader, 24);
323 data_p = base_address;
325 for (field = n->child; field; field = field->next)
327 int control_field = 0;
332 char indicator_data[6];
334 memset (indicator_data, ' ', sizeof(indicator_data)-1);
335 indicator_data[sizeof(indicator_data)-1] = '\0';
337 if (field->which != DATA1N_tag)
340 if (selected && !field->u.tag.node_selected)
347 if (!yaz_matchstr(field->u.tag.tag, "mc?"))
349 else if (!strcmp(field->u.tag.tag, "leader"))
351 else if (!strcmp(field->u.tag.tag, "controlfield"))
356 else if (!strcmp(field->u.tag.tag, "datafield"))
361 else if (subf->which == DATA1N_data)
371 if (marc_xml == 0 && is_indicator (p, subf))
373 strncpy(indicator_data, subf->u.tag.tag, sizeof(indicator_data)-1);
376 else if (marc_xml == 1 && !control_field)
379 for (xa = field->u.tag.attributes; xa; xa = xa->next)
381 if (!strcmp(xa->name, "ind1"))
382 indicator_data[0] = xa->value[0];
383 if (!strcmp(xa->name, "ind2"))
384 indicator_data[1] = xa->value[0];
385 if (!strcmp(xa->name, "ind3"))
386 indicator_data[2] = xa->value[0];
391 memcpy (op + data_p, indicator_data, p->indicator_length);
392 data_p += p->indicator_length;
394 for (; subf; subf = subf->next)
398 const char *identifier = "a";
402 if (subf->which != DATA1N_tag)
404 if (strcmp(subf->u.tag.tag, "subfield"))
405 yaz_log(YLOG_WARN, "Unhandled tag %s",
408 for (xa = subf->u.tag.attributes; xa; xa = xa->next)
409 if (!strcmp(xa->name, "code"))
410 identifier = xa->value;
412 else if (subf->which != DATA1N_tag)
413 yaz_log(YLOG_WARN, "Malformed fields for marc output.");
415 identifier = subf->u.tag.tag;
416 op[data_p] = ISO2709_IDFS;
417 memcpy (op + data_p+1, identifier, p->identifier_length-1);
418 data_p += p->identifier_length;
420 get_data2(subf, &dlen, op + data_p, 100000);
423 op[data_p++] = ISO2709_FS;
428 for (xa = field->u.tag.attributes; xa; xa = xa->next)
429 if (!strcmp(xa->name, "tag"))
433 tag = field->u.tag.tag;
435 if (!tag || strlen(tag) != 3)
437 memcpy (op + entry_p, tag, 3);
440 memint (op + entry_p, data_p - data_0, p->length_data_entry);
441 entry_p += p->length_data_entry;
442 memint (op + entry_p, data_0 - base_address, p->length_starting);
443 entry_p += p->length_starting;
444 entry_p += p->length_implementation;
446 op[entry_p++] = ISO2709_FS;
447 assert (entry_p == base_address);
448 op[data_p++] = ISO2709_RS;
449 assert (data_p == len);
453 char *data1_nodetomarc(data1_handle dh, data1_marctab *p, data1_node *n,
454 int selected, int *len)
457 char **buf = data1_get_map_buf (dh, &size);
459 n = data1_get_root_tag (dh, n);
462 *len = nodetomarc(dh, p, n, selected, buf, size);
468 * indent-tabs-mode: nil
470 * vim: shiftwidth=4 tabstop=8 expandtab