]> git.deb.at Git - pkg/abook.git/blobdiff - database.c
Imported Debian patch 0.5.6-3
[pkg/abook.git] / database.c
index e19d9469bf754b8c725168a50cd99dbda8a52ed4..cabd3305f2c0079a4e2387d74c44ab59b885b9e6 100644 (file)
@@ -1,6 +1,6 @@
 
 /*
- * $Id: database.c,v 1.20 2003/11/22 13:46:02 jheinonen Exp $
+ * $Id: database.c,v 1.29.2.1 2005/10/26 19:45:23 jheinonen Exp $
  *
  * by JH <jheinonen@users.sourceforge.net>
  *
 #include "abook.h"
 #include <assert.h>
 #include "database.h"
+#include "gettext.h"
 #include "list.h"
 #include "misc.h"
 #include "options.h"
 #include "filter.h"
+#include "xmalloc.h"
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
 
-static void    free_item(int i);
-
-
 list_item *database = NULL;
 
 int items = 0;
 
 #define INITIAL_LIST_CAPACITY  30
 
-int list_capacity = 0;
+static int list_capacity = 0;
 
 extern int first_list_item;
 extern int curitem;
 extern char *selected;
 
 extern char *datafile;
-extern char *rcfile;
 
 /*
  * field definitions
@@ -56,26 +54,26 @@ extern char *rcfile;
  */
 
 struct abook_field abook_fields[ITEM_FIELDS] = {
-       {"Name",        "name",         TAB_CONTACT},/* NAME */
-       {"E-mails",     "email",        TAB_CONTACT},/* EMAIL */
-       {"Address",     "address",      TAB_ADDRESS},/* ADDRESS */
-       {"Address2",    "address2",     TAB_ADDRESS},/* ADDRESS2 */   
-       {"City",        "city",         TAB_ADDRESS},/* CITY */
-       {"State/Province","state",      TAB_ADDRESS},/* STATE */
-       {"ZIP/Postal Code","zip",       TAB_ADDRESS},/* ZIP */
-       {"Country",     "country",      TAB_ADDRESS},/* COUNTRY */
-       {"Home Phone",  "phone",        TAB_PHONE},/* PHONE */
-       {"Work Phone",  "workphone",    TAB_PHONE},/* WORKPHONE */
-       {"Fax",         "fax",          TAB_PHONE},/* FAX */
-       {"Mobile",      "mobile",       TAB_PHONE},/* MOBILEPHONE */
-       {"Nickname/Alias", "nick",      TAB_OTHER},/* NICK */
-       {"URL",         "url",          TAB_OTHER},/* URL */
-       {"Notes",       "notes",        TAB_OTHER},/* NOTES */
-       {"Custom1",     "custom1",      TAB_CUSTOM},/* CUSTOM1 */
-       {"Custom2",     "custom2",      TAB_CUSTOM},/* CUSTOM2 */
-       {"Custom3",     "custom3",      TAB_CUSTOM},/* CUSTOM3 */
-       {"Custom4",     "custom4",      TAB_CUSTOM},/* CUSTOM4 */
-       {"Custom5",     "custom5",      TAB_CUSTOM},/* CUSTOM5 */
+       {N_("Name"),    "name",         TAB_CONTACT},/* NAME */
+       {N_("E-mails"), "email",        TAB_CONTACT},/* EMAIL */
+       {N_("Address"), "address",      TAB_ADDRESS},/* ADDRESS */
+       {N_("Address2"),        "address2",     TAB_ADDRESS},/* ADDRESS2 */
+       {N_("City"),    "city",         TAB_ADDRESS},/* CITY */
+       {N_("State/Province"),"state",  TAB_ADDRESS},/* STATE */
+       {N_("ZIP/Postal Code"),"zip",   TAB_ADDRESS},/* ZIP */
+       {N_("Country"), "country",      TAB_ADDRESS},/* COUNTRY */
+       {N_("Home Phone"),      "phone",        TAB_PHONE},/* PHONE */
+       {N_("Work Phone"),      "workphone",    TAB_PHONE},/* WORKPHONE */
+       {N_("Fax"),             "fax",          TAB_PHONE},/* FAX */
+       {N_("Mobile"),  "mobile",       TAB_PHONE},/* MOBILEPHONE */
+       {N_("Nickname/Alias"), "nick",      TAB_OTHER},/* NICK */
+       {N_("URL"),             "url",          TAB_OTHER},/* URL */
+       {N_("Notes"),   "notes",        TAB_OTHER},/* NOTES */
+       {N_("Custom1"), "custom1",      TAB_CUSTOM},/* CUSTOM1 */
+       {N_("Custom2"), "custom2",      TAB_CUSTOM},/* CUSTOM2 */
+       {N_("Custom3"), "custom3",      TAB_CUSTOM},/* CUSTOM3 */
+       {N_("Custom4"), "custom4",      TAB_CUSTOM},/* CUSTOM4 */
+       {N_("Custom5"), "custom5",      TAB_CUSTOM},/* CUSTOM5 */
 };
 
 
