mirror of
https://git.jabber.space/devs/cproof.git
synced 2026-07-27 13:46:21 +00:00
Tidied up contact tab completion
This commit is contained in:
119
contact_list.c
119
contact_list.c
@@ -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
|
|
||||||
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
|
found = _search_contact_list_from(_contact_list);
|
||||||
_last_found = curr;
|
return found;
|
||||||
|
|
||||||
// return the contact, must be free'd by caller
|
|
||||||
strcpy(result, curr_contact->name);
|
|
||||||
return result;
|
|
||||||
}
|
|
||||||
|
|
||||||
curr = curr->next;
|
// subsequent search attempt
|
||||||
}
|
} else {
|
||||||
|
// search from here+1 to end
|
||||||
|
found = _search_contact_list_from(_last_found->next);
|
||||||
|
if (found != NULL)
|
||||||
|
return found;
|
||||||
|
|
||||||
|
// search from beginning
|
||||||
|
found = _search_contact_list_from(_contact_list);
|
||||||
|
if (found != NULL)
|
||||||
|
return found;
|
||||||
|
|
||||||
|
// 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)
|
||||||
{
|
{
|
||||||
|
|||||||
Reference in New Issue
Block a user