More work on new MARC anchor functions.
[egate.git] / util / iso2709dump.c
1 /*
2  * Iso2709 record management
3  *
4  * Europagate, 1994-1995.
5  *
6  * $Log: iso2709dump.c,v $
7  * Revision 1.9  1995/03/30 14:22:18  adam
8  * More work on new MARC anchor functions.
9  *
10  * Revision 1.8  1995/03/30  07:33:37  adam
11  * New 2709 function: iso2709_mk.
12  * First implementation of iso2709_a_insert.
13  *
14  * Revision 1.7  1995/03/28  16:07:07  adam
15  * New function: iso2709_out. This function is the reverse of iso2709_cvt.
16  *
17  * Revision 1.6  1995/03/27  12:52:18  adam
18  * A little more verbose in marc dump.
19  *
20  * Revision 1.5  1995/02/22  21:32:36  adam
21  * Changed header.
22  *
23  * Revision 1.3  1995/02/10  17:05:18  adam
24  * New function iso2709_display to display MARC records in a
25  * line-by-line format. The iso2709_cvt function no longer
26  * prints the record to stderr.
27  *
28  * Revision 1.2  1995/02/10  16:50:33  adam
29  * Indicator field moved to 'struct iso2709_dir' from 'struct
30  * iso2709_field'.
31  * Function iso2709_rm implemented - to delete a MARC record.
32  *
33  * Revision 1.1.1.1  1995/02/09  17:27:11  adam
34  * Initial version of email gateway under CVS control.
35  *
36  */
37
38 #include <stdio.h>
39 #include <stdlib.h>
40 #include <errno.h>
41
42 #include <iso2709.h>
43
44 static char *prog;
45
46 static Iso2709Rec copy_rec (Iso2709Rec rec_in)
47 {
48     Iso2709Rec rec_out;
49     Iso2709Anchor a_in, a_out;
50     char *tag, *indicator, *identifier, *data;
51
52     rec_out = iso2709_mk();
53
54     a_in = iso2709_a_mk (rec_in);
55     a_out = iso2709_a_mk (rec_out);
56
57     do {
58         if (!iso2709_a_info_field (a_in, &tag, &indicator, &identifier, &data))
59             break;
60         iso2709_a_insert (a_out, tag, indicator, identifier, data);
61     } while (iso2709_a_next (a_in));
62     iso2709_a_rm (a_in);
63     iso2709_a_rm (a_out);
64     return rec_out;
65 }
66
67 int main (int argc, char **argv)
68 {
69     char *buf;
70     int no = 0;
71     int start_pos = 0;
72     int end_pos = 99999;
73     char *output_file = NULL;
74     char *input_file = NULL;
75     char *filter = NULL;
76     int verbose = 0;
77     int quiet = 0;
78     FILE *outf = NULL;
79     FILE *inf = stdin;
80
81     prog = *argv;
82     while (--argc > 0)
83     {
84         if (**++argv == '-')
85         {
86             switch (argv[0][1])
87             {
88             case 'h':
89             case 'H':
90                 fprintf (stderr, "iso2709dump [options] [file]\n");
91                 fprintf (stderr, "If no file is specified stdin is used\n");
92                 fprintf (stderr, "Options:\n");
93                 fprintf (stderr, " -p no        Starting position\n");
94                 fprintf (stderr, " -l end       Ending position\n");
95                 fprintf (stderr, " -o file      Raw MARC output file\n");
96                 fprintf (stderr, " -v           Verbose\n");
97                 fprintf (stderr, " -q           Quiet mode\n");
98                 exit (1);
99             case 'p':
100                 if (argv[0][2])
101                     start_pos = atoi (argv[0]+2);
102                 else if (argc > 0)
103                 {
104                     --argc;
105                     start_pos = atoi (*++argv);
106                 }
107                 else
108                 {
109                     fprintf (stderr, "%s: missing start pos\n", prog);
110                     exit (1);
111                 }
112                 break;
113             case 'l':
114                 if (argv[0][2])
115                     end_pos = atoi (argv[0]+2);
116                 else if (argc > 0)
117                 {
118                     --argc;
119                     end_pos = atoi (*++argv);
120                 }
121                 else
122                 {
123                     fprintf (stderr, "%s: missing end pos\n", prog);
124                     exit (1);
125                 }
126                 break;
127             case 'o':
128                 if (argv[0][2])
129                     output_file = argv[0]+2;
130                 else if (argc > 0)
131                 {
132                     --argc;
133                     output_file = *++argv;
134                 }
135                 else
136                 {
137                     fprintf (stderr, "%s: missing output file\n", prog);
138                     exit (1);
139                 }
140                 break;
141             case 'v':
142                 verbose = 1;
143                 break;
144             case 'q':
145                 quiet = 1;
146                 break;
147             case 'f':
148                 if (argv[0][2])
149                     filter = argv[0]+2;
150                 else if (argc > 0)
151                 {
152                     --argc;
153                     filter = *++argv;
154                 }
155                 else
156                 {
157                     fprintf (stderr, "%s: missing filter\n", prog);
158                     exit (1);
159                 }
160                 break;
161             default:
162                 fprintf (stderr, "%s: unknown option %s; use -h for help\n",
163                          prog, *argv);
164                 exit (1);
165             }
166         }
167         else
168             input_file = *argv;
169     }
170     if (input_file)
171     {
172         inf = fopen (input_file, "r");
173         if (!inf)
174         {
175             fprintf (stderr, "%s: cannot open %s: %s\n",
176                      prog, input_file, strerror (errno));
177             exit (1);
178         }
179     }
180     if (output_file)
181     {
182         outf = fopen (output_file, "w");
183         if (!outf)
184         {
185             fprintf (stderr, "%s: cannot open %s: %s\n",
186                      prog, output_file, strerror (errno));
187             exit (1);
188         }
189     }
190     while ((buf = iso2709_read (inf)))
191     {
192         if (no >= start_pos && no <= end_pos)
193         {
194             char *obuf;
195             int olen;
196             Iso2709Rec rec_input;
197             Iso2709Rec rec_output;
198             
199             rec_input = iso2709_cvt (buf);
200             if (!quiet)
201                 printf ("------- %d --------\n", no);
202             if (!rec_input)
203             {
204                 if (!quiet)
205                     printf ("Bad record\n");
206                 fprintf (stderr, "%s: bad record at position %d\n", prog, no);
207                 break;
208             }
209             olen = iso2709_out (rec_input, &obuf, 0);
210             rec_output = copy_rec (rec_input);
211             if (!quiet)
212             {
213                 iso2709_display (rec_input, stdout);
214                 iso2709_display (rec_output, stdout);
215             }
216             if (outf && fwrite (obuf, 1, olen, outf) != olen)
217             {
218                 fprintf (stderr, "%s: write fail of %s: %s\n",
219                          prog, output_file, strerror (errno));
220                 exit (1);
221             }
222             free (obuf);
223             iso2709_rm (rec_input);
224         }
225         free (buf);
226         no++;
227     }
228     if (outf)
229         fclose (outf);
230     return 0;
231 }