Refactor autocompleters

This commit is contained in:
James Booth
2013-06-02 19:56:35 +01:00
parent f72e7d89b8
commit af95c82fe7
3 changed files with 362 additions and 330 deletions

View File

@@ -20,6 +20,7 @@
*
*/
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
@@ -201,6 +202,65 @@ autocomplete_complete(Autocomplete ac, gchar *search_str)
}
}
char *
autocomplete_param_with_func(char *input, int *size, char *command,
autocomplete_func func)
{
char *found = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
char *command_cpy = malloc(strlen(command) + 2);
sprintf(command_cpy, "%s ", command);
int len = strlen(command_cpy);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
found = func(inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, command_cpy);
strcat(auto_msg, found);
free(found);
}
}
free(command_cpy);
return auto_msg;
}
char *
autocomplete_param_with_ac(char *input, int *size, char *command,
Autocomplete ac)
{
char *found = NULL;
char *auto_msg = NULL;
char inp_cpy[*size];
int i;
char *command_cpy = malloc(strlen(command) + 2);
sprintf(command_cpy, "%s ", command);
int len = strlen(command_cpy);
if ((strncmp(input, command_cpy, len) == 0) && (*size > len)) {
for(i = len; i < *size; i++) {
inp_cpy[i-len] = input[i];
}
inp_cpy[(*size) - len] = '\0';
found = autocomplete_complete(ac, inp_cpy);
if (found != NULL) {
auto_msg = (char *) malloc((len + (strlen(found) + 1)) * sizeof(char));
strcpy(auto_msg, command_cpy);
strcat(auto_msg, found);
free(found);
}
}
free(command_cpy);
return auto_msg;
}
static gchar *
_search_from(Autocomplete ac, GSList *curr)
{

View File

@@ -25,6 +25,7 @@
#include <glib.h>
typedef char*(*autocomplete_func)(char *);
typedef struct autocomplete_t *Autocomplete;
typedef const char * (*PStrFunc)(const void *obj);
typedef void * (*PCopyFunc)(const void *obj);
@@ -42,5 +43,9 @@ gboolean autocomplete_remove(Autocomplete ac, const char * const item);
GSList * autocomplete_get_list(Autocomplete ac);
gchar * autocomplete_complete(Autocomplete ac, gchar *search_str);
gint autocomplete_length(Autocomplete ac);
char * autocomplete_param_with_func(char *input, int *size, char *command,
autocomplete_func func);
char * autocomplete_param_with_ac(char *input, int *size, char *command,
Autocomplete ac);
#endif