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