-
Notifications
You must be signed in to change notification settings - Fork 0
/
dictfile.c
157 lines (133 loc) · 3.4 KB
/
dictfile.c
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
149
150
151
152
153
154
155
156
157
/*
* This file is part of dicepwgen
*
* Copyright (C) 2015 T.v.Dein.
*
* This program is free software: you can redistribute it and/or modify
* it under the terms of the GNU General Public License as published by
* the Free Software Foundation, either version 3 of the License, or
* (at your option) any later version.
*
* This program is distributed in the hope that it will be useful,
* but WITHOUT ANY WARRANTY; without even the implied warranty of
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
* GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*
* You can contact me by mail: <tom AT vondein DOT org>.
*/
#include "dictfile.h"
int *incr_dicedigit(int *digits) {
/*
increment an array of dice digits, we expect the first to
be a multiple of 10000, the 2nd a multiple of 1000 and so on.
*/
if(digits[4] == 6) {
digits[4] = 1;
if(digits[3] == 60) {
digits[3] = 10;
if(digits[2] == 600) {
digits[2] = 100;
if(digits[1] == 6000) {
digits[1] = 1000;
digits[0] += 10000; /* may overflow to 71111, must be catched by caller */
}
else
digits[1] += 1000;
}
else
digits[2] += 100;
}
else
digits[3] += 10;
}
else
digits[4]++;
return digits;
}
int get_dicenum(int *digits) {
/*
get the actual number of an array of dice digits
*/
int i = 0;
int pos = 0;
for(i=0; i<5; i++)
pos += digits[i];
return pos;
}
char **fetch_dict(char *dictfile) {
/*
read in the dictionary file. we generate an array of max
66666 entries from the dictionary, each entry's index will
be a dice number (1 <=> 6). to enhance randomness, we jump
over a number of lines (1-32 lines) and start from the beginning
of the file if we reach the end before our array is full.
*/
char **words;
int pos, i, next, jump;
char *line = NULL;
size_t len = 0;
ssize_t linelen;
FILE *DICT;
int *digits;
if((DICT = fopen(dictfile, "rb")) == NULL) {
perror("Could not open dictfile");
exit(1);
}
words = malloc(66666 * sizeof(char *));
digits = malloc(5 * sizeof(int));
jump = rand_lim(32);
digits[0] = 10000;
digits[1] = 1000;
digits[2] = 100;
digits[3] = 10;
digits[4] = 1;
pos = 11111;
next = 0;
for(i=0; i<6666; i++)
words[i] = NULL;
LOOP:
while ((linelen = getline(&line, &len, DICT)) != -1) {
if(! dontjump) {
if(jump > 0) {
jump--;
continue;
}
else {
jump = rand_lim(32);
}
}
if(linelen >= WMIN+1 && linelen <= WMAX+1) {
line[linelen-1] = '\0'; /* remove newline */
for(i=0; i<linelen-1; i++) {
if(isalnum((int)line[i]) == 0) {
next = 1;
break;
}
}
if(next) {
next = 0;
continue;
}
words[pos] = malloc(linelen);
strncpy( words[pos], line, linelen);
if(verbose > 1)
debug("add to wordlist at index %d: %s", pos, line);
digits = incr_dicedigit(digits);
pos = get_dicenum(digits);
/* this is what pos gets next after 66666, which is max reachable with 5 dices */
if(pos == 71111)
break;
} /* endif word 4<=>10 */
} /* while read */
if(pos < 66666) {
fseek(DICT, 0L, SEEK_SET);
goto LOOP;
}
fclose(DICT);
free(line);
free(digits);
return words;
}