Added functions to add, remove and move substructs.
[gedcom-parse.git] / gom / association.c
index b42d481af54a6e6c050b2aec787ac6204482eb55..9e392099c5fe73daed06146a375ed7e339952c11 100644 (file)
 Gedcom_ctxt sub_assoc_start(_ELT_PARAMS_)
 {
   Gom_ctxt ctxt = (Gom_ctxt)parent;
-  struct association *assoc = NULL;
-
-  if (ctxt) {
-    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);
+  Gom_ctxt result = NULL;
+
+  if (!ctxt)
+    NO_CONTEXT;
+  else {
+    struct association *assoc = SUB_MAKEFUNC(association)();
+    if (assoc) {
+      assoc->to = GEDCOM_XREF_PTR(parsed_value);
+      
+      switch (ctxt->ctxt_type) {
+       case REC_INDI:
+         ADDFUNC2(individual,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)
+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)
+
+DEFINE_ADDFUNC2(association, note_sub, note)
+DEFINE_ADDFUNC2(association, source_citation, citation)
+DEFINE_ADDFUNC2(association, user_data, extra)
      
 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()
@@ -74,31 +101,54 @@ void association_subscribe()
                              def_elt_end);
 }
 
-void association_add_note(Gom_ctxt ctxt, struct note_sub* note)
+void UNREFALLFUNC(association)(struct association *obj)
 {
-  struct association *assoc = SAFE_CTXT_CAST(association, ctxt);
-  LINK_CHAIN_ELT(note_sub, assoc->note, note)
+  if (obj) {
+    struct association* runner;
+    for (runner = obj; runner; runner = runner->next) {
+      unref_xref_value(runner->to);
+      UNREFALLFUNC(source_citation)(runner->citation);
+      UNREFALLFUNC(note_sub)(runner->note);
+      UNREFALLFUNC(user_data)(runner->extra);
+    }
+  }
 }
 
-void association_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
+void CLEANFUNC(association)(struct association* assoc)
 {
-  struct association *assoc = SAFE_CTXT_CAST(association, ctxt);
-  LINK_CHAIN_ELT(source_citation, assoc->citation, cit)
+  if (assoc) {
+    SAFE_FREE(assoc->type);
+    SAFE_FREE(assoc->relation);
+    DESTROY_CHAIN_ELTS(note_sub, assoc->note);
+    DESTROY_CHAIN_ELTS(source_citation, assoc->citation);
+    DESTROY_CHAIN_ELTS(user_data, assoc->extra);
+  }
 }
 
-void association_add_user_data(Gom_ctxt ctxt, struct user_data* data)
+int write_associations(Gedcom_write_hndl hndl, int parent,
+                      struct association *assoc)
 {
-  struct association *obj = SAFE_CTXT_CAST(association, ctxt);
-  LINK_CHAIN_ELT(user_data, obj->extra, data)
-}
+  int result = 0;
+  struct association* obj;
 
-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)
+  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;
 }