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