Added several type casts due to no portable way of doing printf of
[yaz-moved-to-github.git] / util / marcdump.c
1 /*
2  * Copyright (C) 1995-2006, Index Data ApS
3  * See the file LICENSE for details.
4  *
5  * $Id: marcdump.c,v 1.37 2006-04-21 10:28:07 adam Exp $
6  */
7
8 #define _FILE_OFFSET_BITS 64
9
10 #if HAVE_CONFIG_H
11 #include <config.h>
12 #endif
13
14 #if HAVE_XML2
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
17
18 #include <libxml/xpath.h>
19 #include <libxml/xpathInternals.h>
20
21 #endif
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <errno.h>
27 #include <assert.h>
28
29 #if HAVE_LOCALE_H
30 #include <locale.h>
31 #endif
32 #if HAVE_LANGINFO_H
33 #include <langinfo.h>
34 #endif
35
36 #include <yaz/marcdisp.h>
37 #include <yaz/yaz-util.h>
38 #include <yaz/xmalloc.h>
39 #include <yaz/options.h>
40
41 #ifndef SEEK_SET
42 #define SEEK_SET 0
43 #endif
44 #ifndef SEEK_END
45 #define SEEK_END 2
46 #endif
47
48
49 static char *prog;
50
51 static void usage(const char *prog)
52 {
53     fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] [-x] [-X] [-e] [-I] [-v] [-s splitfname] file...\n",
54              prog);
55
56
57 #if HAVE_XML2
58 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
59 {
60     xmlNodePtr ptr;
61     xmlDocPtr doc = xmlParseFile(fname);
62     if (!doc)
63         return;
64
65     ptr = xmlDocGetRootElement(doc);
66     if (ptr)
67     {
68         int r;
69         WRBUF wrbuf = wrbuf_alloc();
70         r = yaz_marc_read_xml(mt, ptr);
71         if (r)
72             fprintf(stderr, "yaz_marc_read_xml failed\n");
73         
74         yaz_marc_write_mode(mt, wrbuf);
75
76         fputs(wrbuf_buf(wrbuf), stdout);
77
78         wrbuf_free(wrbuf, 1);
79     }
80     xmlFreeDoc(doc);
81 }
82 #endif
83
84 static void dump(const char *fname, const char *from, const char *to,
85                  int read_xml, int xml,
86                  int print_offset, const char *split_fname, int verbose,
87                  FILE *cfile)
88 {
89     yaz_marc_t mt = yaz_marc_create();
90     yaz_iconv_t cd = 0;
91     
92     if (from && to)
93     {
94         cd = yaz_iconv_open(to, from);
95         if (!cd)
96         {
97             fprintf(stderr, "conversion from %s to %s "
98                     "unsupported\n", from, to);
99             exit(2);
100         }
101         yaz_marc_iconv(mt, cd);
102     }
103     yaz_marc_xml(mt, xml);
104     yaz_marc_debug(mt, verbose);
105
106     if (read_xml)
107     {
108 #if HAVE_XML2
109         marcdump_read_xml(mt, fname);
110 #else
111         return;
112 #endif
113     }
114     else
115     {
116         FILE *inf = fopen(fname, "rb");
117         int count = 0;
118         int num = 1;
119         if (!inf)
120         {
121             fprintf (stderr, "%s: cannot open %s:%s\n",
122                      prog, fname, strerror (errno));
123             exit(1);
124         }
125         if (cfile)
126             fprintf (cfile, "char *marc_records[] = {\n");
127         if (1)
128         {
129             int marc_no = 0;
130             for(;; marc_no++)
131             {
132                 int len;
133                 char *result = 0;
134                 int rlen;
135                 size_t r;
136                 char buf[100001];
137                 
138                 r = fread (buf, 1, 5, inf);
139                 if (r < 5)
140                 {
141                     if (r && print_offset && verbose)
142                         printf ("<!-- Extra %ld bytes at end of file -->\n",
143                                 (long) r);
144                     break;
145                 }
146                 while (*buf < '0' || *buf > '9')
147                 {
148                     int i;
149                     long off = ftell(inf) - 5;
150                     if (verbose || print_offset)
151                         printf("<!-- Skipping bad byte %d (0x%02X) at offset "
152                                "%ld (0x%lx) -->\n", 
153                                *buf & 0xff, *buf & 0xff,
154                                off, off);
155                     for (i = 0; i<4; i++)
156                         buf[i] = buf[i+1];
157                     r = fread(buf+4, 1, 1, inf);
158                     if (r < 1)
159                         break;
160                 }
161                 if (r < 1)
162                 {
163                     if (verbose || print_offset)
164                         printf ("<!-- End of file with data -->\n");
165                     break;
166                 }
167                 if (print_offset)
168                 {
169                     long off = ftell(inf) - 5;
170                     printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
171                             num, off, off);
172                 }
173                 len = atoi_n(buf, 5);
174                 if (len < 25 || len > 100000)
175                 {
176                     long off = ftell(inf) - 5;
177                     printf("Bad Length %d read at offset %ld (%lx)\n",
178                            len, (long) off, (long) off);
179                     break;
180                 }
181                 rlen = len - 5;
182                 r = fread (buf + 5, 1, rlen, inf);
183                 if (r < rlen)
184                     break;
185                 if (split_fname)
186                 {
187                     char fname[256];
188                     FILE *sf;
189                     sprintf(fname, "%.200s%07d", split_fname, marc_no);
190                     sf = fopen(fname, "wb");
191                     if (!sf)
192                     {
193                         fprintf(stderr, "Could not open %s\n", fname);
194                         split_fname = 0;
195                     }
196                     else
197                     {
198                         if (fwrite(buf, 1, len, sf) != len)
199                         {
200                             fprintf(stderr, "Could write content to %s\n",
201                                     fname);
202                             split_fname = 0;
203                         }
204                         fclose(sf);
205                     }
206                 }
207                 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
208                 if (r > 0 && result)
209                 {
210                     fwrite (result, rlen, 1, stdout);
211                 }
212                 if (r > 0 && cfile)
213                 {
214                     char *p = buf;
215                     int i;
216                     if (count)
217                         fprintf (cfile, ",");
218                     fprintf (cfile, "\n");
219                     for (i = 0; i < r; i++)
220                     {
221                         if ((i & 15) == 0)
222                             fprintf (cfile, "  \"");
223                         fprintf (cfile, "\\x%02X", p[i] & 255);
224                         
225                         if (i < r - 1 && (i & 15) == 15)
226                             fprintf (cfile, "\"\n");
227                         
228                     }
229                     fprintf (cfile, "\"\n");
230                 }
231                 num++;
232                 if (verbose)
233                     printf("\n");
234             }
235             count++;
236         }
237         if (cfile)
238             fprintf (cfile, "};\n");
239         fclose(inf);
240     }
241     if (cd)
242         yaz_iconv_close(cd);
243     yaz_marc_destroy(mt);
244 }
245
246 int main (int argc, char **argv)
247 {
248     int r;
249     int print_offset = 0;
250     char *arg;
251     int verbose = 0;
252     int no = 0;
253     int xml = 0;
254     FILE *cfile = 0;
255     char *from = 0, *to = 0;
256     int read_xml = 0;
257     const char *split_fname = 0;
258     
259 #if HAVE_LOCALE_H
260     setlocale(LC_CTYPE, "");
261 #endif
262 #if HAVE_LANGINFO_H
263 #ifdef CODESET
264     to = nl_langinfo(CODESET);
265 #endif
266 #endif
267
268     prog = *argv;
269     while ((r = options("pvc:xOeXIf:t:s:", argv, argc, &arg)) != -2)
270     {
271         no++;
272         switch (r)
273         {
274         case 'f':
275             from = arg;
276             break;
277         case 't':
278             to = arg;
279             break;
280         case 'c':
281             if (cfile)
282                 fclose (cfile);
283             cfile = fopen(arg, "w");
284             break;
285         case 'x':
286 #if HAVE_XML2
287             read_xml = 1;
288 #else
289             fprintf(stderr, "%s: -x not supported."
290                     " YAZ not compiled with Libxml2 support\n", prog);
291             exit(3);
292 #endif
293             break;
294         case 'O':
295             fprintf(stderr, "%s: OAI MARC no longer supported."
296                     " Use MARCXML instead.\n", prog);
297             exit(1);
298             break;
299         case 'e':
300             xml = YAZ_MARC_XCHANGE;
301             break;
302         case 'X':
303             xml = YAZ_MARC_MARCXML;
304             break;
305         case 'I':
306             xml = YAZ_MARC_ISO2709;
307             break;
308         case 'p':
309             print_offset = 1;
310             break;
311         case 's':
312             split_fname = arg;
313             break;
314         case 0:
315             dump(arg, from, to, read_xml, xml,
316                  print_offset, split_fname, verbose, cfile);
317             break;
318         case 'v':
319             verbose++;
320             break;
321         default:
322             usage(prog);
323             exit (1);
324         }
325     }
326     if (cfile)
327         fclose (cfile);
328     if (!no)
329     {
330         usage(prog);
331         exit (1);
332     }
333     exit (0);
334 }
335 /*
336  * Local variables:
337  * c-basic-offset: 4
338  * indent-tabs-mode: nil
339  * End:
340  * vim: shiftwidth=4 tabstop=8 expandtab
341  */
342