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