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