-
Notifications
You must be signed in to change notification settings - Fork 0
/
indexer.c
380 lines (367 loc) · 12.4 KB
/
indexer.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
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
376
377
378
379
380
#include <dirent.h>
#include "indexer.h"
Index *IndexCreate(char* indexName) {
int i;
Index* index = (Index*)calloc(1, sizeof(Index));
index->Array = malloc(36 * sizeof(tkNode));
/*initialize the hash table*/
for(i = 0; i < 36; i++){
index->Array[i].next = NULL;
index->Array[i].fileNodePTR = NULL;
index->Array[i].tk = NULL;
}
/*creates output file*/
file_write = fopen(indexName, "w");
if (index != NULL) {
return index;
}
return NULL;
}
void IndexInsert (Index *indx, char *tk,const char *fileName)
{
int compare;
int duplicate = 0;
int dupCompare;
int hashPosition;
int tokenNode = tk[0];
int compareFile;
fileNode *myFileNode = calloc(1, sizeof(fileNode));
fileNode *currFile, *prevFile;
fileNode *tempCurr, *tempPrev;
fileNode *MynewFile;
tkNode *myTokenNode;
tkNode *current;
tkNode *previous;
myFileNode->fileName = fileName;
myFileNode->next = NULL;
myFileNode->Count = 1;
hashPosition = Hash(tokenNode);
/*front of linked list is empty*/
if(indx->Array[hashPosition].tk == NULL)
{
indx->Array[hashPosition].tk = tk;
indx->Array[hashPosition].fileNodePTR = myFileNode;
}
else
{
/*linked list not empty*/
myTokenNode = malloc(sizeof(tkNode));
myTokenNode->tk = tk;
myTokenNode->next = NULL;
myTokenNode->fileNodePTR = myFileNode;
current = &indx->Array[hashPosition];
previous = NULL;
/*determine if a duplicate token exists*/
while(current != NULL)
{
dupCompare = compareStrings(myTokenNode->tk, current->tk);
if(dupCompare == 0){
duplicate = 1;
}
previous = current;
current = current->next;
}
current = &indx->Array[hashPosition];
previous = NULL;
/*determine token position in linked list*/
while(current != NULL)
{
compare = compareStrings(myTokenNode->tk, current->tk);
/*token goes before the token its being compared to*/
if(compare < 0 && !duplicate)
{
/*new token needs to be at front of linked list*/
if(previous == NULL)
{
/*a node already exists will be placed front of list*/
if(indx->Array[hashPosition].next != NULL)
{
current->next = indx->Array[hashPosition].next;
indx->Array[hashPosition].next = myTokenNode;
myTokenNode->tk = indx->Array[hashPosition].tk;
myTokenNode->fileNodePTR = indx->Array[hashPosition].fileNodePTR;
indx->Array[hashPosition].tk = tk;
indx->Array[hashPosition].fileNodePTR = myFileNode;
}
/*only one token in the list*/
else
{
indx->Array[hashPosition].next = myTokenNode;
myTokenNode->tk = indx->Array[hashPosition].tk;
myTokenNode->fileNodePTR = indx->Array[hashPosition].fileNodePTR;
indx->Array[hashPosition].tk = tk;
indx->Array[hashPosition].fileNodePTR = myFileNode;
}
}
/*insert token in the middle*/
else
{
myTokenNode->next = previous->next;
previous->next = myTokenNode;
}
return;
}
else if(compare > 0 && !duplicate)
{
/*token goes at the end of linked list*/
if(current->next == NULL)
{
current->next = myTokenNode;
return;
}
}
/*increase file frequency count or add new file node*/
else if(compare == 0)
{
currFile = current->fileNodePTR;
prevFile = NULL;
/*find if there exists the same file*/
while(currFile != NULL)
{
compareFile = compareStrings((char*)fileName, (char*)currFile->fileName);
/*word and file already exists increment frequency count*/
if(compareFile == 0)
{
currFile->Count++;
/*sort if necassary*/
if(prevFile != NULL)
{
/*sorts filenames according to token count*/
if(prevFile->Count > currFile->Count)
{
return;
}
/*files out of order*/
else
{
/*take file_node out of list to re-sort*/
prevFile->next = currFile->next;
tempCurr = current->fileNodePTR;
tempPrev = NULL;
while(tempCurr != NULL)
{
if(tempCurr->Count < currFile->Count)
{
if(tempPrev == NULL)
{
current->fileNodePTR = currFile;
currFile->next = tempCurr;
return;
}
else
{
tempPrev->next = currFile;
currFile->next = tempCurr;
return;
}
}
tempPrev = tempCurr;
tempCurr = tempCurr->next;
}
}
}
/*file incremented was first in list*/
else
{
return;
}
}
prevFile = currFile;
currFile = currFile->next;
}
/*first occurence of this word in this file*/
MynewFile = (fileNode*)calloc(1,(sizeof(fileNode)));
MynewFile->fileName = fileName;
MynewFile->Count = 1;
MynewFile->next = NULL;
prevFile->next = MynewFile;
return;
}
previous = current;
current = current->next;
}
}
}
void IndexOutput(Index *indx) {/*original*/
int i, j;
tkNode *tkNode_Ptr;
fileNode *fileNodeTmpPtr;
j = 0;
/*check all buckets in index*/
for(i = 0; i < 36; i++)
{
tkNode_Ptr = &indx->Array[i];
fileNodeTmpPtr = indx->Array[i].fileNodePTR;
/*while there is a token in the index bucket*/
while(tkNode_Ptr != NULL)
{
/*if the token is not null, print header with token*/
if(tkNode_Ptr->tk != NULL)
{
fprintf(file_write, "<list> %s \n \n", tkNode_Ptr->tk);
}
/*while the fileNode is not null*/
while(fileNodeTmpPtr != NULL)
{
/*if the next fileNode done, print the end of the list*/
if (fileNodeTmpPtr->next == NULL)
{
fprintf(file_write, " %s %d \n \n </list> \n \n", fileNodeTmpPtr->fileName, fileNodeTmpPtr->Count);
break;
/*otherwise print the file nodes and counts*/
}
else
{
/*if there are 5 nodes on a line, make a new line*/
if (j == 5)
{
fprintf(file_write, "\n");
j = 0;
/*print the file nodes and counts*/
}
else
{
fprintf(file_write, " %s %d ", fileNodeTmpPtr->fileName, fileNodeTmpPtr->Count);
fileNodeTmpPtr = fileNodeTmpPtr->next;
j++;
}
}
}
j = 0;
tkNode_Ptr = tkNode_Ptr->next;
if (tkNode_Ptr != NULL)
{
fileNodeTmpPtr = tkNode_Ptr->fileNodePTR;
}
}
}
}
int Hash(char c) {
int charNum = c;
if(charNum >= 48 && charNum <= 57) {
charNum = charNum - 48;
return charNum;
}
if(charNum >= 97 && charNum <= 122) {
charNum = charNum - 87;
return charNum;
}
printf("Character not in expected range!\n ");
return 0;
}
int hashToken(const char *fileName){
int fileCheck;
char c, *sep, *token;
char *string = (char*)malloc(sizeof(char));
string = "";
sep = " ~`!@#$%^&*()-_=+{[}]|;:\"<,.>/?\n";
fileCheck = access(fileName, F_OK);
if(fileCheck == 0){ /*open file */
file_read = fopen(fileName, "r");
c = getc(file_read);
while(c != EOF){ /*create string to send to tokenizer*/
c = tolower(c);
string = Concat(string, c);
c = getc(file_read);
}
}
tokenizer = TKCreate(sep, string);
/*free(string);*/
token = TKGetNextToken(tokenizer);
while(token != NULL ){ /*parse and insert tokens into hash table*/
IndexInsert(indx, token, fileName);
token = TKGetNextToken(tokenizer);
}
/*free(token);*/
fclose(file_read);
return 0;
}
int ReadDir(char* dirPath)
{
char *dirPATH = dirPath; /*string contains directory path*/
char *fileAccesPath; /*string containing file path */
DIR *dirStream; /*directory stream */
struct dirent *readDir;
/*if file hash it*/
if(access(dirPATH, F_OK) ==-1)
{
printf("Directory or file doesn't exist\n");
return 0;
}
if((dirStream = opendir(dirPATH)) == 0)
{
hashToken(dirPATH);
return 0;
}
/*search through the file system to find and read all files*/
while((readDir = readdir(dirStream)) != 0)
{
if(readDir->d_type == DT_REG && compareStrings(readDir->d_name, ".DS_Store")) /*not hidden file*/
{
fileAccesPath = dirPATH;
fileAccesPath = Concat(dirPATH, '/');/*add "/" to string in dirPath*/
fileAccesPath = ConcatStr(fileAccesPath, readDir->d_name);
hashToken(fileAccesPath);
}
else if(readDir->d_type == DT_DIR)/*it is a directory*/
{
if(!compareStrings(readDir->d_name,".") || !compareStrings(readDir->d_name,".."))
{ /*if directory is a . or .. skip it*/
continue;
}
else
{
dirPATH = Concat(dirPATH, '/');
dirPATH = ConcatStr(dirPATH, readDir->d_name); /*concatenate "/" and directory name for recursing*/
ReadDir(dirPATH);
dirPATH = dirPath; /*after recursing reset path to current directory*/
}
}
}
closedir(dirStream);
return 0;
}
char *Concat(char *string, char letter){ /*concatenate character to end of a string*/
char *str = string;
char let = letter;
size_t len = strlen(str);
char *result = (char*) malloc((len + 2) * sizeof(char));
memset (result,0,len+2);
strcpy(result, str);
result[len] = let;
return result;
}
char *ConcatStr(char *string1, char *string2){ /*concatenate 2 strings together*/
size_t len = strlen(string2);
int i;
for(i = 0; i<len; i++){
string1 = Concat(string1, string2[i]);
}
return string1;
}
void IndexDestroy(Index *indx) {
int i;
tkNode *tempT, *next;
fileNode *tempF, *next2;
for(i = 0; i < 36; i++){
for(tempT = indx->Array[i].next; tempT != NULL; tempT = next){
next = tempT->next;
tempT->tk = NULL;
for(tempF = tempT->fileNodePTR; tempF != NULL; tempF = next2){
next2 = tempF->next;
tempF->fileName = NULL;
tempF->Count = 0;
/*free(tempF);*/
}
/*free(tempT);*/
}
/*free(indx);*/
}
if(tokenizer != NULL){
/*TKDestroy(tokenizer);*/
}
fclose(file_write);
}
int compareStrings(char *p1, char *p2){
return strcmp(p1, p2);
}