renamed the package to libgedcom-dev
[gedcom-parse.git] / gedcom / write.c
index f880bbbe90ef0279d4ab3b1cbfcccab7c608b38b..f04697e6fb03ed4e56fcdaf2b6010937483daa45 100644 (file)
@@ -104,7 +104,8 @@ int write_simple(Gedcom_write_hndl hndl,
 }
 
 int write_encoding_value(Gedcom_write_hndl hndl,
-                        int level, char* xref, char* tag, char* value)
+                        int level, const char* xref, const char* tag,
+                        const char* value)
 {
   if (strcmp(value, write_encoding.charset))
     gedcom_warning(_("Forcing HEAD.CHAR value to '%s'"),
@@ -153,8 +154,13 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec,
 
        if (prefix_len + line_len + term_len > MAXWRITELEN) {
          line_len = MAXWRITELEN - prefix_len - term_len;
-         if (!cont_as_conc)
+         if (!cont_as_conc) {
            cont_tag = "CONC";
+           while (value_ptr[line_len] == ' '
+                  || value_ptr[line_len-1] == ' ') {
+             line_len--;
+           }
+         }
        }
        
        memset(value_part, 0, sizeof(value_part));
@@ -164,7 +170,7 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec,
        if (line_len < value_len) {
          value_ptr   = value_ptr + line_len;
          value_len   = value_len - line_len;
-         while (*value_ptr == '\n') {
+         if (*value_ptr == '\n') {
            value_ptr++;
            value_len--;
          }
@@ -183,6 +189,13 @@ int write_long(Gedcom_write_hndl hndl, int elt_or_rec,
   return 0;
 }
 
+/** The basic function for opening a GEDCOM file for writing.
+
+    \param filename  The name of the file to write
+
+    \return A write handle, which needs to be used in the writing functions,
+    or \c NULL in case of errors.
+ */
 Gedcom_write_hndl gedcom_write_open(const char *filename)
 {
   Gedcom_write_hndl hndl;
@@ -231,6 +244,16 @@ Gedcom_write_hndl gedcom_write_open(const char *filename)
   return hndl;
 }
 
+/** The basic function for closing a GEDCOM file for writing.
+
+    \param hndl  The write handle as returned by gedcom_write_open().
+    \param total_conv_fails  If you pass an actual integer pointer for this,
+    the function will write in it the total number of conversion failures;
+    you can pass \c NULL if you're not interested
+
+    \retval 0 in case of success
+    \retval >0 in case of failure.
+ */
 int gedcom_write_close(Gedcom_write_hndl hndl, int* total_conv_fails)
 {
   int result = 0;
@@ -321,7 +344,7 @@ char* convert_at(const char* input)
 
 int _gedcom_write_val(Gedcom_write_hndl hndl,
                      int rec_or_elt, int tag, int parent_rec_or_elt,
-                     char* xrefstr, char* val)
+                     const char* xrefstr, const char* val)
 {
   int result = 1;
   int level = 0;
@@ -341,8 +364,24 @@ int _gedcom_write_val(Gedcom_write_hndl hndl,
   return result;
 }
 
+/** Function for writing lines corresponding to standard records (i.e. on
+    level 0).
+
+    \param hndl The write handle that was returned by gedcom_write_open().
+    \param rec  One of the identifiers given in the first column in
+    <a href=interface.html#Record_identifiers>this table</a> (except REC_USER).
+    \param xrefstr The cross-reference key of the record (something like
+    \c "@FAM01@".
+    \param val  The value of the record line, which should be \c NULL for some
+    record types, according to
+    <a href=interface.html#Record_identifiers>this table</a>.
+
+    \retval 0 on success
+    \retval >0 on failure
+*/  
 int gedcom_write_record_str(Gedcom_write_hndl hndl,
-                           Gedcom_rec rec, char* xrefstr, char* val)
+                           Gedcom_rec rec, const char* xrefstr,
+                           const char* val)
 {
   int result = 1;
   if (check_type(rec, (val ? GV_CHAR_PTR : GV_NULL)))
@@ -350,9 +389,34 @@ int gedcom_write_record_str(Gedcom_write_hndl hndl,
   return result;
 }
 
+/** Function for writing lines corresponding to standard elements (i.e. on
+    level bigger than 0), with a string as value.
+
+    \param hndl The write handle that was returned by gedcom_write_open().
+    \param elt  One of the identifiers given in the first column in
+    <a href=interface.html#Element_identifiers>this table</a>
+    (except ELT_USER).
+    \param tag Some of the \c elt identifiers can actually stand for different
+    tags.  For this reason, the \c tag has to be passed for some of them.  This
+    parsed tag is the same as was returned by the callback functions, and is
+    an identifier of the form <code>TAG_<em>name</em></code>.  This parameter
+    is needed whenever the second column in 
+    <a href=interface.html#Element_identifiers>this table</a> shows several
+    possible tags (this is e.g. the case for \c ELT_SUB_FAM_EVT).  Otherwise,
+    you can pass 0.
+    \param parent_rec_or_elt The corresponding \c rec or \c elt identifier of
+    the logically enclosing statement: this will determine the level number
+    written on the line, as the level number of the parent + 1.
+    \param val  The value of the element line, which should be \c NULL for some
+    element types, according to
+    <a href=interface.html#Element_identifiers>this table</a>.
+
+    \retval 0 on success
+    \retval >0 on failure
+*/  
 int gedcom_write_element_str(Gedcom_write_hndl hndl,
                             Gedcom_elt elt, int tag, int parent_rec_or_elt,
-                            char* val)
+                            const char* val)
 {
   int result = 1;
   if (check_type(elt, (val ? GV_CHAR_PTR : GV_NULL)))
@@ -361,9 +425,14 @@ int gedcom_write_element_str(Gedcom_write_hndl hndl,
   return result;
 }
 
+/** Function for writing lines corresponding to standard elements (i.e. on
+    level bigger than 0), with a cross-reference as value.
+
+    See gedcom_write_element_str() for details.
+*/  
 int gedcom_write_element_xref(Gedcom_write_hndl hndl,
                              Gedcom_elt elt, int tag, int parent_rec_or_elt,
-                             struct xref_value* val)
+                             const struct xref_value* val)
 {
   int result = 1;
   if (check_type(elt, (val ? GV_XREF_PTR : GV_NULL)))
@@ -372,9 +441,14 @@ int gedcom_write_element_xref(Gedcom_write_hndl hndl,
   return result;
 }
 
+/** Function for writing lines corresponding to standard elements (i.e. on
+    level bigger than 0), with a date as value.
+
+    See gedcom_write_element_str() for details.
+*/  
 int gedcom_write_element_date(Gedcom_write_hndl hndl,
                              Gedcom_elt elt, int tag, int parent_rec_or_elt,
-                             struct date_value* val)
+                             const struct date_value* val)
 {
   int result = 1;
   if (check_type(elt, (val ? GV_DATE_VALUE : GV_NULL)))
@@ -383,9 +457,14 @@ int gedcom_write_element_date(Gedcom_write_hndl hndl,
   return result;
 }
 
+/** Function for writing lines corresponding to standard elements (i.e. on
+    level bigger than 0), with an age as value.
+
+    See gedcom_write_element_str() for details.
+*/  
 int gedcom_write_element_age(Gedcom_write_hndl hndl,
                             Gedcom_elt elt, int tag, int parent_rec_or_elt,
-                            struct age_value* val)
+                            const struct age_value* val)
 {
   int result = 1;
   if (check_type(elt, (val ? GV_AGE_VALUE : GV_NULL)))
@@ -394,8 +473,25 @@ int gedcom_write_element_age(Gedcom_write_hndl hndl,
   return result;
 }
 
-int gedcom_write_user_str(Gedcom_write_hndl hndl, int level, char* tag,
-                         char* xrefstr, char* value)
+/** Function for writing lines corresponding to user-defined records and
+    elements, with a string as value.
+
+    In the case of user-defined tags, the
+    level and tag string are passed verbatim (not controlled by the library).
+    This allows to write any extra data that doesn't use a standard tag, but
+    is only allowed for tags starting with an underscore.
+
+    \param hndl The write handle that was returned by gedcom_write_open().
+    \param level  The integer level of the GEDCOM line
+    \param tag  The tag, as a literal string
+    \param xrefstr An optional cross-reference of the record or element.
+    \param value The value of the record or element line.
+
+    \retval 0 on success
+    \retval >0 on failure
+*/  
+int gedcom_write_user_str(Gedcom_write_hndl hndl, int level, const char* tag,
+                         const char* xrefstr, const char* value)
 {
   int result = 1;
   if (tag && tag[0] == '_')
@@ -403,8 +499,13 @@ int gedcom_write_user_str(Gedcom_write_hndl hndl, int level, char* tag,
   return result;
 }
 
-int gedcom_write_user_xref(Gedcom_write_hndl hndl, int level, char* tag,
-                          char* xrefstr, struct xref_value* val)
+/** Function for writing lines corresponding to user-defined records and
+    elements, with a cross-reference as value.
+
+    See gedcom_write_user_str() for details.
+*/  
+int gedcom_write_user_xref(Gedcom_write_hndl hndl, int level, const char* tag,
+                          const char* xrefstr, const struct xref_value* val)
 {
   int result = 1;
   if (tag && tag[0] == '_')