From: Peter Verthez Date: Sat, 11 Jan 2003 18:42:53 +0000 (+0000) Subject: Reworked the xref functions. X-Git-Url: https://git.dlugolecki.net.pl/?a=commitdiff_plain;h=28297bd9b57fa7d8c3600d040cc08e5e73a62c27;p=gedcom-parse.git Reworked the xref functions. --- diff --git a/gom/family.c b/gom/family.c index 9375b1f..8389569 100644 --- a/gom/family.c +++ b/gom/family.c @@ -45,7 +45,6 @@ DEFINE_DESTROYFUNC(family, gom_first_family) DEFINE_ADDFUNC(family, XREF_FAM) DEFINE_DELETEFUNC(family) DEFINE_GETXREFFUNC(family, XREF_FAM) -DEFINE_MAKELINKFUNC(family, XREF_FAM) DEFINE_REC_CB(family, fam_start) DEFINE_XREF_CB(family, fam_husb_start, husband, individual) diff --git a/gom/func_template.h b/gom/func_template.h index 24e1ea6..a21424d 100644 --- a/gom/func_template.h +++ b/gom/func_template.h @@ -33,7 +33,6 @@ #define SUB_ADDFUNC(STRUCTTYPE) gom_add_ ## STRUCTTYPE #define DELETEFUNC(STRUCTTYPE) gom_delete_ ## STRUCTTYPE #define SUB_DELETEFUNC(STRUCTTYPE) gom_delete_ ## STRUCTTYPE -#define MAKELINKFUNC(STRUCTTYPE) gom_make_ ## STRUCTTYPE ## _link #define ADDFUNC2(T1,T2) T1 ## _add_ ## T2 #define ADDFUNC2_TOVAR(T1,T2,F) T1 ## _add_ ## T2 ## _to_ ## F #define ADDFUNC2_NOLIST(T1,T2) ADDFUNC2(T1,T2) @@ -75,9 +74,9 @@ } \ else { \ VAL->next = NULL; \ - FIRSTVAL->previous->next = VAL; \ - VAL->previous = FIRSTVAL->previous; \ - FIRSTVAL->previous = VAL; \ + (FIRSTVAL)->previous->next = VAL; \ + VAL->previous = (FIRSTVAL)->previous; \ + (FIRSTVAL)->previous = VAL; \ } \ } @@ -90,6 +89,8 @@ VAL->previous->next = VAL->next; \ if (VAL->next) \ VAL->next->previous = VAL->previous; \ + else if (FIRSTVAL) \ + (FIRSTVAL)->previous = VAL->previous; \ } #define MAKE_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, VAL) \ @@ -108,6 +109,7 @@ if (FIRSTVAL) { \ struct STRUCTTYPE *runner, *next; \ runner = FIRSTVAL; \ + FIRSTVAL = NULL; \ while (runner) { \ next = runner->next; \ CLEANFUNC(STRUCTTYPE)(runner); \ @@ -148,7 +150,7 @@ if (obj) { \ CLEANFUNC(STRUCTTYPE)(obj); \ UNLINK_CHAIN_ELT(STRUCTTYPE, FIRSTVAL, obj); \ - free(obj); \ + SAFE_FREE(obj); \ } \ } @@ -193,6 +195,7 @@ return obj; \ } +/* TODO: Check whether there are still xrefs linked in */ #define DEFINE_DELETEFUNC(STRUCTTYPE) \ int DELETEFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj) \ { \ @@ -211,23 +214,12 @@ int result = 1; \ if (obj && *obj) { \ CLEANFUNC(STRUCTTYPE)(*obj); \ - free(*obj); \ - *obj = NULL; \ + SAFE_FREE(*obj); \ result = 0; \ } \ return result; \ } -#define DEFINE_MAKELINKFUNC(STRUCTTYPE,XREF_TYPE) \ - struct xref_value* MAKELINKFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj) \ - { \ - struct xref_value* xr = NULL; \ - if (obj && obj->xrefstr) { \ - xr = gedcom_get_by_xref(obj->xrefstr); \ - } \ - return xr; \ - } - #define DEFINE_ADDFUNC2(STRUCTTYPE,T2,FIELD) \ void ADDFUNC2(STRUCTTYPE,T2)(Gom_ctxt ctxt, struct T2* addobj) \ { \ diff --git a/gom/gom_internal.h b/gom/gom_internal.h index 5b707a5..910e156 100644 --- a/gom/gom_internal.h +++ b/gom/gom_internal.h @@ -116,8 +116,8 @@ int update_time(char** tv, struct tm* tm_ptr); void NULL_DESTROY(void* anything); -#define xref_list_cleanup NULL_DESTROY - #include "func_template.h" +DECLARE_CLEANFUNC(xref_list); + #endif /* __GOM_INTERNAL_H */ diff --git a/gom/gom_modify.c b/gom/gom_modify.c index 82205ff..b876a9e 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,31 @@ 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 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 +119,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; +} diff --git a/gom/individual.c b/gom/individual.c index 33c4c6c..103ad31 100644 --- a/gom/individual.c +++ b/gom/individual.c @@ -47,7 +47,6 @@ DEFINE_DESTROYFUNC(individual, gom_first_individual) DEFINE_ADDFUNC(individual, XREF_INDI) DEFINE_DELETEFUNC(individual) DEFINE_GETXREFFUNC(individual, XREF_INDI) -DEFINE_MAKELINKFUNC(individual, XREF_INDI) DEFINE_REC_CB(individual, indi_start) DEFINE_STRING_CB(individual, indi_resn_start, restriction_notice) diff --git a/gom/multimedia.c b/gom/multimedia.c index 1b1f923..38ffa3f 100644 --- a/gom/multimedia.c +++ b/gom/multimedia.c @@ -39,7 +39,6 @@ DEFINE_DESTROYFUNC(multimedia, gom_first_multimedia) DEFINE_ADDFUNC(multimedia, XREF_OBJE) DEFINE_DELETEFUNC(multimedia) DEFINE_GETXREFFUNC(multimedia, XREF_OBJE) -DEFINE_MAKELINKFUNC(multimedia, XREF_OBJE) DEFINE_REC_CB(multimedia, obje_start) DEFINE_STRING_CB(multimedia, obje_form_start, form) diff --git a/gom/note.c b/gom/note.c index fbe253f..3c8bc69 100644 --- a/gom/note.c +++ b/gom/note.c @@ -56,7 +56,6 @@ DEFINE_DESTROYFUNC(note, gom_first_note) DEFINE_ADDFUNC(note, XREF_NOTE) DEFINE_DELETEFUNC(note) DEFINE_GETXREFFUNC(note, XREF_NOTE) -DEFINE_MAKELINKFUNC(note, XREF_NOTE) DEFINE_STRING_END_REC_CB(note, note_end, text) diff --git a/gom/repository.c b/gom/repository.c index 2d070c6..9d383fa 100644 --- a/gom/repository.c +++ b/gom/repository.c @@ -40,7 +40,6 @@ DEFINE_DESTROYFUNC(repository, gom_first_repository) DEFINE_ADDFUNC(repository, XREF_REPO) DEFINE_DELETEFUNC(repository) DEFINE_GETXREFFUNC(repository, XREF_REPO) -DEFINE_MAKELINKFUNC(repository, XREF_REPO) DEFINE_REC_CB(repository, repo_start) DEFINE_STRING_CB(repository, repo_name_start, name) diff --git a/gom/source.c b/gom/source.c index fc46d03..c13acb3 100644 --- a/gom/source.c +++ b/gom/source.c @@ -43,7 +43,6 @@ DEFINE_DESTROYFUNC(source, gom_first_source) DEFINE_ADDFUNC(source, XREF_SOUR) DEFINE_DELETEFUNC(source) DEFINE_GETXREFFUNC(source, XREF_SOUR) -DEFINE_MAKELINKFUNC(source, XREF_SOUR) DEFINE_REC_CB(source, sour_start) DEFINE_NULL_CB(source, sour_data_start) diff --git a/gom/submission.c b/gom/submission.c index 7e5260f..3ecee29 100644 --- a/gom/submission.c +++ b/gom/submission.c @@ -32,8 +32,6 @@ struct submission* gom_submission = NULL; -DEFINE_MAKELINKFUNC(submission, XREF_SUBN) - DEFINE_REC_CB(submission, subn_start) DEFINE_XREF_CB(submission, subn_subm_start, submitter, submitter) DEFINE_STRING_CB(submission, subn_famf_start, family_file) diff --git a/gom/submitter.c b/gom/submitter.c index bb2ef3e..29de511 100644 --- a/gom/submitter.c +++ b/gom/submitter.c @@ -39,7 +39,6 @@ DEFINE_DESTROYFUNC(submitter, gom_first_submitter) DEFINE_ADDFUNC(submitter, XREF_SUBM) DEFINE_DELETEFUNC(submitter) DEFINE_GETXREFFUNC(submitter, XREF_SUBM) -DEFINE_MAKELINKFUNC(submitter, XREF_SUBM) DEFINE_REC_CB(submitter, subm_start) DEFINE_STRING_CB(submitter, subm_name_start, name)