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