2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.18 2002-02-28 13:21:16 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);
62 sprintf (str, "Record length %5d\n", record_length);
64 sprintf (str, "Indicator length %5d\n", indicator_length);
66 sprintf (str, "Identifier length %5d\n", identifier_length);
68 sprintf (str, "Base address %5d\n", base_address);
70 sprintf (str, "Length data entry %5d\n", length_data_entry);
72 sprintf (str, "Length starting %5d\n", length_starting);
74 sprintf (str, "Length implementation %5d\n", length_implementation);
77 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
79 entry_p += 3+length_data_entry+length_starting;
80 if (entry_p >= record_length)
83 base_address = entry_p+1;
84 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
91 int identifier_flag = 1;
93 memcpy (tag, buf+entry_p, 3);
98 wrbuf_puts (wr, "<field name=\"");
100 wrbuf_puts (wr, "\"");
105 wrbuf_puts (wr, "Tag: ");
106 wrbuf_puts (wr, tag);
107 wrbuf_puts (wr, " ");
109 data_length = atoi_n (buf+entry_p, length_data_entry);
110 entry_p += length_data_entry;
111 data_offset = atoi_n (buf+entry_p, length_starting);
112 entry_p += length_starting;
113 i = data_offset + base_address;
114 end_offset = i+data_length-1;
116 if (indicator_length < 4 && indicator_length > 0)
118 if (buf[i + indicator_length] != ISO2709_IDFS)
121 else if (!memcmp (tag, "00", 2))
127 wrbuf_puts (wr, " Ind: ");
128 for (j = 0; j<indicator_length; j++, i++)
133 sprintf (nostr, " indicator%d=\"%c\"", j+1, buf[i]);
134 wrbuf_puts (wr, nostr);
137 wrbuf_putc (wr, buf[i]);
142 wrbuf_puts (wr, ">");
144 wrbuf_puts (wr, "\n");
149 wrbuf_puts (wr, " Fields: ");
151 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
158 wrbuf_puts (wr, " <field name=\"");
159 for (j = 1; j<identifier_length; j++, i++)
160 wrbuf_putc (wr, buf[i]);
161 wrbuf_puts (wr, "\">");
165 wrbuf_puts (wr, " $");
166 for (j = 1; j<identifier_length; j++, i++)
167 wrbuf_putc (wr, buf[i]);
168 wrbuf_putc (wr, ' ');
170 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
171 buf[i] != ISO2709_FS && i < end_offset)
173 wrbuf_putc (wr, buf[i]);
177 wrbuf_puts (wr, "</field>\n");
181 wrbuf_putc (wr, buf[i]);
186 wrbuf_putc (wr, '\n');
188 wrbuf_puts (wr, " <!-- separator but not at end of field -->\n");
189 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
190 wrbuf_puts (wr, " <!-- no separator at end of field -->\n");
192 wrbuf_puts (wr, "</field>\n");
195 return record_length;
198 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
201 return yaz_marc_decode (buf, wr, debug, bsize, 0);
204 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
208 WRBUF wrbuf = wrbuf_alloc ();
209 record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
212 if (record_length > 0)
213 fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
214 wrbuf_free (wrbuf, 1);
215 return record_length;
218 int marc_display_ex (const char *buf, FILE *outf, int debug)
220 return marc_display_exl (buf, outf, debug, -1);
223 int marc_display (const char *buf, FILE *outf)
225 return marc_display_ex (buf, outf, 0);