2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.38 2006-06-07 10:51:14 adam Exp $
8 #define _FILE_OFFSET_BITS 64
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
18 #include <libxml/xpath.h>
19 #include <libxml/xpathInternals.h>
36 #include <yaz/marcdisp.h>
37 #include <yaz/yaz-util.h>
38 #include <yaz/xmalloc.h>
39 #include <yaz/options.h>
51 static void usage(const char *prog)
53 fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] [-x] [-X] [-e] [-I] [-v] [-s splitfname] file...\n",
58 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
61 xmlDocPtr doc = xmlParseFile(fname);
65 ptr = xmlDocGetRootElement(doc);
69 WRBUF wrbuf = wrbuf_alloc();
70 r = yaz_marc_read_xml(mt, ptr);
72 fprintf(stderr, "yaz_marc_read_xml failed\n");
74 yaz_marc_write_mode(mt, wrbuf);
76 fputs(wrbuf_buf(wrbuf), stdout);
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,
89 yaz_marc_t mt = yaz_marc_create();
94 cd = yaz_iconv_open(to, from);
97 fprintf(stderr, "conversion from %s to %s "
98 "unsupported\n", from, to);
101 yaz_marc_iconv(mt, cd);
103 yaz_marc_xml(mt, xml);
104 yaz_marc_debug(mt, verbose);
109 marcdump_read_xml(mt, fname);
116 FILE *inf = fopen(fname, "rb");
121 fprintf (stderr, "%s: cannot open %s:%s\n",
122 prog, fname, strerror (errno));
126 fprintf (cfile, "char *marc_records[] = {\n");
138 r = fread (buf, 1, 5, inf);
141 if (r && print_offset && verbose)
142 printf ("<!-- Extra %ld bytes at end of file -->\n",
146 while (*buf < '0' || *buf > '9')
149 long off = ftell(inf) - 5;
150 if (verbose || print_offset)
151 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
153 *buf & 0xff, *buf & 0xff,
155 for (i = 0; i<4; i++)
157 r = fread(buf+4, 1, 1, inf);
163 if (verbose || print_offset)
164 printf ("<!-- End of file with data -->\n");
169 long off = ftell(inf) - 5;
170 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
173 len = atoi_n(buf, 5);
174 if (len < 25 || len > 100000)
176 long off = ftell(inf) - 5;
177 printf("Bad Length %d read at offset %ld (%lx)\n",
178 len, (long) off, (long) off);
182 r = fread (buf + 5, 1, rlen, inf);
189 sprintf(fname, "%.200s%07d", split_fname, marc_no);
190 sf = fopen(fname, "wb");
193 fprintf(stderr, "Could not open %s\n", fname);
198 if (fwrite(buf, 1, len, sf) != len)
200 fprintf(stderr, "Could write content to %s\n",
207 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
210 fwrite (result, rlen, 1, stdout);
217 fprintf (cfile, ",");
218 fprintf (cfile, "\n");
219 for (i = 0; i < r; i++)
222 fprintf (cfile, " \"");
223 fprintf (cfile, "\\x%02X", p[i] & 255);
225 if (i < r - 1 && (i & 15) == 15)
226 fprintf (cfile, "\"\n");
229 fprintf (cfile, "\"\n");
238 fprintf (cfile, "};\n");
243 yaz_marc_destroy(mt);
246 int main (int argc, char **argv)
249 int print_offset = 0;
255 char *from = 0, *to = 0;
257 const char *split_fname = 0;
260 setlocale(LC_CTYPE, "");
264 to = nl_langinfo(CODESET);
269 while ((r = options("pvc:xOeXIf:t:s:", argv, argc, &arg)) != -2)
283 cfile = fopen(arg, "w");
289 fprintf(stderr, "%s: -x not supported."
290 " YAZ not compiled with Libxml2 support\n", prog);
295 fprintf(stderr, "%s: OAI MARC no longer supported."
296 " Use MARCXML instead.\n", prog);
300 xml = YAZ_MARC_XCHANGE;
303 xml = YAZ_MARC_MARCXML;
306 xml = YAZ_MARC_ISO2709;
315 dump(arg, from, to, read_xml, xml,
316 print_offset, split_fname, verbose, cfile);
338 * indent-tabs-mode: nil
340 * vim: shiftwidth=4 tabstop=8 expandtab