2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.23 2002-10-04 19:11:17 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 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
69 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
71 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
75 sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
76 buf[5], buf[6], buf[7]);
81 wrbuf_puts (wr, "<iso2709\n");
82 sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
84 sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
86 for (i = 1; i<=19; i++)
88 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
91 wrbuf_puts (wr, ">\n");
99 wrbuf_puts (wr, "<!--\n");
100 sprintf (str, "Record length %5d\n", record_length);
101 wrbuf_puts (wr, str);
102 sprintf (str, "Indicator length %5d\n", indicator_length);
103 wrbuf_puts (wr, str);
104 sprintf (str, "Identifier length %5d\n", identifier_length);
105 wrbuf_puts (wr, str);
106 sprintf (str, "Base address %5d\n", base_address);
107 wrbuf_puts (wr, str);
108 sprintf (str, "Length data entry %5d\n", length_data_entry);
109 wrbuf_puts (wr, str);
110 sprintf (str, "Length starting %5d\n", length_starting);
111 wrbuf_puts (wr, str);
112 sprintf (str, "Length implementation %5d\n", length_implementation);
113 wrbuf_puts (wr, str);
115 wrbuf_puts (wr, "-->\n");
118 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
120 entry_p += 3+length_data_entry+length_starting;
121 if (entry_p >= record_length)
124 base_address = entry_p+1;
125 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
132 int identifier_flag = 1;
134 memcpy (tag, buf+entry_p, 3);
137 data_length = atoi_n (buf+entry_p, length_data_entry);
138 entry_p += length_data_entry;
139 data_offset = atoi_n (buf+entry_p, length_starting);
140 entry_p += length_starting;
141 i = data_offset + base_address;
142 end_offset = i+data_length-1;
144 if (indicator_length < 4 && indicator_length > 0)
146 if (buf[i + indicator_length] != ISO2709_IDFS)
149 else if (!memcmp (tag, "00", 2))
158 wrbuf_puts (wr, "<varfield id=\"");
160 wrbuf_puts (wr, "<fixfield id=\"");
161 wrbuf_puts (wr, tag);
162 wrbuf_puts (wr, "\"");
166 wrbuf_puts (wr, "<field tag=\"");
167 wrbuf_puts (wr, tag);
168 wrbuf_puts (wr, "\"");
174 wrbuf_puts (wr, "Tag: ");
175 wrbuf_puts (wr, tag);
176 wrbuf_puts (wr, " ");
182 wrbuf_puts (wr, " Ind: ");
183 for (j = 0; j<indicator_length; j++, i++)
189 sprintf (nostr, " i%d=\"%c\"", j+1, buf[i]);
191 sprintf (nostr, " Indicator%d=\"%c\"", j+1, buf[i]);
192 wrbuf_puts (wr, nostr);
195 wrbuf_putc (wr, buf[i]);
200 wrbuf_puts (wr, ">");
202 wrbuf_puts (wr, "\n");
207 wrbuf_puts (wr, " Fields: ");
209 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
217 wrbuf_puts (wr, " <subfield label=\"");
219 wrbuf_puts (wr, " <subfield code=\"");
220 for (j = 1; j<identifier_length; j++, i++)
221 wrbuf_putc (wr, buf[i]);
222 wrbuf_puts (wr, "\">");
226 wrbuf_puts (wr, " $");
227 for (j = 1; j<identifier_length; j++, i++)
228 wrbuf_putc (wr, buf[i]);
229 wrbuf_putc (wr, ' ');
231 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
232 buf[i] != ISO2709_FS && i < end_offset)
234 if (xml && buf[i] == '<')
235 wrbuf_puts(wr, "<");
236 else if (xml && buf[i] == '&')
237 wrbuf_puts(wr, "&");
239 wrbuf_putc (wr, buf[i]);
243 wrbuf_puts (wr, "</subfield>\n");
247 if (xml && buf[i] == '<')
248 wrbuf_puts(wr, "<");
249 else if (xml && buf[i] == '&')
250 wrbuf_puts(wr, "&");
251 else if (xml && buf[i] == '"')
252 wrbuf_puts(wr, """);
254 wrbuf_putc (wr, buf[i]);
259 wrbuf_putc (wr, '\n');
261 wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
262 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
263 wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
269 wrbuf_puts (wr, "</varfield>\n");
271 wrbuf_puts (wr, "</fixfield>\n");
274 wrbuf_puts (wr, "</field>\n");
280 wrbuf_puts (wr, "</oai_marc>\n");
282 wrbuf_puts (wr, "</iso2709>\n");
285 return record_length;
288 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
291 return yaz_marc_decode (buf, wr, debug, bsize, 0);
294 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
298 WRBUF wrbuf = wrbuf_alloc ();
299 record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
302 if (record_length > 0)
303 fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
304 wrbuf_free (wrbuf, 1);
305 return record_length;
308 int marc_display_ex (const char *buf, FILE *outf, int debug)
310 return marc_display_exl (buf, outf, debug, -1);
313 int marc_display (const char *buf, FILE *outf)
315 return marc_display_ex (buf, outf, 0);