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