1 /* $Id: marcread.c,v 1.24 2004-06-16 22:12:30 adam Exp $
2 Copyright (C) 1995,1996,1997,1998,1999,2000,2001,2002,2003,2004
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 Zebra; see the file LICENSE.zebra. If not, write to the
19 Free Software Foundation, 59 Temple Place - Suite 330, Boston, MA
28 #include <yaz/yaz-util.h>
29 #include <yaz/marcdisp.h>
35 #define MARCOMP_DEBUG 0
37 static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
43 int identifier_length;
45 int length_data_entry;
47 int length_implementation;
52 data1_node *res_root, *res_top;
54 data1_marctab *marctab;
56 if ((*p->readf)(p->fh, buf, 5) != 5)
58 record_length = atoi_n (buf, 5);
59 if (record_length < 25)
61 logf (LOG_WARN, "MARC record length < 25, is %d", record_length);
64 /* read remaining part - attempt to read one byte furhter... */
65 read_bytes = (*p->readf)(p->fh, buf+5, record_length-4);
66 if (read_bytes < record_length-5)
68 logf (LOG_WARN, "Couldn't read whole MARC record");
71 if (read_bytes == record_length - 4)
73 off_t cur_offset = (*p->tellf)(p->fh);
77 (*p->endf)(p->fh, cur_offset - 1);
80 res_root = data1_mk_root (p->dh, p->mem, absynName);
83 yaz_log (LOG_WARN, "cannot read MARC without an abstract syntax");
89 const char *attr[] = { "xmlns", "http://www.loc.gov/MARC21/slim", 0};
91 res_top = data1_mk_tag (p->dh, p->mem, "record", attr, res_root);
93 lead = data1_mk_tag(p->dh, p->mem, "leader", 0, res_top);
94 data1_mk_text_n(p->dh, p->mem, buf, 24, lead);
97 res_top = data1_mk_tag (p->dh, p->mem, absynName, 0, res_root);
99 if ((marctab = res_root->u.root.absyn->marc))
101 memcpy(marctab->leader, buf, 24);
102 memcpy(marctab->implementation_codes, buf+6, 4);
103 marctab->implementation_codes[4] = '\0';
104 memcpy(marctab->user_systems, buf+17, 3);
105 marctab->user_systems[3] = '\0';
108 if (marctab && marctab->force_indicator_length >= 0)
109 indicator_length = marctab->force_indicator_length;
111 indicator_length = atoi_n (buf+10, 1);
112 if (marctab && marctab->force_identifier_length >= 0)
113 identifier_length = marctab->force_identifier_length;
115 identifier_length = atoi_n (buf+11, 1);
116 base_address = atoi_n (buf+12, 5);
118 length_data_entry = atoi_n (buf+20, 1);
119 length_starting = atoi_n (buf+21, 1);
120 length_implementation = atoi_n (buf+22, 1);
122 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
123 entry_p += 3+length_data_entry+length_starting;
124 base_address = entry_p+1;
125 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
133 data1_node *parent = res_top;
135 memcpy (tag, buf+entry_p, 3);
142 res = data1_mk_tag_n (p->dh, p->mem, tag, 3, 0 /* attr */, parent);
145 fprintf (outf, "%s ", tag);
147 data_length = atoi_n (buf+entry_p, length_data_entry);
148 entry_p += length_data_entry;
149 data_offset = atoi_n (buf+entry_p, length_starting);
150 entry_p += length_starting;
151 i = data_offset + base_address;
152 end_offset = i+data_length-1;
154 if (memcmp (tag, "00", 2) && indicator_length)
156 /* generate indicator node */
159 const char *attr[10];
166 res = data1_mk_tag(p->dh, p->mem, "datafield", attr, res);
168 for (j = 0; j<indicator_length; j++)
170 char str1[18], str2[2];
171 sprintf (str1, "ind%d", j+1);
178 data1_tag_add_attr (p->dh, p->mem, res, attr);
186 res = data1_mk_tag_n (p->dh, p->mem,
187 buf+i, indicator_length, 0 /* attr */, res);
189 for (j = 0; j<indicator_length; j++)
190 fprintf (outf, "%c", buf[j+i]);
193 i += indicator_length;
199 const char *attr[10];
205 res = data1_mk_tag(p->dh, p->mem, "controlfield", attr, res);
209 /* traverse sub fields */
211 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
213 if (memcmp (tag, "00", 2) && identifier_length)
222 for (j = 1; j<identifier_length && j < 9; j++)
223 code[j-1] = buf[i+j];
228 res = data1_mk_tag(p->dh, p->mem, "subfield",
233 res = data1_mk_tag_n (p->dh, p->mem,
234 buf+i+1, identifier_length-1,
235 0 /* attr */, parent);
238 fprintf (outf, " $");
239 for (j = 1; j<identifier_length; j++)
240 fprintf (outf, "%c", buf[j+i]);
243 i += identifier_length;
245 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
246 buf[i] != ISO2709_FS && i < end_offset)
249 fprintf (outf, "%c", buf[i]);
253 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, res);
259 fprintf (outf, "%c", buf[i]);
266 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, parent);
269 fprintf (outf, "\n");
271 fprintf (outf, "-- separator but not at end of field\n");
272 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
273 fprintf (outf, "-- no separator at end of field\n");
280 * Locate some data under this node. This routine should handle variants
283 static char *get_data(data1_node *n, int *len)
289 if (n->which == DATA1N_data)
292 *len = n->u.data.len;
294 for (i = 0; i<*len; i++)
295 if (!d1_isspace(n->u.data.data[i]))
297 while (*len && d1_isspace(n->u.data.data[*len - 1]))
301 return n->u.data.data + i;
303 if (n->which == DATA1N_tag)
305 else if (n->which == DATA1N_data)
315 static data1_node *lookup_subfield(data1_node *node, const char *name)
319 for (p=node; p; p=p->next)
321 if (!yaz_matchstr(p->u.tag.tag, name))
327 static inline_subfield *lookup_inline_subfield(inline_subfield *pisf,
332 for (p=pisf; p; p=p->next)
334 if (!yaz_matchstr(p->name, name))
340 static inline_subfield *cat_inline_subfield(mc_subfield *psf, WRBUF buf,
341 inline_subfield *pisf)
345 for (p = psf; p && pisf; p = p->next)
347 if (p->which == MC_SF)
349 inline_subfield *found = lookup_inline_subfield(pisf, p->name);
353 if (strcmp(p->prefix, "_"))
355 wrbuf_puts(buf, " ");
356 wrbuf_puts(buf, p->prefix);
358 if (p->interval.start == -1)
360 wrbuf_puts(buf, found->data);
364 wrbuf_write(buf, found->data+p->interval.start,
365 p->interval.end-p->interval.start);
368 if (strcmp(p->suffix, "_"))
370 wrbuf_puts(buf, p->suffix);
371 wrbuf_puts(buf, " ");
374 logf(LOG_LOG, "cat_inline_subfield(): add subfield $%s", found->name);
379 else if (p->which == MC_SFVARIANT)
381 inline_subfield *next;
384 next = cat_inline_subfield(p->u.child, buf, pisf);
390 else if (p->which == MC_SFGROUP)
395 for (pp = p->u.child, found = 0; pp; pp = pp->next)
397 if (!yaz_matchstr(pisf->name, p->name))
405 wrbuf_puts(buf, " (");
406 pisf = cat_inline_subfield(p->u.child, buf, pisf);
407 wrbuf_puts(buf, ") ");
414 static void cat_inline_field(mc_field *pf, WRBUF buf, data1_node *subfield)
416 if (!pf || !subfield)
422 inline_field *pif=NULL;
425 if (yaz_matchstr(subfield->u.tag.tag, "1"))
427 subfield = subfield->next;
432 pif = inline_mk_field();
436 if ((i=inline_parse(pif, psubf->u.tag.tag, get_data(psubf, &len)))<0)
438 logf(LOG_WARN, "inline subfield ($%s): parse error",
440 inline_destroy_field(pif);
444 } while (psubf && yaz_matchstr(psubf->u.tag.tag, "1"));
448 if (pif && !yaz_matchstr(pif->name, pf->name))
450 if (!pf->list && pif->list)
452 wrbuf_puts(buf, pif->list->data);
462 ind1 = (pif->ind1[0] == ' ') ? '_':pif->ind1[0];
463 ind2 = (pif->ind2[0] == ' ') ? '_':pif->ind2[0];
465 if (((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
466 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0])))
468 cat_inline_subfield(pf->list, buf, pif->list);
471 add separator for inline fields
475 wrbuf_puts(buf, "\n");
480 logf(LOG_WARN, "In-line field %s missed -- indicators do not match", pif->name);
484 inline_destroy_field(pif);
487 logf(LOG_LOG, "cat_inline_field(): got buffer {%s}", buf);
491 static data1_node *cat_subfield(mc_subfield *psf, WRBUF buf,
492 data1_node *subfield)
496 for (p = psf; p && subfield; p = p->next)
498 if (p->which == MC_SF)
500 data1_node *found = lookup_subfield(subfield, p->name);
506 if (strcmp(p->prefix, "_"))
508 wrbuf_puts(buf, " ");
509 wrbuf_puts(buf, p->prefix);
514 cat_inline_field(p->u.in_line, buf, found);
516 else if (p->interval.start == -1)
518 wrbuf_puts(buf, get_data(found, &len));
522 wrbuf_write(buf, get_data(found, &len)+p->interval.start,
523 p->interval.end-p->interval.start);
526 if (strcmp(p->suffix, "_"))
528 wrbuf_puts(buf, p->suffix);
529 wrbuf_puts(buf, " ");
532 logf(LOG_LOG, "cat_subfield(): add subfield $%s", found->u.tag.tag);
534 subfield = found->next;
537 else if (p->which == MC_SFVARIANT)
541 next = cat_subfield(p->u.child, buf, subfield);
542 if (next == subfield)
547 else if (p->which == MC_SFGROUP)
552 for (pp = p->u.child, found = 0; pp; pp = pp->next)
554 if (!yaz_matchstr(subfield->u.tag.tag, pp->name))
562 wrbuf_puts(buf, " (");
563 subfield = cat_subfield(p->u.child, buf, subfield);
564 wrbuf_puts(buf, ") ");
571 static data1_node *cat_field(struct grs_read_info *p, mc_field *pf,
572 WRBUF buf, data1_node *field)
574 data1_node *subfield;
581 if (yaz_matchstr(field->u.tag.tag, pf->name))
584 subfield = field->child;
590 check subfield without indicators
593 if (!pf->list && subfield->which == DATA1N_data)
597 if (pf->interval.start == -1)
599 wrbuf_puts(buf, get_data(field, &len));
603 wrbuf_write(buf, get_data(field, &len)+pf->interval.start,
604 pf->interval.end-pf->interval.start);
608 logf(LOG_LOG, "cat_field(): got buffer {%s}", buf);
617 ind1 = (subfield->u.tag.tag[0] == ' ') ? '_':subfield->u.tag.tag[0];
618 ind2 = (subfield->u.tag.tag[1] == ' ') ? '_':subfield->u.tag.tag[1];
621 ((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
622 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0]))
626 logf(LOG_WARN, "Field %s missed -- does not match indicators", field->u.tag.tag);
631 subfield = subfield->child;
636 cat_subfield(pf->list, buf, subfield);
639 logf(LOG_LOG, "cat_field(): got buffer {%s}", buf);
645 static int is_empty(char *s)
657 static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
660 data1_marctab *marctab = root->u.root.absyn->marc;
661 data1_node *top = root->child;
667 c = mc_mk_context(mc_stmnt+3);
676 mc_destroy_context(c);
681 logf(LOG_LOG, "parse_data1_tree(): statement -{%s}", mc_stmnt);
683 if (!yaz_matchstr(pf->name, "ldr"))
687 logf(LOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
688 pf->interval.start, pf->interval.end);
690 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
691 data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
692 pf->interval.end-pf->interval.start+1, new);
700 if (!yaz_matchstr(field->u.tag.tag, pf->name))
705 logf(LOG_LOG, "parse_data1_tree(): try field {%s}", field->u.tag.tag);
710 field = cat_field(p, pf, buf, field);
713 for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
717 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
718 data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
728 mc_destroy_field(pf);
729 mc_destroy_context(c);
733 data1_node *grs_read_marcxml(struct grs_read_info *p)
735 data1_node *root = grs_read_iso2709(p, 1);
741 for (e=root->u.root.absyn->main_elements; e; e=e->next)
743 data1_tag *tag = e->tag;
745 if (tag && tag->which == DATA1T_string &&
746 !yaz_matchstr(tag->value.string, "mc?"))
747 parse_data1_tree(p, tag->value.string, root);
752 data1_node *grs_read_marc(struct grs_read_info *p)
754 data1_node *root = grs_read_iso2709(p, 0);
760 for (e=root->u.root.absyn->main_elements; e; e=e->next)
762 data1_tag *tag = e->tag;
764 if (tag && tag->which == DATA1T_string &&
765 !yaz_matchstr(tag->value.string, "mc?"))
766 parse_data1_tree(p, tag->value.string, root);
771 static void *grs_init_marc(void)
776 static void grs_destroy_marc(void *clientData)
780 static struct recTypeGrs marc_type = {
787 RecTypeGrs recTypeGrs_marc = &marc_type;
789 static struct recTypeGrs marcxml_type = {
796 RecTypeGrs recTypeGrs_marcxml = &marcxml_type;