More thorough error handling.
[gedcom-parse.git] / gom / association.c
index b42d481af54a6e6c050b2aec787ac6204482eb55..fdd833baa97dd94f8a3819bba35462ee7df60c55 100644 (file)
 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,8 @@ 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);
   }
 }