A little simplification in the write interface.
[gedcom-parse.git] / gedcom / write.c
index aba4195be34c88f9dadeb63fc13a999dfe9badc3..5bc9efa10fd33e841b25b6cae66e0b25a2440569 100644 (file)
@@ -26,7 +26,7 @@
 #include "encoding.h"
 #include "tag_data.h"
 #include "buffer.h"
-#include "utf8.h"
+#include "utf8tools.h"
 #include <unistd.h>
 #include <sys/types.h>
 #include <sys/stat.h>
@@ -127,13 +127,14 @@ int supports_continuation(int elt_or_rec, int which_continuation)
 int write_long(Gedcom_write_hndl hndl, int elt_or_rec,
               int level, char* xref, char* tag, char* value)
 {
-  int prefix_len, value_len, term_len;
-  char* nl_pos = strchr(value, '\n');
+  int prefix_len, value_len = 0, term_len;
+  char* nl_pos = NULL;
+  if (value) nl_pos = strchr(value, '\n');
 
   prefix_len = utf8_strlen(tag) + 3;  /* for e.g. "0 INDI " */
   if (level > 9) prefix_len++;
   if (xref)      prefix_len += utf8_strlen(xref) + 1;
-  value_len  = utf8_strlen(value);
+  if (value)     value_len  = utf8_strlen(value);
   term_len   = strlen(hndl->term);
 
   if (!nl_pos && prefix_len + value_len + term_len <= MAXWRITELEN)
@@ -379,12 +380,11 @@ int _gedcom_write_val(Gedcom_write_hndl hndl,
 }
 
 int gedcom_write_record_str(Gedcom_write_hndl hndl,
-                           Gedcom_rec rec, int tag,
-                           char* xrefstr, char* val)
+                           Gedcom_rec rec, char* xrefstr, char* val)
 {
   int result = 1;
   if (check_type(rec, (val ? GV_CHAR_PTR : GV_NULL)))
-    result = _gedcom_write_val(hndl, rec, tag, -1, xrefstr, convert_at(val));
+    result = _gedcom_write_val(hndl, rec, 0, -1, xrefstr, convert_at(val));
   return result;
 }
 
@@ -399,24 +399,36 @@ int gedcom_write_element_str(Gedcom_write_hndl hndl,
   return result;
 }
 
-int gedcom_write_record_xref(Gedcom_write_hndl hndl,
-                            Gedcom_rec rec, int tag,
-                            char* xrefstr, struct xref_value* val)
+int gedcom_write_element_xref(Gedcom_write_hndl hndl,
+                             Gedcom_elt elt, int tag, int parent_rec_or_elt,
+                             struct xref_value* val)
 {
   int result = 1;
-  if (check_type(rec, (val ? GV_XREF_PTR : GV_NULL)))
-    result = _gedcom_write_val(hndl, rec, tag, -1, xrefstr, val->string);
+  if (check_type(elt, (val ? GV_XREF_PTR : GV_NULL)))
+    result = _gedcom_write_val(hndl, elt, tag, parent_rec_or_elt, NULL,
+                              val->string);
   return result;
 }
 
-int gedcom_write_element_xref(Gedcom_write_hndl hndl,
+int gedcom_write_element_date(Gedcom_write_hndl hndl,
                              Gedcom_elt elt, int tag, int parent_rec_or_elt,
-                             struct xref_value* val)
+                             struct date_value* val)
 {
   int result = 1;
-  if (check_type(elt, (val ? GV_XREF_PTR : GV_NULL)))
+  if (check_type(elt, (val ? GV_DATE_VALUE : GV_NULL)))
     result = _gedcom_write_val(hndl, elt, tag, parent_rec_or_elt, NULL,
-                              val->string);
+                              gedcom_date_to_string(val));
+  return result;
+}
+
+int gedcom_write_element_age(Gedcom_write_hndl hndl,
+                            Gedcom_elt elt, int tag, int parent_rec_or_elt,
+                            struct age_value* val)
+{
+  int result = 1;
+  if (check_type(elt, (val ? GV_AGE_VALUE : GV_NULL)))
+    result = _gedcom_write_val(hndl, elt, tag, parent_rec_or_elt, NULL,
+                              gedcom_age_to_string(val));
   return result;
 }