#include <stdlib.h>
#include <string.h>
#include "utf8tools.h"
+#include "user_rec.h"
#include "gom.h"
#include "gom_internal.h"
return result;
}
+
+void unref_xref_value(struct xref_value *xref)
+{
+ if (xref)
+ gedcom_unlink_xref(xref->type, xref->string);
+}
+
+void UNREFALLFUNC(xref_list)(struct xref_list* obj)
+{
+ if (obj) {
+ struct xref_list* runner;
+ for (runner = obj; runner; runner = runner->next) {
+ unref_xref_value(runner->xref);
+ UNREFALLFUNC(user_data)(runner->extra);
+ }
+ }
+}
+
+void CLEANFUNC(xref_list)(struct xref_list *obj)
+{
+ if (obj) {
+ DESTROY_CHAIN_ELTS(user_data, obj->extra);
+ }
+}
+
+struct xref_value* gom_set_xref(struct xref_value** data, const char* xref)
+{
+ struct xref_value* result = NULL;
+ struct xref_value* newval = NULL;
+
+ if (data) {
+ if (xref) {
+ newval = gedcom_get_by_xref(xref);
+ if (!newval)
+ gedcom_error(_("No record found for xref '%s'"), xref);
+ }
+
+ /* Unreference the old value if not NULL */
+ if (*data)
+ result = gedcom_unlink_xref((*data)->type, (*data)->string);
+ else
+ result = newval;
+
+ /* Reference the new value if not NULL */
+ if (result != NULL && newval) {
+ result = gedcom_link_xref(newval->type, newval->string);
+ /* On error, perform rollback to old value (guaranteed to work) */
+ if (result == NULL)
+ gedcom_link_xref((*data)->type, (*data)->string);
+ }
+
+ if (result != NULL) {
+ *data = newval;
+ result = newval;
+ }
+ }
+ return result;
+}
+
+struct xref_list* gom_add_xref(struct xref_list** data, const char* xref)
+{
+ struct xref_value* result = NULL;
+ struct xref_value* newval = NULL;
+ struct xref_list* xrl = NULL;
+
+ if (data && xref) {
+ newval = gedcom_get_by_xref(xref);
+ if (!newval)
+ gedcom_error(_("No record found for xref '%s'"), xref);
+ else {
+ result = gedcom_link_xref(newval->type, newval->string);
+ if (result != NULL) {
+ MAKE_CHAIN_ELT(xref_list, *data, xrl);
+ if (xrl) xrl->xref = newval;
+ }
+ }
+ }
+
+ 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)
+{
+ int result = 1;
+
+ if (data && 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;
+ }
+ }
+
+ return result;
+}