-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.c
104 lines (95 loc) · 3.46 KB
/
app.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
#include "app.h"
/*
* The programs main method
* Takes in command line pseudo agruments
* and takes the corresponding action
*
* ./executable parse <file_name>
* will parse the filename to stdout.
* Approximate execution time 1 minutes.
*
* ./executable index <parsed_file_name>
* will create three index files from this parsed input file.
* You need to ensure there is a folder relative to where you're executing called index for the indexes to be written too.
* Approximate execution time 2 minutes.
*
* ./executable search
* Will listen on stdin until EOF, therefore we can pipe a line seperated query file to this.
* The executable must be run with the indexs relative to the executable file.
*
*
*/
int main(int argc, char **argv){
char buffer[BUFFER_SIZE];
char* token;
char* temp_token;
int term_count = 0;
int term_max = 10;
char** terms;
int i;
int len;
int inner_i;
htable dict;
if(argc > 0){
if(strcmp(argv[1], "parse") == 0){
fprintf(stderr, "Parsing! input file: %s\n\n", argv[2]);
parse(argv[2]);
}
else if(strcmp(argv[1], "index") == 0){
fprintf(stderr, "Indexing!\ninput file: %s, this may take a while!\n\n", argv[2]);
create_index(argv[2]);
}
else if(strcmp(argv[1], "search") == 0){
/* Search from std in */
dict = search_load_index();
while(fgets(buffer, BUFFER_SIZE, stdin)){
terms = emalloc(sizeof(terms[0]) * term_max);
token = strtok(buffer, " ");
while(token != NULL) {
/* Test to make sure this is valid first */
/* Remove Non Alphabetical */
len = strlen(token);
temp_token = emalloc( ( len + 1) * sizeof(token[0]));
inner_i = 0;
for(i = 0; i < len; i++){
if(isalpha(token[i])){
temp_token[inner_i++] = tolower(token[i]);
}
}
temp_token[inner_i] = '\0';
if(term_count == term_max - 1){
term_max *= 2;
terms = erealloc(terms, sizeof(terms[0]) * term_max);
}
terms[term_count] = emalloc( (len + 1) * sizeof(temp_token[0]));
strcpy(terms[term_count++], temp_token);
token = strtok(NULL, " ");
}
if(term_count > 0){
search_for_terms(dict, terms, term_count);
for(i = 0; i < term_count; i++){
free(terms[i]);
}
}
free(temp_token);
free(terms);
term_max = 10;
term_count = 0;
}
}
else if( strcmp(argv[1], "help") == 0
|| strcmp(argv[1], "-h") == 0
|| strcmp(argv[1], "man") == 0
){
fprintf(stderr, "\nUsage:\nparse\tfile_to_parse > output_file\n");
fprintf(stderr, "index\tfile_to_index (Indexes will be created in index folder)\n");
fprintf(stderr, "search\tQuery to search for will be taken from stdin (Indexes will be loaded from index folder)\n");
fprintf(stderr, "\n");
}
return EXIT_SUCCESS;
}
else{
printf("What");
}
return EXIT_SUCCESS;
}