]> git.deb.at Git - pkg/abook.git/blobdiff - database.c
* don't unlink empty datafile, just truncate
[pkg/abook.git] / database.c
index 729db926bb922ca214f210f0e71b46fe85388e91..1b337c6b59f69bcc6687aa45b0000dfdfed5c6ad 100644 (file)
@@ -358,18 +358,13 @@ save_database()
                goto out;
        }
 
-       if(list_is_empty()) {
-               fclose(out);
-               unlink(datafile);
-               ret = 1;
-               goto out;
-       }
+       if(!list_is_empty())
+               /*
+                * Possibly should check if write_database failed.
+                * Currently it returns always zero.
+                */
+               write_database(out, e);
 
-       /*
-        * Possibly should check if write_database failed.
-        * Currently it returns always zero.
-        */
-       write_database(out, e);
        fclose(out);
 
        if(access(datafile, F_OK) == 0 &&
@@ -543,9 +538,6 @@ surnamecmp(const void *i1, const void *i2)
        int ret, idx = field_id(NAME);
        char *n1, *n2, *s1, *s2;
 
-       if(idx == 0)
-               return 0; /* no 'name' field */
-
        n1 = (*(list_item *)i1)[idx];
        n2 = (*(list_item *)i2)[idx];
        
@@ -837,3 +829,22 @@ db_item_get(int i)
        return database[i];
 }
 
+/* Fetch addresses from all fields of FIELD_EMAILS type */
+/* Memory has to be freed by the caller */
+char *
+db_email_get(int item)
+{
+       int i;
+       char *res;
+       abook_field_list *cur;
+       abook_list *emails = NULL;
+
+       for(cur = fields_list, i = 0; cur; cur = cur->next, i++)
+               if(cur->field->type == FIELD_EMAILS && *database[item][i])
+                       abook_list_append(&emails, database[item][i]);
+
+       res = abook_list_to_csv(emails);
+       abook_list_free(&emails);
+       return res ? res : xstrdup("");
+}
+