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