Unlink xrefs properly when struct is deleted.
[gedcom-parse.git] / gom / repository.c
index 5fd888351415ff722ce77f43cf323cf71e225582..57c6f2889dd5fd5cc8dd9c7a072da904ec71e137 100644 (file)
 
 struct repository* gom_first_repository = NULL;
 
-REC_CB(repository, repo_start, make_repository_record)
-GET_REC_BY_XREF(repository, XREF_REPO, gom_get_repository_by_xref)
-STRING_CB(repository, repo_name_start, name)
+DEFINE_MAKEFUNC(repository, gom_first_repository)
+DEFINE_DESTROYFUNC(repository, gom_first_repository)
+DEFINE_ADDFUNC(repository, XREF_REPO)
+DEFINE_DELETEFUNC(repository)
+DEFINE_GETXREFFUNC(repository, XREF_REPO)
+     
+DEFINE_REC_CB(repository, repo_start)
+DEFINE_STRING_CB(repository, repo_name_start, name)
+
+DEFINE_ADDFUNC2(repository, note_sub, note)
+DEFINE_ADDFUNC2(repository, user_ref_number, ref)
+DEFINE_ADDFUNC2(repository, user_data, extra)
+DEFINE_ADDFUNC2_NOLIST(repository, address, address)
+DEFINE_ADDFUNC2_NOLIST(repository, change_date, change_date)
+DEFINE_ADDFUNC2_STRN(repository, phone, 3)
+DEFINE_ADDFUNC2_STR(repository, record_id)
 
 void repository_subscribe()
 {
@@ -45,71 +58,37 @@ void repository_subscribe()
   gedcom_subscribe_to_element(ELT_REPO_NAME, repo_name_start, def_elt_end);
 }
 
-void repository_add_address(Gom_ctxt ctxt, struct address* address)
+void UNREFALLFUNC(repository)(struct repository *obj)
 {
-  struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  repo->address = address;
+  if (obj) {
+    UNREFALLFUNC(address)(obj->address);
+    UNREFALLFUNC(note_sub)(obj->note);
+    UNREFALLFUNC(user_ref_number)(obj->ref);
+    UNREFALLFUNC(change_date)(obj->change_date);
+    UNREFALLFUNC(user_data)(obj->extra);
+  }
 }
 
-void repository_add_phone(Gom_ctxt ctxt, char *phone)
+void CLEANFUNC(repository)(struct repository* repo)
 {
-  struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  if (! repo->phone[0])
-    repo->phone[0] = strdup(phone);
-  else if (! repo->phone[1])
-    repo->phone[1] = strdup(phone);
-  else if (! repo->phone[2])
-    repo->phone[2] = strdup(phone);
-}
-
-void repository_add_note(Gom_ctxt ctxt, struct note_sub* note)
-{
-  struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  LINK_CHAIN_ELT(note_sub, repo->note, note)
-}
-
-void repository_add_user_ref(Gom_ctxt ctxt, struct user_ref_number* ref)
-{
-  struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  LINK_CHAIN_ELT(user_ref_number, repo->ref, ref)
-}
-
-void repository_set_record_id(Gom_ctxt ctxt, char *rin)
-{
-  struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  repo->record_id = strdup(rin);
-}
-
-void repository_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
-{
-  struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  repo->change_date = chan;
-}
-
-void repository_add_user_data(Gom_ctxt ctxt, struct user_data* data)
-{
-  struct repository *obj = SAFE_CTXT_CAST(repository, ctxt);
-  LINK_CHAIN_ELT(user_data, obj->extra, data)
-}
-
-void repository_cleanup(struct repository* repo)
-{
-  SAFE_FREE(repo->xrefstr);
-  SAFE_FREE(repo->name);
-  address_cleanup(repo->address);
-  SAFE_FREE(repo->phone[0]);
-  SAFE_FREE(repo->phone[1]);
-  SAFE_FREE(repo->phone[2]);
-  DESTROY_CHAIN_ELTS(note_sub, repo->note, note_sub_cleanup)
-  DESTROY_CHAIN_ELTS(user_ref_number, repo->ref, user_ref_cleanup)
-  SAFE_FREE(repo->record_id);
-  change_date_cleanup(repo->change_date);
-  DESTROY_CHAIN_ELTS(user_data, repo->extra, user_data_cleanup)
+  if (repo) {
+    SAFE_FREE(repo->xrefstr);
+    SAFE_FREE(repo->name);
+    CLEANFUNC(address)(repo->address);
+    SAFE_FREE(repo->phone[0]);
+    SAFE_FREE(repo->phone[1]);
+    SAFE_FREE(repo->phone[2]);
+    DESTROY_CHAIN_ELTS(note_sub, repo->note);
+    DESTROY_CHAIN_ELTS(user_ref_number, repo->ref);
+    SAFE_FREE(repo->record_id);
+    CLEANFUNC(change_date)(repo->change_date);
+    DESTROY_CHAIN_ELTS(user_data, repo->extra);
+  }
 }
 
 void repositories_cleanup()
 {
-  DESTROY_CHAIN_ELTS(repository, gom_first_repository, repository_cleanup);
+  DESTROY_CHAIN_ELTS(repository, gom_first_repository);
 }
 
 struct repository* gom_get_first_repository()
@@ -117,10 +96,34 @@ struct repository* gom_get_first_repository()
   return gom_first_repository;
 }
 
-struct repository* make_repository_record(char* xrefstr)
+int write_repositories(Gedcom_write_hndl hndl)
 {
-  struct repository* repo;
-  MAKE_CHAIN_ELT(repository, gom_first_repository, repo);
-  repo->xrefstr = strdup(xrefstr);
-  return repo;
+  int result = 0;
+  int i;
+  struct repository* obj;
+
+  for (obj = gom_first_repository; obj; obj = obj->next) {
+    result |= gedcom_write_record_str(hndl, REC_REPO, obj->xrefstr, NULL);
+    if (obj->name)
+      result |= gedcom_write_element_str(hndl, ELT_REPO_NAME, 0,
+                                        REC_REPO, obj->name);
+    if (obj->address)
+      result |= write_address(hndl, REC_REPO, obj->address);
+    for (i = 0; i < 3 && obj->phone[i]; i++)
+      result |= gedcom_write_element_str(hndl, ELT_SUB_PHON, 0, REC_REPO,
+                                        obj->phone[i]);
+    if (obj->note)
+      result |= write_note_subs(hndl, REC_REPO, obj->note);
+    if (obj->ref)
+      result |= write_user_refs(hndl, REC_REPO, obj->ref);
+    if (obj->record_id)
+      result |= gedcom_write_element_str(hndl, ELT_SUB_IDENT_RIN, 0,
+                                        REC_REPO, obj->record_id);
+    if (obj->change_date)
+      result |= write_change_date(hndl, REC_REPO, obj->change_date);
+    if (obj->extra)
+      result |= write_user_data(hndl, obj->extra);
+  }
+  
+  return result;
 }