Allow empty date as string (needed for compatibility with Lifelines).
[gedcom-parse.git] / gedcom / multilex.c
index 9a27c187f9867f56454e51cfc53f7ed69a620edb..96af2952b714d022462e171c199b6b58b170d0bf 100644 (file)
@@ -1,5 +1,5 @@
 /* The lexer multiplexer for Gedcom.
-   Copyright (C) 2001 The Genes Development Team
+   Copyright (C) 2001,2002 The Genes Development Team
    This file is part of the Gedcom parser library.
    Contributed by Peter Verthez <Peter.Verthez@advalvas.be>, 2001.
 
@@ -24,6 +24,7 @@
 #include "gedcom_internal.h"
 #include "multilex.h"
 #include "encoding.h"
+#include "xref.h"
 
 int line_no;
 
@@ -71,59 +72,88 @@ int determine_encoding(FILE* f)
 
   fread(first, 1, 2, f);
   if ((first[0] == '0') && (first[1] == ' ')) {
-    gedcom_message("One-byte encoding");
+    gedcom_debug_print(_("One-byte encoding"));
     fseek(f, 0, 0);
     return ONE_BYTE;
   }
   else if ((first[0] == '\0') && (first[1] == '0'))
   {
-    gedcom_message("Two-byte encoding, high-low");
+    gedcom_debug_print(_("Two-byte encoding, high-low"));
     fseek(f, 0, 0);
     return TWO_BYTE_HILO;
   }
   else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
   {
-    gedcom_message("Two-byte encoding, high-low, with BOM");
+    gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
     return TWO_BYTE_HILO;
   }
   else if ((first[0] == '0') && (first[1] == '\0'))
   {
-    gedcom_message("Two-byte encoding, low-high");
+    gedcom_debug_print(_("Two-byte encoding, low-high"));
     fseek(f, 0, 0);
     return TWO_BYTE_LOHI;
   }
   else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
   {
-    gedcom_message("Two-byte encoding, low-high, with BOM");
+    gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
     return TWO_BYTE_LOHI;
   }
   else {
-    gedcom_message("Unknown encoding, falling back to one-byte");
+    gedcom_warning(_("Unknown encoding, falling back to one-byte"));
     fseek(f, 0, 0);
     return ONE_BYTE;
   }
 }
 
+static int init_called = 0;
+
+void gedcom_init()
+{
+  init_called = 1;
+  update_gconv_search_path();
+}
+
 int gedcom_parse_file(char* file_name)
 {
   ENCODING enc;
   int result = 1;
-  FILE* file = fopen (file_name, "r");
-  line_no = 1;
-  if (!file) {
-    gedcom_error("Could not open file '%s'\n", file_name);
-    return 1;
-  }
-
-  init_encodings();
-  enc = determine_encoding(file);
+  FILE* file;
   
-  if (lexer_init(enc, file)) {
-    result = gedcom_parse();
+  char *save_locale  = strdup(setlocale(LC_ALL, NULL));
+  char *save_textdom = textdomain(NULL);
+  setlocale(LC_ALL, "");
+  bindtextdomain(PACKAGE, LOCALEDIR);
+  bind_textdomain_codeset(PACKAGE, INTERNAL_ENCODING);
+  textdomain(PACKAGE);
+
+  if (!init_called) {
+    gedcom_error(_("Internal error: GEDCOM parser not initialized"));
   }
-  lexer_close();
-  fclose(file);
-  
+  else {
+    line_no = 1;
+    file = fopen(file_name, "r");
+    if (!file) {
+      gedcom_error(_("Could not open file '%s'"), file_name);
+    }
+    else {
+      init_encodings();
+      enc = determine_encoding(file);
+      
+      if (lexer_init(enc, file)) {
+       line_no = 1;
+       make_xref_table();
+       result = gedcom_parse();
+       if (result == 0)
+         result = check_xref_table();
+      }
+      lexer_close();
+      fclose(file);
+    }
+  }
+
+  textdomain(save_textdom);
+  setlocale(LC_ALL, save_locale);
+  free(save_locale);
   return result;
 }