Getting and setting strings.
[gedcom-parse.git] / gom / repository.c
index 5fd888351415ff722ce77f43cf323cf71e225582..b34081cb84f739a9801e33a0c6961a7b9533ef9a 100644 (file)
@@ -48,63 +48,75 @@ void repository_subscribe()
 void repository_add_address(Gom_ctxt ctxt, struct address* address)
 {
   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  repo->address = address;
+  if (repo)
+    repo->address = address;
 }
 
-void repository_add_phone(Gom_ctxt ctxt, char *phone)
+void repository_add_phone(Gom_ctxt ctxt, const char *phone)
 {
   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);
+  if (repo) {
+    int i = 0;
+    while (i<2 && repo->phone[i]) i++;
+    if (! repo->phone[i]) {
+      repo->phone[i] = strdup(phone);
+      if (! repo->phone[i]) MEMORY_ERROR;
+    }
+  }
 }
 
 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)
+  if (repo)
+    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)
+  if (repo)
+    LINK_CHAIN_ELT(user_ref_number, repo->ref, ref);
 }
 
-void repository_set_record_id(Gom_ctxt ctxt, char *rin)
+void repository_set_record_id(Gom_ctxt ctxt, const char *rin)
 {
   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  repo->record_id = strdup(rin);
+  if (repo) {
+    repo->record_id = strdup(rin);
+    if (! repo->record_id) MEMORY_ERROR;
+  }
 }
 
 void repository_set_change_date(Gom_ctxt ctxt, struct change_date* chan)
 {
   struct repository *repo = SAFE_CTXT_CAST(repository, ctxt);
-  repo->change_date = chan;
+  if (repo)
+    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)
+  if (obj)
+    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);
+    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);
+  }
 }
 
 void repositories_cleanup()
@@ -117,10 +129,13 @@ struct repository* gom_get_first_repository()
   return gom_first_repository;
 }
 
-struct repository* make_repository_record(char* xrefstr)
+struct repository* make_repository_record(const char* xrefstr)
 {
-  struct repository* repo;
+  struct repository* repo = NULL;
   MAKE_CHAIN_ELT(repository, gom_first_repository, repo);
-  repo->xrefstr = strdup(xrefstr);
+  if (repo) {
+    repo->xrefstr = strdup(xrefstr);
+    if (! repo->xrefstr) MEMORY_ERROR;
+  }
   return repo;
 }