Add call to gedcom_init().
[gedcom-parse.git] / standalone.c
index 9f435f511acbc6506ba60747146544a2ecc50889..3db5eda7af45b2fcb98d3437dba9ac3d7f66354a 100644 (file)
 #include <string.h>
 #include <stdlib.h>
 #include <stdarg.h>
+#include <locale.h>
+#include <errno.h>
+#include <iconv.h>
 #include "gedcom.h"
+#include "utf8-locale.h"
 
 #define OUTFILE "testgedcom.out"
 FILE* outfile = NULL;
+int quiet = 0;
 
 void output(int to_stdout_too, char* format, ...)
 {
@@ -37,7 +42,7 @@ void output(int to_stdout_too, char* format, ...)
   if (outfile) {
     vfprintf(outfile, format, ap);
   }
-  if (to_stdout_too) {
+  if (to_stdout_too && !quiet) {
     vprintf(format, ap);
   }
   va_end(ap);
@@ -57,6 +62,7 @@ void show_help ()
   printf("  -da   Debug setting: libgedcom + yacc debug messages\n");
   printf("  -2    Run the test parse 2 times instead of once\n");
   printf("  -3    Run the test parse 3 times instead of once\n");
+  printf("  -q    No output to standard output\n");
 }
 
 Gedcom_ctxt header_start(int level, Gedcom_val xref, char *tag,
@@ -64,7 +70,7 @@ Gedcom_ctxt header_start(int level, Gedcom_val xref, char *tag,
                         Gedcom_val parsed_value)
 {
   output(1, "Header start\n");
-  return (Gedcom_ctxt)0;
+  return (Gedcom_ctxt)1;
 }
 
 void header_end(Gedcom_ctxt self)
@@ -73,14 +79,16 @@ void header_end(Gedcom_ctxt self)
 }
 
 char family_xreftags[100][255];
-int  family_nr = 0;
+int  family_nr = 1;
 
 Gedcom_ctxt family_start(int level, Gedcom_val xref, char *tag,
                         char *raw_value, int tag_value,
                         Gedcom_val parsed_value)
 {
-  output(1, "Family start, xref is %s\n", GEDCOM_STRING(xref));
-  strcpy(family_xreftags[family_nr], GEDCOM_STRING(xref));
+  struct xref_value *xr = GEDCOM_XREF_PTR(xref);
+  output(1, "Family start, xref is %s\n", xr->string);
+  strcpy(family_xreftags[family_nr], xr->string);
+  xr->object = (Gedcom_ctxt)family_nr;
   return (Gedcom_ctxt)(family_nr++);
 }
 
@@ -88,9 +96,9 @@ Gedcom_ctxt rec_start(int level, Gedcom_val xref, char *tag,
                      char *raw_value, int tag_value,
                      Gedcom_val parsed_value)
 {
-  char *xref_str = NULL;
+  charxref_str = NULL;
   if (! GEDCOM_IS_NULL(xref))
-    xref_str = GEDCOM_STRING(xref);
+    xref_str = GEDCOM_XREF_PTR(xref)->string;
   output(1, "Rec %s start, xref is %s\n", tag, xref_str);
   return (Gedcom_ctxt)tag_value;
 }
@@ -99,9 +107,9 @@ Gedcom_ctxt note_start(int level, Gedcom_val xref, char *tag,
                       char *raw_value, int tag_value,
                       Gedcom_val parsed_value)
 {
-  output(0, "== %d %s (%d) %s (xref is %s)\n",
+  output(1, "== %d %s (%d) %s (xref is %s)\n",
         level, tag, tag_value, GEDCOM_STRING(parsed_value),
-        GEDCOM_STRING(xref));
+        GEDCOM_XREF_PTR(xref)->string);
   return (Gedcom_ctxt)tag_value;
 }
 
@@ -114,7 +122,7 @@ Gedcom_ctxt submit_start(int level, Gedcom_val xref, char *tag,
                         char *raw_value, int tag_value,
                         Gedcom_val parsed_value)
 {
-  output(1, "Submitter, xref is %s\n", GEDCOM_STRING(xref));
+  output(1, "Submitter, xref is %s\n", GEDCOM_XREF_PTR(xref)->string);
   return (Gedcom_ctxt)10000;
 }
 
@@ -166,8 +174,12 @@ Gedcom_ctxt source_date_start(Gedcom_ctxt parent, int level, char *tag,
 void default_cb(Gedcom_ctxt ctxt, int level, char *tag, char *raw_value,
                int tag_value)
 {
-  output(0, "== %d %s (%d) %s (ctxt is %d)\n",
-        level, tag, tag_value, raw_value, (int)ctxt);
+  char   *converted = NULL;
+  int    conv_fails = 0;
+  if (raw_value)
+    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);
 }
 
 void subscribe_callbacks()
@@ -232,6 +244,9 @@ int main(int argc, char* argv[])
       else if (!strncmp(argv[i], "-3", 3)) {
        run_times = 3;
       }
+      else if (!strncmp(argv[i], "-q", 3)) {
+       quiet = 1;
+      }
       else if (strncmp(argv[i], "-", 1)) {
        file_name = argv[i];
        break;
@@ -250,6 +265,8 @@ int main(int argc, char* argv[])
     exit(1);
   }
 
+  setlocale(LC_ALL, "");
+  gedcom_init();
   gedcom_set_debug_level(debug_level, NULL);
   gedcom_set_compat_handling(compat_enabled);
   gedcom_set_error_handling(mech);
@@ -267,11 +284,11 @@ int main(int argc, char* argv[])
   }
   fclose(outfile);
   if (result == 0) {
-    printf("Parse succeeded\n");
+    output(1, "Parse succeeded\n");
     return 0;
   }
   else {
-    printf("Parse failed\n");
+    output(1, "Parse failed\n");
     return 1;
   }  
 }