2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.55 2007-12-18 21:13:06 adam Exp $
8 #define _FILE_OFFSET_BITS 64
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
17 #include <libxml/xpath.h>
18 #include <libxml/xpathInternals.h>
20 /* Libxml2 version < 2.6.15. xmlreader not reliable/present */
21 #if LIBXML_VERSION < 20615
22 #define USE_XMLREADER 0
24 #define USE_XMLREADER 1
28 #include <libxml/xmlreader.h>
46 #include <yaz/marcdisp.h>
47 #include <yaz/yaz-util.h>
48 #include <yaz/xmalloc.h>
49 #include <yaz/options.h>
61 static void usage(const char *prog)
63 fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] "
64 "[-i format] [-o format] "
65 "[-n] [-l pos=value] [-v] [-C chunk] [-s splitfname] [-p] file...\n",
69 static int getbyte_stream(void *client_data)
71 FILE *f = (FILE*) client_data;
79 static void ungetbyte_stream(int c, void *client_data)
81 FILE *f = (FILE*) client_data;
88 static void marcdump_read_line(yaz_marc_t mt, const char *fname)
90 FILE *inf = fopen(fname, "rb");
93 fprintf (stderr, "%s: cannot open %s:%s\n",
94 prog, fname, strerror (errno));
98 while (yaz_marc_read_line(mt, getbyte_stream,
99 ungetbyte_stream, inf) == 0)
101 WRBUF wrbuf = wrbuf_alloc();
102 yaz_marc_write_mode(mt, wrbuf);
103 fputs(wrbuf_cstr(wrbuf), stdout);
104 wrbuf_destroy(wrbuf);
110 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
112 WRBUF wrbuf = wrbuf_alloc();
114 xmlTextReaderPtr reader = xmlReaderForFile(fname, 0 /* encoding */,
120 while ((ret = xmlTextReaderRead(reader)) == 1)
122 int type = xmlTextReaderNodeType(reader);
123 if (type == XML_READER_TYPE_ELEMENT)
125 const char *name = (const char *)
126 xmlTextReaderLocalName(reader);
127 if (!strcmp(name, "record"))
129 xmlNodePtr ptr = xmlTextReaderExpand(reader);
131 int r = yaz_marc_read_xml(mt, ptr);
133 fprintf(stderr, "yaz_marc_read_xml failed\n");
136 yaz_marc_write_mode(mt, wrbuf);
138 fputs(wrbuf_cstr(wrbuf), stdout);
146 xmlDocPtr doc = xmlParseFile(fname);
149 xmlNodePtr ptr = xmlDocGetRootElement(doc);
150 for (; ptr; ptr = ptr->next)
152 if (ptr->type == XML_ELEMENT_NODE)
154 if (!strcmp((const char *) ptr->name, "collection"))
159 if (!strcmp((const char *) ptr->name, "record"))
161 int r = yaz_marc_read_xml(mt, ptr);
163 fprintf(stderr, "yaz_marc_read_xml failed\n");
166 yaz_marc_write_mode(mt, wrbuf);
168 fputs(wrbuf_cstr(wrbuf), stdout);
177 fputs(wrbuf_cstr(wrbuf), stdout);
178 wrbuf_destroy(wrbuf);
182 static void dump(const char *fname, const char *from, const char *to,
183 int input_format, int output_format,
184 int write_using_libxml2,
185 int print_offset, const char *split_fname, int split_chunk,
186 int verbose, FILE *cfile, const char *leader_spec)
188 yaz_marc_t mt = yaz_marc_create();
191 if (yaz_marc_leader_spec(mt, leader_spec))
193 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
194 yaz_marc_destroy(mt);
199 cd = yaz_iconv_open(to, from);
202 fprintf(stderr, "conversion from %s to %s "
203 "unsupported\n", from, to);
204 yaz_marc_destroy(mt);
207 yaz_marc_iconv(mt, cd);
209 yaz_marc_xml(mt, output_format);
210 yaz_marc_enable_collection(mt);
211 yaz_marc_write_using_libxml2(mt, write_using_libxml2);
212 yaz_marc_debug(mt, verbose);
214 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_XCHANGE)
217 marcdump_read_xml(mt, fname);
220 else if (input_format == YAZ_MARC_LINE)
222 marcdump_read_line(mt, fname);
224 else if (input_format == YAZ_MARC_ISO2709)
226 FILE *inf = fopen(fname, "rb");
229 int split_file_no = -1;
232 fprintf (stderr, "%s: cannot open %s:%s\n",
233 prog, fname, strerror (errno));
237 fprintf (cfile, "char *marc_records[] = {\n");
240 const char *result = 0;
247 r = fread (buf, 1, 5, inf);
250 if (r && print_offset && verbose)
251 printf ("<!-- Extra %ld bytes at end of file -->\n",
255 while (*buf < '0' || *buf > '9')
258 long off = ftell(inf) - 5;
259 if (verbose || print_offset)
260 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
262 *buf & 0xff, *buf & 0xff,
264 for (i = 0; i<4; i++)
266 r = fread(buf+4, 1, 1, inf);
272 if (verbose || print_offset)
273 printf ("<!-- End of file with data -->\n");
278 long off = ftell(inf) - 5;
279 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
282 len = atoi_n(buf, 5);
283 if (len < 25 || len > 100000)
285 long off = ftell(inf) - 5;
286 printf("Bad Length %ld read at offset %ld (%lx)\n",
287 (long)len, (long) off, (long) off);
291 r = fread (buf + 5, 1, rlen, inf);
294 while (buf[len-1] != ISO2709_RS)
296 if (len > sizeof(buf)-2)
298 r = fread (buf + len, 1, 1, inf);
306 const char *mode = 0;
308 if ((marc_no % split_chunk) == 0)
315 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
316 sf = fopen(fname, mode);
319 fprintf(stderr, "Could not open %s\n", fname);
324 if (fwrite(buf, 1, len, sf) != len)
326 fprintf(stderr, "Could write content to %s\n",
334 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
337 fwrite (result, len_result, 1, stdout);
344 fprintf (cfile, ",");
345 fprintf (cfile, "\n");
346 for (i = 0; i < r; i++)
349 fprintf (cfile, " \"");
350 fprintf (cfile, "\\x%02X", p[i] & 255);
352 if (i < r - 1 && (i & 15) == 15)
353 fprintf (cfile, "\"\n");
356 fprintf (cfile, "\"\n");
363 fprintf (cfile, "};\n");
367 WRBUF wrbuf = wrbuf_alloc();
368 yaz_marc_write_trailer(mt, wrbuf);
369 fputs(wrbuf_cstr(wrbuf), stdout);
370 wrbuf_destroy(wrbuf);
374 yaz_marc_destroy(mt);
377 int main (int argc, char **argv)
380 int print_offset = 0;
384 int output_format = YAZ_MARC_LINE;
386 char *from = 0, *to = 0;
387 int input_format = YAZ_MARC_ISO2709;
389 const char *split_fname = 0;
390 const char *leader_spec = 0;
391 int write_using_libxml2 = 0;
394 setlocale(LC_CTYPE, "");
398 to = nl_langinfo(CODESET);
403 while ((r = options("i:o:C:npvc:xOeXIf:t:s:l:", argv, argc, &arg)) != -2)
409 input_format = yaz_marc_decode_formatstr(arg);
410 if (input_format == -1)
412 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
417 if (input_format == YAZ_MARC_MARCXML
418 || input_format == YAZ_MARC_XCHANGE)
420 fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
426 /* dirty hack so we can make Libxml2 do the writing ..
428 if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
431 write_using_libxml2 = 1;
433 output_format = yaz_marc_decode_formatstr(arg);
434 if (output_format == -1)
436 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
452 cfile = fopen(arg, "w");
455 fprintf(stderr, "%s: -x no longer supported. "
456 "Use -i marcxml instead\n", prog);
460 fprintf(stderr, "%s: OAI MARC no longer supported."
461 " Use MARCXML instead.\n", prog);
465 fprintf(stderr, "%s: -e no longer supported. "
466 "Use -o marcxchange instead\n", prog);
470 fprintf(stderr, "%s: -X no longer supported. "
471 "Use -o marcxml instead\n", prog);
475 fprintf(stderr, "%s: -I no longer supported. "
476 "Use -o marc instead\n", prog);
480 output_format = YAZ_MARC_CHECK;
489 split_chunk = atoi(arg);
492 dump(arg, from, to, input_format, output_format,
494 print_offset, split_fname, split_chunk,
495 verbose, cfile, leader_spec);
517 * indent-tabs-mode: nil
519 * vim: shiftwidth=4 tabstop=8 expandtab