1 /* $Id: marcread.c,v 1.29 2005-01-03 19:27:53 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
27 #include <yaz/yaz-util.h>
28 #include <yaz/marcdisp.h>
29 #include <idzebra/recgrs.h>
34 #define MARCOMP_DEBUG 0
40 static data1_node *grs_read_iso2709 (struct grs_read_info *p, int marc_xml)
42 struct marc_info *mi = (struct marc_info*) p->clientData;
47 int identifier_length;
49 int length_data_entry;
51 int length_implementation;
56 data1_node *res_root, *res_top;
58 data1_marctab *marctab;
60 if ((*p->readf)(p->fh, buf, 5) != 5)
62 record_length = atoi_n (buf, 5);
63 if (record_length < 25)
65 yaz_log (YLOG_WARN, "MARC record length < 25, is %d", record_length);
68 /* read remaining part - attempt to read one byte furhter... */
69 read_bytes = (*p->readf)(p->fh, buf+5, record_length-4);
70 if (read_bytes < record_length-5)
72 yaz_log (YLOG_WARN, "Couldn't read whole MARC record");
75 if (read_bytes == record_length - 4)
77 off_t cur_offset = (*p->tellf)(p->fh);
81 (*p->endf)(p->fh, cur_offset - 1);
84 res_root = data1_mk_root (p->dh, p->mem, absynName);
87 yaz_log (YLOG_WARN, "cannot read MARC without an abstract syntax");
93 const char *attr[] = { "xmlns", "http://www.loc.gov/MARC21/slim", 0};
95 res_top = data1_mk_tag (p->dh, p->mem, "record", attr, res_root);
97 lead = data1_mk_tag(p->dh, p->mem, "leader", 0, res_top);
98 data1_mk_text_n(p->dh, p->mem, buf, 24, lead);
101 res_top = data1_mk_tag (p->dh, p->mem, absynName, 0, res_root);
103 if ((marctab = data1_absyn_getmarctab(p->dh, res_root->u.root.absyn)))
105 memcpy(marctab->leader, buf, 24);
106 memcpy(marctab->implementation_codes, buf+6, 4);
107 marctab->implementation_codes[4] = '\0';
108 memcpy(marctab->user_systems, buf+17, 3);
109 marctab->user_systems[3] = '\0';
112 if (marctab && marctab->force_indicator_length >= 0)
113 indicator_length = marctab->force_indicator_length;
115 indicator_length = atoi_n (buf+10, 1);
116 if (marctab && marctab->force_identifier_length >= 0)
117 identifier_length = marctab->force_identifier_length;
119 identifier_length = atoi_n (buf+11, 1);
120 base_address = atoi_n (buf+12, 5);
122 length_data_entry = atoi_n (buf+20, 1);
123 length_starting = atoi_n (buf+21, 1);
124 length_implementation = atoi_n (buf+22, 1);
126 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
127 entry_p += 3+length_data_entry+length_starting;
128 base_address = entry_p+1;
129 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
137 data1_node *parent = res_top;
139 memcpy (tag, buf+entry_p, 3);
146 res = data1_mk_tag_n (p->dh, p->mem, tag, 3, 0 /* attr */, parent);
149 fprintf (outf, "%s ", tag);
151 data_length = atoi_n (buf+entry_p, length_data_entry);
152 entry_p += length_data_entry;
153 data_offset = atoi_n (buf+entry_p, length_starting);
154 entry_p += length_starting;
155 i = data_offset + base_address;
156 end_offset = i+data_length-1;
158 if (memcmp (tag, "00", 2) && indicator_length)
160 /* generate indicator node */
163 const char *attr[10];
170 res = data1_mk_tag(p->dh, p->mem, "datafield", attr, res);
172 for (j = 0; j<indicator_length; j++)
174 char str1[18], str2[2];
175 sprintf (str1, "ind%d", j+1);
182 data1_tag_add_attr (p->dh, p->mem, res, attr);
190 res = data1_mk_tag_n (p->dh, p->mem,
191 buf+i, indicator_length, 0 /* attr */, res);
193 for (j = 0; j<indicator_length; j++)
194 fprintf (outf, "%c", buf[j+i]);
197 i += indicator_length;
203 const char *attr[10];
209 res = data1_mk_tag(p->dh, p->mem, "controlfield", attr, res);
213 /* traverse sub fields */
215 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
217 if (memcmp (tag, "00", 2) && identifier_length)
226 for (j = 1; j<identifier_length && j < 9; j++)
227 code[j-1] = buf[i+j];
232 res = data1_mk_tag(p->dh, p->mem, "subfield",
237 res = data1_mk_tag_n (p->dh, p->mem,
238 buf+i+1, identifier_length-1,
239 0 /* attr */, parent);
242 fprintf (outf, " $");
243 for (j = 1; j<identifier_length; j++)
244 fprintf (outf, "%c", buf[j+i]);
247 i += identifier_length;
249 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
250 buf[i] != ISO2709_FS && i < end_offset)
253 fprintf (outf, "%c", buf[i]);
257 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, res);
263 fprintf (outf, "%c", buf[i]);
270 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, parent);
273 fprintf (outf, "\n");
275 fprintf (outf, "-- separator but not at end of field\n");
276 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
277 fprintf (outf, "-- no separator at end of field\n");
284 * Locate some data under this node. This routine should handle variants
287 static char *get_data(data1_node *n, int *len)
293 if (n->which == DATA1N_data)
296 *len = n->u.data.len;
298 for (i = 0; i<*len; i++)
299 if (!d1_isspace(n->u.data.data[i]))
301 while (*len && d1_isspace(n->u.data.data[*len - 1]))
305 return n->u.data.data + i;
307 if (n->which == DATA1N_tag)
309 else if (n->which == DATA1N_data)
319 static data1_node *lookup_subfield(data1_node *node, const char *name)
323 for (p=node; p; p=p->next)
325 if (!yaz_matchstr(p->u.tag.tag, name))
331 static inline_subfield *lookup_inline_subfield(inline_subfield *pisf,
336 for (p=pisf; p; p=p->next)
338 if (!yaz_matchstr(p->name, name))
344 static inline_subfield *cat_inline_subfield(mc_subfield *psf, WRBUF buf,
345 inline_subfield *pisf)
349 for (p = psf; p && pisf; p = p->next)
351 if (p->which == MC_SF)
353 inline_subfield *found = lookup_inline_subfield(pisf, p->name);
357 if (strcmp(p->prefix, "_"))
359 wrbuf_puts(buf, " ");
360 wrbuf_puts(buf, p->prefix);
362 if (p->interval.start == -1)
364 wrbuf_puts(buf, found->data);
368 wrbuf_write(buf, found->data+p->interval.start,
369 p->interval.end-p->interval.start);
372 if (strcmp(p->suffix, "_"))
374 wrbuf_puts(buf, p->suffix);
375 wrbuf_puts(buf, " ");
378 yaz_log(YLOG_LOG, "cat_inline_subfield(): add subfield $%s", found->name);
383 else if (p->which == MC_SFVARIANT)
385 inline_subfield *next;
388 next = cat_inline_subfield(p->u.child, buf, pisf);
394 else if (p->which == MC_SFGROUP)
399 for (pp = p->u.child, found = 0; pp; pp = pp->next)
401 if (!yaz_matchstr(pisf->name, p->name))
409 wrbuf_puts(buf, " (");
410 pisf = cat_inline_subfield(p->u.child, buf, pisf);
411 wrbuf_puts(buf, ") ");
418 static void cat_inline_field(mc_field *pf, WRBUF buf, data1_node *subfield)
420 if (!pf || !subfield)
426 inline_field *pif=NULL;
429 if (yaz_matchstr(subfield->u.tag.tag, "1"))
431 subfield = subfield->next;
436 pif = inline_mk_field();
440 if ((i=inline_parse(pif, psubf->u.tag.tag, get_data(psubf, &len)))<0)
442 yaz_log(YLOG_WARN, "inline subfield ($%s): parse error",
444 inline_destroy_field(pif);
448 } while (psubf && yaz_matchstr(psubf->u.tag.tag, "1"));
452 if (pif && !yaz_matchstr(pif->name, pf->name))
454 if (!pf->list && pif->list)
456 wrbuf_puts(buf, pif->list->data);
466 ind1 = (pif->ind1[0] == ' ') ? '_':pif->ind1[0];
467 ind2 = (pif->ind2[0] == ' ') ? '_':pif->ind2[0];
469 if (((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
470 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0])))
472 cat_inline_subfield(pf->list, buf, pif->list);
475 add separator for inline fields
479 wrbuf_puts(buf, "\n");
484 yaz_log(YLOG_WARN, "In-line field %s missed -- indicators do not match", pif->name);
488 inline_destroy_field(pif);
491 yaz_log(YLOG_LOG, "cat_inline_field(): got buffer {%s}", buf);
495 static data1_node *cat_subfield(mc_subfield *psf, WRBUF buf,
496 data1_node *subfield)
500 for (p = psf; p && subfield; p = p->next)
502 if (p->which == MC_SF)
504 data1_node *found = lookup_subfield(subfield, p->name);
510 if (strcmp(p->prefix, "_"))
512 wrbuf_puts(buf, " ");
513 wrbuf_puts(buf, p->prefix);
518 cat_inline_field(p->u.in_line, buf, found);
520 else if (p->interval.start == -1)
522 wrbuf_puts(buf, get_data(found, &len));
526 wrbuf_write(buf, get_data(found, &len)+p->interval.start,
527 p->interval.end-p->interval.start);
530 if (strcmp(p->suffix, "_"))
532 wrbuf_puts(buf, p->suffix);
533 wrbuf_puts(buf, " ");
536 yaz_log(YLOG_LOG, "cat_subfield(): add subfield $%s", found->u.tag.tag);
538 subfield = found->next;
541 else if (p->which == MC_SFVARIANT)
545 next = cat_subfield(p->u.child, buf, subfield);
546 if (next == subfield)
551 else if (p->which == MC_SFGROUP)
556 for (pp = p->u.child, found = 0; pp; pp = pp->next)
558 if (!yaz_matchstr(subfield->u.tag.tag, pp->name))
566 wrbuf_puts(buf, " (");
567 subfield = cat_subfield(p->u.child, buf, subfield);
568 wrbuf_puts(buf, ") ");
575 static data1_node *cat_field(struct grs_read_info *p, mc_field *pf,
576 WRBUF buf, data1_node *field)
578 data1_node *subfield;
585 if (yaz_matchstr(field->u.tag.tag, pf->name))
588 subfield = field->child;
594 check subfield without indicators
597 if (!pf->list && subfield->which == DATA1N_data)
601 if (pf->interval.start == -1)
603 wrbuf_puts(buf, get_data(field, &len));
607 wrbuf_write(buf, get_data(field, &len)+pf->interval.start,
608 pf->interval.end-pf->interval.start);
612 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
621 ind1 = (subfield->u.tag.tag[0] == ' ') ? '_':subfield->u.tag.tag[0];
622 ind2 = (subfield->u.tag.tag[1] == ' ') ? '_':subfield->u.tag.tag[1];
625 ((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
626 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0]))
630 yaz_log(YLOG_WARN, "Field %s missed -- does not match indicators", field->u.tag.tag);
635 subfield = subfield->child;
640 cat_subfield(pf->list, buf, subfield);
643 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
649 static int is_empty(char *s)
655 if (!isspace(*(unsigned char *)p))
661 static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
664 data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root->u.root.absyn);
665 data1_node *top = root->child;
671 c = mc_mk_context(mc_stmnt+3);
680 mc_destroy_context(c);
685 yaz_log(YLOG_LOG, "parse_data1_tree(): statement -{%s}", mc_stmnt);
687 if (!yaz_matchstr(pf->name, "ldr"))
691 yaz_log(YLOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
692 pf->interval.start, pf->interval.end);
694 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
695 data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
696 pf->interval.end-pf->interval.start+1, new);
704 if (!yaz_matchstr(field->u.tag.tag, pf->name))
709 yaz_log(YLOG_LOG, "parse_data1_tree(): try field {%s}", field->u.tag.tag);
714 field = cat_field(p, pf, buf, field);
717 for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
721 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
722 data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
732 mc_destroy_field(pf);
733 mc_destroy_context(c);
737 data1_node *grs_read_marcxml(struct grs_read_info *p)
739 data1_node *root = grs_read_iso2709(p, 1);
745 for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
747 data1_tag *tag = e->tag;
749 if (tag && tag->which == DATA1T_string &&
750 !yaz_matchstr(tag->value.string, "mc?"))
751 parse_data1_tree(p, tag->value.string, root);
756 data1_node *grs_read_marc(struct grs_read_info *p)
758 data1_node *root = grs_read_iso2709(p, 0);
764 for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
766 data1_tag *tag = e->tag;
768 if (tag && tag->which == DATA1T_string &&
769 !yaz_matchstr(tag->value.string, "mc?"))
770 parse_data1_tree(p, tag->value.string, root);
775 static void *init_marc(Res res, RecType rt)
777 struct marc_info *p = xmalloc(sizeof(*p));
782 static void config_marc(void *clientData, Res res, const char *args)
784 struct marc_info *p = (struct marc_info*) clientData;
785 if (strlen(args) < sizeof(p->type))
786 strcpy(p->type, args);
789 static void destroy_marc(void *clientData)
791 struct marc_info *p = (struct marc_info*) clientData;
796 static int extract_marc(void *clientData, struct recExtractCtrl *ctrl)
798 return zebra_grs_extract(clientData, ctrl, grs_read_marc);
801 static int retrieve_marc(void *clientData, struct recRetrieveCtrl *ctrl)
803 return zebra_grs_retrieve(clientData, ctrl, grs_read_marc);
806 static struct recType marc_type = {
815 static int extract_marcxml(void *clientData, struct recExtractCtrl *ctrl)
817 return zebra_grs_extract(clientData, ctrl, grs_read_marcxml);
820 static int retrieve_marcxml(void *clientData, struct recRetrieveCtrl *ctrl)
822 return zebra_grs_retrieve(clientData, ctrl, grs_read_marcxml);
825 static struct recType marcxml_type = {
835 #ifdef IDZEBRA_STATIC_GRS_MARC
836 idzebra_filter_grs_marc