1 /* $Id: marcread.c,v 1.3 2006-08-22 13:39:27 adam Exp $
2 Copyright (C) 1995-2006
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
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;
50 int length_data_entry;
52 int length_implementation;
57 data1_node *res_root, *res_top;
59 data1_marctab *marctab;
61 if (p->stream->readf(p->stream, buf, 5) != 5)
63 while (*buf < '0' || *buf > '9')
67 yaz_log(YLOG_WARN, "MARC: Skipping bad byte %d (0x%02X)",
68 *buf & 0xff, *buf & 0xff);
72 if (p->stream->readf(p->stream, buf+4, 1) != 1)
75 record_length = atoi_n (buf, 5);
76 if (record_length < 25)
78 yaz_log (YLOG_WARN, "MARC record length < 25, is %d", record_length);
81 /* read remaining part - attempt to read one byte furhter... */
82 read_bytes = p->stream->readf(p->stream, buf+5, record_length-4);
83 if (read_bytes < record_length-5)
85 yaz_log (YLOG_WARN, "Couldn't read whole MARC record");
88 if (read_bytes == record_length - 4)
90 off_t cur_offset = p->stream->tellf(p->stream);
95 off_t end_offset = cur_offset - 1;
96 p->stream->endf(p->stream, &end_offset);
100 res_root = data1_mk_root (p->dh, p->mem, absynName);
103 yaz_log (YLOG_WARN, "cannot read MARC without an abstract syntax");
109 const char *attr[] = { "xmlns", "http://www.loc.gov/MARC21/slim", 0};
111 res_top = data1_mk_tag (p->dh, p->mem, "record", attr, res_root);
113 lead = data1_mk_tag(p->dh, p->mem, "leader", 0, res_top);
114 data1_mk_text_n(p->dh, p->mem, buf, 24, lead);
117 res_top = data1_mk_tag (p->dh, p->mem, absynName, 0, res_root);
119 if ((marctab = data1_absyn_getmarctab(p->dh, res_root->u.root.absyn)))
121 memcpy(marctab->leader, buf, 24);
122 memcpy(marctab->implementation_codes, buf+6, 4);
123 marctab->implementation_codes[4] = '\0';
124 memcpy(marctab->user_systems, buf+17, 3);
125 marctab->user_systems[3] = '\0';
128 if (marctab && marctab->force_indicator_length >= 0)
129 indicator_length = marctab->force_indicator_length;
131 indicator_length = atoi_n (buf+10, 1);
132 if (marctab && marctab->force_identifier_length >= 0)
133 identifier_length = marctab->force_identifier_length;
135 identifier_length = atoi_n (buf+11, 1);
136 base_address = atoi_n (buf+12, 5);
138 length_data_entry = atoi_n (buf+20, 1);
139 length_starting = atoi_n (buf+21, 1);
140 length_implementation = atoi_n (buf+22, 1);
142 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
144 int l = 3 + length_data_entry + length_starting;
145 if (entry_p + l >= record_length)
147 yaz_log(YLOG_WARN, "MARC: Directory offset %d: end of record.",
151 /* check for digits in length info */
153 if (!isdigit(*(const unsigned char *) (buf + entry_p+l)))
157 /* not all digits, so stop directory scan */
158 yaz_log(YLOG_LOG, "MARC: Bad directory");
161 entry_p += 3 + length_data_entry + length_starting;
163 end_of_directory = entry_p;
164 if (base_address != entry_p+1)
166 yaz_log(YLOG_WARN, "MARC: Base address does not follow directory");
168 for (entry_p = 24; entry_p != end_of_directory; )
176 data1_node *parent = res_top;
178 memcpy (tag, buf+entry_p, 3);
185 res = data1_mk_tag_n (p->dh, p->mem, tag, 3, 0 /* attr */, parent);
188 fprintf (outf, "%s ", tag);
190 data_length = atoi_n (buf+entry_p, length_data_entry);
191 entry_p += length_data_entry;
192 data_offset = atoi_n (buf+entry_p, length_starting);
193 entry_p += length_starting;
194 i = data_offset + base_address;
195 end_offset = i+data_length-1;
197 if (data_length <= 0 || data_offset < 0 || end_offset >= record_length)
199 yaz_log(YLOG_WARN, "MARC: Bad offsets in data. Skipping rest");
203 if (memcmp (tag, "00", 2) && indicator_length)
205 /* generate indicator node */
208 const char *attr[10];
215 res = data1_mk_tag(p->dh, p->mem, "datafield", attr, res);
217 for (j = 0; j<indicator_length; j++)
219 char str1[18], str2[2];
220 sprintf (str1, "ind%d", j+1);
227 data1_tag_add_attr (p->dh, p->mem, res, attr);
235 res = data1_mk_tag_n (p->dh, p->mem,
236 buf+i, indicator_length, 0 /* attr */, res);
238 for (j = 0; j<indicator_length; j++)
239 fprintf (outf, "%c", buf[j+i]);
242 i += indicator_length;
248 const char *attr[10];
254 res = data1_mk_tag(p->dh, p->mem, "controlfield", attr, res);
258 /* traverse sub fields */
260 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
262 if (memcmp (tag, "00", 2) && identifier_length)
271 for (j = 1; j<identifier_length && j < 9; j++)
272 code[j-1] = buf[i+j];
277 res = data1_mk_tag(p->dh, p->mem, "subfield",
282 res = data1_mk_tag_n (p->dh, p->mem,
283 buf+i+1, identifier_length-1,
284 0 /* attr */, parent);
287 fprintf (outf, " $");
288 for (j = 1; j<identifier_length; j++)
289 fprintf (outf, "%c", buf[j+i]);
292 i += identifier_length;
294 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
295 buf[i] != ISO2709_FS && i < end_offset)
298 fprintf (outf, "%c", buf[i]);
302 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, res);
308 fprintf (outf, "%c", buf[i]);
315 data1_mk_text_n (p->dh, p->mem, buf + i0, i - i0, parent);
318 fprintf (outf, "\n");
320 fprintf (outf, "-- separator but not at end of field\n");
321 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
322 fprintf (outf, "-- no separator at end of field\n");
329 * Locate some data under this node. This routine should handle variants
332 static char *get_data(data1_node *n, int *len)
338 if (n->which == DATA1N_data)
341 *len = n->u.data.len;
343 for (i = 0; i<*len; i++)
344 if (!d1_isspace(n->u.data.data[i]))
346 while (*len && d1_isspace(n->u.data.data[*len - 1]))
350 return n->u.data.data + i;
352 if (n->which == DATA1N_tag)
354 else if (n->which == DATA1N_data)
364 static data1_node *lookup_subfield(data1_node *node, const char *name)
368 for (p=node; p; p=p->next)
370 if (!yaz_matchstr(p->u.tag.tag, name))
376 static inline_subfield *lookup_inline_subfield(inline_subfield *pisf,
381 for (p=pisf; p; p=p->next)
383 if (!yaz_matchstr(p->name, name))
389 static inline_subfield *cat_inline_subfield(mc_subfield *psf, WRBUF buf,
390 inline_subfield *pisf)
394 for (p = psf; p && pisf; p = p->next)
396 if (p->which == MC_SF)
398 inline_subfield *found = lookup_inline_subfield(pisf, p->name);
402 if (strcmp(p->prefix, "_"))
404 wrbuf_puts(buf, " ");
405 wrbuf_puts(buf, p->prefix);
407 if (p->interval.start == -1)
409 wrbuf_puts(buf, found->data);
413 wrbuf_write(buf, found->data+p->interval.start,
414 p->interval.end-p->interval.start);
417 if (strcmp(p->suffix, "_"))
419 wrbuf_puts(buf, p->suffix);
420 wrbuf_puts(buf, " ");
423 yaz_log(YLOG_LOG, "cat_inline_subfield(): add subfield $%s", found->name);
428 else if (p->which == MC_SFVARIANT)
430 inline_subfield *next;
433 next = cat_inline_subfield(p->u.child, buf, pisf);
439 else if (p->which == MC_SFGROUP)
444 for (pp = p->u.child, found = 0; pp; pp = pp->next)
446 if (!yaz_matchstr(pisf->name, p->name))
454 wrbuf_puts(buf, " (");
455 pisf = cat_inline_subfield(p->u.child, buf, pisf);
456 wrbuf_puts(buf, ") ");
463 static void cat_inline_field(mc_field *pf, WRBUF buf, data1_node *subfield)
465 if (!pf || !subfield)
471 inline_field *pif=NULL;
474 if (yaz_matchstr(subfield->u.tag.tag, "1"))
476 subfield = subfield->next;
481 pif = inline_mk_field();
485 if ((i=inline_parse(pif, psubf->u.tag.tag, get_data(psubf, &len)))<0)
487 yaz_log(YLOG_WARN, "inline subfield ($%s): parse error",
489 inline_destroy_field(pif);
493 } while (psubf && yaz_matchstr(psubf->u.tag.tag, "1"));
497 if (pif && !yaz_matchstr(pif->name, pf->name))
499 if (!pf->list && pif->list)
501 wrbuf_puts(buf, pif->list->data);
511 ind1 = (pif->ind1[0] == ' ') ? '_':pif->ind1[0];
512 ind2 = (pif->ind2[0] == ' ') ? '_':pif->ind2[0];
514 if (((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
515 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0])))
517 cat_inline_subfield(pf->list, buf, pif->list);
520 add separator for inline fields
524 wrbuf_puts(buf, "\n");
529 yaz_log(YLOG_WARN, "In-line field %s missed -- indicators do not match", pif->name);
533 inline_destroy_field(pif);
536 yaz_log(YLOG_LOG, "cat_inline_field(): got buffer {%s}", buf);
540 static data1_node *cat_subfield(mc_subfield *psf, WRBUF buf,
541 data1_node *subfield)
545 for (p = psf; p && subfield; p = p->next)
547 if (p->which == MC_SF)
549 data1_node *found = lookup_subfield(subfield, p->name);
555 if (strcmp(p->prefix, "_"))
557 wrbuf_puts(buf, " ");
558 wrbuf_puts(buf, p->prefix);
563 cat_inline_field(p->u.in_line, buf, found);
565 else if (p->interval.start == -1)
567 wrbuf_puts(buf, get_data(found, &len));
571 wrbuf_write(buf, get_data(found, &len)+p->interval.start,
572 p->interval.end-p->interval.start);
575 if (strcmp(p->suffix, "_"))
577 wrbuf_puts(buf, p->suffix);
578 wrbuf_puts(buf, " ");
581 yaz_log(YLOG_LOG, "cat_subfield(): add subfield $%s", found->u.tag.tag);
583 subfield = found->next;
586 else if (p->which == MC_SFVARIANT)
590 next = cat_subfield(p->u.child, buf, subfield);
591 if (next == subfield)
596 else if (p->which == MC_SFGROUP)
601 for (pp = p->u.child, found = 0; pp; pp = pp->next)
603 if (!yaz_matchstr(subfield->u.tag.tag, pp->name))
611 wrbuf_puts(buf, " (");
612 subfield = cat_subfield(p->u.child, buf, subfield);
613 wrbuf_puts(buf, ") ");
620 static data1_node *cat_field(struct grs_read_info *p, mc_field *pf,
621 WRBUF buf, data1_node *field)
623 data1_node *subfield;
630 if (yaz_matchstr(field->u.tag.tag, pf->name))
633 subfield = field->child;
639 check subfield without indicators
642 if (!pf->list && subfield->which == DATA1N_data)
646 if (pf->interval.start == -1)
648 wrbuf_puts(buf, get_data(field, &len));
652 wrbuf_write(buf, get_data(field, &len)+pf->interval.start,
653 pf->interval.end-pf->interval.start);
657 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
666 ind1 = (subfield->u.tag.tag[0] == ' ') ? '_':subfield->u.tag.tag[0];
667 ind2 = (subfield->u.tag.tag[1] == ' ') ? '_':subfield->u.tag.tag[1];
670 ((pf->ind1[0] == '.') || (ind1 == pf->ind1[0])) &&
671 ((pf->ind2[0] == '.') || (ind2 == pf->ind2[0]))
675 yaz_log(YLOG_WARN, "Field %s missed -- does not match indicators", field->u.tag.tag);
680 subfield = subfield->child;
685 cat_subfield(pf->list, buf, subfield);
688 yaz_log(YLOG_LOG, "cat_field(): got buffer {%s}", buf);
694 static int is_empty(char *s)
700 if (!isspace(*(unsigned char *)p))
706 static void parse_data1_tree(struct grs_read_info *p, const char *mc_stmnt,
709 data1_marctab *marctab = data1_absyn_getmarctab(p->dh, root->u.root.absyn);
710 data1_node *top = root->child;
716 c = mc_mk_context(mc_stmnt+3);
725 mc_destroy_context(c);
730 yaz_log(YLOG_LOG, "parse_data1_tree(): statement -{%s}", mc_stmnt);
732 if (!yaz_matchstr(pf->name, "ldr"))
736 yaz_log(YLOG_LOG,"parse_data1_tree(): try LEADER from {%d} to {%d} positions",
737 pf->interval.start, pf->interval.end);
739 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
740 data1_mk_text_n(p->dh, p->mem, marctab->leader+pf->interval.start,
741 pf->interval.end-pf->interval.start+1, new);
749 if (!yaz_matchstr(field->u.tag.tag, pf->name))
754 yaz_log(YLOG_LOG, "parse_data1_tree(): try field {%s}", field->u.tag.tag);
759 field = cat_field(p, pf, buf, field);
762 for (pb = strtok(pb, "\n"); pb; pb = strtok(NULL, "\n"))
766 new = data1_mk_tag_n(p->dh, p->mem, mc_stmnt, strlen(mc_stmnt), 0, top);
767 data1_mk_text_n(p->dh, p->mem, pb, strlen(pb), new);
777 mc_destroy_field(pf);
778 mc_destroy_context(c);
782 data1_node *grs_read_marcxml(struct grs_read_info *p)
784 data1_node *root = grs_read_iso2709(p, 1);
790 for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
792 data1_tag *tag = e->tag;
794 if (tag && tag->which == DATA1T_string &&
795 !yaz_matchstr(tag->value.string, "mc?"))
796 parse_data1_tree(p, tag->value.string, root);
801 data1_node *grs_read_marc(struct grs_read_info *p)
803 data1_node *root = grs_read_iso2709(p, 0);
809 for (e = data1_absyn_getelements(p->dh, root->u.root.absyn); e; e=e->next)
811 data1_tag *tag = e->tag;
813 if (tag && tag->which == DATA1T_string &&
814 !yaz_matchstr(tag->value.string, "mc?"))
815 parse_data1_tree(p, tag->value.string, root);
820 static void *init_marc(Res res, RecType rt)
822 struct marc_info *p = xmalloc(sizeof(*p));
827 static ZEBRA_RES config_marc(void *clientData, Res res, const char *args)
829 struct marc_info *p = (struct marc_info*) clientData;
830 if (strlen(args) < sizeof(p->type))
831 strcpy(p->type, args);
835 static void destroy_marc(void *clientData)
837 struct marc_info *p = (struct marc_info*) clientData;
842 static int extract_marc(void *clientData, struct recExtractCtrl *ctrl)
844 return zebra_grs_extract(clientData, ctrl, grs_read_marc);
847 static int retrieve_marc(void *clientData, struct recRetrieveCtrl *ctrl)
849 return zebra_grs_retrieve(clientData, ctrl, grs_read_marc);
852 static struct recType marc_type = {
862 static int extract_marcxml(void *clientData, struct recExtractCtrl *ctrl)
864 return zebra_grs_extract(clientData, ctrl, grs_read_marcxml);
867 static int retrieve_marcxml(void *clientData, struct recRetrieveCtrl *ctrl)
869 return zebra_grs_retrieve(clientData, ctrl, grs_read_marcxml);
872 static struct recType marcxml_type = {
883 #ifdef IDZEBRA_STATIC_GRS_MARC
884 idzebra_filter_grs_marc
898 * indent-tabs-mode: nil
900 * vim: shiftwidth=4 tabstop=8 expandtab