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