1
1
mirror of https://github.com/profanity-im/profanity.git synced 2025-01-03 14:57:42 -05:00

Added find contact

This commit is contained in:
James Booth 2012-03-10 21:37:46 +00:00
parent e568c58824
commit f23f768d54
3 changed files with 70 additions and 2 deletions

View File

@ -20,6 +20,7 @@
* *
*/ */
#include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
@ -134,7 +135,7 @@ int contact_list_add(const char * const name, const char * const show,
} }
} }
contact_list_t *get_contact_list(void) contact_list_t * get_contact_list(void)
{ {
int count = 0; int count = 0;
@ -163,6 +164,29 @@ contact_list_t *get_contact_list(void)
return list; return list;
} }
char * find_contact(const char * const search_str)
{
struct _contact_node_t *curr = _contact_list;
if (!curr) {
return NULL;
} else {
while(curr) {
contact_t *curr_contact = curr->contact;
if (strncmp(curr_contact->name, search_str, strlen(search_str)) == 0) {
char *result =
(char *) malloc((strlen(curr_contact->name) + 1) * sizeof(char));
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)
{ {

View File

@ -39,5 +39,6 @@ int contact_list_add(const char * const name, const char * const show,
const char * const status); const char * const status);
int contact_list_remove(const char * const name); int contact_list_remove(const char * const name);
contact_list_t * get_contact_list(void); contact_list_t * get_contact_list(void);
char * find_contact(const char * const search_str);
#endif #endif

View File

@ -1,5 +1,4 @@
#include <stdio.h> #include <stdio.h>
#include <string.h>
#include <head-unit.h> #include <head-unit.h>
#include "contact_list.h" #include "contact_list.h"
@ -314,6 +313,46 @@ static void set_status_to_null(void)
assert_is_null(james->status); assert_is_null(james->status);
} }
static void find_first_exists(void)
{
contact_list_add("James", NULL, NULL);
contact_list_add("Dave", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result = find_contact("B");
assert_string_equals("Bob", result);
}
static void find_second_exists(void)
{
contact_list_add("James", NULL, NULL);
contact_list_add("Dave", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result = find_contact("Dav");
assert_string_equals("Dave", result);
}
static void find_third_exists(void)
{
contact_list_add("James", NULL, NULL);
contact_list_add("Dave", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result = find_contact("Ja");
assert_string_equals("James", result);
}
static void find_returns_null(void)
{
contact_list_add("James", NULL, NULL);
contact_list_add("Dave", NULL, NULL);
contact_list_add("Bob", NULL, NULL);
char *result = find_contact("Mike");
assert_is_null(result);
}
void register_contact_list_tests(void) void register_contact_list_tests(void)
{ {
TEST_MODULE("contact_list tests"); TEST_MODULE("contact_list tests");
@ -344,4 +383,8 @@ void register_contact_list_tests(void)
TEST(set_show_to_null); TEST(set_show_to_null);
TEST(update_status); TEST(update_status);
TEST(set_status_to_null); TEST(set_status_to_null);
TEST(find_first_exists);
TEST(find_second_exists);
TEST(find_third_exists);
TEST(find_returns_null);
} }