cscvs to tla changeset 135
[onak.git] / wordlist.c
1 /*
2  * wordlist.c - Routines for manipulating word lists
3  *
4  * Jonathan McDowell <noodles@earth.li>
5  *
6  * Copyright 2004 Project Purple
7  *
8  * $Id: wordlist.c,v 1.2 2004/05/28 02:55:49 noodles Exp $
9  */
10
11 #include <ctype.h>
12 #include <stdio.h>
13 #include <string.h>
14
15 #include "ll.h"
16 #include "decodekey.h"
17 #include "log.h"
18 #include "wordlist.h"
19
20 /**
21  *      makewordlist - Takes a string and splits it into a set of unique words.
22  *      @wordlist: The current word list.
23  *      @words: The string to split and add.
24  *
25  *      We take words and split it on non alpha numeric characters. These get
26  *      added to the word list if they're not already present. If the wordlist
27  *      is NULL then we start a new list, otherwise it's search for already
28  *      added words. Note that words is modified in the process of scanning.
29  *
30  *      Returns the new word list.
31  */
32 struct ll *makewordlist(struct ll *wordlist, char *word)
33 {
34         char *start = NULL;
35         char *end = NULL;
36
37         /*
38          * Walk through the words string, spliting on non alphanumerics and
39          * then checking if the word already exists in the list. If not then
40          * we add it.
41          */
42         end = word;
43         while (end != NULL && *end != 0) {
44                 start = end;
45                 while (*start != 0 && !isalnum(*start)) {
46                         start++;
47                 }
48                 end = start;
49                 while (*end != 0 && isalnum(*end)) {
50                         *end = tolower(*end);
51                         end++;
52                 }
53                 if (end - start > 1) {
54                         if (*end != 0) {
55                                 *end = 0;
56                                 end++;
57                         }
58
59                         if (llfind(wordlist, start, strcmp) == NULL) {
60                                 wordlist = lladd(wordlist, start);
61                         }
62                 }
63         }
64         return wordlist;
65 }
66
67 /**
68  *      makewordlistfromkey - Takes a public key and splits it into a set of 
69  *                     unique words.
70  *      @wordlist: The current word list.
71  *      @key: The key to return the words from.
72  *
73  *      We take words and split it on non alpha numeric characters. These get
74  *      added to the word list if they're not already present. If the wordlist
75  *      is NULL then we start a new list, otherwise it's search for already
76  *      added words. Note that words is modified in the process of scanning.
77  *
78  *      Returns the new word list.
79  */
80 struct ll *makewordlistfromkey(struct ll *wordlist,
81                                struct openpgp_publickey *key)
82 {
83         char      **uids;
84         int         i;
85         struct ll  *words = NULL;
86         struct ll  *wl = NULL;
87
88         uids = keyuids(key, NULL);
89         for (i = 0; uids[i] != NULL; ++i) {
90                 words = makewordlist(wordlist, uids[i]);
91                 for (wl = words; wl->next; wl = wl->next) {
92                         if (llfind(wordlist, wl->object, strcmp) == NULL) {
93                                 wordlist = lladd(wordlist, strdup(wl->object));
94                         }
95                 }
96                 free(uids[i]);
97                 uids[i] = NULL;
98         }
99         free(uids);
100
101         return wordlist;
102 }