1
0
mirror of https://github.com/profanity-im/profanity.git synced 2024-09-15 19:38:07 -04:00
profanity/src/prof_autocomplete.c

212 lines
4.8 KiB
C
Raw Normal View History

/*
2012-05-13 16:40:39 -04:00
* prof_autocomplete.c
*
2013-01-10 21:05:29 -05:00
* Copyright (C) 2012, 2013 James Booth <boothj5@gmail.com>
*
2012-05-13 16:40:39 -04:00
* This file is part of Profanity.
*
* Profanity is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* Profanity is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with Profanity. If not, see <http://www.gnu.org/licenses/>.
*
*/
#include <stdlib.h>
#include <string.h>
#include "prof_autocomplete.h"
struct p_autocomplete_t {
GSList *items;
GSList *last_found;
gchar *search_str;
};
static gchar * _search_from(PAutocomplete ac, GSList *curr);
2012-05-13 16:40:39 -04:00
2012-07-24 18:19:48 -04:00
PAutocomplete
p_autocomplete_new(void)
2012-05-13 16:40:39 -04:00
{
PAutocomplete new = malloc(sizeof(struct p_autocomplete_t));
new->items = NULL;
new->last_found = NULL;
new->search_str = NULL;
return new;
}
2012-07-24 18:19:48 -04:00
void
p_autocomplete_clear(PAutocomplete ac)
2012-05-13 16:40:39 -04:00
{
2012-10-28 19:38:10 -04:00
g_slist_free_full(ac->items, free);
2012-05-13 16:40:39 -04:00
ac->items = NULL;
p_autocomplete_reset(ac);
}
2012-07-24 18:19:48 -04:00
void
p_autocomplete_reset(PAutocomplete ac)
2012-05-13 16:40:39 -04:00
{
ac->last_found = NULL;
if (ac->search_str != NULL) {
free(ac->search_str);
ac->search_str = NULL;
}
}
void
p_autocomplete_free(PAutocomplete ac)
{
p_autocomplete_clear(ac);
g_free(ac);
ac = NULL;
}
2012-07-24 18:19:48 -04:00
gboolean
p_autocomplete_add(PAutocomplete ac, void *item)
2012-05-13 16:40:39 -04:00
{
if (ac->items == NULL) {
ac->items = g_slist_append(ac->items, item);
return TRUE;
2012-05-13 16:40:39 -04:00
} else {
GSList *curr = ac->items;
2012-05-13 16:40:39 -04:00
while(curr) {
2012-05-13 16:40:39 -04:00
// insert
2012-10-28 19:38:10 -04:00
if (g_strcmp0(curr->data, item) > 0) {
2012-05-13 16:40:39 -04:00
ac->items = g_slist_insert_before(ac->items,
curr, item);
return TRUE;
2012-05-13 16:40:39 -04:00
// update
2012-10-28 19:38:10 -04:00
} else if (g_strcmp0(curr->data, item) == 0) {
2012-05-23 19:33:23 -04:00
// only update if data different
2012-10-28 19:38:10 -04:00
if (strcmp(curr->data, item) != 0) {
free(curr->data);
2012-05-23 19:33:23 -04:00
curr->data = item;
return TRUE;
} else {
return FALSE;
}
2012-05-13 16:40:39 -04:00
}
2012-05-13 16:40:39 -04:00
curr = g_slist_next(curr);
}
// hit end, append
ac->items = g_slist_append(ac->items, item);
return TRUE;
2012-05-13 16:40:39 -04:00
}
}
2012-07-24 18:19:48 -04:00
gboolean
p_autocomplete_remove(PAutocomplete ac, const char * const item)
2012-05-13 16:40:39 -04:00
{
// reset last found if it points to the item to be removed
if (ac->last_found != NULL)
2012-10-28 19:38:10 -04:00
if (g_strcmp0(ac->last_found->data, item) == 0)
2012-05-13 16:40:39 -04:00
ac->last_found = NULL;
if (!ac->items) {
return FALSE;
2012-05-13 16:40:39 -04:00
} else {
GSList *curr = ac->items;
2012-05-13 16:40:39 -04:00
while(curr) {
2012-10-28 19:38:10 -04:00
if (g_strcmp0(curr->data, item) == 0) {
void *current_item = curr->data;
2012-05-13 16:40:39 -04:00
ac->items = g_slist_remove(ac->items, curr->data);
2012-10-28 19:38:10 -04:00
free(current_item);
return TRUE;
2012-05-13 16:40:39 -04:00
}
curr = g_slist_next(curr);
}
return FALSE;
2012-05-13 16:40:39 -04:00
}
}
2012-07-24 18:19:48 -04:00
GSList *
p_autocomplete_get_list(PAutocomplete ac)
2012-05-13 16:40:39 -04:00
{
GSList *copy = NULL;
GSList *curr = ac->items;
while(curr) {
2012-10-28 19:38:10 -04:00
copy = g_slist_append(copy, strdup(curr->data));
2012-05-13 16:40:39 -04:00
curr = g_slist_next(curr);
}
return copy;
}
2012-07-24 18:19:48 -04:00
gchar *
p_autocomplete_complete(PAutocomplete ac, gchar *search_str)
2012-05-13 16:40:39 -04:00
{
gchar *found = NULL;
// no items to search
if (!ac->items)
return NULL;
// first search attempt
if (ac->last_found == NULL) {
ac->search_str =
2012-05-13 16:40:39 -04:00
(gchar *) malloc((strlen(search_str) + 1) * sizeof(gchar));
strcpy(ac->search_str, search_str);
found = _search_from(ac, ac->items);
2012-05-13 16:40:39 -04:00
return found;
// subsequent search attempt
2012-05-13 16:40:39 -04:00
} else {
// search from here+1 tp end
found = _search_from(ac, g_slist_next(ac->last_found));
2012-05-13 16:40:39 -04:00
if (found != NULL)
return found;
// search from beginning
found = _search_from(ac, ac->items);
2012-05-13 16:40:39 -04:00
if (found != NULL)
return found;
2012-05-13 16:40:39 -04:00
// we found nothing, reset search
p_autocomplete_reset(ac);
return NULL;
}
}
2012-07-24 18:19:48 -04:00
static gchar *
_search_from(PAutocomplete ac, GSList *curr)
2012-05-13 16:40:39 -04:00
{
while(curr) {
2012-05-13 16:40:39 -04:00
// match found
2013-01-14 17:09:31 -05:00
if (strncmp(curr->data, ac->search_str, strlen(ac->search_str)) == 0) {
2012-05-13 16:40:39 -04:00
// set pointer to last found
ac->last_found = curr;
// return the string, must be free'd by caller
2013-01-14 17:09:31 -05:00
return strdup(curr->data);
2012-05-13 16:40:39 -04:00
}
curr = g_slist_next(curr);
}
return NULL;
}