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