Improved context handling, to allow elements out of context.
[gedcom-parse.git] / gom / place.c
index bb6ead6586f43499bea300d1be4047778af01c9c..779ea87881591a1c558d9fd06df7efe53aeb7840 100644 (file)
@@ -41,11 +41,8 @@ Gedcom_ctxt sub_place_start(_ELT_PARAMS_)
   if (! ctxt)
     NO_CONTEXT;
   else {
-    struct place *place = (struct place *)malloc(sizeof(struct place));
-    if (! place)
-      MEMORY_ERROR;
-    else {
-      memset (place, 0, sizeof(struct place));
+    struct place *place = SUB_MAKEFUNC(place)();
+    if (place) {
       place->value = strdup(GEDCOM_STRING(parsed_value));
       
       if (!place->value) {
@@ -53,7 +50,8 @@ Gedcom_ctxt sub_place_start(_ELT_PARAMS_)
        free(place);
       }
       else {
-       switch (ctxt->ctxt_type) {
+       int type = ctxt_type(ctxt);
+       switch (type) {
          case ELT_SUB_FAM_EVT:
          case ELT_SUB_FAM_EVT_EVEN:
          case ELT_SUB_INDIV_ATTR:
@@ -62,9 +60,9 @@ Gedcom_ctxt sub_place_start(_ELT_PARAMS_)
          case ELT_SUB_INDIV_GEN:
          case ELT_SUB_INDIV_ADOP:
          case ELT_SUB_INDIV_EVEN:
-           event_add_place(ctxt, place); break;
+           ADDFUNC2_NOLIST(event,place)(ctxt, place); break;
          default:
-           UNEXPECTED_CONTEXT(ctxt->ctxt_type);
+           UNEXPECTED_CONTEXT(type);
        }
        result = MAKE_GOM_CTXT(elt, place, place);
       }
@@ -74,28 +72,15 @@ Gedcom_ctxt sub_place_start(_ELT_PARAMS_)
   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);
-  if (place)
-    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);
-  if (place)
-    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);
-  if (obj)
-    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()
 {
@@ -104,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;
+}