Put invalid date (e.g. 29 FEB 1739) in phrase member.
[gedcom-parse.git] / gedcom / write.c
index f0b1529ca8ca9271d107dbf1cc69aa98bca241c6..f880bbbe90ef0279d4ab3b1cbfcccab7c608b38b 100644 (file)
@@ -24,6 +24,7 @@
 #include "gedcom_internal.h"
 #include "gedcom.h"
 #include "encoding.h"
+#include "encoding_state.h"
 #include "tag_data.h"
 #include "buffer.h"
 #include "utf8tools.h"
 #include <fcntl.h>
 
 #define MAXWRITELEN MAXGEDCLINELEN
-#define MAXCHARSETLEN 32
-
-char charset[MAXCHARSETLEN+1]  = "ASCII";
-const char* encoding = "ASCII";
-int write_encoding_details = ONE_BYTE;
-/* SYS_NEWLINE is defined in config.h */
-const char* write_terminator = SYS_NEWLINE;
 
 struct Gedcom_write_struct {
   int       filedesc;
@@ -50,19 +44,6 @@ struct Gedcom_write_struct {
   int       ctxt_level;
 };
 
-const char* default_encoding[] = {
-  /* ONE_BYTE */      "ASCII",
-  /* TWO_BYTE_HILO */ "UCS-2BE",
-  /* TWO_BYTE_LOHI */ "UCS-2LE"
-};
-
-const char* terminator[] = {
-  /* END_CR */     "\x0D",
-  /* END_LF */     "\x0A",
-  /* END_CR_LF */  "\x0D\x0A",
-  /* END_LF_CR */  "\x0A\x0D"
-};
-
 void cleanup_write_buffer();
 struct safe_buffer write_buffer = { NULL, 0, NULL, 0, cleanup_write_buffer };
 
@@ -122,12 +103,13 @@ int write_simple(Gedcom_write_hndl hndl,
   return 0;
 }
 
-int write_encoding(Gedcom_write_hndl hndl,
-                  int level, char* xref, char* tag, char* value)
+int write_encoding_value(Gedcom_write_hndl hndl,
+                        int level, char* xref, char* tag, char* value)
 {
-  if (strcmp(value, charset))
-    gedcom_warning(_("Forcing HEAD.CHAR value to '%s'"), charset);
-  return write_simple(hndl, level, xref, tag, charset);
+  if (strcmp(value, write_encoding.charset))
+    gedcom_warning(_("Forcing HEAD.CHAR value to '%s'"),
+                  write_encoding.charset);
+  return write_simple(hndl, level, xref, tag, write_encoding.charset);
 }
 
 int supports_continuation(int elt_or_rec, int which_continuation)
@@ -201,45 +183,6 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec,
   return 0;
 }
 
-int gedcom_write_set_encoding(const char* new_charset,
-                             Encoding width, Enc_bom bom)
-{
-  char* new_encoding = NULL;
-  if (!strcmp(new_charset, "UNICODE")) {
-    if (width == ONE_BYTE) {
-      gedcom_error(_("Unicode cannot be encoded into one byte"));
-      return 1;
-    }
-    else {
-      new_encoding = get_encoding(new_charset, width);
-      if (new_encoding) {
-       encoding = new_encoding;
-       write_encoding_details = width | bom;
-       strncpy(charset, new_charset, MAXCHARSETLEN);
-      }
-      else
-       return 1;
-    }
-  }
-  else {
-    new_encoding = get_encoding(new_charset, ONE_BYTE);
-    if (new_encoding) {
-      encoding = new_encoding;
-      write_encoding_details = ONE_BYTE;
-      strncpy(charset, new_charset, MAXCHARSETLEN);
-    }
-    else
-      return 1;
-  }
-  return 0;
-}
-
-int gedcom_write_set_line_terminator(Enc_line_end end)
-{
-  write_terminator = terminator[end];
-  return 0;
-}
-
 Gedcom_write_hndl gedcom_write_open(const char *filename)
 {
   Gedcom_write_hndl hndl;
@@ -249,11 +192,13 @@ Gedcom_write_hndl gedcom_write_open(const char *filename)
   if (!hndl)
     MEMORY_ERROR;
   else {
+    init_write_encoding();
+    init_write_terminator();
     hndl->total_conv_fails = 0;
-    hndl->conv = initialize_utf8_conversion(encoding, 0);
+    hndl->conv = initialize_utf8_conversion(write_encoding.encoding, 0);
     if (!hndl->conv) {
       gedcom_error(_("Could not open encoding '%s' for writing: %s"),
-                  encoding, strerror(errno));
+                  write_encoding.encoding, strerror(errno));
       free(hndl);
       hndl = NULL;
     }
@@ -267,15 +212,17 @@ Gedcom_write_hndl gedcom_write_open(const char *filename)
        hndl = NULL;
       }
       else {
-       hndl->term = write_terminator;
+       hndl->term = write_encoding.terminator;
        hndl->ctxt_level = -1;
-       if (write_encoding_details & WITH_BOM) {
-         if (write_encoding_details & TWO_BYTE_HILO)
+       if (write_encoding.bom == WITH_BOM) {
+         if (write_encoding.width == TWO_BYTE_HILO)
            write(hndl->filedesc, "\xFE\xFF", 2);
-         else if (write_encoding_details & TWO_BYTE_LOHI)
+         else if (write_encoding.width == TWO_BYTE_LOHI)
            write(hndl->filedesc, "\xFF\xFE", 2);
+         else if (!strcmp(write_encoding.encoding, "UTF-8"))
+           write(hndl->filedesc, "\xEF\xBB\xBF", 3);
          else
-           gedcom_warning(_("Byte order mark configured, but no Unicode"));
+           gedcom_warning(_("Byte order mark configured, but not relevant"));
        }
       }
     }
@@ -384,7 +331,7 @@ int _gedcom_write_val(Gedcom_write_hndl hndl,
   level   = get_level(hndl, rec_or_elt, parent_rec_or_elt);
   if (tag_str && (level != -1)) {
     if (rec_or_elt == ELT_HEAD_CHAR)
-      result = write_encoding(hndl, level, xrefstr, tag_str, val);
+      result = write_encoding_value(hndl, level, xrefstr, tag_str, val);
     else if (supports_continuation(rec_or_elt, OPT_CONT|OPT_CONC))
       result = write_long(hndl, rec_or_elt, level, xrefstr, tag_str, val);
     else