From: Jaakko Heinonen Date: Mon, 11 Jun 2001 08:40:38 +0000 (+0000) Subject: query/find code cleanup X-Git-Tag: upstream/0.6.1~2^2~368 X-Git-Url: https://git.deb.at/w?a=commitdiff_plain;h=ea5b79b21b3e6b5d009dca4afcab7f3ed9cabdd5;p=pkg%2Fabook.git query/find code cleanup --- diff --git a/ChangeLog b/ChangeLog index d64af02..262390e 100644 --- a/ChangeLog +++ b/ChangeLog @@ -7,6 +7,7 @@ - ablity to print/export only selected items - new editor commands - text filter update + - mutt query code cleanup 0.4.12 - added man page for abookrc (Alan Ford) diff --git a/abook.c b/abook.c index c80ea53..e01eb42 100644 --- a/abook.c +++ b/abook.c @@ -224,73 +224,28 @@ muttq_print_item(int item) ); } -static int -mutt_query_name(char *str) -{ - int i, j; - char *tmp; - - for(i = 0, j = 0 ; i < items; i++) { - tmp = strdup(database[i][NAME]); - if( strstr( strupper(tmp), strupper(str) ) != NULL ) { - if( !j ) - putchar('\n'); - muttq_print_item(i); - j++; - } - free(tmp); - } - - return j; -} - -static int -mutt_query_email(char *str) -{ - int i, j, k; - char *tmp, emails[MAX_EMAILS][MAX_EMAIL_LEN]; - - for(i = 0, j = 0; i < items; i++) { - split_emailstr(i, emails); - for(k = 0; k < MAX_EMAILS; k++) { - if( *emails[k] ) { - tmp = strdup( emails[k] ); - if( strstr( strupper(tmp), strupper(str) ) != NULL ) { - if( !j ) - putchar('\n'); - j++; - if( options_get_int("mutt_return_all_emails") ) { - muttq_print_item(i); - free(tmp); - break; - } else - printf("%s\t%s\n", emails[k], - database[i][NAME]); - } - free(tmp); - } - } - } - - return j; -} - static void mutt_query(char *str) { - int i; - init_mutt_query(); if( str == NULL || !strcasecmp(str, "all") ) { + struct db_enumerator e = init_db_enumerator(ENUM_ALL); printf("All items\n"); - for(i = 0; i < items; i++) - muttq_print_item(i); + db_enumerate_items(e) + muttq_print_item(e.item); } else { - if( !mutt_query_name(str) && !mutt_query_email(str) ) { + int search_fields[] = {NAME, EMAIL, NICK, -1}; + int i; + if( (i = find_item(str, 0, search_fields)) < 0 ) { printf("Not found\n"); quit_mutt_query(1); } + putchar('\n'); + while(i >= 0) { + muttq_print_item(i); + i = find_item(str, i+1, search_fields); + } } quit_mutt_query(0); diff --git a/database.c b/database.c index ddc96ab..1577966 100644 --- a/database.c +++ b/database.c @@ -386,26 +386,30 @@ sort_surname() } int -find_item(char *str, int start) +find_item(char *str, int start, int search_fields[]) { int i; char *findstr = NULL; char *tmp = NULL; int ret = -1; /* not found */ + struct db_enumerator e = init_db_enumerator(ENUM_ALL); - if(items < 1 || start < 0 || start >= LAST_ITEM) + if(list_is_empty() || !is_valid_item(start)) return -2; /* error */ findstr = strdup(str); findstr = strupper(findstr); - for( i = start; i < items; i++ ) { - tmp = strdup(database[i][NAME]); - if( strstr(strupper(tmp), findstr) != NULL ) { - ret = i; - goto out; + 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); } - my_free(tmp); } out: diff --git a/database.h b/database.h index eff0e59..7d947aa 100644 --- a/database.h +++ b/database.h @@ -57,7 +57,7 @@ void remove_selected_items(); void sort_surname(); void sort_database(); char *get_surname(char *s); -int find_item(char *str, int start); +int find_item(char *str, int start, int search_fields[]); int is_selected(int item); int is_valid_item(int item); diff --git a/ui.c b/ui.c index a6e1492..d490fc3 100644 --- a/ui.c +++ b/ui.c @@ -506,6 +506,7 @@ ui_find(int next) { int item; static char findstr[81]; + int search_fields[] = {NAME, EMAIL, NICK, -1}; if(next) { if( !*findstr ) @@ -517,7 +518,8 @@ ui_find(int next) clear_statusline(); } - if( (item = find_item(findstr, next ? curitem+1 : curitem)) >= 0 ) { + if( (item = find_item(findstr, next ? curitem+1 : curitem, + search_fields )) >= 0 ) { curitem = item; refresh_list(); }