Functions for moving an xref in a list.
authorPeter Verthez <Peter.Verthez@advalvas.be>
Sun, 12 Jan 2003 20:01:35 +0000 (20:01 +0000)
committerPeter Verthez <Peter.Verthez@advalvas.be>
Sun, 12 Jan 2003 20:01:35 +0000 (20:01 +0000)
gom/func_template.h
gom/gom.c
gom/gom_internal.h
gom/gom_modify.c

index c25fb11f52a37a10bcba235ca264da7184c4fede..769debb2a96e84c350da8c118a6e0067885bd7dc 100644 (file)
       (FIRSTVAL)->previous = VAL->previous;                                   \
   }
 
+#define MOVE_CHAIN_ELT(STRUCTTYPE, DIR, FIRSTVAL, VAL)                        \
+  {                                                                           \
+    struct STRUCTTYPE *first, *second;                                        \
+    if (DIR == MOVE_UP) {                                                     \
+      first  = VAL->previous;                                                 \
+      second = VAL;                                                           \
+    }                                                                         \
+    else {                                                                    \
+      first  = VAL;                                                           \
+      second = VAL->next;                                                     \
+    }                                                                         \
+    if (second && (second != FIRSTVAL)) {                                     \
+      if (first != FIRSTVAL)                                                  \
+       first->previous->next  = second;                                      \
+      else                                                                    \
+       FIRSTVAL               = second;                                      \
+                                                                              \
+      if (second->next)                                                       \
+       second->next->previous = first;                                       \
+      else                                                                    \
+       (FIRSTVAL)->previous   = first;                                       \
+                                                                              \
+      first->next            = second->next;                                  \
+      second->next           = first;                                         \
+                                                                              \
+      second->previous       = first->previous;                               \
+      first->previous        = second;                                        \
+    }                                                                         \
+    else {                                                                    \
+      gom_move_error(#STRUCTTYPE);                                            \
+    }                                                                         \
+  }
+
 #define MAKE_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL)                             \
   {                                                                           \
     VAL = (struct STRUCTTYPE*) malloc(sizeof(struct STRUCTTYPE));             \
index adf6caba464babf045030d39b0abbaae65727e94..3616e79a241600a531e507fb58a022143d0090c6 100644 (file)
--- a/gom/gom.c
+++ b/gom/gom.c
@@ -204,6 +204,11 @@ void gom_no_context(const char* file, int line)
                 file, line);
 }
 
+void gom_move_error(const char* type)
+{
+  gedcom_warning(_("Could not move struct of type %s"), type);
+}
+
 void gom_default_callback (Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED,
                           int level, char* tag, char* raw_value,
                           int parsed_tag UNUSED)
index 1def3f9ee15835bef2655033ffba331047866ec6..476cc9e362a7a1ed790f969d47c7912daf845b9f 100644 (file)
@@ -76,6 +76,7 @@ void gom_cast_error(const char* file, int line,
 void gom_no_context(const char* file, int line);
 void gom_unexpected_context(const char* file, int line, OBJ_TYPE found);
 void gom_xref_already_in_use(const char *xrefstr);
+void gom_move_error(const char* type);
 void unref_xref_value(struct xref_value *xref);
 
 int gom_write_xref_list(Gedcom_write_hndl hndl,
index 11db4d775d5c5be0be9e49c0e995bb772b324846..5bfb34592fb981b9be37d098261eb2ba9f8726ce 100644 (file)
@@ -159,29 +159,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;
     }
   }