Only try to delete address if present.
[gedcom-parse.git] / gom / gom_modify.c
index 11db4d775d5c5be0be9e49c0e995bb772b324846..7e31a974e3b26b4e767263aeff937f9ed88ed393 100644 (file)
@@ -48,17 +48,22 @@ char* gom_set_string(char** data, const char* utf8_str)
   char* result = NULL;
   char* newptr;
 
-  if (!is_utf8_string(utf8_str)) {
-    gedcom_error(_("The input '%s' is not a valid UTF-8 string"), utf8_str);
+  if (utf8_str == NULL) {
+    SAFE_FREE(*data);
   }
   else {
-    newptr = strdup(utf8_str);
-    if (!newptr)
-      MEMORY_ERROR;
+    if (!is_utf8_string(utf8_str)) {
+      gedcom_error(_("The input '%s' is not a valid UTF-8 string"), utf8_str);
+    }
     else {
-      if (*data) free(*data);
-      *data = newptr;
-      result = *data;
+      newptr = strdup(utf8_str);
+      if (!newptr)
+       MEMORY_ERROR;
+      else {
+       SAFE_FREE(*data);
+       *data = newptr;
+       result = *data;
+      }
     }
   }
   
@@ -68,13 +73,19 @@ char* gom_set_string(char** data, const char* utf8_str)
 char* gom_set_string_for_locale(char** data, const char* locale_str)
 {
   char* result = NULL;
-  char* utf8_str = convert_locale_to_utf8(locale_str);
-  
-  if (!utf8_str)
-    gedcom_error(_("The input '%s' is not a valid string for the locale"),
-                locale_str);
-  else
-    result = gom_set_string(data, utf8_str);
+
+  if (locale_str == NULL) {
+    result = gom_set_string(data, NULL);
+  }
+  else {
+    char* utf8_str = convert_locale_to_utf8(locale_str);
+    
+    if (!utf8_str)
+      gedcom_error(_("The input '%s' is not a valid string for the locale"),
+                  locale_str);
+    else
+      result = gom_set_string(data, utf8_str);
+  }
 
   return result;
 }
@@ -159,29 +170,53 @@ struct xref_list* gom_add_xref(struct xref_list** data, const char* xref)
   return xrl;
 }
 
+struct xref_list* find_xref(struct xref_list** data, const char* xref)
+{
+  struct xref_list* result = NULL;
+  struct xref_value* xr = gedcom_get_by_xref(xref);
+  if (!xr)
+    gedcom_error(_("No record found for xref '%s'"), xref);
+  else {
+    struct xref_list* xrl;
+    for (xrl = *data ; xrl ; xrl = xrl->next) {
+      if (xrl->xref == xr) {
+       result = xrl;
+       break;
+      }
+    }
+    if (! result)
+      gedcom_error(_("Xref '%s' not part of chain"), xref);
+  }
+  return result;
+}
+
 int gom_remove_xref(struct xref_list** data, const char* xref)
 {
-  struct xref_value* xr = NULL;
   int result = 1;
-  
+
   if (data && xref) {
-    xr = gedcom_get_by_xref(xref);
-    if (!xr)
-      gedcom_error(_("No record found for xref '%s'"), xref);
-    else {
-      struct xref_list* xrl = NULL;
-      for (xrl = *data ; xrl ; xrl = xrl->next) {
-       if (xrl->xref == xr) {
-         UNLINK_CHAIN_ELT(xref_list, *data, xrl);
-         gedcom_unlink_xref(xr->type, xr->string);
-         CLEANFUNC(xref_list)(xrl);
-         SAFE_FREE(xrl);
-         result = 0;
-         break;
-       }
-      }
-      if (result == 1)
-       gedcom_error(_("Xref '%s' to remove not part of chain"), xref);
+    struct xref_list* xrl = find_xref(data, xref);
+    if (xrl) {
+      UNLINK_CHAIN_ELT(xref_list, *data, xrl);
+      gedcom_unlink_xref(xrl->xref->type, xrl->xref->string);
+      CLEANFUNC(xref_list)(xrl);
+      SAFE_FREE(xrl);
+      result = 0;
+    }
+  }
+
+  return result;
+}
+
+int gom_move_xref(Gom_direction dir, struct xref_list** data, const char* xref)
+{
+  int result = 1;
+
+  if (data && xref) {
+    struct xref_list* xrl = find_xref(data, xref);
+    if (xrl) {
+      MOVE_CHAIN_ELT(xref_list, dir, *data, xrl);
+      result = 0;
     }
   }