2 * Copyright (c) 1995-2004, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.9 2004-11-25 09:43:10 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>
31 yaz_marc_t yaz_marc_create(void)
33 yaz_marc_t mt = (yaz_marc_t) xmalloc(sizeof(*mt));
34 mt->xml = YAZ_MARC_LINE;
36 mt->m_wr = wrbuf_alloc();
41 void yaz_marc_destroy(yaz_marc_t mt)
45 wrbuf_free (mt->m_wr, 1);
49 static void marc_cdata (yaz_marc_t mt, const char *buf, size_t len, WRBUF wr)
51 if (mt->xml == YAZ_MARC_ISO2709)
52 wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
53 else if (mt->xml == YAZ_MARC_LINE)
54 wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
56 wrbuf_iconv_write_cdata(wr, mt->iconv_cd, buf, len);
59 int yaz_marc_decode_wrbuf (yaz_marc_t mt, const char *buf, int bsize, WRBUF wr)
64 int identifier_length;
66 int length_data_entry;
68 int length_implementation;
72 record_length = atoi_n (buf, 5);
73 if (record_length < 25)
79 sprintf (str, "Record length %d - aborting\n", record_length);
84 /* ballout if bsize is known and record_length is less than that */
85 if (bsize != -1 && record_length > bsize)
88 indicator_length = atoi_n (buf+10, 1);
92 identifier_length = atoi_n (buf+11, 1);
94 identifier_length = 2;
95 base_address = atoi_n (buf+12, 5);
97 length_data_entry = atoi_n (buf+20, 1);
98 length_starting = atoi_n (buf+21, 1);
99 length_implementation = atoi_n (buf+22, 1);
101 if (mt->xml != YAZ_MARC_LINE)
107 case YAZ_MARC_ISO2709:
109 case YAZ_MARC_SIMPLEXML:
110 wrbuf_puts (wr, "<iso2709\n");
111 sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
112 wrbuf_puts (wr, str);
113 sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
114 wrbuf_puts (wr, str);
115 for (i = 1; i<=19; i++)
117 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
118 wrbuf_puts (wr, str);
120 wrbuf_puts (wr, ">\n");
122 case YAZ_MARC_OAIMARC:
125 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
127 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
129 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
133 sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
134 buf[5], buf[6], buf[7]);
135 wrbuf_puts (wr, str);
137 case YAZ_MARC_MARCXML:
140 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
143 marc_cdata(mt, buf, 9, wr);
144 marc_cdata(mt, "a", 1, wr); /* set leader to signal unicode */
145 marc_cdata(mt, buf+10, 14, wr);
147 marc_cdata(mt, buf, 24, wr); /* leave header as is .. */
149 wrbuf_printf(wr, "</leader>\n");
158 wrbuf_puts (wr, "<!--\n");
159 sprintf (str, "Record length %5d\n", record_length);
160 wrbuf_puts (wr, str);
161 sprintf (str, "Indicator length %5d\n", indicator_length);
162 wrbuf_puts (wr, str);
163 sprintf (str, "Identifier length %5d\n", identifier_length);
164 wrbuf_puts (wr, str);
165 sprintf (str, "Base address %5d\n", base_address);
166 wrbuf_puts (wr, str);
167 sprintf (str, "Length data entry %5d\n", length_data_entry);
168 wrbuf_puts (wr, str);
169 sprintf (str, "Length starting %5d\n", length_starting);
170 wrbuf_puts (wr, str);
171 sprintf (str, "Length implementation %5d\n", length_implementation);
172 wrbuf_puts (wr, str);
174 wrbuf_puts (wr, "-->\n");
177 /* first pass. determine length of directory & base of data */
178 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
180 entry_p += 3+length_data_entry+length_starting;
181 if (entry_p >= record_length)
184 if (mt->debug && base_address != entry_p+1)
186 wrbuf_printf (wr," <!-- base address not at end of directory "
187 "base=%d end=%d -->\n", base_address, entry_p+1);
189 base_address = entry_p+1;
191 if (mt->xml == YAZ_MARC_ISO2709)
193 WRBUF wr_head = wrbuf_alloc();
194 WRBUF wr_dir = wrbuf_alloc();
195 WRBUF wr_tmp = wrbuf_alloc();
198 /* second pass. create directory for ISO2709 output */
199 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
201 int data_length, data_offset, end_offset;
204 wrbuf_write(wr_dir, buf+entry_p, 3);
207 data_length = atoi_n (buf+entry_p, length_data_entry);
208 entry_p += length_data_entry;
209 data_offset = atoi_n (buf+entry_p, length_starting);
210 entry_p += length_starting;
211 i = data_offset + base_address;
212 end_offset = i+data_length-1;
214 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS &&
217 sz1 = 1+i - (data_offset + base_address);
220 sz2 = wrbuf_iconv_write(wr_tmp, mt->iconv_cd,
221 buf + data_offset+base_address, sz1);
222 wrbuf_rewind(wr_tmp);
226 wrbuf_printf(wr_dir, "%0*d", length_data_entry, sz2);
227 wrbuf_printf(wr_dir, "%0*d", length_starting, data_p);
230 wrbuf_putc(wr_dir, ISO2709_FS);
231 wrbuf_printf(wr_head, "%05d", data_p+1 + base_address);
232 wrbuf_write(wr_head, buf+5, 7);
233 wrbuf_printf(wr_head, "%05d", base_address);
234 wrbuf_write(wr_head, buf+17, 7);
236 wrbuf_write(wr, wrbuf_buf(wr_head), 24);
237 wrbuf_write(wr, wrbuf_buf(wr_dir), wrbuf_len(wr_dir));
238 wrbuf_free(wr_head, 1);
239 wrbuf_free(wr_dir, 1);
240 wrbuf_free(wr_tmp, 1);
242 /* third pass. create data output */
243 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
250 int identifier_flag = 0;
252 memcpy (tag, buf+entry_p, 3);
255 data_length = atoi_n (buf+entry_p, length_data_entry);
256 entry_p += length_data_entry;
257 data_offset = atoi_n (buf+entry_p, length_starting);
258 entry_p += length_starting;
259 i = data_offset + base_address;
260 end_offset = i+data_length-1;
262 if (indicator_length < 4 && indicator_length > 0)
264 if (buf[i + indicator_length] == ISO2709_IDFS)
266 else if (buf[i + indicator_length + 1] == ISO2709_IDFS)
269 else if (memcmp (tag, "00", 2))
276 wrbuf_puts (wr, "Tag: ");
277 wrbuf_puts (wr, tag);
278 wrbuf_puts (wr, " ");
280 case YAZ_MARC_SIMPLEXML:
281 wrbuf_printf (wr, "<field tag=\"");
282 marc_cdata(mt, tag, strlen(tag), wr);
283 wrbuf_printf(wr, "\"");
285 case YAZ_MARC_OAIMARC:
287 wrbuf_printf (wr, " <varfield id=\"");
289 wrbuf_printf (wr, " <fixfield id=\"");
290 marc_cdata(mt, tag, strlen(tag), wr);
291 wrbuf_printf(wr, "\"");
293 case YAZ_MARC_MARCXML:
295 wrbuf_printf (wr, " <datafield tag=\"");
297 wrbuf_printf (wr, " <controlfield tag=\"");
298 marc_cdata(mt, tag, strlen(tag), wr);
299 wrbuf_printf(wr, "\"");
304 i += identifier_flag-1;
305 for (j = 0; j<indicator_length; j++, i++)
309 case YAZ_MARC_ISO2709:
310 wrbuf_putc(wr, buf[i]);
314 wrbuf_puts (wr, " Ind: ");
315 wrbuf_putc(wr, buf[i]);
317 case YAZ_MARC_SIMPLEXML:
318 wrbuf_printf(wr, " Indicator%d=\"", j+1);
319 marc_cdata(mt, buf+i, 1, wr);
320 wrbuf_printf(wr, "\"");
322 case YAZ_MARC_OAIMARC:
323 wrbuf_printf(wr, " i%d=\"", j+1);
324 marc_cdata(mt, buf+i, 1, wr);
325 wrbuf_printf(wr, "\"");
327 case YAZ_MARC_MARCXML:
328 wrbuf_printf(wr, " ind%d=\"", j+1);
329 marc_cdata(mt, buf+i, 1, wr);
330 wrbuf_printf(wr, "\"");
334 if (mt->xml == YAZ_MARC_SIMPLEXML || mt->xml == YAZ_MARC_MARCXML
335 || mt->xml == YAZ_MARC_OAIMARC)
337 wrbuf_puts (wr, ">");
339 wrbuf_puts (wr, "\n");
341 if (mt->xml == YAZ_MARC_LINE)
344 wrbuf_puts (wr, " Fields: ");
348 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
354 case YAZ_MARC_ISO2709:
356 wrbuf_iconv_write(wr, mt->iconv_cd,
357 buf+i, identifier_length);
358 i += identifier_length;
361 wrbuf_puts (wr, " $");
362 marc_cdata(mt, buf+i, identifier_length-1, wr);
363 i = i+identifier_length-1;
364 wrbuf_putc (wr, ' ');
366 case YAZ_MARC_SIMPLEXML:
367 wrbuf_puts (wr, " <subfield code=\"");
368 marc_cdata(mt, buf+i, identifier_length-1, wr);
369 i = i+identifier_length-1;
370 wrbuf_puts (wr, "\">");
372 case YAZ_MARC_OAIMARC:
373 wrbuf_puts (wr, " <subfield label=\"");
374 marc_cdata(mt, buf+i, identifier_length-1, wr);
375 i = i+identifier_length-1;
376 wrbuf_puts (wr, "\">");
378 case YAZ_MARC_MARCXML:
379 wrbuf_puts (wr, " <subfield code=\"");
380 marc_cdata(mt, buf+i, identifier_length-1, wr);
381 i = i+identifier_length-1;
382 wrbuf_puts (wr, "\">");
386 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
387 buf[i] != ISO2709_FS && i < end_offset)
389 marc_cdata(mt, buf + i0, i - i0, wr);
391 if (mt->xml == YAZ_MARC_ISO2709 && buf[i] != ISO2709_IDFS)
392 marc_cdata(mt, buf + i, 1, wr);
394 if (mt->xml == YAZ_MARC_SIMPLEXML ||
395 mt->xml == YAZ_MARC_MARCXML ||
396 mt->xml == YAZ_MARC_OAIMARC)
397 wrbuf_puts (wr, "</subfield>\n");
403 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
405 marc_cdata(mt, buf + i0, i - i0, wr);
406 if (mt->xml == YAZ_MARC_ISO2709)
407 marc_cdata(mt, buf + i, 1, wr);
409 if (mt->xml == YAZ_MARC_LINE)
410 wrbuf_putc (wr, '\n');
412 wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
413 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
414 wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
417 case YAZ_MARC_SIMPLEXML:
418 wrbuf_puts (wr, "</field>\n");
420 case YAZ_MARC_OAIMARC:
422 wrbuf_puts (wr, " </varfield>\n");
424 wrbuf_puts (wr, " </fixfield>\n");
426 case YAZ_MARC_MARCXML:
428 wrbuf_puts (wr, " </datafield>\n");
430 wrbuf_puts (wr, " </controlfield>\n");
439 case YAZ_MARC_SIMPLEXML:
440 wrbuf_puts (wr, "</iso2709>\n");
442 case YAZ_MARC_OAIMARC:
443 wrbuf_puts (wr, "</oai_marc>\n");
445 case YAZ_MARC_MARCXML:
446 wrbuf_puts (wr, "</record>\n");
448 case YAZ_MARC_ISO2709:
449 wrbuf_putc (wr, ISO2709_RS);
452 return record_length;
455 int yaz_marc_decode_buf (yaz_marc_t mt, const char *buf, int bsize,
456 char **result, int *rsize)
458 int r = yaz_marc_decode_wrbuf(mt, buf, bsize, mt->m_wr);
462 *result = wrbuf_buf(mt->m_wr);
464 *rsize = wrbuf_len(mt->m_wr);
469 void yaz_marc_xml(yaz_marc_t mt, int xmlmode)
475 void yaz_marc_debug(yaz_marc_t mt, int level)
481 void yaz_marc_iconv(yaz_marc_t mt, yaz_iconv_t cd)
487 int yaz_marc_decode(const char *buf, WRBUF wr, int debug, int bsize, int xml)
489 yaz_marc_t mt = yaz_marc_create();
494 r = yaz_marc_decode_wrbuf(mt, buf, bsize, wr);
495 yaz_marc_destroy(mt);
500 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug, int bsize)
502 return yaz_marc_decode(buf, wr, debug, bsize, 0);
506 int marc_display_exl (const char *buf, FILE *outf, int debug, int bsize)
508 yaz_marc_t mt = yaz_marc_create();
512 r = yaz_marc_decode_wrbuf (mt, buf, bsize, mt->m_wr);
516 fwrite (wrbuf_buf(mt->m_wr), 1, wrbuf_len(mt->m_wr), outf);
517 yaz_marc_destroy(mt);
522 int marc_display_ex (const char *buf, FILE *outf, int debug)
524 return marc_display_exl (buf, outf, debug, -1);
528 int marc_display (const char *buf, FILE *outf)
530 return marc_display_ex (buf, outf, 0);