Use portability.c to convert void* to integer.
[gedcom-parse.git] / t / src / standalone.c
index 5efb08197bc7026034277db5bbb26ac02574c08e..4b21ddcd7e14bf186f22ca64f030190b2f357118 100644 (file)
 /* $Name$ */
 
 #include "gedcom.h"
+#include "output.h"
 #include <stdio.h>
 #include <string.h>
 #include <stdlib.h>
-#include <stdarg.h>
 #include <locale.h>
 #include <errno.h>
 #include <iconv.h>
 #include "utf8-locale.h"
 
-#define OUTFILE "testgedcom.out"
 #define BOGUS_FILE_NAME "Makefile.am"
-FILE* outfile = NULL;
-int quiet = 0;
-
-void output(int to_stdout_too, char* format, ...)
-{
-  va_list ap;
-  va_start(ap, format);
-  if (outfile) {
-    vfprintf(outfile, format, ap);
-  }
-  if (to_stdout_too && !quiet) {
-    vprintf(format, ap);
-  }
-  va_end(ap);
-}
+int total_conv_fails = 0;
 
 void show_help ()
 {
@@ -65,6 +50,7 @@ void show_help ()
   printf("  -3    Run the test parse 3 times instead of once\n");
   printf("  -b    Parse a bogus file before parsing the main file\n");
   printf("  -q    No output to standard output\n");
+  printf("  -o <outfile>  File to generate output to (def. testgedcom.out)\n");
 }
 
 Gedcom_ctxt header_start(Gedcom_rec rec, int level, Gedcom_val xref, char *tag,
@@ -201,6 +187,7 @@ void default_cb(Gedcom_elt elt, Gedcom_ctxt ctxt, int level, char *tag,
     converted = convert_utf8_to_locale(raw_value, &conv_fails);
   output(0, "== %d %s (%d) %s (ctxt is %d, conversion failures: %d)\n",
         level, tag, tag_value, converted, (int)ctxt, conv_fails);
+  total_conv_fails += conv_fails;
 }
 
 void subscribe_callbacks()
@@ -241,6 +228,7 @@ int main(int argc, char* argv[])
   int run_times   = 1;
   int bogus       = 0;
   int result      = 0;
+  char* outfilename = NULL;
   char* file_name = NULL;
 
   if (argc > 1) {
@@ -272,7 +260,18 @@ int main(int argc, char* argv[])
        bogus = 1;
       }
       else if (!strncmp(argv[i], "-q", 3)) {
-       quiet = 1;
+       output_set_quiet(1);
+      }
+      else if (!strncmp(argv[i], "-o", 3)) {
+       i++;
+       if (i < argc) {
+         outfilename = argv[i];
+       }
+       else {
+         printf ("Missing output file name\n");
+         show_help();
+         exit(1);
+       }
       }
       else if (strncmp(argv[i], "-", 1)) {
        file_name = argv[i];
@@ -301,10 +300,7 @@ int main(int argc, char* argv[])
   gedcom_set_default_callback(default_cb);
   
   subscribe_callbacks();
-  outfile = fopen(OUTFILE, "a");
-  if (!outfile) {
-    printf("Could not open %s for appending\n", OUTFILE);
-  }
+  output_open(outfilename);
   if (bogus) {
     output(0, "\n=== Parsing bogus file %s\n", BOGUS_FILE_NAME);
     gedcom_parse_file(BOGUS_FILE_NAME);
@@ -312,6 +308,7 @@ int main(int argc, char* argv[])
   while (run_times-- > 0) {
     output(0, "\n=== Parsing file %s\n", file_name);
     result |= gedcom_parse_file(file_name);
+    output(0, "\n=== Total conversion failures: %d\n", total_conv_fails);
   }
   if (result == 0) {
     output(1, "Parse succeeded\n");
@@ -319,6 +316,6 @@ int main(int argc, char* argv[])
   else {
     output(1, "Parse failed\n");
   }
-  fclose(outfile);
+  output_close();
   return result;
 }