2 * Copyright (c) 1995-2002, Index Data
3 * See the file LICENSE for details.
5 * $Id: marcdisp.c,v 1.17 2002-02-01 14:50:29 adam Exp $
15 #include <yaz/marcdisp.h>
16 #include <yaz/wrbuf.h>
17 #include <yaz/yaz-util.h>
19 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
25 int identifier_length;
27 int length_data_entry;
29 int length_implementation;
31 record_length = atoi_n (buf, 5);
32 if (record_length < 25)
38 sprintf (str, "Record length %d - aborting\n", record_length);
43 /* ballout if bsize is known and record_length is than that */
44 if (bsize != -1 && record_length > bsize)
47 indicator_length = atoi_n (buf+10, 1);
51 identifier_length = atoi_n (buf+11, 1);
53 identifier_length = 2;
54 base_address = atoi_n (buf+12, 5);
56 length_data_entry = atoi_n (buf+20, 1);
57 length_starting = atoi_n (buf+21, 1);
58 length_implementation = atoi_n (buf+22, 1);
63 sprintf (str, "Record length %5d\n", record_length);
65 sprintf (str, "Indicator length %5d\n", indicator_length);
67 sprintf (str, "Identifier length %5d\n", identifier_length);
69 sprintf (str, "Base address %5d\n", base_address);
71 sprintf (str, "Length data entry %5d\n", length_data_entry);
73 sprintf (str, "Length starting %5d\n", length_starting);
75 sprintf (str, "Length implementation %5d\n", length_implementation);
78 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
80 entry_p += 3+length_data_entry+length_starting;
81 if (entry_p >= record_length)
84 base_address = entry_p+1;
85 for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
92 int identifier_flag = 1;
94 memcpy (tag, buf+entry_p, 3);
98 wrbuf_puts (wr, "Tag: ");
100 wrbuf_puts (wr, " ");
101 data_length = atoi_n (buf+entry_p, length_data_entry);
102 entry_p += length_data_entry;
103 data_offset = atoi_n (buf+entry_p, length_starting);
104 entry_p += length_starting;
105 i = data_offset + base_address;
106 end_offset = i+data_length-1;
108 if (indicator_length < 4 && indicator_length > 0)
110 if (buf[i + indicator_length] != ISO2709_IDFS)
113 else if (!memcmp (tag, "00", 2))
119 wrbuf_puts (wr, " Ind: ");
120 for (j = 0; j<indicator_length; j++, i++)
121 wrbuf_putc (wr, buf[i]);
124 wrbuf_puts (wr, " Fields: ");
125 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
130 wrbuf_puts (wr, " $");
131 for (j = 1; j<identifier_length; j++, i++)
132 wrbuf_putc (wr, buf[i]);
133 wrbuf_putc (wr, ' ');
134 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
135 buf[i] != ISO2709_FS && i < end_offset)
137 wrbuf_putc (wr, buf[i]);
143 wrbuf_putc (wr, buf[i]);
147 wrbuf_putc (wr, '\n');
149 wrbuf_puts (wr, "-- separator but not at end of field\n");
150 if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
151 wrbuf_puts (wr, "-- no separator at end of field\n");
154 return record_length;
157 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
161 WRBUF wrbuf = wrbuf_alloc ();
162 record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
165 if (record_length > 0)
166 fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
167 wrbuf_free (wrbuf, 1);
168 return record_length;
172 int marc_display_ex (const char *buf, FILE *outf, int debug)
174 return marc_display_exl (buf, outf, debug, -1);
177 int marc_display (const char *buf, FILE *outf)
179 return marc_display_ex (buf, outf, 0);