For Libxml2 and friends, YAZ defines YAZ_HAVE_{XML2,XSLT,EXSLT) in
[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.40 2006-07-06 10:17:55 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 YAZ_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 YAZ_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 YAZ_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                 size_t len;
133                 char *result = 0;
134                 size_t 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                 { int rlentmp = (int) rlen;
208                   r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlentmp);
209                   rlen = (size_t) rlentmp; }
210                 if (r > 0 && result)
211                 {
212                     fwrite (result, rlen, 1, stdout);
213                 }
214                 if (r > 0 && cfile)
215                 {
216                     char *p = buf;
217                     size_t i;
218                     if (count)
219                         fprintf (cfile, ",");
220                     fprintf (cfile, "\n");
221                     for (i = 0; i < r; i++)
222                     {
223                         if ((i & 15) == 0)
224                             fprintf (cfile, "  \"");
225                         fprintf (cfile, "\\x%02X", p[i] & 255);
226                         
227                         if (i < r - 1 && (i & 15) == 15)
228                             fprintf (cfile, "\"\n");
229                         
230                     }
231                     fprintf (cfile, "\"\n");
232                 }
233                 num++;
234                 if (verbose)
235                     printf("\n");
236             }
237             count++;
238         }
239         if (cfile)
240             fprintf (cfile, "};\n");
241         fclose(inf);
242     }
243     if (cd)
244         yaz_iconv_close(cd);
245     yaz_marc_destroy(mt);
246 }
247
248 int main (int argc, char **argv)
249 {
250     int r;
251     int print_offset = 0;
252     char *arg;
253     int verbose = 0;
254     int no = 0;
255     int xml = 0;
256     FILE *cfile = 0;
257     char *from = 0, *to = 0;
258     int read_xml = 0;
259     const char *split_fname = 0;
260     
261 #if HAVE_LOCALE_H
262     setlocale(LC_CTYPE, "");
263 #endif
264 #if HAVE_LANGINFO_H
265 #ifdef CODESET
266     to = nl_langinfo(CODESET);
267 #endif
268 #endif
269
270     prog = *argv;
271     while ((r = options("pvc:xOeXIf:t:s:", argv, argc, &arg)) != -2)
272     {
273         no++;
274         switch (r)
275         {
276         case 'f':
277             from = arg;
278             break;
279         case 't':
280             to = arg;
281             break;
282         case 'c':
283             if (cfile)
284                 fclose (cfile);
285             cfile = fopen(arg, "w");
286             break;
287         case 'x':
288 #if YAZ_HAVE_XML2
289             read_xml = 1;
290 #else
291             fprintf(stderr, "%s: -x not supported."
292                     " YAZ not compiled with Libxml2 support\n", prog);
293             exit(3);
294 #endif
295             break;
296         case 'O':
297             fprintf(stderr, "%s: OAI MARC no longer supported."
298                     " Use MARCXML instead.\n", prog);
299             exit(1);
300             break;
301         case 'e':
302             xml = YAZ_MARC_XCHANGE;
303             break;
304         case 'X':
305             xml = YAZ_MARC_MARCXML;
306             break;
307         case 'I':
308             xml = YAZ_MARC_ISO2709;
309             break;
310         case 'p':
311             print_offset = 1;
312             break;
313         case 's':
314             split_fname = arg;
315             break;
316         case 0:
317             dump(arg, from, to, read_xml, xml,
318                  print_offset, split_fname, verbose, cfile);
319             break;
320         case 'v':
321             verbose++;
322             break;
323         default:
324             usage(prog);
325             exit (1);
326         }
327     }
328     if (cfile)
329         fclose (cfile);
330     if (!no)
331     {
332         usage(prog);
333         exit (1);
334     }
335     exit (0);
336 }
337 /*
338  * Local variables:
339  * c-basic-offset: 4
340  * indent-tabs-mode: nil
341  * End:
342  * vim: shiftwidth=4 tabstop=8 expandtab
343  */
344