2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.36 2006-04-19 10:05:04 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 %d bytes at end of file -->\n", r);
145 while (*buf < '0' || *buf > '9')
148 long off = ftell(inf) - 5;
149 if (verbose || print_offset)
150 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
152 *buf & 0xff, *buf & 0xff,
154 for (i = 0; i<4; i++)
156 r = fread(buf+4, 1, 1, inf);
162 if (verbose || print_offset)
163 printf ("<!-- End of file with data -->\n");
168 long off = ftell(inf) - 5;
169 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
172 len = atoi_n(buf, 5);
173 if (len < 25 || len > 100000)
175 long off = ftell(inf) - 5;
176 printf("Bad Length %d read at offset %ld (%lx)\n",
177 len, (long) off, (long) off);
181 r = fread (buf + 5, 1, rlen, inf);
188 sprintf(fname, "%.200s%07d", split_fname, marc_no);
189 sf = fopen(fname, "wb");
192 fprintf(stderr, "Could not open %s\n", fname);
197 if (fwrite(buf, 1, len, sf) != len)
199 fprintf(stderr, "Could write content to %s\n",
206 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
209 fwrite (result, rlen, 1, stdout);
216 fprintf (cfile, ",");
217 fprintf (cfile, "\n");
218 for (i = 0; i < r; i++)
221 fprintf (cfile, " \"");
222 fprintf (cfile, "\\x%02X", p[i] & 255);
224 if (i < r - 1 && (i & 15) == 15)
225 fprintf (cfile, "\"\n");
228 fprintf (cfile, "\"\n");
237 fprintf (cfile, "};\n");
242 yaz_marc_destroy(mt);
245 int main (int argc, char **argv)
248 int print_offset = 0;
254 char *from = 0, *to = 0;
256 const char *split_fname = 0;
259 setlocale(LC_CTYPE, "");
263 to = nl_langinfo(CODESET);
268 while ((r = options("pvc:xOeXIf:t:s:", argv, argc, &arg)) != -2)
282 cfile = fopen(arg, "w");
288 fprintf(stderr, "%s: -x not supported."
289 " YAZ not compiled with Libxml2 support\n", prog);
294 fprintf(stderr, "%s: OAI MARC no longer supported."
295 " Use MARCXML instead.\n", prog);
299 xml = YAZ_MARC_XCHANGE;
302 xml = YAZ_MARC_MARCXML;
305 xml = YAZ_MARC_ISO2709;
314 dump(arg, from, to, read_xml, xml,
315 print_offset, split_fname, verbose, cfile);
337 * indent-tabs-mode: nil
339 * vim: shiftwidth=4 tabstop=8 expandtab