From 7d5dbf4a19ec6bbd36784f5d7307629b69dda873 Mon Sep 17 00:00:00 2001 From: Paulo Cesar Pereira de Andrade Date: Wed, 12 Mar 2008 21:52:30 -0300 Subject: Add a generic hash table interface to replace the other implementations. --- hook.c | 59 +++++++++++++++++++++++++++-------------------------------- 1 file changed, 27 insertions(+), 32 deletions(-) (limited to 'hook.c') diff --git a/hook.c b/hook.c index 13a8b94..913ccc3 100644 --- a/hook.c +++ b/hook.c @@ -39,6 +39,7 @@ #include "xedit.h" #include "re.h" +#include "util.h" #include #include #include @@ -46,11 +47,11 @@ /* * Types */ -typedef struct _ReplaceList { - char *word; +typedef struct _ReplaceEntry { + hash_key *word; + struct _ReplaceEntry *next; char *replace; - struct _ReplaceList *next; -} ReplaceList; +} ReplaceEntry; typedef enum { SubstituteDisabled, @@ -108,7 +109,7 @@ static void SubstituteCallback(Widget, XtPointer, XtPointer); * Initialization */ #define STRTBLSZ 11 -static ReplaceList *replace_list[STRTBLSZ]; +static hash_table *replace_hash; static EditInfo einfo; extern Widget scratch; @@ -191,6 +192,8 @@ StartAutoReplace(void) if (!replace || !*replace) return (False); + replace_hash = hash_new(STRTBLSZ, NULL); + left = XtMalloc(llen = 256); right = XtMalloc(rlen = 256); while (*replace) { @@ -247,34 +250,26 @@ StartAutoReplace(void) static char * ReplacedWord(char *word, char *replace) { - ReplaceList *list; - int ii = 0; - char *pp = word; - - while (*pp) - ii = (ii << 1) ^ *pp++; - if (ii < 0) - ii = -ii; - ii %= STRTBLSZ; - for (list = replace_list[ii]; list; list = list->next) - if (strcmp(list->word, word) == 0) { - if (replace) { - XtFree(list->replace); - list->replace = XtNewString(replace); - } - return (list->replace); - } - - if (!replace) - return (NULL); - - list = XtNew(ReplaceList); - list->word = XtNewString(word); - list->replace = XtNewString(replace); - list->next = replace_list[ii]; - replace_list[ii] = list; + int length; + ReplaceEntry *entry; + + length = strlen(word); + entry = (ReplaceEntry *)hash_check(replace_hash, word, length); + if (entry == NULL && replace != NULL) { + entry = XtNew(ReplaceEntry); + entry->word = XtNew(hash_key); + entry->word->value = XtNewString(word); + entry->word->length = length; + entry->next = NULL; + entry->replace = XtNewString(replace); + hash_put(replace_hash, (hash_entry *)entry); + } + else if (replace) { + XtFree(entry->replace); + entry->replace = XtNewString(replace); + } - return (list->replace); + return (entry ? entry->replace : NULL); } static void -- cgit v1.2.3