X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gom%2Fgom_modify.c;h=7e31a974e3b26b4e767263aeff937f9ed88ed393;hb=8073f669d16f11bfd50d42bb2cf6fdb79d358565;hp=bc65a3a380412b2386af7e855cd1685ed7f4430b;hpb=08936924a7a4b1541ee5f9778cae6bffef1767b3;p=gedcom-parse.git diff --git a/gom/gom_modify.c b/gom/gom_modify.c index bc65a3a..7e31a97 100644 --- a/gom/gom_modify.c +++ b/gom/gom_modify.c @@ -23,8 +23,8 @@ #include #include -#include #include "utf8tools.h" +#include "user_rec.h" #include "gom.h" #include "gom_internal.h" @@ -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,44 +73,152 @@ 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; } -int update_date(struct date_value** dv, struct tm* tm_ptr) +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) { - int result; - struct date_value* dval = gedcom_new_date_value(NULL); - dval->type = DV_NO_MODIFIER; - dval->date1.cal = CAL_GREGORIAN; - dval->date1.day = tm_ptr->tm_mday; - dval->date1.month = tm_ptr->tm_mon + 1; - dval->date1.year = tm_ptr->tm_year + 1900; - result = gedcom_normalize_date(DI_FROM_NUMBERS, dval); - - if (result == 0) { - if (*dv) free(*dv); - *dv = dval; + 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; } -int update_time(char** tv, struct tm* tm_ptr) +struct xref_list* gom_add_xref(struct xref_list** data, const char* xref) { - char tval[16]; - sprintf(tval, "%02d:%02d:%02d", - tm_ptr->tm_hour, tm_ptr->tm_min, tm_ptr->tm_sec); - - if (gom_set_string(tv, tval)) - return 0; - else - return 1; + 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; +}