Improved context handling, to allow elements out of context.
[gedcom-parse.git] / gom / place.c
index a77ff91a88a48cdc9f6f63e6d91b0b9d654f0e10..779ea87881591a1c558d9fd06df7efe53aeb7840 100644 (file)
 Gedcom_ctxt sub_place_start(_ELT_PARAMS_)
 {
   Gom_ctxt ctxt = (Gom_ctxt)parent;
-  struct place *place = NULL;
+  Gom_ctxt result = NULL;
 
-  if (ctxt) {
-    place = (struct place *)malloc(sizeof(struct place));
-    memset (place, 0, sizeof(struct place));
-    place->value = strdup(GEDCOM_STRING(parsed_value));
-
-    switch (ctxt->ctxt_type) {
-      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_place(ctxt, place); break;
-      default:
-       UNEXPECTED_CONTEXT(ctxt->ctxt_type);
+  if (! ctxt)
+    NO_CONTEXT;
+  else {
+    struct place *place = SUB_MAKEFUNC(place)();
+    if (place) {
+      place->value = strdup(GEDCOM_STRING(parsed_value));
+      
+      if (!place->value) {
+       MEMORY_ERROR;
+       free(place);
+      }
+      else {
+       int type = ctxt_type(ctxt);
+       switch (type) {
+         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_NOLIST(event,place)(ctxt, place); break;
+         default:
+           UNEXPECTED_CONTEXT(type);
+       }
+       result = MAKE_GOM_CTXT(elt, place, place);
+      }
     }
   }
 
-  return (Gedcom_ctxt) MAKE_GOM_CTXT(elt, place, place);
+  return (Gedcom_ctxt)result;
 }
 
-STRING_CB(place, sub_place_form_start, place_hierarchy)
+DEFINE_SUB_MAKEFUNC(place)
+DEFINE_SUB_SETFUNC(place)
+DEFINE_SUB_DELETEFUNC(place)
+     
+DEFINE_STRING_CB(place, sub_place_form_start, place_hierarchy)
 
-void place_add_citation(Gom_ctxt ctxt, struct source_citation* cit)
-{
-  struct place *place = SAFE_CTXT_CAST(place, ctxt);
-  LINK_CHAIN_ELT(source_citation, place->citation, cit)  
-}
-
-void place_add_note(Gom_ctxt ctxt, struct note_sub* note)
-{
-  struct place *place = SAFE_CTXT_CAST(place, ctxt);
-  LINK_CHAIN_ELT(note_sub, place->note, note)  
-}
-
-void place_add_user_data(Gom_ctxt ctxt, struct user_data* data)
-{
-  struct place *obj = SAFE_CTXT_CAST(place, ctxt);
-  LINK_CHAIN_ELT(user_data, obj->extra, data)
-}
+DEFINE_ADDFUNC2(place, source_citation, citation)
+DEFINE_ADDFUNC2(place, note_sub, note)
+DEFINE_ADDFUNC2(place, user_data, extra)
 
 void place_subscribe()
 {
@@ -88,14 +89,44 @@ void place_subscribe()
                              sub_place_form_start, def_elt_end);
 }
 
-void place_cleanup(struct place* place)
+void UNREFALLFUNC(place)(struct place* obj)
+{
+  if (obj) {
+    UNREFALLFUNC(source_citation)(obj->citation);
+    UNREFALLFUNC(note_sub)(obj->note);
+    UNREFALLFUNC(user_data)(obj->extra);
+  }
+}
+
+void CLEANFUNC(place)(struct place* place)
 {
   if (place) {
     SAFE_FREE(place->value);
     SAFE_FREE(place->place_hierarchy);
-    DESTROY_CHAIN_ELTS(source_citation, place->citation, citation_cleanup)  
-    DESTROY_CHAIN_ELTS(note_sub, place->note, note_sub_cleanup)
-    DESTROY_CHAIN_ELTS(user_data, place->extra, user_data_cleanup)
+    DESTROY_CHAIN_ELTS(source_citation, place->citation);  
+    DESTROY_CHAIN_ELTS(note_sub, place->note);
+    DESTROY_CHAIN_ELTS(user_data, place->extra);
   }
   SAFE_FREE(place);
 }
+
+int write_place(Gedcom_write_hndl hndl, int parent, struct place* place)
+{
+  int result = 0;
+  
+  if (!place) return 1;
+  
+  result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC, 0,
+                                    parent, place->value);
+  if (place->place_hierarchy)
+    result |= gedcom_write_element_str(hndl, ELT_SUB_PLAC_FORM, 0,
+                                      ELT_SUB_PLAC, place->place_hierarchy);
+  if (place->citation)
+    result |= write_citations(hndl, ELT_SUB_PLAC, place->citation);
+  if (place->note)
+    result |= write_note_subs(hndl, ELT_SUB_PLAC, place->note);
+  if (place->extra)
+    result |= write_user_data(hndl, place->extra);
+
+  return result;
+}