2 * Copyright (C) 1995-2006, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.45 2006-12-15 19:28:48 adam Exp $
8 #define _FILE_OFFSET_BITS 64
15 #include <libxml/parser.h>
16 #include <libxml/tree.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] 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_buf(wrbuf), stdout);
100 static void marcdump_read_xml(yaz_marc_t mt, const char *fname)
103 xmlDocPtr doc = xmlParseFile(fname);
107 ptr = xmlDocGetRootElement(doc);
111 WRBUF wrbuf = wrbuf_alloc();
112 r = yaz_marc_read_xml(mt, ptr);
114 fprintf(stderr, "yaz_marc_read_xml failed\n");
117 yaz_marc_write_mode(mt, wrbuf);
119 fputs(wrbuf_buf(wrbuf), stdout);
121 wrbuf_free(wrbuf, 1);
127 static void dump(const char *fname, const char *from, const char *to,
128 int input_format, int output_format,
129 int print_offset, const char *split_fname, int split_chunk,
130 int verbose, FILE *cfile, const char *leader_spec)
132 yaz_marc_t mt = yaz_marc_create();
135 if (yaz_marc_leader_spec(mt, leader_spec))
137 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
138 yaz_marc_destroy(mt);
143 cd = yaz_iconv_open(to, from);
146 fprintf(stderr, "conversion from %s to %s "
147 "unsupported\n", from, to);
148 yaz_marc_destroy(mt);
151 yaz_marc_iconv(mt, cd);
153 yaz_marc_xml(mt, output_format);
154 yaz_marc_debug(mt, verbose);
156 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_XCHANGE)
159 marcdump_read_xml(mt, fname);
162 else if (input_format == YAZ_MARC_LINE)
164 marcdump_read_line(mt, fname);
166 else if (input_format == YAZ_MARC_ISO2709)
168 FILE *inf = fopen(fname, "rb");
171 int split_file_no = -1;
174 fprintf (stderr, "%s: cannot open %s:%s\n",
175 prog, fname, strerror (errno));
179 fprintf (cfile, "char *marc_records[] = {\n");
189 r = fread (buf, 1, 5, inf);
192 if (r && print_offset && verbose)
193 printf ("<!-- Extra %ld bytes at end of file -->\n",
197 while (*buf < '0' || *buf > '9')
200 long off = ftell(inf) - 5;
201 if (verbose || print_offset)
202 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
204 *buf & 0xff, *buf & 0xff,
206 for (i = 0; i<4; i++)
208 r = fread(buf+4, 1, 1, inf);
214 if (verbose || print_offset)
215 printf ("<!-- End of file with data -->\n");
220 long off = ftell(inf) - 5;
221 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
224 len = atoi_n(buf, 5);
225 if (len < 25 || len > 100000)
227 long off = ftell(inf) - 5;
228 printf("Bad Length %ld read at offset %ld (%lx)\n",
229 (long)len, (long) off, (long) off);
233 r = fread (buf + 5, 1, rlen, inf);
239 const char *mode = 0;
241 if ((marc_no % split_chunk) == 0)
248 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
249 sf = fopen(fname, mode);
252 fprintf(stderr, "Could not open %s\n", fname);
257 if (fwrite(buf, 1, len, sf) != len)
259 fprintf(stderr, "Could write content to %s\n",
266 len_result = (int) rlen;
267 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
270 fwrite (result, len_result, 1, stdout);
277 fprintf (cfile, ",");
278 fprintf (cfile, "\n");
279 for (i = 0; i < r; i++)
282 fprintf (cfile, " \"");
283 fprintf (cfile, "\\x%02X", p[i] & 255);
285 if (i < r - 1 && (i & 15) == 15)
286 fprintf (cfile, "\"\n");
289 fprintf (cfile, "\"\n");
296 fprintf (cfile, "};\n");
301 yaz_marc_destroy(mt);
304 int main (int argc, char **argv)
307 int print_offset = 0;
311 int output_format = YAZ_MARC_LINE;
313 char *from = 0, *to = 0;
314 int input_format = YAZ_MARC_ISO2709;
316 const char *split_fname = 0;
317 const char *leader_spec = 0;
320 setlocale(LC_CTYPE, "");
324 to = nl_langinfo(CODESET);
329 while ((r = options("i:o:C:npvc:xOeXIf:t:s:l:", argv, argc, &arg)) != -2)
335 input_format = yaz_marc_decode_formatstr(arg);
336 if (input_format == -1)
338 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
343 output_format = yaz_marc_decode_formatstr(arg);
344 if (output_format == -1)
346 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
362 cfile = fopen(arg, "w");
365 fprintf(stderr, "%s: -x no longer supported. "
366 "Use -i marcxml instead\n", prog);
370 fprintf(stderr, "%s: OAI MARC no longer supported."
371 " Use MARCXML instead.\n", prog);
375 fprintf(stderr, "%s: -e no longer supported. "
376 "Use -o marcxchange instead\n", prog);
380 fprintf(stderr, "%s: -X no longer supported. "
381 "Use -o marcxml instead\n", prog);
385 fprintf(stderr, "%s: -I no longer supported. "
386 "Use -o marc instead\n", prog);
390 output_format = YAZ_MARC_CHECK;
399 split_chunk = atoi(arg);
402 dump(arg, from, to, input_format, output_format,
403 print_offset, split_fname, split_chunk,
404 verbose, cfile, leader_spec);
426 * indent-tabs-mode: nil
428 * vim: shiftwidth=4 tabstop=8 expandtab