2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.2 2003-12-11 00:37:22 adam Exp $
15 #include <yaz/marcdisp.h>
16 #include <yaz/wrbuf.h>
17 #include <yaz/yaz-util.h>
26 yaz_marc_t yaz_marc_create(void)
28 yaz_marc_t mt = (yaz_marc_t) xmalloc(sizeof(*mt));
29 mt->xml = YAZ_MARC_LINE;
31 mt->m_wr = wrbuf_alloc();
36 void yaz_marc_destroy(yaz_marc_t mt)
40 wrbuf_free (mt->m_wr, 1);
44 static void marc_cdata (yaz_marc_t mt, const char *buf, size_t len, WRBUF wr)
47 if (mt->xml == YAZ_MARC_ISO2709)
49 wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
51 else if (mt->xml == YAZ_MARC_LINE)
53 wrbuf_iconv_write(wr, mt->iconv_cd, buf, len);
58 for (i = 0; i<len; i++)
63 wrbuf_iconv_write(wr, mt->iconv_cd, buf+j, i-j);
64 wrbuf_puts(wr, "<");
69 wrbuf_iconv_write(wr, mt->iconv_cd, buf+j, i-j);
70 wrbuf_puts(wr, ">");
75 wrbuf_iconv_write(wr, mt->iconv_cd, buf+j, i-j);
76 wrbuf_puts(wr, "&");
81 wrbuf_iconv_write(wr, mt->iconv_cd, buf+j, i-j);
82 wrbuf_puts(wr, """);
87 wrbuf_iconv_write(wr, mt->iconv_cd, buf+j, i-j);
88 wrbuf_puts(wr, "'");
94 wrbuf_iconv_write(wr, mt->iconv_cd, buf+j, i-j);
98 int yaz_marc_decode_wrbuf (yaz_marc_t mt, const char *buf, int bsize, WRBUF wr)
102 int indicator_length;
103 int identifier_length;
105 int length_data_entry;
107 int length_implementation;
111 record_length = atoi_n (buf, 5);
112 if (record_length < 25)
118 sprintf (str, "Record length %d - aborting\n", record_length);
119 wrbuf_puts (wr, str);
123 /* ballout if bsize is known and record_length is less than that */
124 if (bsize != -1 && record_length > bsize)
126 if (isdigit(buf[10]))
127 indicator_length = atoi_n (buf+10, 1);
129 indicator_length = 2;
130 if (isdigit(buf[11]))
131 identifier_length = atoi_n (buf+11, 1);
133 identifier_length = 2;
134 base_address = atoi_n (buf+12, 5);
136 length_data_entry = atoi_n (buf+20, 1);
137 length_starting = atoi_n (buf+21, 1);
138 length_implementation = atoi_n (buf+22, 1);
140 if (mt->xml != YAZ_MARC_LINE)
146 case YAZ_MARC_ISO2709:
148 case YAZ_MARC_SIMPLEXML:
149 wrbuf_puts (wr, "<iso2709\n");
150 sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
151 wrbuf_puts (wr, str);
152 sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
153 wrbuf_puts (wr, str);
154 for (i = 1; i<=19; i++)
156 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
157 wrbuf_puts (wr, str);
159 wrbuf_puts (wr, ">\n");
161 case YAZ_MARC_OAIMARC:
164 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
166 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
168 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
172 sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
173 buf[5], buf[6], buf[7]);
174 wrbuf_puts (wr, str);
176 case YAZ_MARC_MARCXML:
179 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
180 " <leader>%.24s</leader>\n", buf);
189 wrbuf_puts (wr, "<!--\n");
190 sprintf (str, "Record length %5d\n", record_length);
191 wrbuf_puts (wr, str);
192 sprintf (str, "Indicator length %5d\n", indicator_length);
193 wrbuf_puts (wr, str);
194 sprintf (str, "Identifier length %5d\n", identifier_length);
195 wrbuf_puts (wr, str);
196 sprintf (str, "Base address %5d\n", base_address);
197 wrbuf_puts (wr, str);
198 sprintf (str, "Length data entry %5d\n", length_data_entry);
199 wrbuf_puts (wr, str);
200 sprintf (str, "Length starting %5d\n", length_starting);
201 wrbuf_puts (wr, str);
202 sprintf (str, "Length implementation %5d\n", length_implementation);
203 wrbuf_puts (wr, str);
205 wrbuf_puts (wr, "-->\n");
208 /* first pass. determine length of directory & base of data */
209 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
211 entry_p += 3+length_data_entry+length_starting;
212 if (entry_p >= record_length)
215 base_address = entry_p+1;
217 if (mt->xml == YAZ_MARC_ISO2709)
219 WRBUF wr_head = wrbuf_alloc();
220 WRBUF wr_dir = wrbuf_alloc();
221 WRBUF wr_tmp = wrbuf_alloc();
224 /* second pass. create directory for ISO2709 output */
225 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
227 int data_length, data_offset, end_offset;
230 wrbuf_write(wr_dir, buf+entry_p, 3);
233 data_length = atoi_n (buf+entry_p, length_data_entry);
234 entry_p += length_data_entry;
235 data_offset = atoi_n (buf+entry_p, length_starting);
236 entry_p += length_starting;
237 i = data_offset + base_address;
238 end_offset = i+data_length-1;
240 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS &&
243 sz1 = 1+i - (data_offset + base_address);
246 sz2 = wrbuf_iconv_write(wr_tmp, mt->iconv_cd,
247 buf + data_offset+base_address, sz1);
248 wrbuf_rewind(wr_tmp);
252 wrbuf_printf(wr_dir, "%0*d", length_data_entry, sz2);
253 wrbuf_printf(wr_dir, "%0*d", length_starting, data_p);
256 wrbuf_putc(wr_dir, ISO2709_FS);
257 wrbuf_printf(wr_head, "%05d", data_p+1 + base_address);
258 wrbuf_write(wr_head, buf+5, 7);
259 wrbuf_printf(wr_head, "%05d", base_address);
260 wrbuf_write(wr_head, buf+17, 7);
262 wrbuf_write(wr, wrbuf_buf(wr_head), 24);
263 wrbuf_write(wr, wrbuf_buf(wr_dir), wrbuf_len(wr_dir));
264 wrbuf_free(wr_head, 1);
265 wrbuf_free(wr_dir, 1);
266 wrbuf_free(wr_tmp, 1);
268 /* third pass. create data output */
269 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
276 int identifier_flag = 1;
278 memcpy (tag, buf+entry_p, 3);
281 data_length = atoi_n (buf+entry_p, length_data_entry);
282 entry_p += length_data_entry;
283 data_offset = atoi_n (buf+entry_p, length_starting);
284 entry_p += length_starting;
285 i = data_offset + base_address;
286 end_offset = i+data_length-1;
288 if (indicator_length < 4 && indicator_length > 0)
290 if (buf[i + indicator_length] != ISO2709_IDFS)
293 else if (!memcmp (tag, "00", 2))
300 wrbuf_puts (wr, "Tag: ");
301 wrbuf_puts (wr, tag);
302 wrbuf_puts (wr, " ");
304 case YAZ_MARC_SIMPLEXML:
305 wrbuf_printf (wr, "<field tag=\"%s\"", tag);
307 case YAZ_MARC_OAIMARC:
309 wrbuf_printf (wr, " <varfield id=\"%s\"", tag);
311 wrbuf_printf (wr, " <fixfield id=\"%s\"", tag);
313 case YAZ_MARC_MARCXML:
315 wrbuf_printf (wr, " <datafield tag=\"%s\"", tag);
317 wrbuf_printf (wr, " <controlfield tag=\"%s\"", tag);
322 for (j = 0; j<indicator_length; j++, i++)
326 case YAZ_MARC_ISO2709:
327 wrbuf_putc(wr, buf[i]);
331 wrbuf_puts (wr, " Ind: ");
332 wrbuf_putc(wr, buf[i]);
334 case YAZ_MARC_SIMPLEXML:
335 wrbuf_printf(wr, " Indicator%d=\"%c\"", j+1, buf[i]);
337 case YAZ_MARC_OAIMARC:
338 wrbuf_printf(wr, " i%d=\"%c\"", j+1, buf[i]);
340 case YAZ_MARC_MARCXML:
341 wrbuf_printf(wr, " ind%d=\"%c\"", j+1, buf[i]);
345 if (mt->xml == YAZ_MARC_SIMPLEXML || mt->xml == YAZ_MARC_MARCXML
346 || mt->xml == YAZ_MARC_OAIMARC)
348 wrbuf_puts (wr, ">");
350 wrbuf_puts (wr, "\n");
352 if (mt->xml == YAZ_MARC_LINE)
355 wrbuf_puts (wr, " Fields: ");
359 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
365 case YAZ_MARC_ISO2709:
367 wrbuf_iconv_write(wr, mt->iconv_cd,
368 buf+i, identifier_length);
369 i += identifier_length;
372 wrbuf_puts (wr, " $");
373 for (j = 1; j<identifier_length; j++, i++)
374 wrbuf_putc (wr, buf[i]);
375 wrbuf_putc (wr, ' ');
377 case YAZ_MARC_SIMPLEXML:
378 wrbuf_puts (wr, " <subfield code=\"");
379 for (j = 1; j<identifier_length; j++, i++)
380 wrbuf_putc (wr, buf[i]);
381 wrbuf_puts (wr, "\">");
383 case YAZ_MARC_OAIMARC:
384 wrbuf_puts (wr, " <subfield label=\"");
385 for (j = 1; j<identifier_length; j++, i++)
386 wrbuf_putc (wr, buf[i]);
387 wrbuf_puts (wr, "\">");
389 case YAZ_MARC_MARCXML:
390 wrbuf_puts (wr, " <subfield code=\"");
391 for (j = 1; j<identifier_length; j++, i++)
392 wrbuf_putc (wr, buf[i]);
393 wrbuf_puts (wr, "\">");
397 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
398 buf[i] != ISO2709_FS && i < end_offset)
400 marc_cdata(mt, buf + i0, i - i0, wr);
402 if (mt->xml == YAZ_MARC_ISO2709 && buf[i] != ISO2709_IDFS)
403 marc_cdata(mt, buf + i, 1, wr);
405 if (mt->xml == YAZ_MARC_SIMPLEXML ||
406 mt->xml == YAZ_MARC_MARCXML ||
407 mt->xml == YAZ_MARC_OAIMARC)
408 wrbuf_puts (wr, "</subfield>\n");
414 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
416 marc_cdata(mt, buf + i0, i - i0, wr);
417 if (mt->xml == YAZ_MARC_ISO2709)
418 marc_cdata(mt, buf + i, 1, wr);
420 if (mt->xml == YAZ_MARC_LINE)
421 wrbuf_putc (wr, '\n');
423 wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
424 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
425 wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
428 case YAZ_MARC_SIMPLEXML:
429 wrbuf_puts (wr, "</field>\n");
431 case YAZ_MARC_OAIMARC:
433 wrbuf_puts (wr, " </varfield>\n");
435 wrbuf_puts (wr, " </fixfield>\n");
437 case YAZ_MARC_MARCXML:
439 wrbuf_puts (wr, " </datafield>\n");
441 wrbuf_puts (wr, " </controlfield>\n");
450 case YAZ_MARC_SIMPLEXML:
451 wrbuf_puts (wr, "</iso2709>\n");
453 case YAZ_MARC_OAIMARC:
454 wrbuf_puts (wr, "</oai_marc>\n");
456 case YAZ_MARC_MARCXML:
457 wrbuf_puts (wr, "</record>\n");
459 case YAZ_MARC_ISO2709:
460 wrbuf_putc (wr, ISO2709_RS);
463 return record_length;
466 int yaz_marc_decode_buf (yaz_marc_t mt, const char *buf, int bsize,
467 char **result, int *rsize)
469 int r = yaz_marc_decode_wrbuf(mt, buf, bsize, mt->m_wr);
473 *result = wrbuf_buf(mt->m_wr);
475 *rsize = wrbuf_len(mt->m_wr);
480 void yaz_marc_xml(yaz_marc_t mt, int xmlmode)
486 void yaz_marc_debug(yaz_marc_t mt, int level)
492 void yaz_marc_iconv(yaz_marc_t mt, yaz_iconv_t cd)
498 int yaz_marc_decode(const char *buf, WRBUF wr, int debug, int bsize, int xml)
500 yaz_marc_t mt = yaz_marc_create();
505 r = yaz_marc_decode_wrbuf(mt, buf, bsize, wr);
506 yaz_marc_destroy(mt);
511 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug, int bsize)
513 return yaz_marc_decode(buf, wr, debug, bsize, 0);
517 int marc_display_exl (const char *buf, FILE *outf, int debug, int bsize)
519 yaz_marc_t mt = yaz_marc_create();
523 r = yaz_marc_decode_wrbuf (mt, buf, bsize, mt->m_wr);
527 fwrite (wrbuf_buf(mt->m_wr), 1, wrbuf_len(mt->m_wr), outf);
528 yaz_marc_destroy(mt);
533 int marc_display_ex (const char *buf, FILE *outf, int debug)
535 return marc_display_exl (buf, outf, debug, -1);
539 int marc_display (const char *buf, FILE *outf)
541 return marc_display_ex (buf, outf, 0);