1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2013 Index Data
3 * See the file LICENSE for details.
6 #define _FILE_OFFSET_BITS 64
13 #include <libxml/parser.h>
14 #include <libxml/tree.h>
15 #include <libxml/xpath.h>
16 #include <libxml/xpathInternals.h>
18 /* Libxml2 version < 2.6.15. xmlreader not reliable/present */
19 #if LIBXML_VERSION < 20615
20 #define USE_XMLREADER 0
22 #define USE_XMLREADER 1
26 #include <libxml/xmlreader.h>
44 #include <yaz/marcdisp.h>
45 #include <yaz/yaz-util.h>
46 #include <yaz/xmalloc.h>
47 #include <yaz/options.h>
59 static int no_errors = 0;
61 static void usage(const char *prog)
63 fprintf(stderr, "Usage: %s [-i format] [-o format] [-f from] [-t to] "
64 "[-l pos=value] [-c cfile] [-s prefix] [-C size] [-n] "
65 "[-p] [-v] [-V] file...\n",
69 static void show_version(void)
71 char vstr[20], sha1_str[41];
73 yaz_version(vstr, sha1_str);
74 printf("YAZ version: %s %s\n", YAZ_VERSION, YAZ_VERSION_SHA1);
75 if (strcmp(sha1_str, YAZ_VERSION_SHA1))
76 printf("YAZ DLL/SO: %s %s\n", vstr, sha1_str);
80 static int getbyte_stream(void *client_data)
82 FILE *f = (FILE*) client_data;
90 static void ungetbyte_stream(int c, void *client_data)
92 FILE *f = (FILE*) client_data;
99 static void marcdump_read_line(yaz_marc_t mt, const char *fname)
101 FILE *inf = fopen(fname, "rb");
104 fprintf(stderr, "%s: cannot open %s:%s\n",
105 prog, fname, strerror(errno));
109 while (yaz_marc_read_line(mt, getbyte_stream,
110 ungetbyte_stream, inf) == 0)
112 WRBUF wrbuf = wrbuf_alloc();
113 yaz_marc_write_mode(mt, wrbuf);
114 fputs(wrbuf_cstr(wrbuf), stdout);
115 wrbuf_destroy(wrbuf);
121 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
123 WRBUF wrbuf = wrbuf_alloc();
125 xmlTextReaderPtr reader = xmlReaderForFile(fname, 0 /* encoding */,
131 while ((ret = xmlTextReaderRead(reader)) == 1)
133 int type = xmlTextReaderNodeType(reader);
134 if (type == XML_READER_TYPE_ELEMENT)
136 char *name = (char *) xmlTextReaderLocalName(reader);
137 if (!strcmp(name, "record") || !strcmp(name, "r"))
139 xmlNodePtr ptr = xmlTextReaderExpand(reader);
141 int r = yaz_marc_read_xml(mt, ptr);
145 fprintf(stderr, "yaz_marc_read_xml failed\n");
149 int write_rc = yaz_marc_write_mode(mt, wrbuf);
152 yaz_log(YLOG_WARN, "yaz_marc_write_mode: "
153 "write error: %d", write_rc);
156 fputs(wrbuf_cstr(wrbuf), stdout);
163 xmlFreeTextReader(reader);
166 xmlDocPtr doc = xmlParseFile(fname);
169 xmlNodePtr ptr = xmlDocGetRootElement(doc);
170 for (; ptr; ptr = ptr->next)
172 if (ptr->type == XML_ELEMENT_NODE)
174 if (!strcmp((const char *) ptr->name, "collection"))
179 if (!strcmp((const char *) ptr->name, "record") ||
180 !strcmp((const char *) ptr->name, "r"))
182 int r = yaz_marc_read_xml(mt, ptr);
186 fprintf(stderr, "yaz_marc_read_xml failed\n");
190 yaz_marc_write_mode(mt, wrbuf);
192 fputs(wrbuf_cstr(wrbuf), stdout);
201 fputs(wrbuf_cstr(wrbuf), stdout);
202 wrbuf_destroy(wrbuf);
206 static void dump(const char *fname, const char *from, const char *to,
207 int input_format, int output_format,
208 int write_using_libxml2,
209 int print_offset, const char *split_fname, int split_chunk,
210 int verbose, FILE *cfile, const char *leader_spec)
212 yaz_marc_t mt = yaz_marc_create();
215 if (yaz_marc_leader_spec(mt, leader_spec))
217 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
218 yaz_marc_destroy(mt);
223 cd = yaz_iconv_open(to, from);
226 fprintf(stderr, "conversion from %s to %s "
227 "unsupported\n", from, to);
228 yaz_marc_destroy(mt);
231 yaz_marc_iconv(mt, cd);
233 yaz_marc_enable_collection(mt);
234 yaz_marc_xml(mt, output_format);
235 yaz_marc_write_using_libxml2(mt, write_using_libxml2);
236 yaz_marc_debug(mt, verbose);
238 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_TURBOMARC || input_format == YAZ_MARC_XCHANGE)
241 marcdump_read_xml(mt, fname);
244 else if (input_format == YAZ_MARC_LINE)
246 marcdump_read_line(mt, fname);
248 else if (input_format == YAZ_MARC_ISO2709)
250 FILE *inf = fopen(fname, "rb");
253 int split_file_no = -1;
256 fprintf(stderr, "%s: cannot open %s:%s\n",
257 prog, fname, strerror(errno));
261 fprintf(cfile, "char *marc_records[] = {\n");
264 const char *result = 0;
271 r = fread(buf, 1, 5, inf);
274 if (r == 0) /* normal EOF, all good */
276 if (print_offset && verbose)
278 printf("<!-- Extra %ld bytes at end of file -->\n",
283 while (*buf < '0' || *buf > '9')
286 long off = ftell(inf) - 5;
287 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
289 *buf & 0xff, *buf & 0xff,
291 for (i = 0; i<4; i++)
293 r = fread(buf+4, 1, 1, inf);
300 if (verbose || print_offset)
301 printf("<!-- End of file with data -->\n");
306 long off = ftell(inf) - 5;
307 printf("<!-- Record %d offset %ld (0x%lx) -->\n",
310 len = atoi_n(buf, 5);
311 if (len < 25 || len > 100000)
313 long off = ftell(inf) - 5;
314 printf("<!-- Bad Length %ld read at offset %ld (%lx) -->\n",
315 (long)len, (long) off, (long) off);
320 r = fread(buf + 5, 1, rlen, inf);
323 long off = ftell(inf);
324 printf("<!-- Premature EOF at offset %ld (%lx) -->\n",
325 (long) off, (long) off);
329 while (buf[len-1] != ISO2709_RS)
331 if (len > sizeof(buf)-2)
336 r = fread(buf + len, 1, 1, inf);
343 printf("<!-- EOF while searching for RS -->\n");
350 const char *mode = 0;
352 if ((marc_no % split_chunk) == 0)
359 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
360 sf = fopen(fname, mode);
363 fprintf(stderr, "Could not open %s\n", fname);
368 if (fwrite(buf, 1, len, sf) != len)
370 fprintf(stderr, "Could write content to %s\n",
379 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
382 if (r > 0 && result && len_result)
384 if (fwrite(result, len_result, 1, stdout) != 1)
386 fprintf(stderr, "Write to stdout failed\n");
397 fprintf(cfile, "\n");
398 for (i = 0; i < r; i++)
401 fprintf(cfile, " \"");
402 if (p[i] < 32 || p[i] > 126)
403 fprintf(cfile, "\" \"\\x%02X\" \"", p[i] & 255);
407 if (i < r - 1 && (i & 15) == 15)
408 fprintf(cfile, "\"\n");
411 fprintf(cfile, "\"\n");
418 fprintf(cfile, "};\n");
422 WRBUF wrbuf = wrbuf_alloc();
423 yaz_marc_write_trailer(mt, wrbuf);
424 fputs(wrbuf_cstr(wrbuf), stdout);
425 wrbuf_destroy(wrbuf);
429 yaz_marc_destroy(mt);
432 int main (int argc, char **argv)
435 int print_offset = 0;
439 int output_format = YAZ_MARC_LINE;
441 char *from = 0, *to = 0;
442 int input_format = YAZ_MARC_ISO2709;
444 const char *split_fname = 0;
445 const char *leader_spec = 0;
446 int write_using_libxml2 = 0;
449 setlocale(LC_CTYPE, "");
453 to = nl_langinfo(CODESET);
458 while ((r = options("i:o:C:npc:xOeXIf:t:s:l:Vv", argv, argc, &arg)) != -2)
464 input_format = yaz_marc_decode_formatstr(arg);
465 if (input_format == -1)
467 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
472 if (input_format == YAZ_MARC_MARCXML
473 || input_format == YAZ_MARC_XCHANGE)
475 fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
481 /* dirty hack so we can make Libxml2 do the writing ..
483 if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
485 /* Only supported for Libxml2 2.6.0 or later */
486 #if LIBXML_VERSION >= 20600
488 write_using_libxml2 = 1;
490 fprintf(stderr, "%s: output using Libxml2 unsupported\n", prog);
494 output_format = yaz_marc_decode_formatstr(arg);
495 if (output_format == -1)
497 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
513 cfile = fopen(arg, "w");
516 fprintf(stderr, "%s: -x no longer supported. "
517 "Use -i marcxml instead\n", prog);
521 fprintf(stderr, "%s: OAI MARC no longer supported."
522 " Use MARCXML instead.\n", prog);
526 fprintf(stderr, "%s: -e no longer supported. "
527 "Use -o marcxchange instead\n", prog);
531 fprintf(stderr, "%s: -X no longer supported. "
532 "Use -o marcxml instead\n", prog);
536 fprintf(stderr, "%s: -I no longer supported. "
537 "Use -o marc instead\n", prog);
541 output_format = YAZ_MARC_CHECK;
550 split_chunk = atoi(arg);
553 dump(arg, from, to, input_format, output_format,
555 print_offset, split_fname, split_chunk,
556 verbose, cfile, leader_spec);
583 * c-file-style: "Stroustrup"
584 * indent-tabs-mode: nil
586 * vim: shiftwidth=4 tabstop=8 expandtab