1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148
|
/* $Cambridge: hermes/src/prayer/session/dictionary.c,v 1.3 2008/09/16 09:59:58 dpc22 Exp $ */
/************************************************
* Prayer - a Webmail Interface *
************************************************/
/* Copyright (c) University of Cambridge 2000 - 2008 */
/* See the file NOTICE for conditions of use and distribution. */
#include "prayer_session.h"
/* dictionary_create() ***************************************************
*
* Create a fresh dictionary structure, including own pool.
************************************************************************/
struct dictionary *dictionary_create()
{
struct pool *pool = pool_create(DICTIONARY_POOL_SIZE);
struct dictionary *d;
d = pool_alloc(pool, sizeof(struct dictionary));
d->pool = pool;
d->list = list_create(pool, T);
d->assoc = assoc_create(pool, 16, T);
return (d);
}
/* dictionary_free() ******************************************************
*
* Free dictionary structure
*************************************************************************/
void dictionary_free(struct dictionary *d)
{
pool_free(d->pool);
}
/* ====================================================================== */
/* dictionary_print_options() *********************************************
*
* Convert dictionary to text form suitable for user preferences file.
* dictionary:
* b: Output buffer
*************************************************************************/
void
dictionary_print_options(struct dictionary *dictionary, struct buffer *b)
{
unsigned long offset = 0;
struct list_item *li;
for (li = dictionary->list->head; li; li = li->next) {
if ((offset + strlen(li->name)) >= 76) {
/* Add word to start of line */
if (offset > 0)
bputs(b, "" CRLF);
bputs(b, li->name);
offset = strlen(li->name);
} else {
/* Add word to end of existing (non-empty) line */
if (offset > 0)
bputc(b, ' ');
bputs(b, li->name);
offset += strlen(li->name);
}
}
if (offset > 0)
bputs(b, "" CRLF);
}
/* dictionary_parse_line() ***********************************************
*
* Parse a line from user preferences file.
* dictionary: dictionary to add to
* line: Line to parse
* session: For logging purposes only
*
************************************************************************/
void
dictionary_parse_line(struct dictionary *dictionary, char *line,
struct session *session)
{
char *word;
while ((word = string_get_token(&line))) {
if (word[0])
dictionary_add(dictionary, word);
}
}
/* ====================================================================== */
/* dictionary_add() ******************************************************
*
* Add a word to the dictionary
* d: Dictionary
* word: Word to add (copied into dictionaries private pool)
************************************************************************/
void dictionary_add(struct dictionary *d, char *word)
{
word = pool_strdup(d->pool, word);
/* Check whether word is already in the dictionary */
if (assoc_lookup(d->assoc, word))
return;
/* Add word to assoc chain */
assoc_update(d->assoc, word, "1", NIL);
/* Insert word at correct sorted place in word list */
list_insert_sorted(d->list, NIL, word);
}
/* dictionary_remove() ***************************************************
*
* Remove a word from the dictionary
* d: Dictionary
* word: Word to remove
************************************************************************/
void dictionary_remove(struct dictionary *d, char *word)
{
word = pool_strdup(d->pool, word);
/* Remove word from assoc list */
assoc_delete(d->assoc, word);
/* Remove word from list */
list_remove_byname(d->list, word);
}
/* dictionary_lookup() ****************************************************
*
* Lookup word in the dictionary.
* d: dictionary
* word: Word to look up
*
* Returns: T if word was in dictionary, NIL otherwise
*************************************************************************/
BOOL dictionary_lookup(struct dictionary *d, char *word)
{
return ((assoc_lookup(d->assoc, word)) ? T : NIL);
}
|