X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gom%2Fgom_modify.c;h=11db4d775d5c5be0be9e49c0e995bb772b324846;hb=327069289a45ed1e91d8bfc07d7d89cfbad057d6;hp=82205ffb4ca03cc02548ac0ed131aa9a748313b9;hpb=295e0273f2ff3bb95f7afb1ac639ab7203672c9c;p=gedcom-parse.git diff --git a/gom/gom_modify.c b/gom/gom_modify.c index 82205ff..11db4d7 100644 --- a/gom/gom_modify.c +++ b/gom/gom_modify.c @@ -24,6 +24,7 @@ #include #include #include "utf8tools.h" +#include "user_rec.h" #include "gom.h" #include "gom_internal.h" @@ -78,17 +79,48 @@ char* gom_set_string_for_locale(char** data, const char* locale_str) return result; } -struct xref_value* gom_set_xref_value(struct xref_value** data, - struct xref_value* newval) +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); @@ -104,3 +136,54 @@ struct xref_value* gom_set_xref_value(struct xref_value** data, } 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; +} + +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); + } + } + + return result; +}