X-Git-Url: https://git.dlugolecki.net.pl/?a=blobdiff_plain;f=gom%2Fsource_citation.c;h=17c2b6dcb2d1b19ff08e47a1f70ce192db4c89c5;hb=1568cb8ab16ecc1984d0d00b8ae8189c32508b08;hp=470812925bf041d59aba517b1cad8243ab77076f;hpb=7ea4ef8cae7b52f2bf66371a5e7b493cbd12900e;p=gedcom-parse.git diff --git a/gom/source_citation.c b/gom/source_citation.c index 4708129..17c2b6d 100644 --- a/gom/source_citation.c +++ b/gom/source_citation.c @@ -42,60 +42,99 @@ Gedcom_ctxt sub_citation_start(_ELT_PARAMS_) { Gom_ctxt ctxt = (Gom_ctxt)parent; - struct source_citation *cit = NULL; - - if (ctxt) { - cit = (struct source_citation *)malloc(sizeof(struct source_citation)); - memset (cit, 0, sizeof(struct source_citation)); - if (GEDCOM_IS_STRING(parsed_value)) - cit->description = strdup(GEDCOM_STRING(parsed_value)); - else if (GEDCOM_IS_XREF_PTR(parsed_value)) - cit->reference = GEDCOM_XREF_PTR(parsed_value); - - switch (ctxt->ctxt_type) { - case ELT_SUB_PLAC: - place_add_citation(ctxt, cit); break; - case ELT_SUB_FAM_EVT: - case ELT_SUB_FAM_EVT_EVEN: - case ELT_SUB_INDIV_ATTR: - case ELT_SUB_INDIV_RESI: - case ELT_SUB_INDIV_BIRT: - case ELT_SUB_INDIV_GEN: - case ELT_SUB_INDIV_ADOP: - case ELT_SUB_INDIV_EVEN: - event_add_citation(ctxt, cit); break; - case ELT_SUB_NOTE: - note_sub_add_citation(ctxt, cit); break; - case ELT_SUB_LSS_SLGS: - case ELT_SUB_LIO_BAPL: - case ELT_SUB_LIO_SLGC: - lds_event_add_citation(ctxt, cit); break; - case REC_FAM: - family_add_citation(ctxt, cit); break; - case ELT_SUB_PERS_NAME: - name_add_citation(ctxt, cit); break; - case REC_INDI: - individual_add_citation(ctxt, cit); break; - case ELT_SUB_ASSO: - association_add_citation(ctxt, cit); break; - case REC_NOTE: - note_add_citation(ctxt, cit); break; - default: - UNEXPECTED_CONTEXT(ctxt->ctxt_type); + Gom_ctxt result = NULL; + + if (! ctxt) + NO_CONTEXT; + else { + struct source_citation *cit + = (struct source_citation *)malloc(sizeof(struct source_citation)); + + if (! cit) + MEMORY_ERROR; + else { + memset (cit, 0, sizeof(struct source_citation)); + if (GEDCOM_IS_XREF_PTR(parsed_value)) + cit->reference = GEDCOM_XREF_PTR(parsed_value); + + switch (ctxt->ctxt_type) { + case ELT_SUB_PLAC: + place_add_citation(ctxt, cit); break; + case ELT_SUB_FAM_EVT: + case ELT_SUB_FAM_EVT_EVEN: + case ELT_SUB_INDIV_ATTR: + case ELT_SUB_INDIV_RESI: + case ELT_SUB_INDIV_BIRT: + case ELT_SUB_INDIV_GEN: + case ELT_SUB_INDIV_ADOP: + case ELT_SUB_INDIV_EVEN: + event_add_citation(ctxt, cit); break; + case ELT_SUB_NOTE: + note_sub_add_citation(ctxt, cit); break; + case ELT_SUB_LSS_SLGS: + case ELT_SUB_LIO_BAPL: + case ELT_SUB_LIO_SLGC: + lds_event_add_citation(ctxt, cit); break; + case REC_FAM: + family_add_citation(ctxt, cit); break; + case ELT_SUB_PERS_NAME: + name_add_citation(ctxt, cit); break; + case REC_INDI: + individual_add_citation(ctxt, cit); break; + case ELT_SUB_ASSO: + association_add_citation(ctxt, cit); break; + case REC_NOTE: + note_add_citation(ctxt, cit); break; + default: + UNEXPECTED_CONTEXT(ctxt->ctxt_type); + } + result = MAKE_GOM_CTXT(elt, source_citation, cit); } } - return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, source_citation, cit); + return (Gedcom_ctxt)result; +} + +void sub_citation_end(_ELT_END_PARAMS_) +{ + Gom_ctxt ctxt = (Gom_ctxt)self; + + if (! ctxt) + NO_CONTEXT; + else { + if (GEDCOM_IS_STRING(parsed_value)) { + struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt); + if (cit) { + char *str = GEDCOM_STRING(parsed_value); + char *newvalue = strdup(str); + if (! newvalue) + MEMORY_ERROR; + else + cit->description = newvalue; + } + } + destroy_gom_ctxt(ctxt); + } } Gedcom_ctxt sub_cit_text_start(_ELT_PARAMS_) { Gom_ctxt ctxt = (Gom_ctxt)parent; - struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt); - struct text *t; - MAKE_CHAIN_ELT(text, cit->text, t); - t->text = strdup(GEDCOM_STRING(parsed_value)); - return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, text, t); + Gom_ctxt result = NULL; + + if (! ctxt) + NO_CONTEXT; + else { + struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt); + if (cit) { + struct text *t = NULL; + MAKE_CHAIN_ELT(text, cit->text, t); + if (t) + result = MAKE_GOM_CTXT(elt, text, t); + } + } + + return (Gedcom_ctxt)result; } STRING_CB(source_citation, sub_cit_page_start, page) @@ -104,10 +143,12 @@ STRING_CB(source_citation, sub_cit_even_role_start, role) NULL_CB(source_citation, sub_cit_data_start) DATE_CB(source_citation, sub_cit_data_date_start, date) STRING_CB(source_citation, sub_cit_quay_start, quality) +STRING_END_CB(text, sub_cit_text_end, text) void citation_subscribe() { - gedcom_subscribe_to_element(ELT_SUB_SOUR, sub_citation_start, def_elt_end); + gedcom_subscribe_to_element(ELT_SUB_SOUR, sub_citation_start, + sub_citation_end); gedcom_subscribe_to_element(ELT_SUB_SOUR_PAGE, sub_cit_page_start, def_elt_end); gedcom_subscribe_to_element(ELT_SUB_SOUR_EVEN, sub_cit_even_start, @@ -119,7 +160,7 @@ void citation_subscribe() gedcom_subscribe_to_element(ELT_SUB_SOUR_DATA_DATE, sub_cit_data_date_start, def_elt_end); gedcom_subscribe_to_element(ELT_SUB_SOUR_TEXT, sub_cit_text_start, - def_elt_end); + sub_cit_text_end); gedcom_subscribe_to_element(ELT_SUB_SOUR_QUAY, sub_cit_quay_start, def_elt_end); } @@ -127,31 +168,22 @@ void citation_subscribe() void citation_add_note(Gom_ctxt ctxt, struct note_sub* note) { struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt); - LINK_CHAIN_ELT(note_sub, cit->note, note) + if (cit) + LINK_CHAIN_ELT(note_sub, cit->note, note); } void citation_add_mm_link(Gom_ctxt ctxt, struct multimedia_link* mm) { struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt); - LINK_CHAIN_ELT(multimedia_link, cit->mm_link, mm) -} - -void citation_add_to_desc(NL_TYPE type, Gom_ctxt ctxt, char* str) -{ - struct source_citation *cit = SAFE_CTXT_CAST(source_citation, ctxt); - cit->description = concat_strings (type, cit->description, str); -} - -void citation_add_to_text(NL_TYPE type, Gom_ctxt ctxt, char* str) -{ - struct text *t = SAFE_CTXT_CAST(text, ctxt); - t->text = concat_strings (type, t->text, str); + if (cit) + LINK_CHAIN_ELT(multimedia_link, cit->mm_link, mm); } void citation_add_user_data(Gom_ctxt ctxt, struct user_data* data) { struct source_citation *obj = SAFE_CTXT_CAST(source_citation, ctxt); - LINK_CHAIN_ELT(user_data, obj->extra, data) + if (obj) + LINK_CHAIN_ELT(user_data, obj->extra, data); } void text_cleanup(struct text* t) @@ -169,10 +201,77 @@ void citation_cleanup(struct source_citation* cit) SAFE_FREE(cit->event); SAFE_FREE(cit->role); SAFE_FREE(cit->date); - DESTROY_CHAIN_ELTS(text, cit->text, text_cleanup) + DESTROY_CHAIN_ELTS(text, cit->text, text_cleanup); SAFE_FREE(cit->quality); - DESTROY_CHAIN_ELTS(multimedia_link, cit->mm_link, multimedia_link_cleanup) - DESTROY_CHAIN_ELTS(note_sub, cit->note, note_sub_cleanup) - DESTROY_CHAIN_ELTS(user_data, cit->extra, user_data_cleanup) + DESTROY_CHAIN_ELTS(multimedia_link, cit->mm_link, multimedia_link_cleanup); + DESTROY_CHAIN_ELTS(note_sub, cit->note, note_sub_cleanup); + DESTROY_CHAIN_ELTS(user_data, cit->extra, user_data_cleanup); + } +} + +int write_texts(Gedcom_write_hndl hndl, int parent, struct text* t) +{ + int result = 0; + struct text* obj; + + if (!t) return 1; + + for (obj = t; obj; obj = obj->next) { + result |= gedcom_write_element_str(hndl, ELT_SUB_SOUR_TEXT, 0, parent, + obj->text); + if (obj->extra) + result |= write_user_data(hndl, obj->extra); + } + + return result; +} + +int write_citations(Gedcom_write_hndl hndl, int parent, + struct source_citation* cit) +{ + int result = 0; + struct source_citation* obj; + + if (!cit) return 1; + + for (obj = cit; obj; obj = obj->next) { + if (obj->reference) { + result |= gedcom_write_element_xref(hndl, ELT_SUB_SOUR, 0, parent, + obj->reference); + if (obj->page) + result |= gedcom_write_element_str(hndl, ELT_SUB_SOUR_PAGE, 0, + ELT_SUB_SOUR, obj->page); + if (obj->event) + result |= gedcom_write_element_str(hndl, ELT_SUB_SOUR_EVEN, 0, + ELT_SUB_SOUR, obj->event); + if (obj->role) + result |= gedcom_write_element_str(hndl, ELT_SUB_SOUR_EVEN_ROLE, 0, + ELT_SUB_SOUR_EVEN, obj->role); + if (obj->date || obj->text) + result |= gedcom_write_element_str(hndl, ELT_SUB_SOUR_DATA, 0, + ELT_SUB_SOUR, NULL); + if (obj->date) + result |= gedcom_write_element_date(hndl, ELT_SUB_SOUR_DATA_DATE, 0, + ELT_SUB_SOUR_DATA, obj->date); + if (obj->text) + result |= write_texts(hndl, ELT_SUB_SOUR_DATA, obj->text); + if (obj->quality) + result |= gedcom_write_element_str(hndl, ELT_SUB_SOUR_QUAY, 0, + ELT_SUB_SOUR, obj->quality); + if (obj->mm_link) + result |= write_multimedia_links(hndl, ELT_SUB_SOUR, obj->mm_link); + } + else { + result |= gedcom_write_element_str(hndl, ELT_SUB_SOUR, 0, parent, + obj->description); + if (obj->text) + result |= write_texts(hndl, ELT_SUB_SOUR, obj->text); + } + if (obj->note) + result |= write_note_subs(hndl, ELT_SUB_SOUR, obj->note); + if (obj->extra) + result |= write_user_data(hndl, obj->extra); } + + return result; }