Simplify use of WRBUF in marc handler
[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.27 2002-12-17 13:32:04 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 struct yaz_marc_t_ {
20     WRBUF m_wr;
21     int xml;
22     int debug;
23 };
24
25 yaz_marc_t yaz_marc_create(void)
26 {
27     yaz_marc_t mt = xmalloc(sizeof(*mt));
28     mt->xml = YAZ_MARC_LINE;
29     mt->debug = 0;
30     mt->m_wr = wrbuf_alloc();
31     return mt;
32 }
33
34 void yaz_marc_destroy(yaz_marc_t mt)
35 {
36     if (!mt)
37         return ;
38     wrbuf_free (mt->m_wr, 1);
39     xfree (mt);
40 }
41
42 static void marc_cdata (yaz_marc_t mt, const char *buf, size_t len, WRBUF wr)
43 {
44     size_t i;
45     for (i = 0; i<len; i++)
46     {
47         if (mt->xml)
48         {
49             switch (buf[i]) {
50             case '<':
51                 wrbuf_puts(wr, "&lt;");
52                 break;
53             case '>':
54                 wrbuf_puts(wr, "&gt;");
55                 break;
56             case '&':
57                 wrbuf_puts(wr, "&amp;");
58                 break;
59             default:
60                 wrbuf_putc(wr, buf[i]);
61             }
62         }
63         else
64             wrbuf_putc(wr, buf[i]);
65     }
66 }
67
68 #if 0
69 static void marc_cdata (yaz_marc_t mt, const char *buf, size_t len)
70 {
71     if (!mt->cd)
72         marc_cdata2 (mt, buf, len);
73     else
74     {
75         char outbuf[12];
76         size_t inbytesleft = len;
77         const char *inp = buf;
78         
79         while (inbytesleft)
80         {
81             size_t outbytesleft = sizeof(outbuf);
82             char *outp = outbuf;
83             size_t r = yaz_iconv (mt->cd, (char**) &inp, &inbytesleft, 
84                                   &outp, &outbytesleft);
85             if (r == (size_t) (-1))
86             {
87                 int e = yaz_iconv_error(mt->cd);
88                 if (e != YAZ_ICONV_E2BIG)
89                     break;
90             }
91             marc_cdata2 (mt, outbuf, outp - outbuf);
92         }
93     }
94 }
95 #endif
96
97 int yaz_marc_decode_wrbuf (yaz_marc_t mt, const char *buf, int bsize, WRBUF wr)
98 {
99     int entry_p;
100     int record_length;
101     int indicator_length;
102     int identifier_length;
103     int base_address;
104     int length_data_entry;
105     int length_starting;
106     int length_implementation;
107
108     wrbuf_rewind(wr);
109
110     record_length = atoi_n (buf, 5);
111     if (record_length < 25)
112     {
113         if (mt->debug)
114         {
115             char str[40];
116             
117             sprintf (str, "Record length %d - aborting\n", record_length);
118             wrbuf_puts (wr, str);
119         }
120         return -1;
121     }
122     /* ballout if bsize is known and record_length is than that */
123     if (bsize != -1 && record_length > bsize)
124         return -1;
125     if (isdigit(buf[10]))
126         indicator_length = atoi_n (buf+10, 1);
127     else
128         indicator_length = 2;
129     if (isdigit(buf[11]))
130         identifier_length = atoi_n (buf+11, 1);
131     else
132         identifier_length = 2;
133     base_address = atoi_n (buf+12, 5);
134
135     length_data_entry = atoi_n (buf+20, 1);
136     length_starting = atoi_n (buf+21, 1);
137     length_implementation = atoi_n (buf+22, 1);
138
139     if (mt->xml)
140     {
141         char str[80];
142         int i;
143         switch(mt->xml)
144         {
145         case YAZ_MARC_SIMPLEXML:
146             wrbuf_puts (wr, "<iso2709\n");
147             sprintf (str, " RecordStatus=\"%c\"\n", buf[5]);
148             wrbuf_puts (wr, str);
149             sprintf (str, " TypeOfRecord=\"%c\"\n", buf[6]);
150             wrbuf_puts (wr, str);
151             for (i = 1; i<=19; i++)
152             {
153                 sprintf (str, " ImplDefined%d=\"%c\"\n", i, buf[6+i]);
154                 wrbuf_puts (wr, str);
155             }
156             wrbuf_puts (wr, ">\n");
157             break;
158         case YAZ_MARC_OAIMARC:
159             wrbuf_puts(
160                 wr,
161                 "<oai_marc xmlns=\"http://www.openarchives.org/OIA/oai_marc\""
162                 "\n"
163                 " xmlns:xsi=\"http://www.w3.org/2000/10/XMLSchema-instance\""
164                 "\n"
165                 " xsi:schemaLocation=\"http://www.openarchives.org/OAI/oai_marc.xsd\""
166                 "\n"
167                 );
168             
169             sprintf (str, " status=\"%c\" type=\"%c\" catForm=\"%c\">\n",
170                      buf[5], buf[6], buf[7]);
171             wrbuf_puts (wr, str);
172             break;
173         case YAZ_MARC_MARCXML:
174             wrbuf_printf(
175                 wr,
176                 "<record xmlns=\"http://www.loc.gov/MARC21/slim\">\n"
177                 "  <leader>%.24s</leader>\n", buf);
178             break;
179         }
180     }
181     if (mt->debug)
182     {
183         char str[40];
184
185         if (mt->xml)
186             wrbuf_puts (wr, "<!--\n");
187         sprintf (str, "Record length         %5d\n", record_length);
188         wrbuf_puts (wr, str);
189         sprintf (str, "Indicator length      %5d\n", indicator_length);
190         wrbuf_puts (wr, str);
191         sprintf (str, "Identifier length     %5d\n", identifier_length);
192         wrbuf_puts (wr, str);
193         sprintf (str, "Base address          %5d\n", base_address);
194         wrbuf_puts (wr, str);
195         sprintf (str, "Length data entry     %5d\n", length_data_entry);
196         wrbuf_puts (wr, str);
197         sprintf (str, "Length starting       %5d\n", length_starting);
198         wrbuf_puts (wr, str);
199         sprintf (str, "Length implementation %5d\n", length_implementation);
200         wrbuf_puts (wr, str);
201         if (mt->xml)
202             wrbuf_puts (wr, "-->\n");
203     }
204
205     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
206     {
207         entry_p += 3+length_data_entry+length_starting;
208         if (entry_p >= record_length)
209             return -1;
210     }
211     base_address = entry_p+1;
212     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
213     {
214         int data_length;
215         int data_offset;
216         int end_offset;
217         int i, j;
218         char tag[4];
219         int identifier_flag = 1;
220
221         memcpy (tag, buf+entry_p, 3);
222         entry_p += 3;
223         tag[3] = '\0';
224         data_length = atoi_n (buf+entry_p, length_data_entry);
225         entry_p += length_data_entry;
226         data_offset = atoi_n (buf+entry_p, length_starting);
227         entry_p += length_starting;
228         i = data_offset + base_address;
229         end_offset = i+data_length-1;
230         
231         if (indicator_length < 4 && indicator_length > 0)
232         {
233             if (buf[i + indicator_length] != ISO2709_IDFS)
234                 identifier_flag = 0;
235         }
236         else if (!memcmp (tag, "00", 2))
237             identifier_flag = 0;
238         
239         switch(mt->xml)
240         {
241         case YAZ_MARC_LINE:
242             if (mt->debug)
243                 wrbuf_puts (wr, "Tag: ");
244             wrbuf_puts (wr, tag);
245             wrbuf_puts (wr, " ");
246             break;
247         case YAZ_MARC_SIMPLEXML:
248             wrbuf_printf (wr, "<field tag=\"%s\"", tag);
249             break;
250         case YAZ_MARC_OAIMARC:
251             if (identifier_flag)
252                 wrbuf_printf (wr, "  <varfield id=\"%s\"", tag);
253             else
254                 wrbuf_printf (wr, "  <fixfield id=\"%s\"", tag);
255             break;
256         case YAZ_MARC_MARCXML:
257             if (identifier_flag)
258                 wrbuf_printf (wr, "  <datafield tag=\"%s\"", tag);
259             else
260                 wrbuf_printf (wr, "  <controlfield tag=\"%s\"", tag);
261         }
262         
263         if (identifier_flag)
264         {
265             for (j = 0; j<indicator_length; j++, i++)
266             {
267                 switch(mt->xml)
268                 {
269                 case YAZ_MARC_LINE:
270                     if (mt->debug)
271                         wrbuf_puts (wr, " Ind: ");
272                     wrbuf_putc (wr, buf[i]);
273                     break;
274                 case YAZ_MARC_SIMPLEXML:
275                     wrbuf_printf (wr, " Indicator%d=\"%c\"", j+1, buf[i]);
276                     break;
277                 case YAZ_MARC_OAIMARC:
278                     wrbuf_printf (wr, " i%d=\"%c\"", j+1, buf[i]);
279                     break;
280                 case YAZ_MARC_MARCXML:
281                     wrbuf_printf (wr, " ind%d=\"%c\"", j+1, buf[i]);
282                 }
283             }
284         }
285         if (mt->xml)
286         {
287             wrbuf_puts (wr, ">");
288             if (identifier_flag)
289                 wrbuf_puts (wr, "\n");
290         }
291         else
292         {
293             if (mt->debug && !mt->xml)
294                 wrbuf_puts (wr, " Fields: ");
295         }
296         if (identifier_flag)
297         {
298             while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
299             {
300                 int i0;
301                 i++;
302                 switch(mt->xml)
303                 {
304                 case YAZ_MARC_LINE: 
305                     wrbuf_puts (wr, " $"); 
306                     for (j = 1; j<identifier_length; j++, i++)
307                         wrbuf_putc (wr, buf[i]);
308                     wrbuf_putc (wr, ' ');
309                     break;
310                 case YAZ_MARC_SIMPLEXML:
311                     wrbuf_puts (wr, "  <subfield code=\"");
312                     for (j = 1; j<identifier_length; j++, i++)
313                         wrbuf_putc (wr, buf[i]);
314                     wrbuf_puts (wr, "\">");
315                     break;
316                 case YAZ_MARC_OAIMARC:
317                     wrbuf_puts (wr, "    <subfield label=\"");
318                     for (j = 1; j<identifier_length; j++, i++)
319                         wrbuf_putc (wr, buf[i]);
320                     wrbuf_puts (wr, "\">");
321                     break;
322                 case YAZ_MARC_MARCXML:
323                     wrbuf_puts (wr, "    <subfield code=\"");
324                     for (j = 1; j<identifier_length; j++, i++)
325                         wrbuf_putc (wr, buf[i]);
326                     wrbuf_puts (wr, "\">");
327                     break;
328                 }
329                 i0 = i;
330                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
331                        buf[i] != ISO2709_FS && i < end_offset)
332                     i++;
333                 marc_cdata(mt, buf + i0, i - i0, wr);
334                 
335                 if (mt->xml)
336                     wrbuf_puts (wr, "</subfield>\n");
337             }
338         }
339         else
340         {
341             int i0 = i;
342             while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
343                 i++;
344             marc_cdata(mt, buf + i0, i - i0, wr);
345         }
346         if (!mt->xml)
347             wrbuf_putc (wr, '\n');
348         if (i < end_offset)
349             wrbuf_puts (wr, "  <!-- separator but not at end of field -->\n");
350         if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
351             wrbuf_puts (wr, "  <!-- no separator at end of field -->\n");
352         switch(mt->xml)
353         {
354         case YAZ_MARC_SIMPLEXML:
355             wrbuf_puts (wr, "</field>\n");
356             break;
357         case YAZ_MARC_OAIMARC:
358             if (identifier_flag)
359                 wrbuf_puts (wr, "  </varfield>\n");
360             else
361                 wrbuf_puts (wr, "  </fixfield>\n");
362             break;
363         case YAZ_MARC_MARCXML:
364             if (identifier_flag)
365                 wrbuf_puts (wr, "  </datafield>\n");
366             else
367                 wrbuf_puts (wr, "  </controlfield>\n");
368             break;
369         }
370     }
371     switch (mt->xml)
372     {
373     case YAZ_MARC_LINE:
374         wrbuf_puts (wr, "");
375         break;
376     case YAZ_MARC_SIMPLEXML:
377         wrbuf_puts (wr, "</iso2709>\n");
378         break;
379     case YAZ_MARC_OAIMARC:
380         wrbuf_puts (wr, "</oai_marc>\n");
381         break;
382     case YAZ_MARC_MARCXML:
383         wrbuf_puts (wr, "</record>\n");
384         break;
385     }
386     return record_length;
387 }
388
389 int yaz_marc_decode_buf (yaz_marc_t mt, const char *buf, int bsize,
390                          char **result, int *rsize)
391 {
392     int r = yaz_marc_decode_wrbuf(mt, buf, bsize, mt->m_wr);
393     if (r > 0)
394     {
395         if (result)
396             *result = wrbuf_buf(mt->m_wr);
397         if (rsize)
398             *rsize = wrbuf_len(mt->m_wr);
399     }
400     return r;
401 }
402
403 void yaz_marc_xml(yaz_marc_t mt, int xmlmode)
404 {
405     if (mt)
406         mt->xml = xmlmode;
407 }
408
409 void yaz_marc_debug(yaz_marc_t mt, int level)
410 {
411     if (mt)
412         mt->debug = level;
413 }
414
415 /* depricated */
416 int yaz_marc_decode(const char *buf, WRBUF wr, int debug, int bsize, int xml)
417 {
418     yaz_marc_t mt = yaz_marc_create();
419     int r;
420
421     mt->debug = debug;
422     mt->xml = xml;
423     r = yaz_marc_decode_wrbuf(mt, buf, bsize, wr);
424     yaz_marc_destroy(mt);
425     return r;
426 }
427
428 /* depricated */
429 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug, int bsize)
430 {
431     return yaz_marc_decode(buf, wr, debug, bsize, 0);
432 }
433
434 /* depricated */
435 int marc_display_exl (const char *buf, FILE *outf, int debug, int bsize)
436 {
437     yaz_marc_t mt = yaz_marc_create();
438     int r;
439
440     mt->debug = debug;
441     r = yaz_marc_decode_wrbuf (mt, buf, bsize, mt->m_wr);
442     if (!outf)
443         outf = stdout;
444     if (r > 0)
445         fwrite (wrbuf_buf(mt->m_wr), 1, wrbuf_len(mt->m_wr), outf);
446     yaz_marc_destroy(mt);
447     return r;
448 }
449
450 /* depricated */
451 int marc_display_ex (const char *buf, FILE *outf, int debug)
452 {
453     return marc_display_exl (buf, outf, debug, -1);
454 }
455
456 /* depricated */
457 int marc_display (const char *buf, FILE *outf)
458 {
459     return marc_display_ex (buf, outf, 0);
460 }
461