New facilities for the MARC module. The reading - and writing 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.36 2006-04-19 10:05:04 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 %d bytes at end of file -->\n", r);
143                     break;
144                 }
145                 while (*buf < '0' || *buf > '9')
146                 {
147                     int i;
148                     long off = ftell(inf) - 5;
149                     if (verbose || print_offset)
150                         printf("<!-- Skipping bad byte %d (0x%02X) at offset "
151                                "%ld (0x%lx) -->\n", 
152                                *buf & 0xff, *buf & 0xff,
153                                off, off);
154                     for (i = 0; i<4; i++)
155                         buf[i] = buf[i+1];
156                     r = fread(buf+4, 1, 1, inf);
157                     if (r < 1)
158                         break;
159                 }
160                 if (r < 1)
161                 {
162                     if (verbose || print_offset)
163                         printf ("<!-- End of file with data -->\n");
164                     break;
165                 }
166                 if (print_offset)
167                 {
168                     long off = ftell(inf) - 5;
169                     printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
170                             num, off, off);
171                 }
172                 len = atoi_n(buf, 5);
173                 if (len < 25 || len > 100000)
174                 {
175                     long off = ftell(inf) - 5;
176                     printf("Bad Length %d read at offset %ld (%lx)\n",
177                            len, (long) off, (long) off);
178                     break;
179                 }
180                 rlen = len - 5;
181                 r = fread (buf + 5, 1, rlen, inf);
182                 if (r < rlen)
183                     break;
184                 if (split_fname)
185                 {
186                     char fname[256];
187                     FILE *sf;
188                     sprintf(fname, "%.200s%07d", split_fname, marc_no);
189                     sf = fopen(fname, "wb");
190                     if (!sf)
191                     {
192                         fprintf(stderr, "Could not open %s\n", fname);
193                         split_fname = 0;
194                     }
195                     else
196                     {
197                         if (fwrite(buf, 1, len, sf) != len)
198                         {
199                             fprintf(stderr, "Could write content to %s\n",
200                                     fname);
201                             split_fname = 0;
202                         }
203                         fclose(sf);
204                     }
205                 }
206                 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
207                 if (r > 0 && result)
208                 {
209                     fwrite (result, rlen, 1, stdout);
210                 }
211                 if (r > 0 && cfile)
212                 {
213                     char *p = buf;
214                     int i;
215                     if (count)
216                         fprintf (cfile, ",");
217                     fprintf (cfile, "\n");
218                     for (i = 0; i < r; i++)
219                     {
220                         if ((i & 15) == 0)
221                             fprintf (cfile, "  \"");
222                         fprintf (cfile, "\\x%02X", p[i] & 255);
223                         
224                         if (i < r - 1 && (i & 15) == 15)
225                             fprintf (cfile, "\"\n");
226                         
227                     }
228                     fprintf (cfile, "\"\n");
229                 }
230                 num++;
231                 if (verbose)
232                     printf("\n");
233             }
234             count++;
235         }
236         if (cfile)
237             fprintf (cfile, "};\n");
238         fclose(inf);
239     }
240     if (cd)
241         yaz_iconv_close(cd);
242     yaz_marc_destroy(mt);
243 }
244
245 int main (int argc, char **argv)
246 {
247     int r;
248     int print_offset = 0;
249     char *arg;
250     int verbose = 0;
251     int no = 0;
252     int xml = 0;
253     FILE *cfile = 0;
254     char *from = 0, *to = 0;
255     int read_xml = 0;
256     const char *split_fname = 0;
257     
258 #if HAVE_LOCALE_H
259     setlocale(LC_CTYPE, "");
260 #endif
261 #if HAVE_LANGINFO_H
262 #ifdef CODESET
263     to = nl_langinfo(CODESET);
264 #endif
265 #endif
266
267     prog = *argv;
268     while ((r = options("pvc:xOeXIf:t:s:", argv, argc, &arg)) != -2)
269     {
270         no++;
271         switch (r)
272         {
273         case 'f':
274             from = arg;
275             break;
276         case 't':
277             to = arg;
278             break;
279         case 'c':
280             if (cfile)
281                 fclose (cfile);
282             cfile = fopen(arg, "w");
283             break;
284         case 'x':
285 #if HAVE_XML2
286             read_xml = 1;
287 #else
288             fprintf(stderr, "%s: -x not supported."
289                     " YAZ not compiled with Libxml2 support\n", prog);
290             exit(3);
291 #endif
292             break;
293         case 'O':
294             fprintf(stderr, "%s: OAI MARC no longer supported."
295                     " Use MARCXML instead.\n", prog);
296             exit(1);
297             break;
298         case 'e':
299             xml = YAZ_MARC_XCHANGE;
300             break;
301         case 'X':
302             xml = YAZ_MARC_MARCXML;
303             break;
304         case 'I':
305             xml = YAZ_MARC_ISO2709;
306             break;
307         case 'p':
308             print_offset = 1;
309             break;
310         case 's':
311             split_fname = arg;
312             break;
313         case 0:
314             dump(arg, from, to, read_xml, xml,
315                  print_offset, split_fname, verbose, cfile);
316             break;
317         case 'v':
318             verbose++;
319             break;
320         default:
321             usage(prog);
322             exit (1);
323         }
324     }
325     if (cfile)
326         fclose (cfile);
327     if (!no)
328     {
329         usage(prog);
330         exit (1);
331     }
332     exit (0);
333 }
334 /*
335  * Local variables:
336  * c-basic-offset: 4
337  * indent-tabs-mode: nil
338  * End:
339  * vim: shiftwidth=4 tabstop=8 expandtab
340  */
341