2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.19 2002-03-18 18:11:45 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);
63 wrbuf_puts (wr, "<iso2709\n");
64 sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
66 sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
68 for (i = 1; i<=19; i++)
70 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
73 wrbuf_puts (wr, ">\n");
80 wrbuf_puts (wr, "<!--\n");
81 sprintf (str, "Record length %5d\n", record_length);
83 sprintf (str, "Indicator length %5d\n", indicator_length);
85 sprintf (str, "Identifier length %5d\n", identifier_length);
87 sprintf (str, "Base address %5d\n", base_address);
89 sprintf (str, "Length data entry %5d\n", length_data_entry);
91 sprintf (str, "Length starting %5d\n", length_starting);
93 sprintf (str, "Length implementation %5d\n", length_implementation);
96 wrbuf_puts (wr, "-->\n");
99 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
101 entry_p += 3+length_data_entry+length_starting;
102 if (entry_p >= record_length)
105 base_address = entry_p+1;
106 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
113 int identifier_flag = 1;
115 memcpy (tag, buf+entry_p, 3);
120 wrbuf_puts (wr, "<field tag=\"");
121 wrbuf_puts (wr, tag);
122 wrbuf_puts (wr, "\"");
127 wrbuf_puts (wr, "Tag: ");
128 wrbuf_puts (wr, tag);
129 wrbuf_puts (wr, " ");
131 data_length = atoi_n (buf+entry_p, length_data_entry);
132 entry_p += length_data_entry;
133 data_offset = atoi_n (buf+entry_p, length_starting);
134 entry_p += length_starting;
135 i = data_offset + base_address;
136 end_offset = i+data_length-1;
138 if (indicator_length < 4 && indicator_length > 0)
140 if (buf[i + indicator_length] != ISO2709_IDFS)
143 else if (!memcmp (tag, "00", 2))
149 wrbuf_puts (wr, " Ind: ");
150 for (j = 0; j<indicator_length; j++, i++)
155 sprintf (nostr, " Indicator%d=\"%c\"", j+1, buf[i]);
156 wrbuf_puts (wr, nostr);
159 wrbuf_putc (wr, buf[i]);
164 wrbuf_puts (wr, ">");
166 wrbuf_puts (wr, "\n");
171 wrbuf_puts (wr, " Fields: ");
173 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
180 wrbuf_puts (wr, " <subfield code=\"");
181 for (j = 1; j<identifier_length; j++, i++)
182 wrbuf_putc (wr, buf[i]);
183 wrbuf_puts (wr, "\">");
187 wrbuf_puts (wr, " $");
188 for (j = 1; j<identifier_length; j++, i++)
189 wrbuf_putc (wr, buf[i]);
190 wrbuf_putc (wr, ' ');
192 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
193 buf[i] != ISO2709_FS && i < end_offset)
195 wrbuf_putc (wr, buf[i]);
199 wrbuf_puts (wr, "</subfield>\n");
203 wrbuf_putc (wr, buf[i]);
208 wrbuf_putc (wr, '\n');
210 wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
211 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
212 wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
214 wrbuf_puts (wr, "</field>\n");
217 wrbuf_puts (wr, "</iso2709>\n");
219 return record_length;
222 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
225 return yaz_marc_decode (buf, wr, debug, bsize, 0);
228 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
232 WRBUF wrbuf = wrbuf_alloc ();
233 record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
236 if (record_length > 0)
237 fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
238 wrbuf_free (wrbuf, 1);
239 return record_length;
242 int marc_display_ex (const char *buf, FILE *outf, int debug)
244 return marc_display_exl (buf, outf, debug, -1);
247 int marc_display (const char *buf, FILE *outf)
249 return marc_display_ex (buf, outf, 0);