Functions for moving an xref in a list.
[gedcom-parse.git] / gom / note_sub.c
index 4f3aaec00ccca544b33c344394d2387a980904ea..2ff76db751e6eca2b62c999a446037e6e877d837 100644 (file)
 Gedcom_ctxt sub_note_start(_ELT_PARAMS_)
 {
   Gom_ctxt ctxt = (Gom_ctxt)parent;
-  struct note_sub *note = NULL;
-
-  if (ctxt) {
-    note = (struct note_sub *)malloc(sizeof(struct note_sub));
-    memset (note, 0, sizeof(struct note_sub));
-    if (GEDCOM_IS_STRING(parsed_value))
-      note->text = strdup(GEDCOM_STRING(parsed_value));
-    else if (GEDCOM_IS_XREF_PTR(parsed_value))
-      note->reference = GEDCOM_XREF_PTR(parsed_value);
-
-    switch (ctxt->ctxt_type) {
-      case ELT_SUB_PLAC:
-       place_add_note(ctxt, note); 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_note(ctxt, note); break;
-      case ELT_SUB_SOUR:
-       citation_add_note(ctxt, note); break;
-      case ELT_SUB_MULTIM_OBJE:
-       multimedia_link_add_note(ctxt, note); break;
-      case ELT_SUB_LSS_SLGS:
-      case ELT_SUB_LIO_BAPL:
-      case ELT_SUB_LIO_SLGC:
-       lds_event_add_note(ctxt, note); break;
-      case REC_FAM:
-       family_add_note(ctxt, note); break;
-      case ELT_SUB_CHAN:
-       change_date_add_note(ctxt, note); break;
-      case ELT_SUB_PERS_NAME:
-       name_add_note(ctxt, note); break;
-      case ELT_SUB_FAMC: 
-      case ELT_SUB_FAMS:
-       family_link_add_note(ctxt, note); break;
-      case ELT_SUB_ASSO:
-       association_add_note(ctxt, note); break;
-      case REC_INDI:
-       individual_add_note(ctxt, note); break;
-      case REC_OBJE:
-       multimedia_add_note(ctxt, note); break;
-      case REC_REPO:
-       repository_add_note(ctxt, note); break;
-      case ELT_SOUR_DATA:
-       source_add_note_to_data(ctxt, note); break;
-      case ELT_SUB_REPO:
-       source_add_note_to_repo(ctxt, note); break;
-      case REC_SOUR:
-       source_add_note(ctxt, note); break;
-      default:
-       UNEXPECTED_CONTEXT(ctxt->ctxt_type);
+  Gom_ctxt result = NULL;
+
+  if (! ctxt)
+    NO_CONTEXT;
+  else {
+    struct note_sub *note = SUB_MAKEFUNC(note_sub)();
+    if (note) {
+      if (GEDCOM_IS_XREF_PTR(parsed_value))
+       note->reference = GEDCOM_XREF_PTR(parsed_value);
+
+      switch (ctxt->ctxt_type) {
+       case ELT_SUB_PLAC:
+         ADDFUNC2(place,note_sub)(ctxt, note); 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:
+         ADDFUNC2(event,note_sub)(ctxt, note); break;
+       case ELT_SUB_SOUR:
+         ADDFUNC2(source_citation,note_sub)(ctxt, note); break;
+       case ELT_SUB_MULTIM_OBJE:
+         ADDFUNC2(multimedia_link,note_sub)(ctxt, note); break;
+       case ELT_SUB_LSS_SLGS:
+       case ELT_SUB_LIO_BAPL:
+       case ELT_SUB_LIO_SLGC:
+         ADDFUNC2(lds_event,note_sub)(ctxt, note); break;
+       case REC_FAM:
+         ADDFUNC2(family,note_sub)(ctxt, note); break;
+       case ELT_SUB_CHAN:
+         ADDFUNC2(change_date,note_sub)(ctxt, note); break;
+       case ELT_SUB_PERS_NAME:
+         ADDFUNC2(personal_name,note_sub)(ctxt, note); break;
+       case ELT_SUB_FAMC: 
+       case ELT_SUB_FAMS:
+         ADDFUNC2(family_link,note_sub)(ctxt, note); break;
+       case ELT_SUB_ASSO:
+         ADDFUNC2(association,note_sub)(ctxt, note); break;
+       case REC_INDI:
+         ADDFUNC2(individual,note_sub)(ctxt, note); break;
+       case REC_OBJE:
+         ADDFUNC2(multimedia,note_sub)(ctxt, note); break;
+       case REC_REPO:
+         ADDFUNC2(repository,note_sub)(ctxt, note); break;
+       case ELT_SOUR_DATA:
+         source_add_note_to_data(ctxt, note); break;
+       case ELT_SUB_REPO:
+         source_add_note_to_repo(ctxt, note); break;
+       case REC_SOUR:
+         ADDFUNC2(source,note_sub)(ctxt, note); break;
+       default:
+         UNEXPECTED_CONTEXT(ctxt->ctxt_type);
+      }
+      result = MAKE_GOM_CTXT(elt, note_sub, note);
     }
   }
 
-  return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, note_sub, note);
+  return (Gedcom_ctxt)result;
 }
 
+DEFINE_SUB_MAKEFUNC(note_sub)
+     
+DEFINE_STRING_END_CB(note_sub, sub_note_end, text)
+
+DEFINE_ADDFUNC2(note_sub, source_citation, citation)
+DEFINE_ADDFUNC2(note_sub, user_data, extra)
+     
 void note_sub_subscribe()
 {
-  gedcom_subscribe_to_element(ELT_SUB_NOTE, sub_note_start, def_elt_end);
+  gedcom_subscribe_to_element(ELT_SUB_NOTE, sub_note_start, sub_note_end);
 }
 
-void note_sub_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
+void UNREFALLFUNC(note_sub)(struct note_sub* obj)
 {
-  struct note_sub *note = SAFE_CTXT_CAST(note_sub, ctxt);
-  LINK_CHAIN_ELT(source_citation, note->citation, cit)    
+  if (obj) {
+    struct note_sub* runner;
+    for (runner = obj; runner; runner = runner->next) {
+      unref_xref_value(runner->reference);
+      UNREFALLFUNC(source_citation)(runner->citation);
+      UNREFALLFUNC(user_data)(runner->extra);
+    }
+  }
 }
 
-void note_sub_add_to_note(NL_TYPE type, Gom_ctxt ctxt, char* str)
+void CLEANFUNC(note_sub)(struct note_sub* note)
 {
-  struct note_sub *note = SAFE_CTXT_CAST(note_sub, ctxt);
-  note->text = concat_strings (type, note->text, str);
+  if (note) {
+    SAFE_FREE(note->text);
+    DESTROY_CHAIN_ELTS(source_citation, note->citation);
+    DESTROY_CHAIN_ELTS(user_data, note->extra);
+  }
 }
 
-void note_sub_add_user_data(Gom_ctxt ctxt, struct user_data* data)
+int write_note_subs(Gedcom_write_hndl hndl, int parent, struct note_sub* note)
 {
-  struct note_sub *obj = SAFE_CTXT_CAST(note_sub, ctxt);
-  LINK_CHAIN_ELT(user_data, obj->extra, data)
-}
+  int result = 0;
+  struct note_sub* obj;
 
-void note_sub_cleanup(struct note_sub* note)
-{
-  if (note) {
-    SAFE_FREE(note->text);
-    DESTROY_CHAIN_ELTS(source_citation, note->citation, citation_cleanup)
-    DESTROY_CHAIN_ELTS(user_data, note->extra, user_data_cleanup)
+  if (!note) return 1;
+
+  for (obj = note; obj; obj = obj->next) {
+    if (obj->reference) {
+      result |= gedcom_write_element_xref(hndl, ELT_SUB_NOTE, 0,
+                                         parent, obj->reference);
+    }
+    else {
+      result |= gedcom_write_element_str(hndl, ELT_SUB_NOTE, 0,
+                                        parent, obj->text);
+    }
+    if (obj->citation)
+      result |= write_citations(hndl, ELT_SUB_NOTE, obj->citation);
+    if (obj->extra)
+      result |= write_user_data(hndl, obj->extra);
   }
+
+  return result;
 }