]> git.deb.at Git - pkg/abook.git/commitdiff
query/find code cleanup
authorJaakko Heinonen <jheinonen@users.sourceforge.net>
Mon, 11 Jun 2001 08:40:38 +0000 (08:40 +0000)
committerJaakko Heinonen <jheinonen@users.sourceforge.net>
Mon, 11 Jun 2001 08:40:38 +0000 (08:40 +0000)
ChangeLog
abook.c
database.c
database.h
ui.c

index d64af0264895daac335e9f1e426ca841a541c636..262390ef4bf4fb925ae4cb08f7cae4bf405e4ddf 100644 (file)
--- 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 c80ea53f034609cea3d33bed60e9cdcd84dad831..e01eb427b4a399e78dda5f616191ae5b8a310240 100644 (file)
--- 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);
index ddc96ab21aaa047ee6235a102a4d31f61320b14e..1577966471f4e7e2ab2636562c8cd81ad3f603ce 100644 (file)
@@ -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:
index eff0e59b14797428c15c38e709b483e2e1c17bfd..7d947aa529203301ee82a710e33b4bac0e382cec 100644 (file)
@@ -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 a6e1492871abf15e1f805b529b2a4c124c0ee897..d490fc3b61c56019a41c398a9ed2710095f7ff74 100644 (file)
--- 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();
        }