]> git.deb.at Git - pkg/abook.git/blobdiff - database.c
- code cleanups and minor fixes
[pkg/abook.git] / database.c
index cf4f96632176f61604c8ffb5db5fb74b3783cfc4..e8a4fa390f168185920dfaac61a205e209108a56 100644 (file)
@@ -19,6 +19,7 @@
 #include "misc.h"
 #include "options.h"
 #include "filter.h"
+#include "xmalloc.h"
 #ifdef HAVE_CONFIG_H
 #      include "config.h"
 #endif
@@ -39,7 +40,6 @@ extern int curitem;
 extern char *selected;
 
 extern char *datafile;
-extern char *rcfile;
 
 /*
  * field definitions
@@ -59,7 +59,7 @@ 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 */   
+       {"Address2",    "address2",     TAB_ADDRESS},/* ADDRESS2 */
        {"City",        "city",         TAB_ADDRESS},/* CITY */
        {"State/Province","state",      TAB_ADDRESS},/* STATE */
        {"ZIP/Postal Code","zip",       TAB_ADDRESS},/* ZIP */
@@ -100,7 +100,7 @@ parse_database(FILE *in)
        list_item item;
 
        memset(&item, 0, sizeof(item));
-       
+
        for(;;) {
                line = getaline(in);
                if( feof(in) ) {
@@ -139,7 +139,7 @@ next:
        return 0;
 }
 
-               
+
 
 int
 load_database(char *filename)
@@ -151,7 +151,7 @@ load_database(char *filename)
 
        if ( (in = abook_fopen(filename, "r")) == NULL )
                return -1;
-       
+
        parse_database(in);
 
        return (items == 0) ? 2 : 0;
@@ -197,17 +197,17 @@ save_database()
        if( (out = abook_fopen(datafile, "w")) == NULL )
                return -1;
 
-       if( list_is_empty() ) {
+       if(list_is_empty()) {
                fclose(out);
                unlink(datafile);
                return 1;
        }
 
-       
+
        write_database(out, e);
-       
+
        fclose(out);
-       
+
        return 0;
 }
 
@@ -223,14 +223,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,7 +252,7 @@ validate_item(list_item item)
 {
        int i;
        char *tmp;
-       
+
        if(item[EMAIL] == NULL)
                item[EMAIL] = strdup("");
 
@@ -278,9 +278,8 @@ 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
@@ -312,7 +311,7 @@ remove_selected_items()
 
        if( ! selected_items() )
                selected[ curitem ] = 1;
-       
+
        for( j = LAST_ITEM; j >= 0; j-- ) {
                if( selected[j] ) {
                        free_item(j); /* added for .4 data_s_ */
@@ -320,7 +319,7 @@ remove_selected_items()
                                itemcpy(database[ i ], database[ i + 1 ]);
                                selected[ i ] = selected[ i + 1 ];
                        }
-                       items--;        
+                       items--;
                }
        }
 
@@ -379,7 +378,7 @@ namecmp(const void *i1, const void *i2)
 
        itemcpy(a, i1);
        itemcpy(b, i2);
-       
+
        return safe_strcoll( a[sort_field], b[sort_field] );
 }
 
@@ -415,7 +414,7 @@ sort_by_field(int field)
        }
 
        sort_field = field;
-       
+
        qsort((void *)database, items, sizeof(list_item), namecmp);
 
        refresh_screen();
@@ -454,7 +453,7 @@ find_item(char *str, int start, int search_fields[])
                                ret = e.item;
                                goto out;
                        }
-                       my_free(tmp);
+                       xfree(tmp);
                }
        }
 
@@ -482,7 +481,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:
@@ -524,14 +523,17 @@ assign_fieldname(const char *name, int i)
 {
        char *s;
 
+       assert(name);
+       assert(i >= 0 && i < ITEM_FIELDS);
+
        if(strcasecmp(abook_fields[i].name, name)) { /* name differs */
                /*
                 * 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;
        }
@@ -545,6 +547,8 @@ change_custom_field_name(const char *name, int n)
        int i;
        char keyname[21];
 
+       assert(name);
+
        snprintf(keyname, sizeof(keyname), "custom%d", n);
 
        for(i = CUSTOM_MIN; i <= CUSTOM_MAX; i++) {