1 /* This file is part of the YAZ toolkit.
2 * Copyright (C) 1995-2009 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 void usage(const char *prog)
61 fprintf (stderr, "Usage: %s [-i format] [-o format] [-f from] [-t to] "
62 "[-l pos=value] [-c cfile] [-s prefix] [-C size] [-n] "
63 "[-p] [-v] [-V] file...\n",
67 static void show_version(void)
69 char vstr[20], sha1_str[41];
71 yaz_version(vstr, sha1_str);
72 printf("YAZ version: %s %s\n", YAZ_VERSION, YAZ_VERSION_SHA1);
73 if (strcmp(sha1_str, YAZ_VERSION_SHA1))
74 printf("YAZ DLL/SO: %s %s\n", vstr, sha1_str);
78 static int getbyte_stream(void *client_data)
80 FILE *f = (FILE*) client_data;
88 static void ungetbyte_stream(int c, void *client_data)
90 FILE *f = (FILE*) client_data;
97 static void marcdump_read_line(yaz_marc_t mt, const char *fname)
99 FILE *inf = fopen(fname, "rb");
102 fprintf (stderr, "%s: cannot open %s:%s\n",
103 prog, fname, strerror (errno));
107 while (yaz_marc_read_line(mt, getbyte_stream,
108 ungetbyte_stream, inf) == 0)
110 WRBUF wrbuf = wrbuf_alloc();
111 yaz_marc_write_mode(mt, wrbuf);
112 fputs(wrbuf_cstr(wrbuf), stdout);
113 wrbuf_destroy(wrbuf);
119 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
121 WRBUF wrbuf = wrbuf_alloc();
123 xmlTextReaderPtr reader = xmlReaderForFile(fname, 0 /* encoding */,
129 while ((ret = xmlTextReaderRead(reader)) == 1)
131 int type = xmlTextReaderNodeType(reader);
132 if (type == XML_READER_TYPE_ELEMENT)
134 const char *name = (const char *)
135 xmlTextReaderLocalName(reader);
136 if (!strcmp(name, "record"))
138 xmlNodePtr ptr = xmlTextReaderExpand(reader);
140 int r = yaz_marc_read_xml(mt, ptr);
142 fprintf(stderr, "yaz_marc_read_xml failed\n");
145 yaz_marc_write_mode(mt, wrbuf);
147 fputs(wrbuf_cstr(wrbuf), stdout);
155 xmlDocPtr doc = xmlParseFile(fname);
158 xmlNodePtr ptr = xmlDocGetRootElement(doc);
159 for (; ptr; ptr = ptr->next)
161 if (ptr->type == XML_ELEMENT_NODE)
163 if (!strcmp((const char *) ptr->name, "collection"))
168 if (!strcmp((const char *) ptr->name, "record"))
170 int r = yaz_marc_read_xml(mt, ptr);
172 fprintf(stderr, "yaz_marc_read_xml failed\n");
175 yaz_marc_write_mode(mt, wrbuf);
177 fputs(wrbuf_cstr(wrbuf), stdout);
186 fputs(wrbuf_cstr(wrbuf), stdout);
187 wrbuf_destroy(wrbuf);
191 static void dump(const char *fname, const char *from, const char *to,
192 int input_format, int output_format,
193 int write_using_libxml2,
194 int print_offset, const char *split_fname, int split_chunk,
195 int verbose, FILE *cfile, const char *leader_spec)
197 yaz_marc_t mt = yaz_marc_create();
200 if (yaz_marc_leader_spec(mt, leader_spec))
202 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
203 yaz_marc_destroy(mt);
208 cd = yaz_iconv_open(to, from);
211 fprintf(stderr, "conversion from %s to %s "
212 "unsupported\n", from, to);
213 yaz_marc_destroy(mt);
216 yaz_marc_iconv(mt, cd);
218 yaz_marc_xml(mt, output_format);
219 yaz_marc_enable_collection(mt);
220 yaz_marc_write_using_libxml2(mt, write_using_libxml2);
221 yaz_marc_debug(mt, verbose);
223 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_XCHANGE)
226 marcdump_read_xml(mt, fname);
229 else if (input_format == YAZ_MARC_LINE)
231 marcdump_read_line(mt, fname);
233 else if (input_format == YAZ_MARC_ISO2709)
235 FILE *inf = fopen(fname, "rb");
238 int split_file_no = -1;
241 fprintf (stderr, "%s: cannot open %s:%s\n",
242 prog, fname, strerror (errno));
246 fprintf (cfile, "char *marc_records[] = {\n");
249 const char *result = 0;
256 r = fread (buf, 1, 5, inf);
259 if (r && print_offset && verbose)
260 printf ("<!-- Extra %ld bytes at end of file -->\n",
264 while (*buf < '0' || *buf > '9')
267 long off = ftell(inf) - 5;
268 if (verbose || print_offset)
269 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
271 *buf & 0xff, *buf & 0xff,
273 for (i = 0; i<4; i++)
275 r = fread(buf+4, 1, 1, inf);
281 if (verbose || print_offset)
282 printf ("<!-- End of file with data -->\n");
287 long off = ftell(inf) - 5;
288 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
291 len = atoi_n(buf, 5);
292 if (len < 25 || len > 100000)
294 long off = ftell(inf) - 5;
295 printf("Bad Length %ld read at offset %ld (%lx)\n",
296 (long)len, (long) off, (long) off);
300 r = fread (buf + 5, 1, rlen, inf);
303 while (buf[len-1] != ISO2709_RS)
305 if (len > sizeof(buf)-2)
307 r = fread (buf + len, 1, 1, inf);
315 const char *mode = 0;
317 if ((marc_no % split_chunk) == 0)
324 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
325 sf = fopen(fname, mode);
328 fprintf(stderr, "Could not open %s\n", fname);
333 if (fwrite(buf, 1, len, sf) != len)
335 fprintf(stderr, "Could write content to %s\n",
343 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
346 if (fwrite(result, len_result, 1, stdout) != 1)
348 fprintf(stderr, "Write to stdout failed\n");
357 fprintf (cfile, ",");
358 fprintf (cfile, "\n");
359 for (i = 0; i < r; i++)
362 fprintf (cfile, " \"");
363 fprintf (cfile, "\\x%02X", p[i] & 255);
365 if (i < r - 1 && (i & 15) == 15)
366 fprintf (cfile, "\"\n");
369 fprintf (cfile, "\"\n");
376 fprintf (cfile, "};\n");
380 WRBUF wrbuf = wrbuf_alloc();
381 yaz_marc_write_trailer(mt, wrbuf);
382 fputs(wrbuf_cstr(wrbuf), stdout);
383 wrbuf_destroy(wrbuf);
387 yaz_marc_destroy(mt);
390 int main (int argc, char **argv)
393 int print_offset = 0;
397 int output_format = YAZ_MARC_LINE;
399 char *from = 0, *to = 0;
400 int input_format = YAZ_MARC_ISO2709;
402 const char *split_fname = 0;
403 const char *leader_spec = 0;
404 int write_using_libxml2 = 0;
407 setlocale(LC_CTYPE, "");
411 to = nl_langinfo(CODESET);
416 while ((r = options("i:o:C:npc:xOeXIf:t:s:l:Vv", argv, argc, &arg)) != -2)
422 input_format = yaz_marc_decode_formatstr(arg);
423 if (input_format == -1)
425 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
430 if (input_format == YAZ_MARC_MARCXML
431 || input_format == YAZ_MARC_XCHANGE)
433 fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
439 /* dirty hack so we can make Libxml2 do the writing ..
441 if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
444 write_using_libxml2 = 1;
446 output_format = yaz_marc_decode_formatstr(arg);
447 if (output_format == -1)
449 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
465 cfile = fopen(arg, "w");
468 fprintf(stderr, "%s: -x no longer supported. "
469 "Use -i marcxml instead\n", prog);
473 fprintf(stderr, "%s: OAI MARC no longer supported."
474 " Use MARCXML instead.\n", prog);
478 fprintf(stderr, "%s: -e no longer supported. "
479 "Use -o marcxchange instead\n", prog);
483 fprintf(stderr, "%s: -X no longer supported. "
484 "Use -o marcxml instead\n", prog);
488 fprintf(stderr, "%s: -I no longer supported. "
489 "Use -o marc instead\n", prog);
493 output_format = YAZ_MARC_CHECK;
502 split_chunk = atoi(arg);
505 dump(arg, from, to, input_format, output_format,
507 print_offset, split_fname, split_chunk,
508 verbose, cfile, leader_spec);
533 * indent-tabs-mode: nil
535 * vim: shiftwidth=4 tabstop=8 expandtab