From bb2090762d3ae9355a04286c33b78d24736b783c Mon Sep 17 00:00:00 2001 From: Peter Verthez Date: Sun, 26 Jan 2003 16:33:45 +0000 Subject: [PATCH] Added functions to add, remove and move substructs. --- gom/association.c | 4 ++ gom/event.c | 4 ++ gom/family_link.c | 10 ++++ gom/func_template.h | 63 +++++++++++++++++++++++ gom/gom.c | 5 ++ gom/gom_internal.h | 1 + gom/lds_event.c | 4 ++ gom/multimedia_link.c | 4 ++ gom/note_sub.c | 4 ++ gom/personal_name.c | 4 ++ gom/source_citation.c | 10 ++++ gom/source_description.c | 4 ++ gom/source_event.c | 4 ++ gom/user_rec.c | 6 +++ gom/user_ref.c | 4 ++ include/gom.h | 107 +++++++++++++++++++++++++++++++++++++-- t/output/update_gom.ref | 13 ++++- t/src/update_gom.c | 35 ++++++++++--- 18 files changed, 274 insertions(+), 12 deletions(-) diff --git a/gom/association.c b/gom/association.c index 9398bbc..9e39209 100644 --- a/gom/association.c +++ b/gom/association.c @@ -58,6 +58,10 @@ Gedcom_ctxt sub_assoc_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(association) +DEFINE_SUB_ADDFUNC(association) +DEFINE_SUB_FINDFUNC(association) +DEFINE_SUB_REMOVEFUNC(association) +DEFINE_SUB_MOVEFUNC(association) DEFINE_STRING_CB(association, sub_assoc_rela_start, relation) diff --git a/gom/event.c b/gom/event.c index b946aa6..3b45643 100644 --- a/gom/event.c +++ b/gom/event.c @@ -126,6 +126,10 @@ Gedcom_ctxt sub_attr_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(event) +DEFINE_SUB_ADDFUNC(event) +DEFINE_SUB_FINDFUNC(event) +DEFINE_SUB_REMOVEFUNC(event) +DEFINE_SUB_MOVEFUNC(event) DEFINE_STRING_CB(event, sub_evt_type_start, type) DEFINE_DATE_CB(event, sub_evt_date_start, date) diff --git a/gom/family_link.c b/gom/family_link.c index cb5435b..4ea3fc1 100644 --- a/gom/family_link.c +++ b/gom/family_link.c @@ -84,6 +84,10 @@ Gedcom_ctxt sub_fam_link_pedi_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(family_link) +DEFINE_SUB_ADDFUNC(family_link) +DEFINE_SUB_FINDFUNC(family_link) +DEFINE_SUB_REMOVEFUNC(family_link) +DEFINE_SUB_MOVEFUNC(family_link) DEFINE_ADDFUNC2(family_link, note_sub, note) DEFINE_ADDFUNC2(family_link, user_data, extra) @@ -115,6 +119,12 @@ void CLEANFUNC(pedigree)(struct pedigree* ped) } } +DEFINE_SUB_MAKEFUNC(pedigree) +DEFINE_SUB_ADDFUNC(pedigree) +DEFINE_SUB_FINDFUNC(pedigree) +DEFINE_SUB_REMOVEFUNC(pedigree) +DEFINE_SUB_MOVEFUNC(pedigree) + void UNREFALLFUNC(family_link)(struct family_link* obj) { if (obj) { diff --git a/gom/func_template.h b/gom/func_template.h index 769debb..d5b22c5 100644 --- a/gom/func_template.h +++ b/gom/func_template.h @@ -31,9 +31,13 @@ #define CLEANFUNC(STRUCTTYPE) STRUCTTYPE ## _cleanup #define ADDFUNC(STRUCTTYPE) gom_new_ ## STRUCTTYPE #define SUB_SETFUNC(STRUCTTYPE) gom_set_new_ ## STRUCTTYPE +#define SUB_ADDFUNC(STRUCTTYPE) gom_add_new_ ## STRUCTTYPE #define UNREFALLFUNC(STRUCTTYPE) STRUCTTYPE ## _unref_all #define DELETEFUNC(STRUCTTYPE) gom_delete_ ## STRUCTTYPE #define SUB_DELETEFUNC(STRUCTTYPE) gom_delete_ ## STRUCTTYPE +#define SUB_FINDFUNC(STRUCTTYPE) find_ ## STRUCTTYPE +#define SUB_REMOVEFUNC(STRUCTTYPE) gom_remove_ ## STRUCTTYPE +#define SUB_MOVEFUNC(STRUCTTYPE) gom_move_ ## STRUCTTYPE #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) @@ -232,6 +236,16 @@ return obj; \ } +#define DEFINE_SUB_ADDFUNC(STRUCTTYPE) \ + struct STRUCTTYPE *SUB_ADDFUNC(STRUCTTYPE)(struct STRUCTTYPE** addto) \ + { \ + struct STRUCTTYPE *obj = NULL; \ + if (addto) { \ + MAKE_CHAIN_ELT(STRUCTTYPE, *addto, obj); \ + } \ + return obj; \ + } + #define DEFINE_DELETEFUNC(STRUCTTYPE) \ DECLARE_UNREFALLFUNC(STRUCTTYPE); \ int DELETEFUNC(STRUCTTYPE)(struct STRUCTTYPE* obj) \ @@ -260,6 +274,55 @@ return result; \ } +#define DEFINE_SUB_FINDFUNC(STRUCTTYPE) \ + struct STRUCTTYPE* SUB_FINDFUNC(STRUCTTYPE)(struct STRUCTTYPE** data, \ + struct STRUCTTYPE* obj) \ + { \ + struct STRUCTTYPE* result = NULL; \ + struct STRUCTTYPE* runner; \ + for (runner = *data ; runner ; runner = runner->next) { \ + if (runner == obj) { \ + result = runner; \ + break; \ + } \ + } \ + if (! result) \ + gom_find_error(#STRUCTTYPE); \ + return result; \ + } + +#define DEFINE_SUB_REMOVEFUNC(STRUCTTYPE) \ + int SUB_REMOVEFUNC(STRUCTTYPE) (struct STRUCTTYPE** data, \ + struct STRUCTTYPE* obj) \ + { \ + int result = 1; \ + if (data && obj) { \ + struct STRUCTTYPE* toremove = SUB_FINDFUNC(STRUCTTYPE)(data, obj); \ + if (toremove) { \ + UNLINK_CHAIN_ELT(STRUCTTYPE, *data, toremove); \ + CLEANFUNC(STRUCTTYPE)(toremove); \ + SAFE_FREE(toremove); \ + result = 0; \ + } \ + } \ + return result; \ + } + +#define DEFINE_SUB_MOVEFUNC(STRUCTTYPE) \ + int SUB_MOVEFUNC(STRUCTTYPE)(Gom_direction dir, struct STRUCTTYPE** data, \ + struct STRUCTTYPE* obj) \ + { \ + int result = 1; \ + if (data && obj) { \ + struct STRUCTTYPE* tomove = SUB_FINDFUNC(STRUCTTYPE)(data, obj); \ + if (tomove) { \ + MOVE_CHAIN_ELT(STRUCTTYPE, dir, *data, tomove); \ + result = 0; \ + } \ + } \ + return result; \ + } + #define DEFINE_ADDFUNC2(STRUCTTYPE,T2,FIELD) \ void ADDFUNC2(STRUCTTYPE,T2)(Gom_ctxt ctxt, struct T2* addobj) \ { \ diff --git a/gom/gom.c b/gom/gom.c index a3188ac..6ca25d2 100644 --- a/gom/gom.c +++ b/gom/gom.c @@ -223,6 +223,11 @@ void gom_move_error(const char* type) gedcom_warning(_("Could not move struct of type %s"), type); } +void gom_find_error(const char* type) +{ + gedcom_warning(_("Could not find struct of type %s in chain"), type); +} + void gom_default_callback (Gedcom_elt elt UNUSED, Gedcom_ctxt parent UNUSED, int level, char* tag, char* raw_value, int parsed_tag UNUSED) diff --git a/gom/gom_internal.h b/gom/gom_internal.h index 476cc9e..baa1195 100644 --- a/gom/gom_internal.h +++ b/gom/gom_internal.h @@ -77,6 +77,7 @@ 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 gom_find_error(const char* type); void unref_xref_value(struct xref_value *xref); int gom_write_xref_list(Gedcom_write_hndl hndl, diff --git a/gom/lds_event.c b/gom/lds_event.c index b922bd2..799e23d 100644 --- a/gom/lds_event.c +++ b/gom/lds_event.c @@ -67,6 +67,10 @@ Gedcom_ctxt sub_lds_event_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(lds_event) +DEFINE_SUB_ADDFUNC(lds_event) +DEFINE_SUB_FINDFUNC(lds_event) +DEFINE_SUB_REMOVEFUNC(lds_event) +DEFINE_SUB_MOVEFUNC(lds_event) DEFINE_STRING_CB(lds_event, sub_lds_event_stat_start, date_status) DEFINE_DATE_CB(lds_event, sub_lds_event_date_start, date) diff --git a/gom/multimedia_link.c b/gom/multimedia_link.c index 44e9a2d..fda4feb 100644 --- a/gom/multimedia_link.c +++ b/gom/multimedia_link.c @@ -80,6 +80,10 @@ Gedcom_ctxt sub_obje_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(multimedia_link) +DEFINE_SUB_ADDFUNC(multimedia_link) +DEFINE_SUB_FINDFUNC(multimedia_link) +DEFINE_SUB_REMOVEFUNC(multimedia_link) +DEFINE_SUB_MOVEFUNC(multimedia_link) DEFINE_STRING_CB(multimedia_link, sub_obje_form_start, form) DEFINE_STRING_CB(multimedia_link, sub_obje_titl_start, title) diff --git a/gom/note_sub.c b/gom/note_sub.c index e523b32..8104aae 100644 --- a/gom/note_sub.c +++ b/gom/note_sub.c @@ -132,6 +132,10 @@ void sub_note_end(_ELT_END_PARAMS_) } DEFINE_SUB_MAKEFUNC(note_sub) +DEFINE_SUB_ADDFUNC(note_sub) +DEFINE_SUB_FINDFUNC(note_sub) +DEFINE_SUB_REMOVEFUNC(note_sub) +DEFINE_SUB_MOVEFUNC(note_sub) DEFINE_ADDFUNC2(note_sub, source_citation, citation) DEFINE_ADDFUNC2(note_sub, user_data, extra) diff --git a/gom/personal_name.c b/gom/personal_name.c index bff72bd..686422f 100644 --- a/gom/personal_name.c +++ b/gom/personal_name.c @@ -62,6 +62,10 @@ Gedcom_ctxt sub_name_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(personal_name) +DEFINE_SUB_ADDFUNC(personal_name) +DEFINE_SUB_FINDFUNC(personal_name) +DEFINE_SUB_REMOVEFUNC(personal_name) +DEFINE_SUB_MOVEFUNC(personal_name) DEFINE_STRING_CB(personal_name, sub_name_npfx_start, prefix) DEFINE_STRING_CB(personal_name, sub_name_givn_start, given) diff --git a/gom/source_citation.c b/gom/source_citation.c index 64ad42b..3738636 100644 --- a/gom/source_citation.c +++ b/gom/source_citation.c @@ -133,6 +133,10 @@ Gedcom_ctxt sub_cit_text_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(source_citation) +DEFINE_SUB_ADDFUNC(source_citation) +DEFINE_SUB_FINDFUNC(source_citation) +DEFINE_SUB_REMOVEFUNC(source_citation) +DEFINE_SUB_MOVEFUNC(source_citation) DEFINE_STRING_CB(source_citation, sub_cit_page_start, page) DEFINE_STRING_CB(source_citation, sub_cit_even_start, event) @@ -182,6 +186,12 @@ void CLEANFUNC(text)(struct text* t) } } +DEFINE_SUB_MAKEFUNC(text) +DEFINE_SUB_ADDFUNC(text) +DEFINE_SUB_FINDFUNC(text) +DEFINE_SUB_REMOVEFUNC(text) +DEFINE_SUB_MOVEFUNC(text) + void UNREFALLFUNC(source_citation)(struct source_citation* obj) { if (obj) { diff --git a/gom/source_description.c b/gom/source_description.c index 71bd9e8..65e98ac 100644 --- a/gom/source_description.c +++ b/gom/source_description.c @@ -62,6 +62,10 @@ Gedcom_ctxt sub_sour_caln_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(source_description) +DEFINE_SUB_ADDFUNC(source_description) +DEFINE_SUB_FINDFUNC(source_description) +DEFINE_SUB_REMOVEFUNC(source_description) +DEFINE_SUB_MOVEFUNC(source_description) DEFINE_STRING_CB(source_description, sub_sour_caln_medi_start, media) diff --git a/gom/source_event.c b/gom/source_event.c index 6e353b9..319dbb7 100644 --- a/gom/source_event.c +++ b/gom/source_event.c @@ -62,6 +62,10 @@ Gedcom_ctxt sub_sour_even_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(source_event) +DEFINE_SUB_ADDFUNC(source_event) +DEFINE_SUB_FINDFUNC(source_event) +DEFINE_SUB_REMOVEFUNC(source_event) +DEFINE_SUB_MOVEFUNC(source_event) DEFINE_DATE_CB(source_event, sub_sour_even_date_start, date_period) DEFINE_STRING_CB(source_event, sub_sour_even_plac_start, jurisdiction) diff --git a/gom/user_rec.c b/gom/user_rec.c index 4dfd14f..ba1dc5e 100644 --- a/gom/user_rec.c +++ b/gom/user_rec.c @@ -241,6 +241,12 @@ Gedcom_ctxt user_elt_start(_ELT_PARAMS_) return (Gedcom_ctxt)result; } +DEFINE_SUB_MAKEFUNC(user_data) +DEFINE_SUB_ADDFUNC(user_data) +DEFINE_SUB_FINDFUNC(user_data) +DEFINE_SUB_REMOVEFUNC(user_data) +DEFINE_SUB_MOVEFUNC(user_data) + void user_rec_subscribe() { gedcom_subscribe_to_record(REC_USER, user_rec_start, def_rec_end); diff --git a/gom/user_ref.c b/gom/user_ref.c index 2c08e3a..1dac775 100644 --- a/gom/user_ref.c +++ b/gom/user_ref.c @@ -77,6 +77,10 @@ Gedcom_ctxt sub_user_ref_start(_ELT_PARAMS_) } DEFINE_SUB_MAKEFUNC(user_ref_number) +DEFINE_SUB_ADDFUNC(user_ref_number) +DEFINE_SUB_FINDFUNC(user_ref_number) +DEFINE_SUB_REMOVEFUNC(user_ref_number) +DEFINE_SUB_MOVEFUNC(user_ref_number) DEFINE_STRING_CB(user_ref_number, sub_user_ref_type_start, type) diff --git a/include/gom.h b/include/gom.h index fbc5e39..85997e4 100644 --- a/include/gom.h +++ b/include/gom.h @@ -482,6 +482,7 @@ typedef enum _DIR { } Gom_direction; struct xref_value* gom_set_xref(struct xref_value** data, const char* xref); + struct xref_list* gom_add_xref(struct xref_list** data, const char* xref); int gom_remove_xref(struct xref_list** data, const char* xref); int gom_move_xref(Gom_direction dir, struct xref_list** data, @@ -490,12 +491,112 @@ int gom_move_xref(Gom_direction dir, struct xref_list** data, struct address* gom_set_new_address(struct address** obj); int gom_delete_address(struct address** obj); -struct place* gom_set_new_place(struct place** obj); -int gom_delete_place(struct place** obj); - +struct association* gom_add_new_association(struct association** data); +int gom_remove_association(struct association** data, + struct association* obj); +int gom_move_association(Gom_direction dir, + struct association** data, + struct association* obj); + struct change_date* gom_set_new_change_date(struct change_date** obj); int gom_delete_change_date(struct change_date** obj); int gom_update_timestamp(struct change_date** obj, time_t t); + +struct event* gom_add_new_event(struct event** data); +int gom_remove_event(struct event** data, struct event* obj); +int gom_move_event(Gom_direction dir, struct event** data, + struct event* obj); + +struct family_link* gom_add_new_family_link(struct family_link** data); +int gom_remove_family_link(struct family_link** data, + struct family_link* obj); +int gom_move_family_link(Gom_direction dir, + struct family_link** data, + struct family_link* obj); + +struct lds_event* gom_add_new_lds_event(struct lds_event** data); +int gom_remove_lds_event(struct lds_event** data, + struct lds_event* obj); +int gom_move_lds_event(Gom_direction dir, + struct lds_event** data, + struct lds_event* obj); + +struct multimedia_link* + gom_add_new_multimedia_link(struct multimedia_link** data); +int gom_remove_multimedia_link(struct multimedia_link** data, + struct multimedia_link* obj); +int gom_move_multimedia_link(Gom_direction dir, + struct multimedia_link** data, + struct multimedia_link* obj); + +struct note_sub* gom_add_new_note_sub(struct note_sub** data); +int gom_remove_note_sub(struct note_sub** data, + struct note_sub* obj); +int gom_move_note_sub(Gom_direction dir, + struct note_sub** data, + struct note_sub* obj); + +struct pedigree* gom_add_new_pedigree(struct pedigree** data); +int gom_remove_pedigree(struct pedigree** data, + struct pedigree* obj); +int gom_move_pedigree(Gom_direction dir, + struct pedigree** data, + struct pedigree* obj); + +struct personal_name* gom_add_new_personal_name(struct personal_name** data); +int gom_remove_personal_name(struct personal_name** data, + struct personal_name* obj); +int gom_move_personal_name(Gom_direction dir, + struct personal_name** data, + struct personal_name* obj); + +struct place* gom_set_new_place(struct place** obj); +int gom_delete_place(struct place** obj); + +struct source_citation* + gom_add_new_source_citation(struct source_citation** data); +int gom_remove_source_citation(struct source_citation** data, + struct source_citation* obj); +int gom_move_source_citation(Gom_direction dir, + struct source_citation** data, + struct source_citation* obj); + +struct source_description* + gom_add_new_source_description(struct source_description** data); +int gom_remove_source_description(struct source_description** data, + struct source_description* obj); +int gom_move_source_description(Gom_direction dir, + struct source_description** data, + struct source_description* obj); + +struct source_event* gom_add_new_source_event(struct source_event** data); +int gom_remove_source_event(struct source_event** data, + struct source_event* obj); +int gom_move_source_event(Gom_direction dir, + struct source_event** data, + struct source_event* obj); + +struct text* gom_add_new_text(struct text** data); +int gom_remove_text(struct text** data, + struct text* obj); +int gom_move_text(Gom_direction dir, + struct text** data, + struct text* obj); + +struct user_data* gom_add_new_user_data(struct user_data** data); +int gom_remove_user_data(struct user_data** data, + struct user_data* obj); +int gom_move_user_data(Gom_direction dir, + struct user_data** data, + struct user_data* obj); + +struct user_ref_number* + gom_add_new_user_ref_number(struct user_ref_number** data); +int gom_remove_user_ref_number(struct user_ref_number** data, + struct user_ref_number* obj); +int gom_move_user_ref_number(Gom_direction dir, + struct user_ref_number** data, + struct user_ref_number* obj); #ifdef __cplusplus } diff --git a/t/output/update_gom.ref b/t/output/update_gom.ref index 9f486e5..26f74ff 100644 --- a/t/output/update_gom.ref +++ b/t/output/update_gom.ref @@ -283,7 +283,18 @@ change date: 0x User data: 0x === INDIVIDUAL (@IND2@) === Restriction notice: '(null)' -names: 0x +names: + Name: + Name: 'Testname' + Prefix: '(null)' + Given: '(null)' + Nickname: '(null)' + Surname prefix: '(null)' + Surname: '(null)' + Suffix: '(null)' + citations: 0x + notes: 0x + User data: 0x Sex: '(null)' Individual events: 0x Individual attributes: 0x diff --git a/t/src/update_gom.c b/t/src/update_gom.c index dc48749..a4e0b0e 100644 --- a/t/src/update_gom.c +++ b/t/src/update_gom.c @@ -213,7 +213,7 @@ int test_date_functions() return 0; } -int test_record_add_delete_functions() +int test_add_delete_functions() { struct family* fam1; struct individual *ind1, *ind2, *ind3, *ind4; @@ -226,6 +226,7 @@ int test_record_add_delete_functions() struct user_rec* user1; struct xref_value* xr; struct xref_list* xrl; + struct personal_name* name; int result; char* value; const char* new_nr_of_children = "3"; @@ -301,22 +302,40 @@ int test_record_add_delete_functions() if (!xrl) return 124; result = gom_move_xref(MOVE_UP, &(fam1->children), ind4->xrefstr); - if (result != 0) return 127; + if (result != 0) return 125; result = gom_move_xref(MOVE_UP, &(fam1->children), ind4->xrefstr); - if (result != 0) return 128; + if (result != 0) return 126; result = gom_move_xref(MOVE_UP, &(fam1->children), ind4->xrefstr); - if (result != 0) return 129; + if (result != 0) return 127; result = gom_move_xref(MOVE_DOWN, &(fam1->children), ind4->xrefstr); - if (result != 0) return 130; + if (result != 0) return 128; result = gom_remove_xref(&(fam1->children), ind3->xrefstr); - if (result != 0) return 125; + if (result != 0) return 129; result = gom_remove_xref(&(fam1->children), ind4->xrefstr); - if (result != 0) return 126; + if (result != 0) return 130; + + name = gom_add_new_personal_name(&(ind2->name)); + if (name == NULL) return 131; + + value = gom_set_string(&(name->name), "Testname"); + if (value == NULL) return 132; + + name = gom_add_new_personal_name(&(ind2->name)); + if (name == NULL) return 133; + + value = gom_set_string(&(name->name), "Testname 2"); + if (value == NULL) return 134; + + result = gom_move_personal_name(MOVE_UP, &(ind2->name), name); + if (result != 0) return 135; + + result = gom_remove_personal_name(&(ind2->name), name); + if (result != 0) return 136; output(1, "Intermediate output:\n"); show_data(); @@ -406,7 +425,7 @@ int main(int argc, char* argv[]) if (result == 0) result |= test_date_functions(); if (result == 0) - result |= test_record_add_delete_functions(); + result |= test_add_delete_functions(); if (result == 0) { output(1, "Test succeeded\n"); } -- 2.30.2