2 * Copyright (C) 1995-2005, Index Data ApS
3 * See the file LICENSE for details.
5 * $Id: marcdump.c,v 1.33 2005-08-22 20:34:23 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>
48 static void usage(const char *prog)
50 fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] [-x] [-O] [-X] [-e] [-I] [-v] file...\n",
55 void print_xpath_nodes(xmlNodeSetPtr nodes, FILE* output) {
61 size = (nodes) ? nodes->nodeNr : 0;
63 fprintf(output, "Result (%d nodes):\n", size);
64 for(i = 0; i < size; ++i) {
65 assert(nodes->nodeTab[i]);
67 if(nodes->nodeTab[i]->type == XML_NAMESPACE_DECL)
71 ns = (xmlNsPtr)nodes->nodeTab[i];
72 cur = (xmlNodePtr)ns->next;
74 fprintf(output, "= namespace \"%s\"=\"%s\" for node %s:%s\n",
75 ns->prefix, ns->href, cur->ns->href, cur->name);
77 fprintf(output, "= namespace \"%s\"=\"%s\" for node %s\n",
78 ns->prefix, ns->href, cur->name);
81 else if(nodes->nodeTab[i]->type == XML_ELEMENT_NODE)
83 cur = nodes->nodeTab[i];
85 fprintf(output, "= element node \"%s:%s\"\n",
86 cur->ns->href, cur->name);
90 fprintf(output, "= element node \"%s\"\n",
96 cur = nodes->nodeTab[i];
97 fprintf(output, "= node \"%s\": type %d\n", cur->name, cur->type);
103 int main (int argc, char **argv)
106 int libxml_dom_test = 0;
107 int print_offset = 0;
116 char *from = 0, *to = 0;
120 setlocale(LC_CTYPE, "");
124 to = nl_langinfo(CODESET);
128 while ((r = options("pvc:xOeXIf:t:2", argv, argc, &arg)) != -2)
143 cfile = fopen(arg, "w");
146 xml = YAZ_MARC_SIMPLEXML;
149 xml = YAZ_MARC_OAIMARC;
152 xml = YAZ_MARC_XCHANGE;
155 xml = YAZ_MARC_MARCXML;
158 xml = YAZ_MARC_ISO2709;
167 inf = fopen(arg, "rb");
171 fprintf (stderr, "%s: cannot open %s:%s\n",
172 prog, arg, strerror (errno));
176 fprintf (cfile, "char *marc_records[] = {\n");
179 yaz_marc_t mt = yaz_marc_create();
184 cd = yaz_iconv_open(to, from);
187 fprintf(stderr, "conversion from %s to %s "
188 "unsupported\n", from, to);
191 yaz_marc_iconv(mt, cd);
193 yaz_marc_xml(mt, xml);
194 yaz_marc_debug(mt, verbose);
201 r = fread (buf, 1, 5, inf);
204 if (r && print_offset && verbose)
205 printf ("<!-- Extra %d bytes at end of file -->\n", r);
208 while (*buf < '0' || *buf > '9')
211 long off = ftell(inf) - 5;
212 if (verbose || print_offset)
213 printf("<!-- Skipping bad byte %d (0x%02X) at offset "
215 *buf & 0xff, *buf & 0xff,
217 for (i = 0; i<4; i++)
219 r = fread(buf+4, 1, 1, inf);
225 if (verbose || print_offset)
226 printf ("<!-- End of file with data -->\n");
231 long off = ftell(inf) - 5;
232 printf ("<!-- Record %d offset %ld (0x%lx) -->\n",
235 len = atoi_n(buf, 5);
236 if (len < 25 || len > 100000)
238 long off = ftell(inf) - 5;
239 printf("Bad Length %d read at offset %ld (%lx)\n",
240 len, (long) off, (long) off);
244 r = fread (buf + 5, 1, len, inf);
247 r = yaz_marc_decode_buf (mt, buf, -1, &result, &rlen);
249 fwrite (result, rlen, 1, stdout);
251 if (r > 0 && libxml_dom_test)
253 xmlDocPtr doc = xmlParseMemory(result, rlen);
255 fprintf(stderr, "xmLParseMemory failed\n");
259 xmlXPathContextPtr xpathCtx;
260 xmlXPathObjectPtr xpathObj;
261 static const char *xpathExpr[] = {
262 "/record/datafield[@tag='245']/subfield[@code='a']",
263 "/record/datafield[@tag='100']/subfield",
264 "/record/datafield[@tag='245']/subfield[@code='a']",
265 "/record/datafield[@tag='650']/subfield",
266 "/record/datafield[@tag='650']",
269 xpathCtx = xmlXPathNewContext(doc);
271 for (i = 0; xpathExpr[i]; i++) {
272 xpathObj = xmlXPathEvalExpression(BAD_CAST xpathExpr[i], xpathCtx);
273 if(xpathObj == NULL) {
274 fprintf(stderr,"Error: unable to evaluate xpath expression \"%s\"\n", xpathExpr[i]);
278 print_xpath_nodes(xpathObj->nodesetval, stdout);
279 xmlXPathFreeObject(xpathObj);
282 xmlXPathFreeContext(xpathCtx);
292 fprintf (cfile, ",");
293 fprintf (cfile, "\n");
294 for (i = 0; i < r; i++)
297 fprintf (cfile, " \"");
298 fprintf (cfile, "\\x%02X", p[i] & 255);
300 if (i < r - 1 && (i & 15) == 15)
301 fprintf (cfile, "\"\n");
304 fprintf (cfile, "\"\n");
313 yaz_marc_destroy(mt);
316 fprintf (cfile, "};\n");
339 * indent-tabs-mode: nil
341 * vim: shiftwidth=4 tabstop=8 expandtab