2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.22 2005-06-07 19:25:38 adam Exp $
10 * \brief Implements MARC display - and conversion utilities
20 #include <yaz/marcdisp.h>
21 #include <yaz/wrbuf.h>
22 #include <yaz/yaz-util.h>
33 yaz_marc_t yaz_marc_create(void)
35 yaz_marc_t mt = (yaz_marc_t) xmalloc(sizeof(*mt));
36 mt->xml = YAZ_MARC_LINE;
38 mt->m_wr = wrbuf_alloc();
40 strcpy(mt->subfield_str, " $");
41 strcpy(mt->endline_str, "\n");
45 void yaz_marc_subfield_str(yaz_marc_t mt, const char *s)
47 strncpy(mt->subfield_str, s, sizeof(mt->subfield_str)-1);
48 mt->subfield_str[sizeof(mt->subfield_str)-1] = '\0';
51 void yaz_marc_endline_str(yaz_marc_t mt, const char *s)
53 strncpy(mt->endline_str, s, sizeof(mt->endline_str)-1);
54 mt->endline_str[sizeof(mt->endline_str)-1] = '\0';
57 void yaz_marc_destroy(yaz_marc_t mt)
61 wrbuf_free (mt->m_wr, 1);
65 static void marc_cdata (yaz_marc_t mt, const char *buf, size_t len, WRBUF wr)
67 if (mt->xml == YAZ_MARC_ISO2709)
68 wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
69 else if (mt->xml == YAZ_MARC_LINE)
70 wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
72 wrbuf_iconv_write_cdata(wr, mt->iconv_cd, buf, len);
75 static int atoi_n_check(const char *buf, int size, int *val)
77 if (!isdigit(*(const unsigned char *) buf))
79 *val = atoi_n(buf, size);
83 int yaz_marc_decode_wrbuf (yaz_marc_t mt, const char *buf, int bsize, WRBUF wr)
88 int identifier_length;
91 int length_data_entry;
93 int length_implementation;
95 int produce_warnings = 0;
99 if (mt->xml == YAZ_MARC_SIMPLEXML || mt->xml == YAZ_MARC_OAIMARC
100 || mt->xml == YAZ_MARC_MARCXML || mt->xml == YAZ_MARC_XCHANGE)
101 produce_warnings = 1;
103 record_length = atoi_n (buf, 5);
104 if (record_length < 25)
107 wrbuf_printf(wr, "<!-- Record length %d - aborting -->\n",
111 memcpy(lead, buf, 24); /* se can modify the header for output */
113 /* ballout if bsize is known and record_length is less than that */
114 if (bsize != -1 && record_length > bsize)
116 if (!atoi_n_check(buf+10, 1, &indicator_length))
118 if (produce_warnings)
119 wrbuf_printf(wr, "<!-- Indicator length at offset 10 should hold a digit. Assuming 2 -->\n");
121 indicator_length = 2;
123 if (!atoi_n_check(buf+11, 1, &identifier_length))
125 if (produce_warnings)
126 wrbuf_printf(wr, "<!-- Identifier length at offset 11 should hold a digit. Assuming 2 -->\n");
128 identifier_length = 2;
130 if (!atoi_n_check(buf+12, 5, &base_address))
132 if (produce_warnings)
133 wrbuf_printf(wr, "<!-- Base address at offsets 12..16 should hold a number. Assuming 0 -->\n");
136 if (!atoi_n_check(buf+20, 1, &length_data_entry))
138 if (produce_warnings)
139 wrbuf_printf(wr, "<!-- Length data entry at offset 20 should hold a digit. Assuming 4 -->\n");
140 length_data_entry = 4;
143 if (!atoi_n_check(buf+21, 1, &length_starting))
145 if (produce_warnings)
146 wrbuf_printf(wr, "<!-- Length starting at offset 21 should hold a digit. Assuming 5 -->\n");
150 if (!atoi_n_check(buf+22, 1, &length_implementation))
152 if (produce_warnings)
153 wrbuf_printf(wr, "<!-- Length implementation at offset 22 should hold a digit. Assuming 0 -->\n");
154 length_implementation = 0;
158 if (mt->xml != YAZ_MARC_LINE)
164 case YAZ_MARC_ISO2709:
166 case YAZ_MARC_SIMPLEXML:
167 wrbuf_puts (wr, "<iso2709\n");
168 sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
169 wrbuf_puts (wr, str);
170 sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
171 wrbuf_puts (wr, str);
172 for (i = 1; i<=19; i++)
174 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
175 wrbuf_puts (wr, str);
177 wrbuf_puts (wr, ">\n");
179 case YAZ_MARC_OAIMARC:
182 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
184 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
186 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
190 sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
191 buf[5], buf[6], buf[7]);
192 wrbuf_puts (wr, str);
194 case YAZ_MARC_MARCXML:
197 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
199 lead[9] = 'a'; /* set leader to signal unicode */
200 marc_cdata(mt, lead, 24, wr);
201 wrbuf_printf(wr, "</leader>\n");
203 case YAZ_MARC_XCHANGE:
206 "<record xmlns=\"http://www.bs.dk/standards/MarcXchange\">\n"
208 marc_cdata(mt, lead, 24, wr);
209 wrbuf_printf(wr, "</leader>\n");
217 wrbuf_puts (wr, "<!--\n");
218 sprintf (str, "Record length %5d\n", record_length);
219 wrbuf_puts (wr, str);
220 sprintf (str, "Indicator length %5d\n", indicator_length);
221 wrbuf_puts (wr, str);
222 sprintf (str, "Identifier length %5d\n", identifier_length);
223 wrbuf_puts (wr, str);
224 sprintf (str, "Base address %5d\n", base_address);
225 wrbuf_puts (wr, str);
226 sprintf (str, "Length data entry %5d\n", length_data_entry);
227 wrbuf_puts (wr, str);
228 sprintf (str, "Length starting %5d\n", length_starting);
229 wrbuf_puts (wr, str);
230 sprintf (str, "Length implementation %5d\n", length_implementation);
231 wrbuf_puts (wr, str);
232 wrbuf_puts (wr, "-->\n");
235 /* first pass. determine length of directory & base of data */
236 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
238 /* length of directory entry */
239 int l = 3 + length_data_entry + length_starting;
240 if (entry_p + l >= record_length)
242 wrbuf_printf (wr, "<!-- Directory offset %d: end of record. "
243 "Missing FS char -->\n", entry_p);
247 wrbuf_printf (wr, "<!-- Directory offset %d: Tag %.3s -->\n",
248 entry_p, buf+entry_p);
249 /* check for digits in length info */
251 if (!isdigit(*(const unsigned char *) (buf + entry_p+l)))
255 /* not all digits, so stop directory scan */
256 wrbuf_printf (wr, "<!-- Directory offset %d: Bad data for data "
257 "length and/or length starting -->\n", entry_p);
260 entry_p += 3 + length_data_entry + length_starting;
262 end_of_directory = entry_p;
263 if (base_address != entry_p+1)
265 if (produce_warnings)
266 wrbuf_printf (wr,"<!-- Base address not at end of directory, "
267 "base %d, end %d -->\n", base_address, entry_p+1);
269 if (mt->xml == YAZ_MARC_ISO2709)
271 WRBUF wr_head = wrbuf_alloc();
272 WRBUF wr_dir = wrbuf_alloc();
273 WRBUF wr_tmp = wrbuf_alloc();
276 /* second pass. create directory for ISO2709 output */
277 for (entry_p = 24; entry_p != end_of_directory; )
279 int data_length, data_offset, end_offset;
282 wrbuf_write(wr_dir, buf+entry_p, 3);
285 data_length = atoi_n (buf+entry_p, length_data_entry);
286 entry_p += length_data_entry;
287 data_offset = atoi_n (buf+entry_p, length_starting);
288 entry_p += length_starting;
289 i = data_offset + base_address;
290 end_offset = i+data_length-1;
292 if (data_length <= 0 || data_offset < 0 || end_offset >= record_length)
295 while (i < end_offset &&
296 buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
298 sz1 = 1+i - (data_offset + base_address);
301 sz2 = wrbuf_iconv_write(wr_tmp, mt->iconv_cd,
302 buf + data_offset+base_address, sz1);
303 wrbuf_rewind(wr_tmp);
307 wrbuf_printf(wr_dir, "%0*d", length_data_entry, sz2);
308 wrbuf_printf(wr_dir, "%0*d", length_starting, data_p);
311 wrbuf_putc(wr_dir, ISO2709_FS);
312 wrbuf_printf(wr_head, "%05d", data_p+1 + base_address);
313 wrbuf_write(wr_head, lead+5, 7);
314 wrbuf_printf(wr_head, "%05d", base_address);
315 wrbuf_write(wr_head, lead+17, 7);
317 wrbuf_write(wr, wrbuf_buf(wr_head), 24);
318 wrbuf_write(wr, wrbuf_buf(wr_dir), wrbuf_len(wr_dir));
319 wrbuf_free(wr_head, 1);
320 wrbuf_free(wr_dir, 1);
321 wrbuf_free(wr_tmp, 1);
323 /* third pass. create data output */
324 for (entry_p = 24; entry_p != end_of_directory; )
331 int identifier_flag = 0;
332 int entry_p0 = entry_p;
334 memcpy (tag, buf+entry_p, 3);
337 data_length = atoi_n (buf+entry_p, length_data_entry);
338 entry_p += length_data_entry;
339 data_offset = atoi_n (buf+entry_p, length_starting);
340 entry_p += length_starting;
341 i = data_offset + base_address;
342 end_offset = i+data_length-1;
344 if (data_length <= 0 || data_offset < 0)
349 wrbuf_printf(wr, "<!-- Directory offset %d: data-length %d, "
350 "data-offset %d -->\n",
351 entry_p0, data_length, data_offset);
353 if (end_offset >= record_length)
355 wrbuf_printf (wr,"<!-- Directory offset %d: Data out of bounds "
357 entry_p0, end_offset, record_length);
361 if (memcmp (tag, "00", 2))
362 identifier_flag = 1; /* if not 00X assume subfields */
363 else if (indicator_length < 4 && indicator_length > 0)
365 /* Danmarc 00X have subfields */
366 if (buf[i + indicator_length] == ISO2709_IDFS)
368 else if (buf[i + indicator_length + 1] == ISO2709_IDFS)
374 wrbuf_printf(wr, "<!-- identifier_flag = %d -->\n",
381 wrbuf_puts (wr, tag);
382 wrbuf_puts (wr, " ");
384 case YAZ_MARC_SIMPLEXML:
385 wrbuf_printf (wr, "<field tag=\"");
386 marc_cdata(mt, tag, strlen(tag), wr);
387 wrbuf_printf(wr, "\"");
389 case YAZ_MARC_OAIMARC:
391 wrbuf_printf (wr, " <varfield id=\"");
393 wrbuf_printf (wr, " <fixfield id=\"");
394 marc_cdata(mt, tag, strlen(tag), wr);
395 wrbuf_printf(wr, "\"");
397 case YAZ_MARC_MARCXML:
398 case YAZ_MARC_XCHANGE:
400 wrbuf_printf (wr, " <datafield tag=\"");
402 wrbuf_printf (wr, " <controlfield tag=\"");
403 marc_cdata(mt, tag, strlen(tag), wr);
404 wrbuf_printf(wr, "\"");
409 i += identifier_flag-1;
410 for (j = 0; j<indicator_length; j++, i++)
414 case YAZ_MARC_ISO2709:
415 wrbuf_putc(wr, buf[i]);
418 wrbuf_putc(wr, buf[i]);
420 case YAZ_MARC_SIMPLEXML:
421 wrbuf_printf(wr, " Indicator%d=\"", j+1);
422 marc_cdata(mt, buf+i, 1, wr);
423 wrbuf_printf(wr, "\"");
425 case YAZ_MARC_OAIMARC:
426 wrbuf_printf(wr, " i%d=\"", j+1);
427 marc_cdata(mt, buf+i, 1, wr);
428 wrbuf_printf(wr, "\"");
430 case YAZ_MARC_MARCXML:
431 case YAZ_MARC_XCHANGE:
432 wrbuf_printf(wr, " ind%d=\"", j+1);
433 marc_cdata(mt, buf+i, 1, wr);
434 wrbuf_printf(wr, "\"");
438 if (mt->xml == YAZ_MARC_SIMPLEXML || mt->xml == YAZ_MARC_MARCXML
439 || mt->xml == YAZ_MARC_OAIMARC || mt->xml == YAZ_MARC_XCHANGE)
441 wrbuf_puts (wr, ">");
443 wrbuf_puts (wr, "\n");
447 while (i < end_offset &&
448 buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
454 case YAZ_MARC_ISO2709:
456 wrbuf_iconv_write(wr, mt->iconv_cd,
457 buf+i, identifier_length);
458 i += identifier_length;
461 wrbuf_puts (wr, mt->subfield_str);
462 marc_cdata(mt, buf+i, identifier_length-1, wr);
463 i = i+identifier_length-1;
464 wrbuf_putc (wr, ' ');
466 case YAZ_MARC_SIMPLEXML:
467 wrbuf_puts (wr, " <subfield code=\"");
468 marc_cdata(mt, buf+i, identifier_length-1, wr);
469 i = i+identifier_length-1;
470 wrbuf_puts (wr, "\">");
472 case YAZ_MARC_OAIMARC:
473 wrbuf_puts (wr, " <subfield label=\"");
474 marc_cdata(mt, buf+i, identifier_length-1, wr);
475 i = i+identifier_length-1;
476 wrbuf_puts (wr, "\">");
478 case YAZ_MARC_MARCXML:
479 case YAZ_MARC_XCHANGE:
480 wrbuf_puts (wr, " <subfield code=\"");
481 marc_cdata(mt, buf+i, identifier_length-1, wr);
482 i = i+identifier_length-1;
483 wrbuf_puts (wr, "\">");
487 while (i < end_offset &&
488 buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
489 buf[i] != ISO2709_FS)
491 marc_cdata(mt, buf + i0, i - i0, wr);
493 if (mt->xml == YAZ_MARC_ISO2709 && buf[i] != ISO2709_IDFS)
494 marc_cdata(mt, buf + i, 1, wr);
496 if (mt->xml == YAZ_MARC_SIMPLEXML ||
497 mt->xml == YAZ_MARC_MARCXML ||
498 mt->xml == YAZ_MARC_XCHANGE ||
499 mt->xml == YAZ_MARC_OAIMARC)
500 wrbuf_puts (wr, "</subfield>\n");
506 while (i < end_offset &&
507 buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
509 marc_cdata(mt, buf + i0, i - i0, wr);
510 if (mt->xml == YAZ_MARC_ISO2709)
511 marc_cdata(mt, buf + i, 1, wr);
513 if (mt->xml == YAZ_MARC_LINE)
514 wrbuf_puts (wr, mt->endline_str);
516 wrbuf_printf(wr, "<!-- separator but not at end of field length=%d-->\n", data_length);
517 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
518 wrbuf_printf(wr, "<!-- no separator at end of field length=%d-->\n", data_length);
521 case YAZ_MARC_SIMPLEXML:
522 wrbuf_puts (wr, "</field>\n");
524 case YAZ_MARC_OAIMARC:
526 wrbuf_puts (wr, "</varfield>\n");
528 wrbuf_puts (wr, "</fixfield>\n");
530 case YAZ_MARC_MARCXML:
531 case YAZ_MARC_XCHANGE:
533 wrbuf_puts (wr, " </datafield>\n");
535 wrbuf_puts (wr, "</controlfield>\n");
544 case YAZ_MARC_SIMPLEXML:
545 wrbuf_puts (wr, "</iso2709>\n");
547 case YAZ_MARC_OAIMARC:
548 wrbuf_puts (wr, "</oai_marc>\n");
550 case YAZ_MARC_MARCXML:
551 case YAZ_MARC_XCHANGE:
552 wrbuf_puts (wr, "</record>\n");
554 case YAZ_MARC_ISO2709:
555 wrbuf_putc (wr, ISO2709_RS);
558 return record_length;
561 int yaz_marc_decode_buf (yaz_marc_t mt, const char *buf, int bsize,
562 char **result, int *rsize)
566 wrbuf_rewind(mt->m_wr);
567 r = yaz_marc_decode_wrbuf(mt, buf, bsize, mt->m_wr);
569 *result = wrbuf_buf(mt->m_wr);
571 *rsize = wrbuf_len(mt->m_wr);
575 void yaz_marc_xml(yaz_marc_t mt, int xmlmode)
581 void yaz_marc_debug(yaz_marc_t mt, int level)
587 void yaz_marc_iconv(yaz_marc_t mt, yaz_iconv_t cd)
593 int yaz_marc_decode(const char *buf, WRBUF wr, int debug, int bsize, int xml)
595 yaz_marc_t mt = yaz_marc_create();
600 r = yaz_marc_decode_wrbuf(mt, buf, bsize, wr);
601 yaz_marc_destroy(mt);
606 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug, int bsize)
608 return yaz_marc_decode(buf, wr, debug, bsize, 0);
612 int marc_display_exl (const char *buf, FILE *outf, int debug, int bsize)
614 yaz_marc_t mt = yaz_marc_create();
618 r = yaz_marc_decode_wrbuf (mt, buf, bsize, mt->m_wr);
622 fwrite (wrbuf_buf(mt->m_wr), 1, wrbuf_len(mt->m_wr), outf);
623 yaz_marc_destroy(mt);
628 int marc_display_ex (const char *buf, FILE *outf, int debug)
630 return marc_display_exl (buf, outf, debug, -1);
634 int marc_display (const char *buf, FILE *outf)
636 return marc_display_ex (buf, outf, 0);