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