Added log_mask_str_x routine.
[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.5  1997-05-01 15:08:15  adam
8  * Added log_mask_str_x routine.
9  *
10  * Revision 1.4  1995/09/29 17:12:34  quinn
11  * Smallish
12  *
13  * Revision 1.3  1995/09/27  15:03:03  quinn
14  * Modified function heads & prototypes.
15  *
16  * Revision 1.2  1995/05/16  08:51:12  quinn
17  * License, documentation, and memory fixes
18  *
19  * Revision 1.1  1995/04/10  10:28:46  quinn
20  * Added copy of CCL and MARC display
21  *
22  */
23
24 #include <stdio.h>
25 #include <string.h>
26 #include <ctype.h>
27 #include <marcdisp.h>
28
29 #define ISO2709_RS 035
30 #define ISO2709_FS 036
31 #define ISO2709_IDFS 037
32
33 int atoi_n (const char *buf, int len)
34 {
35     int val = 0;
36
37     while (--len >= 0)
38     {
39         if (isdigit (*buf))
40             val = val*10 + (*buf - '0');
41         buf++;
42     }
43     return val;
44 }
45
46 int marc_display (const char *buf, FILE *outf)
47 {
48     int entry_p;
49     int record_length;
50     int indicator_length;
51     int identifier_length;
52     int base_address;
53     int length_data_entry;
54     int length_starting;
55     int length_implementation;
56
57     if (!outf)
58         outf = stdout;
59     record_length = atoi_n (buf, 5);
60     if (record_length < 25)
61         return -1;
62     indicator_length = atoi_n (buf+10, 1);
63     identifier_length = atoi_n (buf+11, 1);
64     base_address = atoi_n (buf+12, 4);
65
66     length_data_entry = atoi_n (buf+20, 1);
67     length_starting = atoi_n (buf+21, 1);
68     length_implementation = atoi_n (buf+22, 1);
69
70     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
71         entry_p += 3+length_data_entry+length_starting;
72     base_address = entry_p+1;
73     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
74     {
75         int data_length;
76         int data_offset;
77         int end_offset;
78         int i, j;
79         char tag[4];
80
81         memcpy (tag, buf+entry_p, 3);
82         entry_p += 3;
83         tag[3] = '\0';
84         fprintf (outf, "%s ", tag);
85         data_length = atoi_n (buf+entry_p, length_data_entry);
86         entry_p += length_data_entry;
87         data_offset = atoi_n (buf+entry_p, length_starting);
88         entry_p += length_starting;
89         i = data_offset + base_address;
90         end_offset = i+data_length-1;
91         if (memcmp (tag, "00", 2) && indicator_length)
92         {
93             for (j = 0; j<indicator_length; j++)
94                 fprintf (outf, "%c", buf[i++]);
95         }
96         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
97         {
98             if (memcmp (tag, "00", 2) && identifier_length)
99             {
100                 i++;
101                 fprintf (outf, " $"); 
102                 for (j = 1; j<identifier_length; j++)
103                     fprintf (outf, "%c", buf[i++]);
104                 fprintf (outf, " ");
105                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
106                        buf[i] != ISO2709_FS && i < end_offset)
107                     fprintf (outf, "%c", buf[i++]);
108             }
109             else
110                 fprintf (outf, "%c", buf[i++]);
111         }
112         fprintf (outf, "\n");
113         if (i < end_offset)
114             fprintf (outf, "-- separator but not at end of field\n");
115         if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
116             fprintf (outf, "-- no separator at end of field\n");
117     }
118     return record_length;
119 }
120