X-Git-Url: http://git.indexdata.com/?a=blobdiff_plain;f=util%2Fmarcdump.c;h=662445ea00c339808c5c6222a4e2f8027ada089d;hb=7ef1b50f481cda83d012cc3d69d83f9313836f1f;hp=967fdd75ddd62c1b697872d8e7a6d275ddb8deae;hpb=401da56d4a26f687cd42024bdef6f29a29bb7663;p=yaz-moved-to-github.git diff --git a/util/marcdump.c b/util/marcdump.c index 967fdd7..662445e 100644 --- a/util/marcdump.c +++ b/util/marcdump.c @@ -1,8 +1,6 @@ -/* - * Copyright (C) 1995-2007, Index Data ApS +/* This file is part of the YAZ toolkit. + * Copyright (C) 1995-2008 Index Data * See the file LICENSE for details. - * - * $Id: marcdump.c,v 1.51 2007-03-19 14:40:08 adam Exp $ */ #define _FILE_OFFSET_BITS 64 @@ -14,10 +12,20 @@ #if YAZ_HAVE_XML2 #include #include - #include #include +/* Libxml2 version < 2.6.15. xmlreader not reliable/present */ +#if LIBXML_VERSION < 20615 +#define USE_XMLREADER 0 +#else +#define USE_XMLREADER 1 +#endif + +#if USE_XMLREADER +#include +#endif + #endif #include @@ -52,7 +60,7 @@ static void usage(const char *prog) { fprintf (stderr, "Usage: %s [-c cfile] [-f from] [-t to] " "[-i format] [-o format] " - "[-n] [-l pos=value] [-v] [-C chunk] [-s splitfname] file...\n", + "[-n] [-l pos=value] [-v] [-C chunk] [-s splitfname] [-p] file...\n", prog); } @@ -99,28 +107,73 @@ static void marcdump_read_line(yaz_marc_t mt, const char *fname) #if YAZ_HAVE_XML2 static void marcdump_read_xml(yaz_marc_t mt, const char *fname) { - xmlNodePtr ptr; - xmlDocPtr doc = xmlParseFile(fname); - if (!doc) - return; + WRBUF wrbuf = wrbuf_alloc(); +#if USE_XMLREADER + xmlTextReaderPtr reader = xmlReaderForFile(fname, 0 /* encoding */, + 0 /* options */); - ptr = xmlDocGetRootElement(doc); - if (ptr) + if (reader) { - int r; - WRBUF wrbuf = wrbuf_alloc(); - r = yaz_marc_read_xml(mt, ptr); - if (r) - fprintf(stderr, "yaz_marc_read_xml failed\n"); - else + int ret; + while ((ret = xmlTextReaderRead(reader)) == 1) { - yaz_marc_write_mode(mt, wrbuf); - - fputs(wrbuf_cstr(wrbuf), stdout); + int type = xmlTextReaderNodeType(reader); + if (type == XML_READER_TYPE_ELEMENT) + { + const char *name = (const char *) + xmlTextReaderLocalName(reader); + if (!strcmp(name, "record")) + { + xmlNodePtr ptr = xmlTextReaderExpand(reader); + + int r = yaz_marc_read_xml(mt, ptr); + if (r) + fprintf(stderr, "yaz_marc_read_xml failed\n"); + else + { + yaz_marc_write_mode(mt, wrbuf); + + fputs(wrbuf_cstr(wrbuf), stdout); + wrbuf_rewind(wrbuf); + } + } + } } - wrbuf_destroy(wrbuf); } - xmlFreeDoc(doc); +#else + xmlDocPtr doc = xmlParseFile(fname); + if (doc) + { + xmlNodePtr ptr = xmlDocGetRootElement(doc); + for (; ptr; ptr = ptr->next) + { + if (ptr->type == XML_ELEMENT_NODE) + { + if (!strcmp((const char *) ptr->name, "collection")) + { + ptr = ptr->children; + continue; + } + if (!strcmp((const char *) ptr->name, "record")) + { + int r = yaz_marc_read_xml(mt, ptr); + if (r) + fprintf(stderr, "yaz_marc_read_xml failed\n"); + else + { + yaz_marc_write_mode(mt, wrbuf); + + fputs(wrbuf_cstr(wrbuf), stdout); + wrbuf_rewind(wrbuf); + } + } + } + } + xmlFreeDoc(doc); + } +#endif + fputs(wrbuf_cstr(wrbuf), stdout); + wrbuf_destroy(wrbuf); } #endif @@ -152,6 +205,7 @@ static void dump(const char *fname, const char *from, const char *to, yaz_marc_iconv(mt, cd); } yaz_marc_xml(mt, output_format); + yaz_marc_enable_collection(mt); yaz_marc_write_using_libxml2(mt, write_using_libxml2); yaz_marc_debug(mt, verbose); @@ -278,7 +332,11 @@ static void dump(const char *fname, const char *from, const char *to, r = yaz_marc_decode_buf(mt, buf, -1, &result, &len_result); if (r > 0 && result) { - fwrite (result, len_result, 1, stdout); + if (fwrite(result, len_result, 1, stdout) != 1) + { + fprintf(stderr, "Write to stdout failed\n"); + break; + } } if (r > 0 && cfile) { @@ -307,6 +365,12 @@ static void dump(const char *fname, const char *from, const char *to, fprintf (cfile, "};\n"); fclose(inf); } + { + WRBUF wrbuf = wrbuf_alloc(); + yaz_marc_write_trailer(mt, wrbuf); + fputs(wrbuf_cstr(wrbuf), stdout); + wrbuf_destroy(wrbuf); + } if (cd) yaz_iconv_close(cd); yaz_marc_destroy(mt); @@ -328,7 +392,6 @@ int main (int argc, char **argv) const char *leader_spec = 0; int write_using_libxml2 = 0; - nmem_init(); #if HAVE_LOCALE_H setlocale(LC_CTYPE, ""); #endif @@ -443,7 +506,6 @@ int main (int argc, char **argv) } if (cfile) fclose (cfile); - nmem_exit(); if (!no) { usage(prog);