2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.21 2002-10-04 10:19:58 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);
67 "<sequence xmlns=\"http://www.dlib.vt.edu/projects/OAi/marcxml/container\"\n"
69 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\"\n"
70 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc\n"
71 " http://www.openarchives.org/OAI/oai_marc.xsd\n"
72 " http://www.dlib.vt.edu/projects/OAi/marcxml/container\n"
73 " http://www.dlib.vt.edu/projects/OAi/marcxml/container.xsd\"\n"
77 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\"" );
79 sprintf (str, "status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
80 buf[5], buf[6], buf[7]);
85 wrbuf_puts (wr, "<iso2709\n");
86 sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
88 sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
90 for (i = 1; i<=19; i++)
92 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
95 wrbuf_puts (wr, ">\n");
103 wrbuf_puts (wr, "<!--\n");
104 sprintf (str, "Record length %5d\n", record_length);
105 wrbuf_puts (wr, str);
106 sprintf (str, "Indicator length %5d\n", indicator_length);
107 wrbuf_puts (wr, str);
108 sprintf (str, "Identifier length %5d\n", identifier_length);
109 wrbuf_puts (wr, str);
110 sprintf (str, "Base address %5d\n", base_address);
111 wrbuf_puts (wr, str);
112 sprintf (str, "Length data entry %5d\n", length_data_entry);
113 wrbuf_puts (wr, str);
114 sprintf (str, "Length starting %5d\n", length_starting);
115 wrbuf_puts (wr, str);
116 sprintf (str, "Length implementation %5d\n", length_implementation);
117 wrbuf_puts (wr, str);
119 wrbuf_puts (wr, "-->\n");
122 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
124 entry_p += 3+length_data_entry+length_starting;
125 if (entry_p >= record_length)
128 base_address = entry_p+1;
129 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
136 int identifier_flag = 1;
138 memcpy (tag, buf+entry_p, 3);
141 data_length = atoi_n (buf+entry_p, length_data_entry);
142 entry_p += length_data_entry;
143 data_offset = atoi_n (buf+entry_p, length_starting);
144 entry_p += length_starting;
145 i = data_offset + base_address;
146 end_offset = i+data_length-1;
148 if (indicator_length < 4 && indicator_length > 0)
150 if (buf[i + indicator_length] != ISO2709_IDFS)
153 else if (!memcmp (tag, "00", 2))
162 wrbuf_puts (wr, "<varfield id=\"");
164 wrbuf_puts (wr, "<fixfield id=\"");
165 wrbuf_puts (wr, tag);
166 wrbuf_puts (wr, "\"");
170 wrbuf_puts (wr, "<field tag=\"");
171 wrbuf_puts (wr, tag);
172 wrbuf_puts (wr, "\"");
178 wrbuf_puts (wr, "Tag: ");
179 wrbuf_puts (wr, tag);
180 wrbuf_puts (wr, " ");
186 wrbuf_puts (wr, " Ind: ");
187 for (j = 0; j<indicator_length; j++, i++)
193 sprintf (nostr, " i%d=\"%c\"", j+1, buf[i]);
195 sprintf (nostr, " Indicator%d=\"%c\"", j+1, buf[i]);
196 wrbuf_puts (wr, nostr);
199 wrbuf_putc (wr, buf[i]);
204 wrbuf_puts (wr, ">");
206 wrbuf_puts (wr, "\n");
211 wrbuf_puts (wr, " Fields: ");
213 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
221 wrbuf_puts (wr, " <subfield label=\"");
223 wrbuf_puts (wr, " <subfield code=\"");
224 for (j = 1; j<identifier_length; j++, i++)
225 wrbuf_putc (wr, buf[i]);
226 wrbuf_puts (wr, "\">");
230 wrbuf_puts (wr, " $");
231 for (j = 1; j<identifier_length; j++, i++)
232 wrbuf_putc (wr, buf[i]);
233 wrbuf_putc (wr, ' ');
235 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
236 buf[i] != ISO2709_FS && i < end_offset)
238 if (xml && buf[i] == '<')
239 wrbuf_puts(wr, "<");
240 else if (xml && buf[i] == '&')
241 wrbuf_puts(wr, "&");
243 wrbuf_putc (wr, buf[i]);
247 wrbuf_puts (wr, "</subfield>\n");
251 if (xml && buf[i] == '<')
252 wrbuf_puts(wr, "<");
253 else if (xml && buf[i] == '&')
254 wrbuf_puts(wr, "&");
255 else if (xml && buf[i] == '"')
256 wrbuf_puts(wr, """);
258 wrbuf_putc (wr, buf[i]);
263 wrbuf_putc (wr, '\n');
265 wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
266 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
267 wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
273 wrbuf_puts (wr, "</varfield>\n");
275 wrbuf_puts (wr, "</fixfield>\n");
278 wrbuf_puts (wr, "</field>\n");
282 wrbuf_puts (wr, "</iso2709>\n");
284 return record_length;
287 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
290 return yaz_marc_decode (buf, wr, debug, bsize, 0);
293 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
297 WRBUF wrbuf = wrbuf_alloc ();
298 record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
301 if (record_length > 0)
302 fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
303 wrbuf_free (wrbuf, 1);
304 return record_length;
307 int marc_display_ex (const char *buf, FILE *outf, int debug)
309 return marc_display_exl (buf, outf, debug, -1);
312 int marc_display (const char *buf, FILE *outf)
314 return marc_display_ex (buf, outf, 0);