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