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