@@ -100,58 +98,57 @@ parse_database(FILE *in)
        list_item item;
 
        memset(&item, 0, sizeof(item));
-       
+
        for(;;) {
                line = getaline(in);
-               if( feof(in) ) {
-                       if( item[NAME] && sec )
+               if(feof(in)) {
+                       if(item[NAME] && sec)
                                add_item2database(item);
                        else
                                free_list_item(item);
                        break;
                }
 
-               if( !*line || *line == '\n' || *line == '#' ) {
-                       free(line);
-                       continue;
-               } else if( *line == '[' ) {
+               if(!*line || *line == '\n' || *line == '#') {
+                       goto next;
+               } else if(*line == '[') {
                        if( item[NAME] && sec )
                                add_item2database(item);
                        else
                                free_list_item(item);
                        memset(&item, 0, sizeof(item));
                        sec = 1;
-                       if ( !(tmp = strchr(line, ']')))
+                       if(!(tmp = strchr(line, ']')))
                                sec = 0; /*incorrect section lines are skipped*/
-               } else if((tmp = strchr(line, '=') ) && sec ) {
+               } else if((tmp = strchr(line, '=') ) && sec) {
                        *tmp++ = '\0';
-                       for(i=0; i<ITEM_FIELDS; i++)
-                               if( !strcmp(abook_fields[i].key, line) ) {
-                                       item[i] = strdup(tmp);
+                       for(i = 0; i < ITEM_FIELDS; i++)
+                               if(!strcmp(abook_fields[i].key, line)) {
+                                       item[i] = xstrdup(tmp);
                                        goto next;
                                }
                }
 next:
-               free(line);
+               xfree(line);
        }
 
-       free(line);
+       xfree(line);
        return 0;
 }
 
-               
+
 
 int
 load_database(char *filename)
 {
        FILE *in;
 
-       if( database != NULL )
+       if(database != NULL)
                close_database();
 
-       if ( (in = abook_fopen(filename, "r")) == NULL )
+       if ((in = abook_fopen(filename, "r")) == NULL)
                return -1;
-       
+
        parse_database(in);
 
        return (items == 0) ? 2 : 0;
@@ -192,23 +189,41 @@ int
 save_database()
 {
        FILE *out;
+       int ret = 0;
        struct db_enumerator e = init_db_enumerator(ENUM_ALL);
+       char *datafile_new = strconcat(datafile, ".new", NULL);
+       char *datafile_old = strconcat(datafile, "~", NULL);
 
-       if( (out = abook_fopen(datafile, "w")) == NULL )
-               return -1;
+       if( (out = abook_fopen(datafile_new, "w")) == NULL ) {
+               ret = -1;
+               goto out;
+       }
 
-       if( list_is_empty() ) {
+       if(list_is_empty()) {
                fclose(out);
                unlink(datafile);
-               return 1;
+               ret = 1;
+               goto out;
        }
 
-       
+       /*
+        * Possibly should check if write_database failed.
+        * Currently it returns always zero.
+        */
        write_database(out, e);
-       
        fclose(out);
+
+       if(access(datafile, F_OK) == 0 &&
+                       (rename(datafile, datafile_old)) == -1)
+               ret = -1;
        
-       return 0;
+       if((rename(datafile_new, datafile)) == -1)
+               ret = -1;
+
+out:
+       free(datafile_new);
+       free(datafile_old);
+       return ret;
 }
 
 static void
@@ -223,14 +238,14 @@ free_list_item(list_item item)
        int i;
 
        for(i=0; i<ITEM_FIELDS; i++)
-               my_free(item[i]);
+               xfree(item[i]);
 }
 
 void
 close_database()
 {
        int i;
-       
+
        for(i=0; i < items; i++)
                free_item(i);
 
@@ -252,15 +267,15 @@ validate_item(list_item item)
 {
        int i;
        char *tmp;
-       
+
        if(item[EMAIL] == NULL)
-               item[EMAIL] = strdup("");
+               item[EMAIL] = xstrdup("");
 
        for(i=0; i<ITEM_FIELDS; i++)
                if( item[i] && ((int)strlen(item[i]) > _MAX_FIELD_LEN(i) ) ) {
                        tmp = item[i];
                        item[i][_MAX_FIELD_LEN(i)-1] = 0;
-                       item[i] = strdup(item[i]);
+                       item[i] = xstrdup(item[i]);
                        free(tmp);
                }
 }
@@ -278,20 +293,19 @@ adjust_list_capacity()
        else
                return;
 
-       database = (list_item *)abook_realloc(database,
-                       sizeof(list_item) * list_capacity);
-       selected = (char *)abook_realloc(selected, list_capacity);
+       database = xrealloc(database, sizeof(list_item) * list_capacity);
+       selected = xrealloc(selected, list_capacity);
 }
 
 int
 add_item2database(list_item item)
 {
-       if( item[NAME] == NULL || ! *item[NAME] ) {
+       if(item[NAME] == NULL || ! *item[NAME]) {
                free_list_item(item);
                return 1;
        }
 
-       if( ++items > list_capacity)
+       if(++items > list_capacity)
                adjust_list_capacity();
 
        validate_item(item);
@@ -307,27 +321,26 @@ remove_selected_items()
 {
        int i, j;
 
-       if( list_is_empty() )
+       if(list_is_empty())
                return;
 
-       if( ! selected_items() )
-               selected[ curitem ] = 1;
-       
-       for( j = LAST_ITEM; j >= 0; j-- ) {
-               if( selected[j] ) {
+       if(!selected_items())
+               selected[curitem] = 1;
+
+       for(j = LAST_ITEM; j >= 0; j--) {
+               if(selected[j]) {
                        free_item(j); /* added for .4 data_s_ */
-                       for( i = j; i < LAST_ITEM; i++ ) {
-                               itemcpy(database[ i ], database[ i + 1 ]);
-                               selected[ i ] = selected[ i + 1 ];
+                       for(i = j; i < LAST_ITEM; i++) {
+                               itemcpy(database[i], database[i + 1]);
+                               selected[i] = selected[i + 1];
                        }
-                       items--;        
+                       items--;
                }
        }
 
-       if( curitem > LAST_ITEM && items > 0 )
+       if(curitem > LAST_ITEM && items > 0)
                curitem = LAST_ITEM;
 
-
        adjust_list_capacity();
 
        select_none();
@@ -343,7 +356,7 @@ get_surname(char *s)
        while(p > s && *(p - 1) != ' ')
                p--;
 
-       return strdup(p);
+       return xstrdup(p);
 }
 
 static int
@@ -379,8 +392,8 @@ namecmp(const void *i1, const void *i2)
 
        itemcpy(a, i1);
        itemcpy(b, i2);
-       
-       return safe_strcoll( a[sort_field], b[sort_field] );
+
+       return safe_strcoll(a[sort_field], b[sort_field]);
 }
 
 static int
@@ -408,14 +421,14 @@ sort_by_field(int field)
        if(field < 0) {
                field = name2field(opt_get_str(STR_SORT_FIELD));
                if(field < 0) {
-                       statusline_msg("Not valid field value defined "
-                               "in configuration");
+                       statusline_msg(_("Invalid field value defined "
+                               "in configuration"));
                        return;
                }
        }
 
        sort_field = field;
-       
+
        qsort((void *)database, items, sizeof(list_item), namecmp);
 
        refresh_screen();
@@ -443,18 +456,20 @@ find_item(char *str, int start, int search_fields[])
        if(list_is_empty() || !is_valid_item(start))
                return -2; /* error */
 
-       findstr = strdup(str);
+       findstr = xstrdup(str);
        findstr = strlower(findstr);
 
        e.item = start - 1; /* must be "real start" - 1 */
        db_enumerate_items(e) {
-               for( i = 0; search_fields[i] >= 0; i++ ) {
-                       tmp = safe_strdup(database[e.item][search_fields[i]]);
+               for(i = 0; search_fields[i] >= 0; i++) {
+                       if(database[e.item][search_fields[i]] == NULL)
+                               continue;
+                       tmp = xstrdup(database[e.item][search_fields[i]]);
                        if( tmp && strstr(strlower(tmp), findstr) ) {
                                ret = e.item;
                                goto out;
                        }
-                       my_free(tmp);
+                       xfree(tmp);
                }
        }
 
@@ -482,7 +497,7 @@ real_db_enumerate_items(struct db_enumerator e)
 {
        int item = max(0, e.item + 1);
        int i;
-       
+
        switch(e.mode) {
 #ifdef DEBUG
                case ENUM_ALL:
@@ -532,9 +547,9 @@ assign_fieldname(const char *name, int i)
                 * check if we are overwriting statically allocated default
                 */
                if(strcasecmp(abook_fields[i].name, abook_fields[i].key))
-                       my_free(abook_fields[i].name);
-               
-               s = abook_malloc(MAX_FIELDNAME_LEN + 1);
+                       xfree(abook_fields[i].name);
+
+               s = xmalloc_inc(MAX_FIELDNAME_LEN, 1);
                snprintf(s, MAX_FIELDNAME_LEN, "%s", name);
                abook_fields[i].name = s;
        }