2 * Copyright (c) 1995-2003, Index Data
3 * See the file LICENSE for details.
4 * Sebastian Hammer, Adam Dickmeiss
6 * $Id: marcdump.c,v 1.23 2003-12-11 00:37:23 adam Exp $
25 #include <yaz/marcdisp.h>
26 #include <yaz/yaz-util.h>
27 #include <yaz/xmalloc.h>
28 #include <yaz/options.h>
37 static void usage(const char *prog)
39 fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] [-x] [-O] [-X] [-I] [-v] file...\n",
43 int main (int argc, char **argv)
54 char *from = 0, *to = 0;
58 setlocale(LC_CTYPE, "");
62 to = nl_langinfo(CODESET);
66 while ((r = options("vc:xOXIf:t:", argv, argc, &arg)) != -2)
81 cfile = fopen (arg, "w");
84 xml = YAZ_MARC_SIMPLEXML;
87 xml = YAZ_MARC_OAIMARC;
90 xml = YAZ_MARC_MARCXML;
93 xml = YAZ_MARC_ISO2709;
96 inf = fopen (arg, "rb");
100 fprintf (stderr, "%s: cannot open %s:%s\n",
101 prog, arg, strerror (errno));
105 fprintf (cfile, "char *marc_records[] = {\n");
108 yaz_marc_t mt = yaz_marc_create();
113 cd = yaz_iconv_open(to, from);
116 fprintf(stderr, "conversion from %s to %s "
117 "unsupported\n", from, to);
120 yaz_marc_iconv(mt, cd);
122 yaz_marc_xml(mt, xml);
123 yaz_marc_debug(mt, verbose);
130 r = fread (buf, 1, 5, inf);
133 len = atoi_n(buf, 5);
134 if (len < 25 || len > 100000)
137 r = fread (buf + 5, 1, len, inf);
140 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
144 fwrite (result, rlen, 1, stdout);
147 fwrite (result, rlen, 1, stdout);
151 size_t inbytesleft = rlen;
152 const char *inp = result;
156 size_t outbytesleft = sizeof(outbuf);
158 size_t r = yaz_iconv (cd, (char**) &inp,
160 &outp, &outbytesleft);
161 if (r == (size_t) (-1))
163 int e = yaz_iconv_error(cd);
164 if (e != YAZ_ICONV_E2BIG)
167 fwrite (outbuf, outp - outbuf, 1, stdout);
176 fprintf (cfile, ",");
177 fprintf (cfile, "\n");
178 for (i = 0; i < r; i++)
181 fprintf (cfile, " \"");
182 fprintf (cfile, "\\x%02X", p[i] & 255);
184 if (i < r - 1 && (i & 15) == 15)
185 fprintf (cfile, "\"\n");
188 fprintf (cfile, "\"\n");
194 yaz_marc_destroy(mt);
197 fprintf (cfile, "};\n");