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