2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.43 2006-10-09 14:22:44 heikki 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] "
54 "[-I] [-l pos=value] [-v] [-s splitfname] file...\n",
59 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
62 xmlDocPtr doc = xmlParseFile(fname);
66 ptr = xmlDocGetRootElement(doc);
70 WRBUF wrbuf = wrbuf_alloc();
71 r = yaz_marc_read_xml(mt, ptr);
73 fprintf(stderr, "yaz_marc_read_xml failed\n");
75 yaz_marc_write_mode(mt, wrbuf);
77 fputs(wrbuf_buf(wrbuf), stdout);
85 static void dump(const char *fname, const char *from, const char *to,
86 int read_xml, int xml,
87 int print_offset, const char *split_fname, int verbose,
88 FILE *cfile, const char *leader_spec)
90 yaz_marc_t mt = yaz_marc_create();
93 if (yaz_marc_leader_spec(mt, leader_spec))
95 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
101 cd = yaz_iconv_open(to, from);
104 fprintf(stderr, "conversion from %s to %s "
105 "unsupported\n", from, to);
106 yaz_marc_destroy(mt);
109 yaz_marc_iconv(mt, cd);
111 yaz_marc_xml(mt, xml);
112 yaz_marc_debug(mt, verbose);
117 marcdump_read_xml(mt, fname);
124 FILE *inf = fopen(fname, "rb");
129 fprintf (stderr, "%s: cannot open %s:%s\n",
130 prog, fname, strerror (errno));
134 fprintf (cfile, "char *marc_records[] = {\n");
146 r = fread (buf, 1, 5, inf);
149 if (r && print_offset && verbose)
150 printf ("<!-- Extra %ld bytes at end of file -->\n",
154 while (*buf < '0' || *buf > '9')
157 long off = ftell(inf) - 5;
158 if (verbose || print_offset)
159 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
161 *buf & 0xff, *buf & 0xff,
163 for (i = 0; i<4; i++)
165 r = fread(buf+4, 1, 1, inf);
171 if (verbose || print_offset)
172 printf ("<!-- End of file with data -->\n");
177 long off = ftell(inf) - 5;
178 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
181 len = atoi_n(buf, 5);
182 if (len < 25 || len > 100000)
184 long off = ftell(inf) - 5;
185 printf("Bad Length %ld read at offset %ld (%lx)\n",
186 (long)len, (long) off, (long) off);
190 r = fread (buf + 5, 1, rlen, inf);
197 sprintf(fname, "%.200s%07d", split_fname, marc_no);
198 sf = fopen(fname, "wb");
201 fprintf(stderr, "Could not open %s\n", fname);
206 if (fwrite(buf, 1, len, sf) != len)
208 fprintf(stderr, "Could write content to %s\n",
216 int rlentmp = (int) rlen;
217 r = yaz_marc_decode_buf(mt, buf, -1, &result, &rlentmp);
218 rlen = (size_t) rlentmp;
222 fwrite (result, rlen, 1, stdout);
229 fprintf (cfile, ",");
230 fprintf (cfile, "\n");
231 for (i = 0; i < r; i++)
234 fprintf (cfile, " \"");
235 fprintf (cfile, "\\x%02X", p[i] & 255);
237 if (i < r - 1 && (i & 15) == 15)
238 fprintf (cfile, "\"\n");
241 fprintf (cfile, "\"\n");
250 fprintf (cfile, "};\n");
255 yaz_marc_destroy(mt);
258 int main (int argc, char **argv)
261 int print_offset = 0;
267 char *from = 0, *to = 0;
269 const char *split_fname = 0;
270 const char *leader_spec = 0;
273 setlocale(LC_CTYPE, "");
277 to = nl_langinfo(CODESET);
282 while ((r = options("pvc:xOeXIf:t:s:l:", argv, argc, &arg)) != -2)
299 cfile = fopen(arg, "w");
305 fprintf(stderr, "%s: -x not supported."
306 " YAZ not compiled with Libxml2 support\n", prog);
311 fprintf(stderr, "%s: OAI MARC no longer supported."
312 " Use MARCXML instead.\n", prog);
316 xml = YAZ_MARC_XCHANGE;
319 xml = YAZ_MARC_MARCXML;
322 xml = YAZ_MARC_ISO2709;
331 dump(arg, from, to, read_xml, xml,
332 print_offset, split_fname, verbose, cfile, leader_spec);
354 * indent-tabs-mode: nil
356 * vim: shiftwidth=4 tabstop=8 expandtab