New function marc_display_wrbuf.
[yaz-moved-to-github.git] / util / marcdisp.c
1 /*
2  * Copyright (c) 1995-2001, Index Data
3  * See the file LICENSE for details.
4  *
5  * $Log: marcdisp.c,v $
6  * Revision 1.13  2001-10-15 19:36:48  adam
7  * New function marc_display_wrbuf.
8  *
9  * Revision 1.12  2000/10/02 11:07:44  adam
10  * Added peer_name member for bend_init handler. Changed the YAZ
11  * client so that tcp: can be avoided in target spec.
12  *
13  * Revision 1.11  2000/02/29 13:44:55  adam
14  * Check for config.h (currently not generated).
15  *
16  * Revision 1.10  2000/02/05 10:47:19  adam
17  * Identifier-length and indicator-lenght no longer set to 2 (forced).
18  *
19  * Revision 1.9  1999/12/21 16:24:48  adam
20  * More robust ISO2709 handling (in case of real bad formats).
21  *
22  * Revision 1.8  1999/11/30 13:47:12  adam
23  * Improved installation. Moved header files to include/yaz.
24  *
25  * Revision 1.7  1997/09/24 13:29:40  adam
26  * Added verbose option -v to marcdump utility.
27  *
28  * Revision 1.6  1997/09/04 07:52:27  adam
29  * Moved atoi_n function to separate source file.
30  *
31  * Revision 1.5  1997/05/01 15:08:15  adam
32  * Added log_mask_str_x routine.
33  *
34  * Revision 1.4  1995/09/29 17:12:34  quinn
35  * Smallish
36  *
37  * Revision 1.3  1995/09/27  15:03:03  quinn
38  * Modified function heads & prototypes.
39  *
40  * Revision 1.2  1995/05/16  08:51:12  quinn
41  * License, documentation, and memory fixes
42  *
43  * Revision 1.1  1995/04/10  10:28:46  quinn
44  * Added copy of CCL and MARC display
45  *
46  */
47
48 #if HAVE_CONFIG_H
49 #include <config.h>
50 #endif
51
52 #include <stdio.h>
53 #include <string.h>
54 #include <ctype.h>
55 #include <yaz/marcdisp.h>
56 #include <yaz/wrbuf.h>
57 #include <yaz/yaz-util.h>
58
59 int marc_display_wrbuf (const char *buf, WRBUF wr, int debug)
60 {
61     int entry_p;
62     int record_length;
63     int indicator_length;
64     int identifier_length;
65     int base_address;
66     int length_data_entry;
67     int length_starting;
68     int length_implementation;
69
70     record_length = atoi_n (buf, 5);
71     if (record_length < 25)
72     {
73         if (debug)
74         {
75             char str[40];
76             
77             sprintf (str, "Record length %d - aborting\n", record_length);
78             wrbuf_puts (wr, str);
79         }
80         return -1;
81     }
82     if (isdigit(buf[10]))
83         indicator_length = atoi_n (buf+10, 1);
84     else
85         indicator_length = 2;
86     if (isdigit(buf[11]))
87         identifier_length = atoi_n (buf+11, 1);
88     else
89         identifier_length = 2;
90     base_address = atoi_n (buf+12, 4);
91
92     length_data_entry = atoi_n (buf+20, 1);
93     length_starting = atoi_n (buf+21, 1);
94     length_implementation = atoi_n (buf+22, 1);
95
96     if (debug)
97     {
98         char str[40];
99         sprintf (str, "Record length         %5d\n", record_length);
100         wrbuf_puts (wr, str);
101         sprintf (str, "Indicator length      %5d\n", indicator_length);
102         wrbuf_puts (wr, str);
103         sprintf (str, "Identifier length     %5d\n", identifier_length);
104         wrbuf_puts (wr, str);
105         sprintf (str, "Base address          %5d\n", base_address);
106         wrbuf_puts (wr, str);
107         sprintf (str, "Length data entry     %5d\n", length_data_entry);
108         wrbuf_puts (wr, str);
109         sprintf (str, "Length starting       %5d\n", length_starting);
110         wrbuf_puts (wr, str);
111         sprintf (str, "Length implementation %5d\n", length_implementation);
112         wrbuf_puts (wr, str);
113     }
114     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
115     {
116         entry_p += 3+length_data_entry+length_starting;
117         if (entry_p >= record_length)
118             return -1;
119     }
120     base_address = entry_p+1;
121     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
122     {
123         int data_length;
124         int data_offset;
125         int end_offset;
126         int i, j;
127         char tag[4];
128
129         memcpy (tag, buf+entry_p, 3);
130         entry_p += 3;
131         tag[3] = '\0';
132         if (debug)
133             wrbuf_puts (wr, "Tag: ");
134         wrbuf_puts (wr, tag);
135         wrbuf_puts (wr, " ");
136         data_length = atoi_n (buf+entry_p, length_data_entry);
137         entry_p += length_data_entry;
138         data_offset = atoi_n (buf+entry_p, length_starting);
139         entry_p += length_starting;
140         i = data_offset + base_address;
141         end_offset = i+data_length-1;
142         if (debug)
143             wrbuf_puts (wr, " Ind: ");
144         if (memcmp (tag, "00", 2) && indicator_length)
145         {
146             for (j = 0; j<indicator_length; j++, i++)
147                 wrbuf_putc (wr, buf[i]);
148         }
149         if (debug)
150             wrbuf_puts (wr, " Fields: ");
151         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
152         {
153             if (memcmp (tag, "00", 2) && identifier_length)
154             {
155                 i++;
156                 wrbuf_puts (wr, " $"); 
157                 for (j = 1; j<identifier_length; j++, i++)
158                     wrbuf_putc (wr, buf[i]);
159                 wrbuf_putc (wr, ' ');
160                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
161                        buf[i] != ISO2709_FS && i < end_offset)
162                 {
163                     wrbuf_putc (wr, buf[i]);
164                     i++;
165                 }
166             }
167             else
168             {
169                 wrbuf_putc (wr, buf[i]);
170                 i++;
171             }
172         }
173         wrbuf_putc (wr, '\n');
174         if (i < end_offset)
175             wrbuf_puts (wr, "-- separator but not at end of field\n");
176         if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
177             wrbuf_puts (wr, "-- no separator at end of field\n");
178     }
179     wrbuf_puts (wr, "");
180     return record_length;
181 }
182
183 int marc_display_ex (const char *buf, FILE *outf, int debug)
184 {
185     int record_length;
186
187     WRBUF wrbuf = wrbuf_alloc ();
188     record_length = marc_display_wrbuf (buf, wrbuf, debug);
189     if (!outf)
190         outf = stdout;
191     if (record_length > 0)
192         fwrite (wrbuf_buf(wrbuf), 1, wrbuf_len(wrbuf), outf);
193     wrbuf_free (wrbuf, 1);
194     return record_length;
195 }
196
197 int marc_display (const char *buf, FILE *outf)
198 {
199     return marc_display_ex (buf, outf, 0);
200 }
201
202