]> the.earth.li Git - onak.git/blob - wordlist.c
Cleanup various includes
[onak.git] / wordlist.c
1 /*
2  * wordlist.c - Routines for manipulating word lists
3  *
4  * Copyright 2004 Jonathan McDowell <noodles@earth.li>
5  *
6  * This program is free software: you can redistribute it and/or modify it
7  * under the terms of the GNU General Public License as published by the Free
8  * Software Foundation; version 2 of the License.
9  *
10  * This program is distributed in the hope that it will be useful, but WITHOUT
11  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
12  * FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
13  * more details.
14  *
15  * You should have received a copy of the GNU General Public License along with
16  * this program; if not, write to the Free Software Foundation, Inc., 51
17  * Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
18  */
19
20 #include <ctype.h>
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24
25 #include "ll.h"
26 #include "decodekey.h"
27 #include "keystructs.h"
28 #include "wordlist.h"
29
30 /**
31  *      makewordlist - Takes a string and splits it into a set of unique words.
32  *      @wordlist: The current word list.
33  *      @words: The string to split and add.
34  *
35  *      We take words and split it on non alpha numeric characters. These get
36  *      added to the word list if they're not already present. If the wordlist
37  *      is NULL then we start a new list, otherwise it's search for already
38  *      added words. Note that words is modified in the process of scanning.
39  *
40  *      Returns the new word list.
41  */
42 struct ll *makewordlist(struct ll *wordlist, char *word)
43 {
44         char *start = NULL;
45         char *end = NULL;
46
47         /*
48          * Walk through the words string, spliting on non alphanumerics and
49          * then checking if the word already exists in the list. If not then
50          * we add it.
51          */
52         end = word;
53         while (end != NULL && *end != 0) {
54                 start = end;
55                 while (*start != 0 && (ispunct(*start) || isspace (*start))) {
56                         start++;
57                 }
58                 end = start;
59                 while (*end != 0 && (!ispunct(*end) && !isspace (*end))) {
60                         *end = tolower(*end);
61                         end++;
62                 }
63                 if (end - start > 1) {
64                         if (*end != 0) {
65                                 *end = 0;
66                                 end++;
67                         }
68
69                         if (llfind(wordlist, start, 
70                                 (int (*)(const void *, const void *)) strcmp
71                                         ) == NULL) {
72                                 wordlist = lladdend(wordlist, start);
73                         }
74                 }
75         }
76         return wordlist;
77 }
78
79 /**
80  *      makewordlistfromkey - Takes a public key and splits it into a set of 
81  *                     unique words.
82  *      @wordlist: The current word list.
83  *      @key: The key to return the words from.
84  *
85  *      We take words and split it on non alpha numeric characters. These get
86  *      added to the word list if they're not already present. If the wordlist
87  *      is NULL then we start a new list, otherwise it's search for already
88  *      added words. Note that words is modified in the process of scanning.
89  *
90  *      Returns the new word list.
91  */
92 struct ll *makewordlistfromkey(struct ll *wordlist,
93                                struct openpgp_publickey *key)
94 {
95         char      **uids;
96         int         i;
97         struct ll  *words = NULL;
98         struct ll  *wl = NULL;
99
100         uids = keyuids(key, NULL);
101         for (i = 0; uids != NULL && uids[i] != NULL; ++i) {
102                 words = makewordlist(NULL, uids[i]);
103                 for (wl = words; wl != NULL; wl = wl->next) {
104                         if (llfind(wordlist, wl->object, 
105                                 (int (*)(const void *, const void *)) strcmp
106                                                 ) == NULL) {
107                                 wordlist = lladd(wordlist, strdup(wl->object));
108                         }
109                 }
110                 free(uids[i]);
111                 uids[i] = NULL;
112         }
113         free(uids);
114
115         return wordlist;
116 }