2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.25 2002-12-09 23:32:29 adam Exp $
15 #include <yaz/marcdisp.h>
16 #include <yaz/wrbuf.h>
17 #include <yaz/yaz-util.h>
19 int yaz_marc_decode (const char *buf, WRBUF wr, int debug, int bsize, int xml)
24 int identifier_length;
26 int length_data_entry;
28 int length_implementation;
30 record_length = atoi_n (buf, 5);
31 if (record_length < 25)
37 sprintf (str, "Record length %d - aborting\n", record_length);
42 /* ballout if bsize is known and record_length is than that */
43 if (bsize != -1 && record_length > bsize)
46 indicator_length = atoi_n (buf+10, 1);
50 identifier_length = atoi_n (buf+11, 1);
52 identifier_length = 2;
53 base_address = atoi_n (buf+12, 5);
55 length_data_entry = atoi_n (buf+20, 1);
56 length_starting = atoi_n (buf+21, 1);
57 length_implementation = atoi_n (buf+22, 1);
66 wrbuf_puts (wr, "<iso2709\n");
67 sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
69 sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
71 for (i = 1; i<=19; i++)
73 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
76 wrbuf_puts (wr, ">\n");
78 case YAZ_MARC_OAIMARC:
81 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
83 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
85 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
89 sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
90 buf[5], buf[6], buf[7]);
93 case YAZ_MARC_MARCXML:
96 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
97 " <leader>%.24s</leader>\n", buf);
106 wrbuf_puts (wr, "<!--\n");
107 sprintf (str, "Record length %5d\n", record_length);
108 wrbuf_puts (wr, str);
109 sprintf (str, "Indicator length %5d\n", indicator_length);
110 wrbuf_puts (wr, str);
111 sprintf (str, "Identifier length %5d\n", identifier_length);
112 wrbuf_puts (wr, str);
113 sprintf (str, "Base address %5d\n", base_address);
114 wrbuf_puts (wr, str);
115 sprintf (str, "Length data entry %5d\n", length_data_entry);
116 wrbuf_puts (wr, str);
117 sprintf (str, "Length starting %5d\n", length_starting);
118 wrbuf_puts (wr, str);
119 sprintf (str, "Length implementation %5d\n", length_implementation);
120 wrbuf_puts (wr, str);
122 wrbuf_puts (wr, "-->\n");
125 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
127 entry_p += 3+length_data_entry+length_starting;
128 if (entry_p >= record_length)
131 base_address = entry_p+1;
132 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
139 int identifier_flag = 1;
141 memcpy (tag, buf+entry_p, 3);
144 data_length = atoi_n (buf+entry_p, length_data_entry);
145 entry_p += length_data_entry;
146 data_offset = atoi_n (buf+entry_p, length_starting);
147 entry_p += length_starting;
148 i = data_offset + base_address;
149 end_offset = i+data_length-1;
151 if (indicator_length < 4 && indicator_length > 0)
153 if (buf[i + indicator_length] != ISO2709_IDFS)
156 else if (!memcmp (tag, "00", 2))
163 wrbuf_puts (wr, "Tag: ");
164 wrbuf_puts (wr, tag);
165 wrbuf_puts (wr, " ");
168 wrbuf_printf (wr, "<field tag=\"%s\"", tag);
170 case YAZ_MARC_OAIMARC:
172 wrbuf_printf (wr, " <varfield id=\"%s\"", tag);
174 wrbuf_printf (wr, " <fixfield id=\"%s\"", tag);
176 case YAZ_MARC_MARCXML:
178 wrbuf_printf (wr, " <datafield tag=\"%s\"", tag);
180 wrbuf_printf (wr, " <controlfield tag=\"%s\"", tag);
185 for (j = 0; j<indicator_length; j++, i++)
191 wrbuf_puts (wr, " Ind: ");
192 wrbuf_putc (wr, buf[i]);
195 wrbuf_printf (wr, " Indicator%d=\"%c\"", j+1, buf[i]);
197 case YAZ_MARC_OAIMARC:
198 wrbuf_printf (wr, " i%d=\"%c\"", j+1, buf[i]);
200 case YAZ_MARC_MARCXML:
201 wrbuf_printf (wr, " ind%d=\"%c\"", j+1, buf[i]);
207 wrbuf_puts (wr, ">");
209 wrbuf_puts (wr, "\n");
214 wrbuf_puts (wr, " Fields: ");
216 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
224 wrbuf_puts (wr, " $");
225 for (j = 1; j<identifier_length; j++, i++)
226 wrbuf_putc (wr, buf[i]);
227 wrbuf_putc (wr, ' ');
230 wrbuf_puts (wr, " <subfield code=\"");
231 for (j = 1; j<identifier_length; j++, i++)
232 wrbuf_putc (wr, buf[i]);
233 wrbuf_puts (wr, "\">");
235 case YAZ_MARC_OAIMARC:
236 wrbuf_puts (wr, " <subfield label=\"");
237 for (j = 1; j<identifier_length; j++, i++)
238 wrbuf_putc (wr, buf[i]);
239 wrbuf_puts (wr, "\">");
241 case YAZ_MARC_MARCXML:
242 wrbuf_puts (wr, " <subfield code=\"");
243 for (j = 1; j<identifier_length; j++, i++)
244 wrbuf_putc (wr, buf[i]);
245 wrbuf_puts (wr, "\">");
248 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
249 buf[i] != ISO2709_FS && i < end_offset)
251 if (xml && buf[i] == '<')
252 wrbuf_puts(wr, "<");
253 else if (xml && buf[i] == '&')
254 wrbuf_puts(wr, "&");
256 wrbuf_putc (wr, buf[i]);
260 wrbuf_puts (wr, "</subfield>\n");
264 if (xml && buf[i] == '<')
265 wrbuf_puts(wr, "<");
266 else if (xml && buf[i] == '&')
267 wrbuf_puts(wr, "&");
268 else if (xml && buf[i] == '"')
269 wrbuf_puts(wr, """);
271 wrbuf_putc (wr, buf[i]);
276 wrbuf_putc (wr, '\n');
278 wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
279 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
280 wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
286 wrbuf_puts (wr, "</field>\n");
288 case YAZ_MARC_OAIMARC:
290 wrbuf_puts (wr, " </varfield>\n");
292 wrbuf_puts (wr, " </fixfield>\n");
294 case YAZ_MARC_MARCXML:
296 wrbuf_puts (wr, " </datafield>\n");
298 wrbuf_puts (wr, " </controlfield>\n");
308 wrbuf_puts (wr, "</iso2709>\n");
310 case YAZ_MARC_OAIMARC:
311 wrbuf_puts (wr, "</oai_marc>\n");
313 case YAZ_MARC_MARCXML:
314 wrbuf_puts (wr, "</record>\n");
317 return record_length;
320 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
323 return yaz_marc_decode (buf, wr, debug, bsize, 0);
326 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
330 WRBUF wrbuf = wrbuf_alloc ();
331 record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
334 if (record_length > 0)
335 fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
336 wrbuf_free (wrbuf, 1);
337 return record_length;
340 int marc_display_ex (const char *buf, FILE *outf, int debug)
342 return marc_display_exl (buf, outf, debug, -1);
345 int marc_display (const char *buf, FILE *outf)
347 return marc_display_ex (buf, outf, 0);