714c5617c9198918d739d69f96f099ab36ee58e2
[yaz-moved-to-github.git] / util / marcdisp.c
1 /*
2  * Copyright (C) 1994, Index Data I/S 
3  * All rights reserved.
4  * Sebastian Hammer, Adam Dickmeiss
5  *
6  * $Log: marcdisp.c,v $
7  * Revision 1.1  1995-04-10 10:28:46  quinn
8  * Added copy of CCL and MARC display
9  *
10  */
11
12 #include <stdio.h>
13 #include <string.h>
14 #include <ctype.h>
15 #include <marcdisp.h>
16
17 #define ISO2709_RS 035
18 #define ISO2709_FS 036
19 #define ISO2709_IDFS 037
20
21 int atoi_n (const char *buf, int len)
22 {
23     int val = 0;
24
25     while (--len >= 0)
26     {
27         if (isdigit (*buf))
28             val = val*10 + (*buf - '0');
29         buf++;
30     }
31     return val;
32 }
33
34 int marc_display (const char *buf, FILE *outf)
35 {
36     int entry_p;
37     int record_length;
38     int indicator_length;
39     int identifier_length;
40     int base_address;
41     int length_data_entry;
42     int length_starting;
43     int length_implementation;
44
45     record_length = atoi_n (buf, 5);
46     if (record_length < 25)
47         return -1;
48     indicator_length = atoi_n (buf+10, 1);
49     identifier_length = atoi_n (buf+11, 1);
50     base_address = atoi_n (buf+12, 4);
51
52     length_data_entry = atoi_n (buf+20, 1);
53     length_starting = atoi_n (buf+21, 1);
54     length_implementation = atoi_n (buf+22, 1);
55
56     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
57         entry_p += 3+length_data_entry+length_starting;
58     base_address = entry_p+1;
59     for (entry_p = 24; buf[entry_p] != ISO2709_FS; )
60     {
61         int data_length;
62         int data_offset;
63         int end_offset;
64         int i, j;
65         char tag[4];
66
67         memcpy (tag, buf+entry_p, 3);
68         entry_p += 3;
69         tag[3] = '\0';
70         fprintf (outf, "%s ", tag);
71         data_length = atoi_n (buf+entry_p, length_data_entry);
72         entry_p += length_data_entry;
73         data_offset = atoi_n (buf+entry_p, length_starting);
74         entry_p += length_starting;
75         i = data_offset + base_address;
76         end_offset = i+data_length-1;
77         if (memcmp (tag, "00", 2) && indicator_length)
78         {
79             for (j = 0; j<indicator_length; j++)
80                 fprintf (outf, "%c", buf[i++]);
81         }
82         while (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS && i < end_offset)
83         {
84             if (memcmp (tag, "00", 2) && identifier_length)
85             {
86                 i++;
87                 fprintf (outf, " $"); 
88                 for (j = 1; j<identifier_length; j++)
89                     fprintf (outf, "%c", buf[i++]);
90                 fprintf (outf, " ");
91                 while (buf[i] != ISO2709_RS && buf[i] != ISO2709_IDFS &&
92                        buf[i] != ISO2709_FS && i < end_offset)
93                     fprintf (outf, "%c", buf[i++]);
94             }
95             else
96                 fprintf (outf, "%c", buf[i++]);
97         }
98         fprintf (outf, "\n");
99         if (i < end_offset)
100             fprintf (outf, "-- separator but not at end of field\n");
101         if (buf[i] != ISO2709_RS && buf[i] != ISO2709_FS)
102             fprintf (outf, "-- no separator at end of field\n");
103     }
104     return record_length;
105 }
106