Tidied up contact tab completion

This commit is contained in:
James Booth
2012-03-11 00:12:08 +00:00
parent f7584469bb
commit b1bf05400b

View File

@@ -32,13 +32,14 @@ struct _contact_node_t {
struct _contact_node_t *next; struct _contact_node_t *next;
}; };
// the contact list // contact list
static struct _contact_node_t * _contact_list = NULL; static struct _contact_node_t * _contact_list = NULL;
// number of tabs pressed whilst searching // state of current tab completion, currrent node and current search pattern
static struct _contact_node_t * _last_found = NULL; static struct _contact_node_t * _last_found = NULL;
static char * _search_str = NULL; static char * _search_str = NULL;
static char * _search_contact_list_from(struct _contact_node_t * curr);
static struct _contact_node_t * _make_contact_node(const char * const name, static struct _contact_node_t * _make_contact_node(const char * const name,
const char * const show, const char * const status); const char * const show, const char * const status);
static contact_t * _new_contact(const char * const name, const char * const show, static contact_t * _new_contact(const char * const name, const char * const show,
@@ -186,88 +187,62 @@ contact_list_t * get_contact_list(void)
char * find_contact(const char * const search_str) char * find_contact(const char * const search_str)
{ {
struct _contact_node_t *curr = _contact_list; char *found = NULL;
// no contact // no contacts to search
if (!curr) { if (!_contact_list)
return NULL; return NULL;
// not first search attempt // first search attempt
} else if (_last_found != NULL) { if (_last_found == NULL) {
// search from here+1 to end
curr = _last_found->next;
while(curr) {
contact_t *curr_contact = curr->contact;
// match found
if (strncmp(curr_contact->name, _search_str, strlen(_search_str)) == 0) {
char *result =
(char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char));
// set pointer to last found
_last_found = curr;
// return the contact, must be free'd by caller
strcpy(result, curr_contact->name);
return result;
}
curr = curr->next;
}
// search from beginning to last found
curr = _contact_list;
while(curr) {
contact_t *curr_contact = curr->contact;
// match found
if (strncmp(curr_contact->name, _search_str, strlen(_search_str)) == 0) {
char *result =
(char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char));
// set pointer to last found
_last_found = curr;
// return the contact, must be free'd by caller
strcpy(result, curr_contact->name);
return result;
}
curr = curr->next;
}
// we found nothing, reset last_found
reset_search_attempts();
return NULL;
// first attempt at searching
} else {
_search_str = (char *) malloc((strlen(search_str) + 1) * sizeof(char)); _search_str = (char *) malloc((strlen(search_str) + 1) * sizeof(char));
strcpy(_search_str, search_str); strcpy(_search_str, search_str);
while(curr) {
contact_t *curr_contact = curr->contact;
// match found found = _search_contact_list_from(_contact_list);
if (strncmp(curr_contact->name, _search_str, strlen(_search_str)) == 0) { return found;
char *result =
(char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char));
// set pointer to last found // subsequent search attempt
_last_found = curr; } else {
// search from here+1 to end
found = _search_contact_list_from(_last_found->next);
if (found != NULL)
return found;
// return the contact, must be free'd by caller // search from beginning
strcpy(result, curr_contact->name); found = _search_contact_list_from(_contact_list);
return result; if (found != NULL)
} return found;
curr = curr->next;
}
// we found nothing, reset search
reset_search_attempts();
return NULL; return NULL;
} }
} }
static char * _search_contact_list_from(struct _contact_node_t * curr)
{
while(curr) {
contact_t *curr_contact = curr->contact;
// match found
if (strncmp(curr_contact->name, _search_str, strlen(_search_str)) == 0) {
char *result =
(char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char));
// set pointer to last found
_last_found = curr;
// return the contact, must be free'd by caller
strcpy(result, curr_contact->name);
return result;
}
curr = curr->next;
}
return NULL;
}
static struct _contact_node_t * _make_contact_node(const char * const name, static struct _contact_node_t * _make_contact_node(const char * const name,
const char * const show, const char * const status) const char * const show, const char * const status)
{ {