]> git.deb.at Git - pkg/abook.git/blobdiff - database.c
- code cleanups and minor fixes
[pkg/abook.git] / database.c
index c17adae62b7da64a6d4e19d83040b05729b83ae2..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 */
@@ -71,9 +71,26 @@ struct abook_field abook_fields[ITEM_FIELDS] = {
        {"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 */
 };
 
 
+int
+find_field(const char *field)
+{
+       int i;
+
+       for(i = 0; i < ITEM_FIELDS; i++)
+               if( !strcmp(abook_fields[i].key, field) )
+                       return i;
+
+       return -1;
+}
+
 int
 parse_database(FILE *in)
 {
@@ -83,7 +100,7 @@ parse_database(FILE *in)
        list_item item;
 
        memset(&item, 0, sizeof(item));
-       
+
        for(;;) {
                line = getaline(in);
                if( feof(in) ) {
@@ -122,7 +139,7 @@ next:
        return 0;
 }
 
-               
+
 
 int
 load_database(char *filename)
@@ -134,7 +151,7 @@ load_database(char *filename)
 
        if ( (in = abook_fopen(filename, "r")) == NULL )
                return -1;
-       
+
        parse_database(in);
 
        return (items == 0) ? 2 : 0;
@@ -180,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;
 }
 
@@ -206,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);
 
@@ -235,7 +252,7 @@ validate_item(list_item item)
 {
        int i;
        char *tmp;
-       
+
        if(item[EMAIL] == NULL)
                item[EMAIL] = strdup("");
 
@@ -261,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
@@ -295,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_ */
@@ -303,7 +319,7 @@ remove_selected_items()
                                itemcpy(database[ i ], database[ i + 1 ]);
                                selected[ i ] = selected[ i + 1 ];
                        }
-                       items--;        
+                       items--;
                }
        }
 
@@ -362,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] );
 }
 
@@ -398,7 +414,7 @@ sort_by_field(int field)
        }
 
        sort_field = field;
-       
+
        qsort((void *)database, items, sizeof(list_item), namecmp);
 
        refresh_screen();
@@ -437,7 +453,7 @@ find_item(char *str, int start, int search_fields[])
                                ret = e.item;
                                goto out;
                        }
-                       my_free(tmp);
+                       xfree(tmp);
                }
        }
 
@@ -465,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:
@@ -501,3 +517,47 @@ init_db_enumerator(int mode)
 
        return e;
 }
+
+static int
+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))
+                       xfree(abook_fields[i].name);
+
+               s = xmalloc_inc(MAX_FIELDNAME_LEN, 1);
+               snprintf(s, MAX_FIELDNAME_LEN, "%s", name);
+               abook_fields[i].name = s;
+       }
+
+       return 0;
+}
+
+int
+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++) {
+               if(!strcasecmp(abook_fields[i].key, keyname)) {
+                       assign_fieldname(name, i);
+                       return i;
+               }
+       }
+
+       return -1;
+}
+