Change local counter to size_t
[yaz-moved-to-github.git] / util / yaz-xmlquery.c
index 6612183..7ad6804 100644 (file)
@@ -1,9 +1,10 @@
-/*
- * Copyright (C) 1995-2005, Index Data ApS
+/* This file is part of the YAZ toolkit.
+ * Copyright (C) Index Data
  * See the file LICENSE for details.
- *
- * $Id: yaz-xmlquery.c,v 1.1 2006-02-23 10:40:59 adam Exp $
  */
+#if HAVE_CONFIG_H
+#include <config.h>
+#endif
 
 #include <stdlib.h>
 #include <stdio.h>
 #include <yaz/pquery.h>
 #include <yaz/test.h>
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 #include <libxml/parser.h>
 #endif
 
 static char *prog = "yaz-xmlquery";
 
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
 void pqftoxmlquery(const char *pqf)
 {
     YAZ_PQF_Parser parser = yaz_pqf_create();
@@ -51,9 +52,9 @@ void pqftoxmlquery(const char *pqf)
     else
     {
        xmlDocPtr doc = 0;
-       
+
         yaz_rpnquery2xml(rpn, &doc);
-        
+
         if (!doc)
        {
            fprintf(stderr, "%s: yaz_rpnquery2xml failed for query %s\n",
@@ -62,10 +63,10 @@ void pqftoxmlquery(const char *pqf)
        }
         else
         {
-            char *buf_out = 0;
+            xmlChar *buf_out = 0;
             int len_out = 0;
 
-            xmlDocDumpMemory(doc, (xmlChar **) &buf_out, &len_out);
+            xmlDocDumpMemory(doc, &buf_out, &len_out);
 
             if (!len_out || !buf_out)
            {
@@ -74,10 +75,16 @@ void pqftoxmlquery(const char *pqf)
                exit(4);
            }
            else
-               fwrite(buf_out, len_out, 1, stdout);
+           {
+               if (fwrite(buf_out, len_out, 1, stdout) != 1)
+               {
+                   fprintf(stderr, "%s: write failed\n", prog);
+                   exit(5);
+               }
+           }
             xmlFreeDoc(doc);
        }
-    }    
+    }
     odr_destroy(odr);
 }
 
@@ -117,8 +124,8 @@ void xmlquerytopqf(const char *xmlstr)
        {
            WRBUF w = wrbuf_alloc();
            yaz_query_to_wrbuf(w, query);
-           printf("%s\n", wrbuf_buf(w));
-           wrbuf_free(w, 1);
+           printf("%s\n", wrbuf_cstr(w));
+           wrbuf_destroy(w);
        }
        odr_destroy(odr);
        xmlFreeDoc(doc);
@@ -143,16 +150,25 @@ void xmlfiletopqf(const char *xmlfile)
        exit(1);
     }
     rewind(f);
-    xmlstr = xmalloc(sz);
-    fread(xmlstr, sz, 1, f);
-    fclose(f);
-    
+    xmlstr = (char *) xmalloc(sz+1);
+    xmlstr[sz] = '\0';
+    if (fread(xmlstr, sz, 1, f) != 1)
+    {
+       fprintf(stderr, "%s: read failed for file %s\n", prog, xmlfile);
+       exit(1);
+    }
+    if (fclose(f))
+    {
+       fprintf(stderr, "%s: close failed for file %s\n", prog, xmlfile);
+       exit(1);
+    }
+
     xmlquerytopqf(xmlstr);
     xfree(xmlstr);
 }
 #endif
 
-void usage()
+void usage(void)
 {
     fprintf(stderr, "%s [-p pqf] [-x xmlfile]\n", prog);
     fprintf(stderr, " -p pqf      reads pqf. write xml to stdout\n");
@@ -162,7 +178,7 @@ void usage()
 
 int main (int argc, char **argv)
 {
-#if HAVE_XML2
+#if YAZ_HAVE_XML2
     char *arg;
     int r;
     int active = 0;