Debug print statements should not be internationalized.
[gedcom-parse.git] / gedcom / multilex.c
index e8d514cb67296e2777548eeb0ebf80c1ed733ade..721702e2541184339e9bbb7079a861ff2e6d1f8d 100644 (file)
@@ -68,50 +68,68 @@ int gedcom_lex()
   return (*lf)();
 }
 
+void rewind_file(FILE* f)
+{
+  if (fseek(f, 0, 0) != 0)
+    gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
+}
+
 int determine_encoding(FILE* f)
 {
   char first[2];
   int read;
 
+  set_encoding_bom(WITHOUT_BOM);
   read = fread(first, 1, 2, f);
   if (read != 2) {
     gedcom_warning(_("Error reading from input file: %s"), strerror(errno));
+    rewind_file(f);
     return ONE_BYTE;
   }
   else if ((first[0] == '0') && (first[1] == ' ')) {
-    gedcom_debug_print(_("One-byte encoding"));
-    if (fseek(f, 0, 0) != 0)
-      gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
+    gedcom_debug_print("One-byte encoding");
+    rewind_file(f);
     return ONE_BYTE;
   }
-  else if ((first[0] == '\0') && (first[1] == '0'))
-  {
-    gedcom_debug_print(_("Two-byte encoding, high-low"));
-    if (fseek(f, 0, 0) != 0)
-      gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
+  else if ((first[0] == '\0') && (first[1] == '0')) {
+    gedcom_debug_print("Two-byte encoding, high-low");
+    rewind_file(f);
     return TWO_BYTE_HILO;
   }
-  else if ((first[0] == '\xFE') && (first[1] == '\xFF'))
-  {
-    gedcom_debug_print(_("Two-byte encoding, high-low, with BOM"));
+  else if ((first[0] == '\xFE') && (first[1] == '\xFF')) {
+    gedcom_debug_print("Two-byte encoding, high-low, with BOM");
+    set_encoding_bom(WITH_BOM);
     return TWO_BYTE_HILO;
   }
-  else if ((first[0] == '0') && (first[1] == '\0'))
-  {
-    gedcom_debug_print(_("Two-byte encoding, low-high"));
-    if (fseek(f, 0, 0) != 0)
-      gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
+  else if ((first[0] == '0') && (first[1] == '\0')) {
+    gedcom_debug_print("Two-byte encoding, low-high");
+    rewind_file(f);
     return TWO_BYTE_LOHI;
   }
-  else if ((first[0] == '\xFF') && (first[1] == '\xFE'))
-  {
-    gedcom_debug_print(_("Two-byte encoding, low-high, with BOM"));
+  else if ((first[0] == '\xFF') && (first[1] == '\xFE')) {
+    gedcom_debug_print("Two-byte encoding, low-high, with BOM");
+    set_encoding_bom(WITH_BOM);
     return TWO_BYTE_LOHI;
   }
+  else if ((first[0] == '\xEF') && (first[1] == '\xBB')) {
+    read = fread(first, 1, 1, f);
+    if (read != 1) {
+      gedcom_warning(_("Error reading from input file: %s"), strerror(errno));
+      rewind_file(f);
+    }
+    else if (first[0] == '\xBF') {
+      set_encoding_bom(WITH_BOM);
+      gedcom_debug_print("UTF-8 encoding, with BOM");
+    }
+    else {
+      gedcom_warning(_("Unknown encoding, falling back to one-byte"));
+      rewind_file(f);
+    }
+    return ONE_BYTE;
+  }
   else {
     gedcom_warning(_("Unknown encoding, falling back to one-byte"));
-    if (fseek(f, 0, 0) != 0)
-      gedcom_warning(_("Error positioning input file: %s"), strerror(errno));
+    rewind_file(f);
     return ONE_BYTE;
   }
 }