-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathjumble_word.c
More file actions
375 lines (317 loc) · 8.39 KB
/
Copy pathjumble_word.c
File metadata and controls
375 lines (317 loc) · 8.39 KB
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
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <ctype.h>
#include "jumblew.h"
/* TODO: NEED A JUMBLE TREE FREE ROUTINE */
int
jumble (jumble_tree_t * head)
{
char jumbled_word[MAX_LENGTH];
match_list_t *match_head;
while (1)
{
printf ("\n\nJumbled Word: ");
scanf (" %[^\n]", jumbled_word);
if (jumbled_word[0] == 'q' && jumbled_word[1] == NUL)
{
printf ("\nUser quit ");
break;
}
if ((match_head = search_jumble_tree (head, jumbled_word)) == NULL)
printf ("<no matches>");
else
{
// printf(" [FOUND] %p",match_node);
show_matches (match_head);
}
}
return 0;
}
/* Takes void, allocates a jumble_tree_t type pointer, loads the tree. *
* The head of the tree is returned to the calling function. */
jumble_tree_t *
load_file (char *wordlist)
{
FILE *fp;
char word[MAX_LENGTH];
long int current_file_position, last_file_position;
int loading_step_size;
jumble_tree_t *head;
// jumble_tree_t *current_node;
/* Store start time for runtime stats */
printf ("\n[Loading] <%s>...\n", wordlist);
/* Allocate and check if the head pointer */
head = allocate_new_node ();
if (node_is_empty (head) == TRUE)
{
fprintf (stderr, "Failed ... {Tree head is empty} ");
return NULL;
}
/* Open word list file and check */
fp = fopen (wordlist, "rb");
if (fp == NULL)
{
perror ("Failed ... {Cannot open} ");
return NULL;
}
/* Get the last file byte offset. To be used for loading feedback */
fseek (fp, 0, SEEK_END);
last_file_position = ftell (fp);
rewind (fp);
loading_step_size = last_file_position / 100000 + 1;
/* While there are no more words left in the file *
* read words and load them into the tree */
while (!feof (fp))
{
// current_node = head;
current_file_position = ftell (fp);
fscanf (fp, " %s", word);
/* Percentage loading feedback */
if (current_file_position % loading_step_size == 0)
printf ("\r\t%5.2f %% Complete",
((float) current_file_position / last_file_position) * 100);
/* Feedback Loaded Words */
// printf("\n[Loading] %*d -> %*s", NUMBER_FIELD_WIDTH , word_count , STRING_FIELD_WIDTH , word );
/* If the string word contains invalid characters, skip the word */
if (scan_for_valid_word (word) == FAIL)
continue;
word_count++;
index_into_jumble_tree (head, word);
}
fclose (fp);
printf ("\r\t%5.2f %% Complete", 100.0);
return head;
}
void
index_into_jumble_tree (jumble_tree_t *head, char *word)
{
jumble_tree_t *current_node, *temp;
int index, i;
char pattern[MAX_LENGTH];
current_node = head;
strcpy (pattern, word);
tolower_word (pattern);
sort_char (pattern);
/* Direct index */
/* While end of word (string) not reached advance in the histogram tree */
for (i = 0; pattern[i] != NUL; i++)
{
index = char_to_index (pattern[i]);
if (current_node->next[index] == NULL)
{
temp = allocate_new_node ();
current_node->next[index] = temp;
}
current_node = current_node->next[index];
}
/* After a word has end, store string */
add_current_word (current_node, word);
}
/* Searches the tree pointed by head for the unsorted pattern word *
* The function sorts the word characters and them searches the tree */
match_list_t *
search_jumble_tree (jumble_tree_t * head, char *word)
{
char pattern[MAX_LENGTH];
short int index, i;
jumble_tree_t *current_node;
if (node_is_empty (head) == TRUE)
{
fprintf (stderr, "\nTree head is empty ");
return NULL;
}
/* No need to scan because the index function would return EMPTY if an invalid char is found */
// if (scan_for_valid_word (word) == FAIL)
// {
// printf ("\n{invalid characters in} \"%s\"\n", word);
// return NULL;
// }
current_node = head;
/* Backup original word */
strcpy (pattern, word);
/* Sort pattern and make lowercase to make it ready to be searched */
tolower_word (pattern);
sort_char (pattern);
/* While pattern has not ended, advance in the histogram tree through the links *
* whenever the converted index points to null ie no path, stop searching, no match *
* If the pattern traverses through the tree successfully and the pattern has ended *
* check if the last node visited is a word end node, which is detected by, if the *
* file_pos array stores atleast one word location offset. If yes search succesful *
* return match node, else failed return NULL */
for (i = 0; pattern[i] != NUL; i++)
{
index = char_to_index (pattern[i]);
if (index == EMPTY)
return NULL;
if (current_node->next[index] == NULL)
{
return NULL;
}
current_node = current_node->next[index];
}
return (current_node->match_head);
}
/* NOTE: Shall this function print the match numbers also ? */
void
show_matches (match_list_t * match_head)
{
match_list_t *current_match;
short int i;
for (i = 1, current_match = match_head;
current_match != (match_list_t *) NULL;
current_match = current_match->next, i++)
{
printf ("\n\t[%d] %s", i, current_match->word);
}
}
/* Convert a character to index to be used in the historgram_tree -> next array *
* to move to the next node in the path. Each index is accessed directly */
short int char_to_index(char c)
{
short int index;
/* Convert the character to lowercase */
c = tolower(c);
if( isalpha (c) ) {
index = ( c - 'a' );
}
else if ( isdigit (c) ) {
index = ( c - '0' ) + 26;
}
else if( c == '\'' ) {
index = 36;
}
else if( c == '-' ) {
index = 37;
}
else if( c == '`' ) {
index = 38;
}
else if ( c == '.' ) {
index = 39;
}
else if ( c == '/') {
index = 40;
}
else if ( c == '&' ) {
index = 41;
}
else {
/* Not a valid character */
return EMPTY;
}
return index;
}
jumble_tree_t *
allocate_new_node (void)
{
jumble_tree_t *temp;
/* Allocate memory and check */
temp = (jumble_tree_t *) malloc (sizeof (jumble_tree_t));
if (node_is_empty (temp) == TRUE)
{
perror ("Cannot allocate memory ... Terminating ");
exit (1);
}
/* Allocate all the direct index locations */
temp->next = (jumble_tree_t **) calloc (sizeof (jumble_tree_t *), MAX_CHAR_SET);
if (temp->next == NULL)
{
printf ("Cannot allocate memory ... Terminating ");
exit (1);
}
/* Make the match head NULL */
temp->match_head = NULL;
return temp;
}
/* Unallocate TEST functions */
void
free_match_list (match_list_t *head)
{
match_list_t *temp;
while (head!=NULL)
{
temp = head;
head = head->next;
free (temp->word);
free (temp);
}
}
void
free_jumble_tree (jumble_tree_t *head)
{
int i;
if (head == NULL)
return;
for (i=0; i<MAX_CHAR_SET; i++)
{
free_jumble_tree (head->next[i]);
}
free_match_list (head->match_head);
free (head->next);
free (head);
}
/* Unallocate TEST functions END */
void
tolower_word (char *word)
{
int i;
for (i = 0; word[i] != NUL; i++)
{
word[i] = tolower (word[i]);
}
}
void
sort_char (char *word)
{
qsort (word, strlen (word), sizeof (char), compare_char);
}
int
compare_char (const void *a, const void *b)
{
return *((const char *) a) - *((const char *) b);
}
int
scan_for_valid_word (const char *word)
{
int i;
/* Only consider word to be valid with length greater than MIX_LENGTH */
/* NOTE: Keep this commented for faster load, because this will only filter 1 length words,
this is kept for future development */
/*if ( strlen (word) < MIN_LENGTH )
return FAIL;
*/
for (i = 0; word[i] != NUL; i++)
{
if (char_to_index (word[i]) == EMPTY)
return FAIL;
}
return SUCCESS;
}
void
add_current_word (jumble_tree_t *current_node, char *word)
{
int len;
match_list_t *temp;
len = strlen (word);
temp = (match_list_t *) malloc (sizeof (match_list_t));
if (temp == NULL)
{
perror ("Cannot Allocate match_list_t node");
exit (1);
}
temp->word = (char *) malloc (sizeof (char) * (len + 1));
if (temp->word == NULL)
{
perror ("Cannot Allocate match_list_t -> word");
exit (1);
}
strcpy (temp->word, word);
temp->next = current_node->match_head;
current_node->match_head = temp;
}
int
node_is_empty (jumble_tree_t * current_node)
{
return (current_node == (jumble_tree_t *) NULL);
}