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