X-Git-Url: https://git.deb.at/w?a=blobdiff_plain;f=database.c;h=8b22602e03b6a2a60121904a6cf2ae05f2fb22ad;hb=4128f78f5543bebc7cd1868736855207019f1f71;hp=8b8d2bedb58fd644994e3bb75fd67a9759be76ff;hpb=4892aec476c9d7ce2cf11483cc369fcddf7bea6a;p=pkg%2Fabook.git diff --git a/database.c b/database.c index 8b8d2be..8b22602 100644 --- a/database.c +++ b/database.c @@ -1,7 +1,8 @@ /* - * database.c - * by JH + * $Id$ + * + * by JH * * Copyright (C) Jaakko Heinonen */ @@ -11,7 +12,6 @@ #include #include #include -#include "abook_curses.h" #include "abook.h" #include "database.h" #include "list.h" @@ -58,6 +58,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 */ {"City", "city", TAB_ADDRESS},/* CITY */ {"State/Province","state", TAB_ADDRESS},/* STATE */ {"ZIP/Postal Code","zip", TAB_ADDRESS},/* ZIP */ @@ -107,9 +108,12 @@ parse_database(FILE *in) } else if((tmp = strchr(line, '=') ) && sec ) { *tmp++ = '\0'; for(i=0; i _MAX_FIELD_LEN(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]); @@ -251,9 +260,9 @@ adjust_list_capacity() else return; - database = abook_realloc(database, + database = (list_item *)abook_realloc(database, sizeof(list_item) * list_capacity); - selected = abook_realloc(selected, list_capacity); + selected = (char *)abook_realloc(selected, list_capacity); } int @@ -276,23 +285,13 @@ add_item2database(list_item item) } void -remove_items() +remove_selected_items() { int i, j; - if( items < 1 || curitem < 0 ) + if( list_is_empty() ) return; - statusline_addstr("Remove selected item(s) (Y/n)"); - switch( getch() ) { - case '\r': - case 'y': - case 'Y': break; - default: - clear_statusline(); - return; - } - if( ! selected_items() ) selected[ curitem ] = 1; @@ -314,8 +313,6 @@ remove_items() adjust_list_capacity(); select_none(); - clear_statusline(); - refresh_list(); } char * @@ -351,8 +348,8 @@ surnamecmp(const void *i1, const void *i2) s1 = get_surname(a[NAME]); s2 = get_surname(b[NAME]); - if( !(ret = safe_strcmp(s1, s2)) ) - ret = safe_strcmp(a[NAME], b[NAME]); + if( !(ret = safe_strcoll(s1, s2)) ) + ret = safe_strcoll(a[NAME], b[NAME]); free(s1); free(s2); @@ -368,7 +365,7 @@ namecmp(const void *i1, const void *i2) itemcpy(a, i1); itemcpy(b, i2); - return safe_strcmp( a[NAME], b[NAME] ); + return safe_strcoll( a[NAME], b[NAME] ); } void @@ -391,110 +388,90 @@ sort_surname() refresh_screen(); } -void -clear_database() -{ - - statusline_addstr("Clear WHOLE database (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': break; - default: - clear_statusline(); - return; - } - - close_database(); - - refresh_screen(); -} - -void -find(int next) +int +find_item(char *str, int start, int search_fields[]) { int i; - static char findstr[81]; - char tmp[81]; - -#ifdef DEBUG - fprintf(stderr, "find(): findstr = |%s|\n", findstr); -#endif - - if(next) { - if( !*findstr ) - return; - } else { - clear_statusline(); - statusline_addstr("/"); - statusline_getnstr(findstr, 67, 0); - strupper(findstr); - clear_statusline(); - } - - if(items < 1) - return; - - for( i = (curitem < LAST_ITEM) && next ? curitem+1 : curitem; - i < items; i++ ) { - strcpy(tmp, database[i][NAME]); - if( strstr(strupper(tmp), findstr) != NULL ) { - curitem = i; - refresh_list(); - break; + char *findstr = NULL; + char *tmp = NULL; + int ret = -1; /* not found */ + struct db_enumerator e = init_db_enumerator(ENUM_ALL); + + if(list_is_empty() || !is_valid_item(start)) + return -2; /* error */ + + findstr = strdup(str); + findstr = strupper(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]]); + if( tmp && strstr(strupper(tmp), findstr) ) { + ret = e.item; + goto out; + } + my_free(tmp); } } + +out: + free(findstr); + free(tmp); + return ret; } -void -print_number_of_items() +int +is_selected(int item) { - char *str = mkstr(" " "|%3d/%3d", selected_items(), items); - - mvaddstr(0, COLS-strlen(str), str); - - free(str); + return selected[item]; } -void -read_database() +int +is_valid_item(int item) { - if(items > 0) { - statusline_addstr("Your current data will be lost - Press 'y' to continue"); - switch( getch() ) { - case 'y': - case 'Y': break; - default: clear_statusline(); - return; - } - clear_statusline(); - } - - load_database(datafile); - refresh_list(); + return item <= LAST_ITEM && item >= 0; } - -void -print_database() +int +real_db_enumerate_items(struct db_enumerator e) { - FILE *handle; - char *command = options_get_str("print_command"); - - statusline_addstr("Print addressbook? (y/N)"); - switch( getch() ) { - case 'y': - case 'Y': + int item = max(0, e.item + 1); + int i; + + switch(e.mode) { +#ifdef DEBUG + case ENUM_ALL: + break; +#endif + case ENUM_SELECTED: + for(i = item; i < items; i++) { + if(is_selected(i)) { + item = i; + goto out; + } + } + return -1; +#ifdef DEBUG + default: + fprintf(stderr, "real_db_enumerate_items() " + "BUG: unknown db_enumerator mode: %d\n", + e.mode); break; - default: clear_statusline(); return; +#endif } - clear_statusline(); +out: + return (item > LAST_ITEM || item < 0) ? -1 : item; +} - if( ! *command || (handle = popen(command, "w")) == NULL) - return; +struct db_enumerator +init_db_enumerator(int mode) +{ + struct db_enumerator e; - fexport("text", handle); - - pclose(handle); -} + e.item = -1; /* important - means "start from beginning" */ + e.mode = mode; + return e; +}