X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gom%2Fassociation.c;h=2a5bf39c1a403b3ab22052844bceff805616b389;hb=1568cb8ab16ecc1984d0d00b8ae8189c32508b08;hp=b42d481af54a6e6c050b2aec787ac6204482eb55;hpb=7ea4ef8cae7b52f2bf66371a5e7b493cbd12900e;p=gedcom-parse.git diff --git a/gom/association.c b/gom/association.c index b42d481..2a5bf39 100644 --- a/gom/association.c +++ b/gom/association.c @@ -35,34 +35,55 @@ Gedcom_ctxt sub_assoc_start(_ELT_PARAMS_) { Gom_ctxt ctxt = (Gom_ctxt)parent; - struct association *assoc = NULL; + Gom_ctxt result = NULL; - if (ctxt) { + if (!ctxt) + NO_CONTEXT; + else { + struct association *assoc; assoc = (struct association *)malloc(sizeof(struct association)); - memset (assoc, 0, sizeof(struct association)); - assoc->to = GEDCOM_XREF_PTR(parsed_value); - - switch (ctxt->ctxt_type) { - case REC_INDI: - individual_add_association(ctxt, assoc); - default: - UNEXPECTED_CONTEXT(ctxt->ctxt_type); + if (! assoc) + MEMORY_ERROR; + else { + memset (assoc, 0, sizeof(struct association)); + assoc->to = GEDCOM_XREF_PTR(parsed_value); + + switch (ctxt->ctxt_type) { + case REC_INDI: + individual_add_association(ctxt, assoc); + default: + UNEXPECTED_CONTEXT(ctxt->ctxt_type); + } + result = MAKE_GOM_CTXT(elt, association, assoc); } } - return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, association, assoc); + return (Gedcom_ctxt)result; } STRING_CB(association, sub_assoc_rela_start, relation) Gedcom_ctxt sub_assoc_type_start(_ELT_PARAMS_) { - char *str = GEDCOM_STRING(parsed_value); - struct association *obj = SAFE_CTXT_CAST(association, (Gom_ctxt)parent); - if (obj) - obj->type = strdup(str); - set_xref_type(obj->to, str); - return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, association, obj); + Gom_ctxt ctxt = (Gom_ctxt)parent; + Gom_ctxt result = NULL; + + if (! ctxt) + NO_CONTEXT; + else { + struct association *obj = SAFE_CTXT_CAST(association, ctxt); + char *str = GEDCOM_STRING(parsed_value); + if (obj) { + obj->type = strdup(str); + if (! obj->type) + MEMORY_ERROR; + else { + set_xref_type(obj->to, str); + result = MAKE_GOM_CTXT(elt, association, obj); + } + } + } + return (Gedcom_ctxt)result; } void association_subscribe() @@ -77,19 +98,22 @@ void association_subscribe() void association_add_note(Gom_ctxt ctxt, struct note_sub* note) { struct association *assoc = SAFE_CTXT_CAST(association, ctxt); - LINK_CHAIN_ELT(note_sub, assoc->note, note) + if (assoc) + LINK_CHAIN_ELT(note_sub, assoc->note, note); } void association_add_citation(Gom_ctxt ctxt, struct source_citation* cit) { struct association *assoc = SAFE_CTXT_CAST(association, ctxt); - LINK_CHAIN_ELT(source_citation, assoc->citation, cit) + if (assoc) + LINK_CHAIN_ELT(source_citation, assoc->citation, cit); } void association_add_user_data(Gom_ctxt ctxt, struct user_data* data) { struct association *obj = SAFE_CTXT_CAST(association, ctxt); - LINK_CHAIN_ELT(user_data, obj->extra, data) + if (obj) + LINK_CHAIN_ELT(user_data, obj->extra, data); } void association_cleanup(struct association* assoc) @@ -97,8 +121,36 @@ void association_cleanup(struct association* assoc) if (assoc) { SAFE_FREE(assoc->type); SAFE_FREE(assoc->relation); - DESTROY_CHAIN_ELTS(note_sub, assoc->note, note_sub_cleanup) - DESTROY_CHAIN_ELTS(source_citation, assoc->citation, citation_cleanup) - DESTROY_CHAIN_ELTS(user_data, assoc->extra, user_data_cleanup) + DESTROY_CHAIN_ELTS(note_sub, assoc->note, note_sub_cleanup); + DESTROY_CHAIN_ELTS(source_citation, assoc->citation, citation_cleanup); + DESTROY_CHAIN_ELTS(user_data, assoc->extra, user_data_cleanup); + } +} + +int write_associations(Gedcom_write_hndl hndl, int parent, + struct association *assoc) +{ + int result = 0; + struct association* obj; + + if (!assoc) return 1; + + for (obj = assoc; obj; obj = obj->next) { + result |= gedcom_write_element_xref(hndl, ELT_SUB_ASSO, 0, parent, + obj->to); + if (obj->type) + result |= gedcom_write_element_str(hndl, ELT_SUB_ASSO_TYPE, 0, parent, + obj->type); + if (obj->relation) + result |= gedcom_write_element_str(hndl, ELT_SUB_ASSO_RELA, 0, parent, + obj->relation); + if (obj->citation) + result |= write_citations(hndl, ELT_SUB_ASSO, obj->citation); + if (obj->note) + result |= write_note_subs(hndl, ELT_SUB_ASSO, obj->note); + if (obj->extra) + result |= write_user_data(hndl, obj->extra); } + + return result; }