MARC to XML conversion
[yaz-moved-to-github.git] / util / marcdisp.c
1 /*
2  * Copyright (c) 1995-2002, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Id: marcdisp.c,v 1.18 2002-02-28 13:21:16 adam Exp $
6  */
7
8 #if HAVE_CONFIG_H
9 #include <config.h>
10 #endif
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <yaz/marcdisp.h>
16 #include <yaz/wrbuf.h>
17 #include <yaz/yaz-util.h>
18
19 int yaz_marc_decode (const char *buf, WRBUF wr, int debug, int bsize, int xml)
20 {
21     int entry_p;
22     int record_length;
23     int indicator_length;
24     int identifier_length;
25     int base_address;
26     int length_data_entry;
27     int length_starting;
28     int length_implementation;
29
30     record_length = atoi_n (buf, 5);
31     if (record_length < 25)
32     {
33         if (debug)
34         {
35             char str[40];
36             
37             sprintf (str, "Record length %d - aborting\n", record_length);
38             wrbuf_puts (wr, str);
39         }
40         return -1;
41     }
42     /* ballout if bsize is known and record_length is than that */
43     if (bsize != -1 && record_length > bsize)
44         return -1;
45     if (isdigit(buf[10]))
46         indicator_length = atoi_n (buf+10, 1);
47     else
48         indicator_length = 2;
49     if (isdigit(buf[11]))
50         identifier_length = atoi_n (buf+11, 1);
51     else
52         identifier_length = 2;
53     base_address = atoi_n (buf+12, 5);
54
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);
58
59     if (debug)
60     {
61         char str[40];
62         sprintf (str, "Record length         %5d\n", record_length);
63         wrbuf_puts (wr, str);
64         sprintf (str, "Indicator length      %5d\n", indicator_length);
65         wrbuf_puts (wr, str);
66         sprintf (str, "Identifier length     %5d\n", identifier_length);
67         wrbuf_puts (wr, str);
68         sprintf (str, "Base address          %5d\n", base_address);
69         wrbuf_puts (wr, str);
70         sprintf (str, "Length data entry     %5d\n", length_data_entry);
71         wrbuf_puts (wr, str);
72         sprintf (str, "Length starting       %5d\n", length_starting);
73         wrbuf_puts (wr, str);
74         sprintf (str, "Length implementation %5d\n", length_implementation);
75         wrbuf_puts (wr, str);
76     }
77     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
78     {
79         entry_p += 3+length_data_entry+length_starting;
80         if (entry_p >= record_length)
81             return -1;
82     }
83     base_address = entry_p+1;
84     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
85     {
86         int data_length;
87         int data_offset;
88         int end_offset;
89         int i, j;
90         char tag[4];
91         int identifier_flag = 1;
92
93         memcpy (tag, buf+entry_p, 3);
94         entry_p += 3;
95         tag[3] = '\0';
96         if (xml)
97         {
98             wrbuf_puts (wr, "<field name=\"");
99             wrbuf_puts (wr, tag);
100             wrbuf_puts (wr, "\"");
101         }
102         else
103         {
104             if (debug)
105                 wrbuf_puts (wr, "Tag: ");
106             wrbuf_puts (wr, tag);
107             wrbuf_puts (wr, " ");
108         }
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;
115         
116         if (indicator_length < 4 && indicator_length > 0)
117         {
118             if (buf[i + indicator_length] != ISO2709_IDFS)
119                 identifier_flag = 0;
120         }
121         else if (!memcmp (tag, "00", 2))
122             identifier_flag = 0;
123         
124         if (identifier_flag)
125         {
126             if (debug)
127                 wrbuf_puts (wr, " Ind: ");
128             for (j = 0; j<indicator_length; j++, i++)
129             {
130                 if (xml)
131                 {
132                     char nostr[30];
133                     sprintf (nostr, " indicator%d=\"%c\"", j+1, buf[i]);
134                     wrbuf_puts (wr, nostr);
135                 }
136                 else
137                     wrbuf_putc (wr, buf[i]);
138             }
139         }
140         if (xml)
141         {
142             wrbuf_puts (wr, ">");
143             if (identifier_flag)
144                 wrbuf_puts (wr, "\n");
145         }
146         else
147         {
148             if (debug)
149                 wrbuf_puts (wr, " Fields: ");
150         }
151         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
152         {
153             if (identifier_flag)
154             {
155                 i++;
156                 if (xml)
157                 {
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, "\">");
162                 }
163                 else
164                 {
165                     wrbuf_puts (wr, " $"); 
166                     for (j = 1; j<identifier_length; j++, i++)
167                         wrbuf_putc (wr, buf[i]);
168                     wrbuf_putc (wr, ' ');
169                 }
170                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
171                        buf[i] != ISO2709_FS && i < end_offset)
172                 {
173                     wrbuf_putc (wr, buf[i]);
174                     i++;
175                 }
176                 if (xml)
177                     wrbuf_puts (wr, "</field>\n");
178             }
179             else
180             {
181                 wrbuf_putc (wr, buf[i]);
182                 i++;
183             }
184         }
185         if (!xml)
186             wrbuf_putc (wr, '\n');
187         if (i < end_offset)
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");
191         if (xml)
192             wrbuf_puts (wr, "</field>\n");
193     }
194     wrbuf_puts (wr, "");
195     return record_length;
196 }
197
198 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
199                         int bsize)
200 {
201     return yaz_marc_decode (buf, wr, debug, bsize, 0);
202 }
203
204 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
205 {
206     int record_length;
207
208     WRBUF wrbuf = wrbuf_alloc ();
209     record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
210     if (!outf)
211         outf = stdout;
212     if (record_length > 0)
213         fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
214     wrbuf_free (wrbuf, 1);
215     return record_length;
216 }
217
218 int marc_display_ex (const char *buf, FILE *outf, int debug)
219 {
220     return marc_display_exl (buf, outf, debug, -1);
221 }
222
223 int marc_display (const char *buf, FILE *outf)
224 {
225     return marc_display_ex (buf, outf, 0);
226 }
227
228