2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.54 2007-12-17 20:59:32 adam Exp $
8 #define _FILE_OFFSET_BITS 64
15 #include <libxml/parser.h>
16 #include <libxml/tree.h>
17 #include <libxml/xmlreader.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] "
54 "[-i format] [-o format] "
55 "[-n] [-l pos=value] [-v] [-C chunk] [-s splitfname] [-p] file...\n",
59 static int getbyte_stream(void *client_data)
61 FILE *f = (FILE*) client_data;
69 static void ungetbyte_stream(int c, void *client_data)
71 FILE *f = (FILE*) client_data;
78 static void marcdump_read_line(yaz_marc_t mt, const char *fname)
80 FILE *inf = fopen(fname, "rb");
83 fprintf (stderr, "%s: cannot open %s:%s\n",
84 prog, fname, strerror (errno));
88 while (yaz_marc_read_line(mt, getbyte_stream,
89 ungetbyte_stream, inf) == 0)
91 WRBUF wrbuf = wrbuf_alloc();
92 yaz_marc_write_mode(mt, wrbuf);
93 fputs(wrbuf_cstr(wrbuf), stdout);
97 WRBUF wrbuf = wrbuf_alloc();
98 yaz_marc_write_trailer(mt, wrbuf);
99 fputs(wrbuf_cstr(wrbuf), stdout);
100 wrbuf_destroy(wrbuf);
106 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
108 xmlTextReaderPtr reader = xmlReaderForFile(fname, 0 /* encoding */,
114 WRBUF wrbuf = wrbuf_alloc();
115 while ((ret = xmlTextReaderRead(reader)) == 1)
117 int type = xmlTextReaderNodeType(reader);
118 if (type == XML_READER_TYPE_ELEMENT)
120 const char *name = (const char *)
121 xmlTextReaderConstName(reader);
122 if (!strcmp(name, "record"))
124 xmlNodePtr ptr = xmlTextReaderExpand(reader);
126 int r = yaz_marc_read_xml(mt, ptr);
128 fprintf(stderr, "yaz_marc_read_xml failed\n");
131 yaz_marc_write_mode(mt, wrbuf);
133 fputs(wrbuf_cstr(wrbuf), stdout);
139 yaz_marc_write_trailer(mt, wrbuf);
140 fputs(wrbuf_cstr(wrbuf), stdout);
141 wrbuf_destroy(wrbuf);
142 xmlFreeTextReader(reader);
147 static void dump(const char *fname, const char *from, const char *to,
148 int input_format, int output_format,
149 int write_using_libxml2,
150 int print_offset, const char *split_fname, int split_chunk,
151 int verbose, FILE *cfile, const char *leader_spec)
153 yaz_marc_t mt = yaz_marc_create();
156 if (yaz_marc_leader_spec(mt, leader_spec))
158 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
159 yaz_marc_destroy(mt);
164 cd = yaz_iconv_open(to, from);
167 fprintf(stderr, "conversion from %s to %s "
168 "unsupported\n", from, to);
169 yaz_marc_destroy(mt);
172 yaz_marc_iconv(mt, cd);
174 yaz_marc_xml(mt, output_format);
175 yaz_marc_enable_collection(mt);
176 yaz_marc_write_using_libxml2(mt, write_using_libxml2);
177 yaz_marc_debug(mt, verbose);
179 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_XCHANGE)
182 marcdump_read_xml(mt, fname);
185 else if (input_format == YAZ_MARC_LINE)
187 marcdump_read_line(mt, fname);
189 else if (input_format == YAZ_MARC_ISO2709)
191 FILE *inf = fopen(fname, "rb");
194 int split_file_no = -1;
197 fprintf (stderr, "%s: cannot open %s:%s\n",
198 prog, fname, strerror (errno));
202 fprintf (cfile, "char *marc_records[] = {\n");
205 const char *result = 0;
212 r = fread (buf, 1, 5, inf);
215 if (r && print_offset && verbose)
216 printf ("<!-- Extra %ld bytes at end of file -->\n",
220 while (*buf < '0' || *buf > '9')
223 long off = ftell(inf) - 5;
224 if (verbose || print_offset)
225 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
227 *buf & 0xff, *buf & 0xff,
229 for (i = 0; i<4; i++)
231 r = fread(buf+4, 1, 1, inf);
237 if (verbose || print_offset)
238 printf ("<!-- End of file with data -->\n");
243 long off = ftell(inf) - 5;
244 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
247 len = atoi_n(buf, 5);
248 if (len < 25 || len > 100000)
250 long off = ftell(inf) - 5;
251 printf("Bad Length %ld read at offset %ld (%lx)\n",
252 (long)len, (long) off, (long) off);
256 r = fread (buf + 5, 1, rlen, inf);
259 while (buf[len-1] != ISO2709_RS)
261 if (len > sizeof(buf)-2)
263 r = fread (buf + len, 1, 1, inf);
271 const char *mode = 0;
273 if ((marc_no % split_chunk) == 0)
280 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
281 sf = fopen(fname, mode);
284 fprintf(stderr, "Could not open %s\n", fname);
289 if (fwrite(buf, 1, len, sf) != len)
291 fprintf(stderr, "Could write content to %s\n",
299 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
302 fwrite (result, len_result, 1, stdout);
309 fprintf (cfile, ",");
310 fprintf (cfile, "\n");
311 for (i = 0; i < r; i++)
314 fprintf (cfile, " \"");
315 fprintf (cfile, "\\x%02X", p[i] & 255);
317 if (i < r - 1 && (i & 15) == 15)
318 fprintf (cfile, "\"\n");
321 fprintf (cfile, "\"\n");
328 fprintf (cfile, "};\n");
332 WRBUF wrbuf = wrbuf_alloc();
333 yaz_marc_write_trailer(mt, wrbuf);
334 fputs(wrbuf_cstr(wrbuf), stdout);
335 wrbuf_destroy(wrbuf);
339 yaz_marc_destroy(mt);
342 int main (int argc, char **argv)
345 int print_offset = 0;
349 int output_format = YAZ_MARC_LINE;
351 char *from = 0, *to = 0;
352 int input_format = YAZ_MARC_ISO2709;
354 const char *split_fname = 0;
355 const char *leader_spec = 0;
356 int write_using_libxml2 = 0;
359 setlocale(LC_CTYPE, "");
363 to = nl_langinfo(CODESET);
368 while ((r = options("i:o:C:npvc:xOeXIf:t:s:l:", argv, argc, &arg)) != -2)
374 input_format = yaz_marc_decode_formatstr(arg);
375 if (input_format == -1)
377 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
382 if (input_format == YAZ_MARC_MARCXML
383 || input_format == YAZ_MARC_XCHANGE)
385 fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
391 /* dirty hack so we can make Libxml2 do the writing ..
393 if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
396 write_using_libxml2 = 1;
398 output_format = yaz_marc_decode_formatstr(arg);
399 if (output_format == -1)
401 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
417 cfile = fopen(arg, "w");
420 fprintf(stderr, "%s: -x no longer supported. "
421 "Use -i marcxml instead\n", prog);
425 fprintf(stderr, "%s: OAI MARC no longer supported."
426 " Use MARCXML instead.\n", prog);
430 fprintf(stderr, "%s: -e no longer supported. "
431 "Use -o marcxchange instead\n", prog);
435 fprintf(stderr, "%s: -X no longer supported. "
436 "Use -o marcxml instead\n", prog);
440 fprintf(stderr, "%s: -I no longer supported. "
441 "Use -o marc instead\n", prog);
445 output_format = YAZ_MARC_CHECK;
454 split_chunk = atoi(arg);
457 dump(arg, from, to, input_format, output_format,
459 print_offset, split_fname, split_chunk,
460 verbose, cfile, leader_spec);
482 * indent-tabs-mode: nil
484 * vim: shiftwidth=4 tabstop=8 expandtab