235f0405fe474ca36a722aacdad05a3898685665
[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.23 2002-10-04 19:11:17 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 (xml)
60     {
61         char str[80];
62         int i;
63         if (xml > 1)
64         {
65             wrbuf_puts(
66                 wr,
67                 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
68                 "\n"
69                 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
70                 "\n"
71                 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
72                 "\n"
73                 );
74             
75             sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
76                      buf[5], buf[6], buf[7]);
77             wrbuf_puts (wr, str);
78         }
79         else
80         {
81             wrbuf_puts (wr, "<iso2709\n");
82             sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
83             wrbuf_puts (wr, str);
84             sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
85             wrbuf_puts (wr, str);
86             for (i = 1; i<=19; i++)
87             {
88                 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
89                 wrbuf_puts (wr, str);
90             }
91             wrbuf_puts (wr, ">\n");
92         }
93     }
94     if (debug)
95     {
96         char str[40];
97
98         if (xml)
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);
114         if (xml)
115             wrbuf_puts (wr, "-->\n");
116     }
117
118     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
119     {
120         entry_p += 3+length_data_entry+length_starting;
121         if (entry_p >= record_length)
122             return -1;
123     }
124     base_address = entry_p+1;
125     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
126     {
127         int data_length;
128         int data_offset;
129         int end_offset;
130         int i, j;
131         char tag[4];
132         int identifier_flag = 1;
133
134         memcpy (tag, buf+entry_p, 3);
135         entry_p += 3;
136         tag[3] = '\0';
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;
143         
144         if (indicator_length < 4 && indicator_length > 0)
145         {
146             if (buf[i + indicator_length] != ISO2709_IDFS)
147                 identifier_flag = 0;
148         }
149         else if (!memcmp (tag, "00", 2))
150             identifier_flag = 0;
151
152
153         if (xml)
154         {
155             if (xml > 1)
156             {
157                 if (identifier_flag)
158                     wrbuf_puts (wr, "<varfield id=\"");
159                 else
160                     wrbuf_puts (wr, "<fixfield id=\"");
161                 wrbuf_puts (wr, tag);
162                 wrbuf_puts (wr, "\"");
163             }
164             else
165             {
166                 wrbuf_puts (wr, "<field tag=\"");
167                 wrbuf_puts (wr, tag);
168                 wrbuf_puts (wr, "\"");
169             }
170         }
171         else
172         {
173             if (debug)
174                 wrbuf_puts (wr, "Tag: ");
175             wrbuf_puts (wr, tag);
176             wrbuf_puts (wr, " ");
177         }
178         
179         if (identifier_flag)
180         {
181             if (debug && !xml)
182                 wrbuf_puts (wr, " Ind: ");
183             for (j = 0; j<indicator_length; j++, i++)
184             {
185                 if (xml)
186                 {
187                     char nostr[30];
188                     if (xml > 1)
189                         sprintf (nostr, " i%d=\"%c\"", j+1, buf[i]);
190                     else
191                         sprintf (nostr, " Indicator%d=\"%c\"", j+1, buf[i]);
192                     wrbuf_puts (wr, nostr);
193                 }
194                 else
195                     wrbuf_putc (wr, buf[i]);
196             }
197         }
198         if (xml)
199         {
200             wrbuf_puts (wr, ">");
201             if (identifier_flag)
202                 wrbuf_puts (wr, "\n");
203         }
204         else
205         {
206             if (debug && !xml)
207                 wrbuf_puts (wr, " Fields: ");
208         }
209         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
210         {
211             if (identifier_flag)
212             {
213                 i++;
214                 if (xml)
215                 {
216                     if (xml > 1)
217                         wrbuf_puts (wr, "  <subfield label=\"");
218                     else
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, "\">");
223                 }
224                 else
225                 {
226                     wrbuf_puts (wr, " $"); 
227                     for (j = 1; j<identifier_length; j++, i++)
228                         wrbuf_putc (wr, buf[i]);
229                     wrbuf_putc (wr, ' ');
230                 }
231                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
232                        buf[i] != ISO2709_FS && i < end_offset)
233                 {
234                     if (xml && buf[i] == '<')
235                         wrbuf_puts(wr, "&#x003C;");
236                     else if (xml && buf[i] == '&')
237                         wrbuf_puts(wr, "&#x0026;");
238                     else
239                         wrbuf_putc (wr, buf[i]);
240                     i++;
241                 }
242                 if (xml)
243                     wrbuf_puts (wr, "</subfield>\n");
244             }
245             else
246             {
247                 if (xml && buf[i] == '<')
248                     wrbuf_puts(wr, "&lt;");
249                 else if (xml && buf[i] == '&')
250                     wrbuf_puts(wr, "&amp;");
251                 else if (xml && buf[i] == '"')
252                     wrbuf_puts(wr, "&quot;");
253                 else
254                     wrbuf_putc (wr, buf[i]);
255                 i++;
256             }
257         }
258         if (!xml)
259             wrbuf_putc (wr, '\n');
260         if (i < end_offset)
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");
264         if (xml)
265         {
266             if (xml > 1)
267             {
268                 if (identifier_flag)
269                     wrbuf_puts (wr, "</varfield>\n");
270                 else
271                     wrbuf_puts (wr, "</fixfield>\n");
272             }
273             else
274                 wrbuf_puts (wr, "</field>\n");
275         }
276     }
277     if (xml)
278     {
279         if (xml > 1)
280             wrbuf_puts (wr, "</oai_marc>\n");
281         else
282             wrbuf_puts (wr, "</iso2709>\n");
283     }
284     wrbuf_puts (wr, "");
285     return record_length;
286 }
287
288 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug,
289                         int bsize)
290 {
291     return yaz_marc_decode (buf, wr, debug, bsize, 0);
292 }
293
294 int marc_display_exl (const char *buf, FILE *outf, int debug, int length)
295 {
296     int record_length;
297
298     WRBUF wrbuf = wrbuf_alloc ();
299     record_length = marc_display_wrbuf (buf, wrbuf, debug, length);
300     if (!outf)
301         outf = stdout;
302     if (record_length > 0)
303         fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
304     wrbuf_free (wrbuf, 1);
305     return record_length;
306 }
307
308 int marc_display_ex (const char *buf, FILE *outf, int debug)
309 {
310     return marc_display_exl (buf, outf, debug, -1);
311 }
312
313 int marc_display (const char *buf, FILE *outf)
314 {
315     return marc_display_ex (buf, outf, 0);
316 }
317
318