2 * Copyright (C) 1995-2007, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.52 2007-04-17 20:26:18 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_cstr(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_cstr(wrbuf), stdout);
121 wrbuf_destroy(wrbuf);
127 static void dump(const char *fname, const char *from, const char *to,
128 int input_format, int output_format,
129 int write_using_libxml2,
130 int print_offset, const char *split_fname, int split_chunk,
131 int verbose, FILE *cfile, const char *leader_spec)
133 yaz_marc_t mt = yaz_marc_create();
136 if (yaz_marc_leader_spec(mt, leader_spec))
138 fprintf(stderr, "bad leader spec: %s\n", leader_spec);
139 yaz_marc_destroy(mt);
144 cd = yaz_iconv_open(to, from);
147 fprintf(stderr, "conversion from %s to %s "
148 "unsupported\n", from, to);
149 yaz_marc_destroy(mt);
152 yaz_marc_iconv(mt, cd);
154 yaz_marc_xml(mt, output_format);
155 yaz_marc_write_using_libxml2(mt, write_using_libxml2);
156 yaz_marc_debug(mt, verbose);
158 if (input_format == YAZ_MARC_MARCXML || input_format == YAZ_MARC_XCHANGE)
161 marcdump_read_xml(mt, fname);
164 else if (input_format == YAZ_MARC_LINE)
166 marcdump_read_line(mt, fname);
168 else if (input_format == YAZ_MARC_ISO2709)
170 FILE *inf = fopen(fname, "rb");
173 int split_file_no = -1;
176 fprintf (stderr, "%s: cannot open %s:%s\n",
177 prog, fname, strerror (errno));
181 fprintf (cfile, "char *marc_records[] = {\n");
184 const char *result = 0;
191 r = fread (buf, 1, 5, inf);
194 if (r && print_offset && verbose)
195 printf ("<!-- Extra %ld bytes at end of file -->\n",
199 while (*buf < '0' || *buf > '9')
202 long off = ftell(inf) - 5;
203 if (verbose || print_offset)
204 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
206 *buf & 0xff, *buf & 0xff,
208 for (i = 0; i<4; i++)
210 r = fread(buf+4, 1, 1, inf);
216 if (verbose || print_offset)
217 printf ("<!-- End of file with data -->\n");
222 long off = ftell(inf) - 5;
223 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
226 len = atoi_n(buf, 5);
227 if (len < 25 || len > 100000)
229 long off = ftell(inf) - 5;
230 printf("Bad Length %ld read at offset %ld (%lx)\n",
231 (long)len, (long) off, (long) off);
235 r = fread (buf + 5, 1, rlen, inf);
238 while (buf[len-1] != ISO2709_RS)
240 if (len > sizeof(buf)-2)
242 r = fread (buf + len, 1, 1, inf);
250 const char *mode = 0;
252 if ((marc_no % split_chunk) == 0)
259 sprintf(fname, "%.200s%07d", split_fname, split_file_no);
260 sf = fopen(fname, mode);
263 fprintf(stderr, "Could not open %s\n", fname);
268 if (fwrite(buf, 1, len, sf) != len)
270 fprintf(stderr, "Could write content to %s\n",
278 r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result);
281 fwrite (result, len_result, 1, stdout);
288 fprintf (cfile, ",");
289 fprintf (cfile, "\n");
290 for (i = 0; i < r; i++)
293 fprintf (cfile, " \"");
294 fprintf (cfile, "\\x%02X", p[i] & 255);
296 if (i < r - 1 && (i & 15) == 15)
297 fprintf (cfile, "\"\n");
300 fprintf (cfile, "\"\n");
307 fprintf (cfile, "};\n");
312 yaz_marc_destroy(mt);
315 int main (int argc, char **argv)
318 int print_offset = 0;
322 int output_format = YAZ_MARC_LINE;
324 char *from = 0, *to = 0;
325 int input_format = YAZ_MARC_ISO2709;
327 const char *split_fname = 0;
328 const char *leader_spec = 0;
329 int write_using_libxml2 = 0;
332 setlocale(LC_CTYPE, "");
336 to = nl_langinfo(CODESET);
341 while ((r = options("i:o:C:npvc:xOeXIf:t:s:l:", argv, argc, &arg)) != -2)
347 input_format = yaz_marc_decode_formatstr(arg);
348 if (input_format == -1)
350 fprintf(stderr, "%s: bad input format: %s\n", prog, arg);
355 if (input_format == YAZ_MARC_MARCXML
356 || input_format == YAZ_MARC_XCHANGE)
358 fprintf(stderr, "%s: Libxml2 support not enabled\n", prog);
364 /* dirty hack so we can make Libxml2 do the writing ..
366 if (strlen(arg) > 4 && strncmp(arg, "xml,", 4) == 0)
369 write_using_libxml2 = 1;
371 output_format = yaz_marc_decode_formatstr(arg);
372 if (output_format == -1)
374 fprintf(stderr, "%s: bad output format: %s\n", prog, arg);
390 cfile = fopen(arg, "w");
393 fprintf(stderr, "%s: -x no longer supported. "
394 "Use -i marcxml instead\n", prog);
398 fprintf(stderr, "%s: OAI MARC no longer supported."
399 " Use MARCXML instead.\n", prog);
403 fprintf(stderr, "%s: -e no longer supported. "
404 "Use -o marcxchange instead\n", prog);
408 fprintf(stderr, "%s: -X no longer supported. "
409 "Use -o marcxml instead\n", prog);
413 fprintf(stderr, "%s: -I no longer supported. "
414 "Use -o marc instead\n", prog);
418 output_format = YAZ_MARC_CHECK;
427 split_chunk = atoi(arg);
430 dump(arg, from, to, input_format, output_format,
432 print_offset, split_fname, split_chunk,
433 verbose, cfile, leader_spec);
455 * indent-tabs-mode: nil
457 * vim: shiftwidth=4 tabstop=8 expandtab