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 const char *name = (const char *)
137 xmlTextReaderLocalName(reader);
138 if (!strcmp(name, "record") || !strcmp(name, "r"))
140 xmlNodePtr ptr = xmlTextReaderExpand(reader);
142 int r = yaz_marc_read_xml(mt, ptr);
146 fprintf(stderr, "yaz_marc_read_xml failed\n");
150 int write_rc = yaz_marc_write_mode(mt, wrbuf);
153 yaz_log(YLOG_WARN, "yaz_marc_write_mode: "
154 "write error: %d", write_rc);
157 fputs(wrbuf_cstr(wrbuf), stdout);
165 xmlDocPtr doc = xmlParseFile(fname);
168 xmlNodePtr ptr = xmlDocGetRootElement(doc);
169 for (; ptr; ptr = ptr->next)
171 if (ptr->type == XML_ELEMENT_NODE)
173 if (!strcmp((const char *) ptr->name, "collection"))
178 if (!strcmp((const char *) ptr->name, "record") ||
179 !strcmp((const char *) ptr->name, "r"))
181 int r = yaz_marc_read_xml(mt, ptr);
185 fprintf(stderr, "yaz_marc_read_xml failed\n");
189 yaz_marc_write_mode(mt, wrbuf);
191 fputs(wrbuf_cstr(wrbuf), stdout);
200 fputs(wrbuf_cstr(wrbuf), stdout);
201 wrbuf_destroy(wrbuf);
205 static void dump(const char *fname, const char *from, const char *to,
206 int input_format, int output_format,
207 int write_using_libxml2,
208 int print_offset, const char *split_fname, int split_chunk,
209 int verbose, FILE *cfile, const char *leader_spec)
211 yaz_marc_t mt = yaz_marc_create();
214 if (yaz_marc_leader_spec(mt, leader_spec))
216 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
217 yaz_marc_destroy(mt);
222 cd = yaz_iconv_open(to, from);
225 fprintf(stderr, "conversion from %s to %s "
226 "unsupported\n", from, to);
227 yaz_marc_destroy(mt);
230 yaz_marc_iconv(mt, cd);
232 yaz_marc_enable_collection(mt);
233 yaz_marc_xml(mt, output_format);
234 yaz_marc_write_using_libxml2(mt, write_using_libxml2);
235 yaz_marc_debug(mt, verbose);
237 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_TURBOMARC || input_format == YAZ_MARC_XCHANGE)
240 marcdump_read_xml(mt, fname);
243 else if (input_format == YAZ_MARC_LINE)
245 marcdump_read_line(mt, fname);
247 else if (input_format == YAZ_MARC_ISO2709)
249 FILE *inf = fopen(fname, "rb");
252 int split_file_no = -1;
255 fprintf(stderr, "%s: cannot open %s:%s\n",
256 prog, fname, strerror(errno));
260 fprintf(cfile, "char *marc_records[] = {\n");
263 const char *result = 0;
270 r = fread(buf, 1, 5, inf);
273 if (r == 0) /* normal EOF, all good */
275 if (print_offset && verbose)
277 printf("<!-- Extra %ld bytes at end of file -->\n",
282 while (*buf < '0' || *buf > '9')
285 long off = ftell(inf) - 5;
286 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
288 *buf & 0xff, *buf & 0xff,
290 for (i = 0; i<4; i++)
292 r = fread(buf+4, 1, 1, inf);
299 if (verbose || print_offset)
300 printf("<!-- End of file with data -->\n");
305 long off = ftell(inf) - 5;
306 printf("<!-- Record %d offset %ld (0x%lx) -->\n",
309 len = atoi_n(buf, 5);
310 if (len < 25 || len > 100000)
312 long off = ftell(inf) - 5;
313 printf("<!-- Bad Length %ld read at offset %ld (%lx) -->\n",
314 (long)len, (long) off, (long) off);
319 r = fread(buf + 5, 1, rlen, inf);
322 long off = ftell(inf);
323 printf("<!-- Premature EOF at offset %ld (%lx) -->\n",
324 (long) off, (long) off);
328 while (buf[len-1] != ISO2709_RS)
330 if (len > sizeof(buf)-2)
335 r = fread(buf + len, 1, 1, inf);
342 printf("<!-- EOF while searching for RS -->\n");
349 const char *mode = 0;
351 if ((marc_no % split_chunk) == 0)
358 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
359 sf = fopen(fname, mode);
362 fprintf(stderr, "Could not open %s\n", fname);
367 if (fwrite(buf, 1, len, sf) != len)
369 fprintf(stderr, "Could write content to %s\n",
378 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
381 if (r > 0 && result && len_result)
383 if (fwrite(result, len_result, 1, stdout) != 1)
385 fprintf(stderr, "Write to stdout failed\n");
396 fprintf(cfile, "\n");
397 for (i = 0; i < r; i++)
400 fprintf(cfile, " \"");
401 fprintf(cfile, "\\x%02X", p[i] & 255);
403 if (i < r - 1 && (i & 15) == 15)
404 fprintf(cfile, "\"\n");
407 fprintf(cfile, "\"\n");
414 fprintf(cfile, "};\n");
418 WRBUF wrbuf = wrbuf_alloc();
419 yaz_marc_write_trailer(mt, wrbuf);
420 fputs(wrbuf_cstr(wrbuf), stdout);
421 wrbuf_destroy(wrbuf);
425 yaz_marc_destroy(mt);
428 int main (int argc, char **argv)
431 int print_offset = 0;
435 int output_format = YAZ_MARC_LINE;
437 char *from = 0, *to = 0;
438 int input_format = YAZ_MARC_ISO2709;
440 const char *split_fname = 0;
441 const char *leader_spec = 0;
442 int write_using_libxml2 = 0;
445 setlocale(LC_CTYPE, "");
449 to = nl_langinfo(CODESET);
454 while ((r = options("i:o:C:npc:xOeXIf:t:s:l:Vv", argv, argc, &arg)) != -2)
460 input_format = yaz_marc_decode_formatstr(arg);
461 if (input_format == -1)
463 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
468 if (input_format == YAZ_MARC_MARCXML
469 || input_format == YAZ_MARC_XCHANGE)
471 fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
477 /* dirty hack so we can make Libxml2 do the writing ..
479 if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
481 /* Only supported for Libxml2 2.6.0 or later */
482 #if LIBXML_VERSION >= 20600
484 write_using_libxml2 = 1;
486 fprintf(stderr, "%s: output using Libxml2 unsupported\n", prog);
490 output_format = yaz_marc_decode_formatstr(arg);
491 if (output_format == -1)
493 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
509 cfile = fopen(arg, "w");
512 fprintf(stderr, "%s: -x no longer supported. "
513 "Use -i marcxml instead\n", prog);
517 fprintf(stderr, "%s: OAI MARC no longer supported."
518 " Use MARCXML instead.\n", prog);
522 fprintf(stderr, "%s: -e no longer supported. "
523 "Use -o marcxchange instead\n", prog);
527 fprintf(stderr, "%s: -X no longer supported. "
528 "Use -o marcxml instead\n", prog);
532 fprintf(stderr, "%s: -I no longer supported. "
533 "Use -o marc instead\n", prog);
537 output_format = YAZ_MARC_CHECK;
546 split_chunk = atoi(arg);
549 dump(arg, from, to, input_format, output_format,
551 print_offset, split_fname, split_chunk,
552 verbose, cfile, leader_spec);
579 * c-file-style: "Stroustrup"
580 * indent-tabs-mode: nil
582 * vim: shiftwidth=4 tabstop=8 expandtab