From 9b81bd9478e242795e65f2d646b8b8eb123c5c5f Mon Sep 17 00:00:00 2001 From: ache Date: Sun, 10 Feb 2019 23:52:06 +0100 Subject: Implement circular search --- contactList.c | 16 +++++++++++++--- contactList.h | 2 +- main.c | 6 ++---- 3 files changed, 16 insertions(+), 8 deletions(-) diff --git a/contactList.c b/contactList.c index 728cade..b40f3d4 100644 --- a/contactList.c +++ b/contactList.c @@ -44,11 +44,22 @@ void sortContacts(void) { qsort(contactList, nbContacts, sizeof *contactList, cmpContact); } -int findContactFromName(const char* name, contact** out) { +int findContactFromName(const char* name, contact** out, contact* from) { + int I = 0; + + if( from ) + I = from - contactList; + if( !name || !out || !*name) return 1; - for(int i = 0 ; i < nbContacts ; i++) { + for(int i = I+1 ; i < nbContacts ; i++) { + if( strcasestr(contactList[i].display_name, name) ) { + *out = contactList+i; + return 0; + } + } + for(int i = 0 ; i <= I ; i++) { if( strcasestr(contactList[i].display_name, name) ) { *out = contactList+i; return 0; @@ -56,7 +67,6 @@ int findContactFromName(const char* name, contact** out) { } return 2; } - int addContact(const contact* cnct) { nbContacts++; contactList = realloc(contactList, nbContacts * sizeof *contactList); diff --git a/contactList.h b/contactList.h index 0adcd9a..9051f41 100644 --- a/contactList.h +++ b/contactList.h @@ -37,7 +37,7 @@ int findContact(const char*, contact**); // Find a contact // Params: A name // Return: 0 if success, >0 otherwise -int findContactFromName(const char*, contact**); +int findContactFromName(const char*, contact**,contact*); diff --git a/main.c b/main.c index 810ff65..d846674 100644 --- a/main.c +++ b/main.c @@ -108,7 +108,7 @@ int main(int argc, char* argv[]){ if( nameOrNumber = (*optarg == '+' ) ) r = findContact(optarg, &contact); else - r = findContactFromName(optarg, &contact); + r = findContactFromName(optarg, &contact, NULL); if( r ) { #ifdef DEBUG @@ -284,12 +284,10 @@ int main(int argc, char* argv[]){ contact* found = NULL; move(LINES-1,0); readline_n("> ", ""); - int r = findContactFromName(msg_win_str, &found); + int r = findContactFromName(msg_win_str, &found, getContactSelected()); if( r == 0 ) { setContactSelection(found); - } else { - printf("[%d]%s\n", r, msg_win_str); } curs_set(0); showContactListW(); -- cgit v1.2.3