-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathconcord3.c
More file actions
352 lines (320 loc) · 10 KB
/
concord3.c
File metadata and controls
352 lines (320 loc) · 10 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
#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ctype.h>
#include "emalloc.h"
#include "seng265-list.h"
const int MAX_KEYWORD_LEN = 40;
const int MAX_LINE_LEN = 100;
/* prints all text of node_t in linked-list */
void printList(node_t *head) {
while (head != NULL) {
printf("%s\n", head->text);
head = head->next;
}
}
/* prints all keys and lines of node_k in linked-list */
void printListK(node_k *head) {
while (head != NULL) {
printf("key: %s\n", head->keyword);
printf("line: %s\n", head->line);
head = head->next;
}
}
/* prints all chars of node_c in linked-list */
void printListC(node_c *head) {
while (head != NULL) {
printf("%c", head->ch);
head = head->next;
}
printf("\n");
}
/* frees all nodes in node_t linked-list */
void freeList(node_t *head) {
node_t *temp = NULL;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
/* frees all nodes in node_k linked-list */
void freeListK(node_k *head) {
node_k *temp = NULL;
while (head != NULL) {
temp = head;
head = head->next;
free(temp);
}
}
/* returns all uppercase version of word */
char* make_upper(char *word) {
int len = strlen(word);
char *upper = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
strncpy(upper, word, MAX_KEYWORD_LEN);
for (int i = 0; i < len; i++) {
upper[i] = toupper(word[i]);
}
return upper;
}
/* returns all lowercase version of word */
char *make_lower(char *word) {
int len = strlen(word);
char *lower = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
strncpy(lower, word, MAX_KEYWORD_LEN);
for (int i = 0; i < len; i++) {
lower[i] = tolower(word[i]);
}
return lower;
}
/* creates and returns linked-list of node_t storing all exclusion words from input file */
node_t* loadExclusionWords(node_t *exclusion_words) {
char *cur_line = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
node_t *temp_node = NULL;
while (fgets(cur_line, sizeof(char) * MAX_KEYWORD_LEN, stdin)) {
int line_len = strlen(cur_line);
if (cur_line[line_len-1] == '\n') {
cur_line[line_len-1] = 0;
}
if (strcmp(cur_line, "1") == 0) {
printf("Input is version 1, concord3 expected version 2\n");
exit(0);
continue;
} else if (strcmp(cur_line, "2") == 0) {
continue;
} else if (strcmp(cur_line, "\'\'\'\'") == 0) {
continue;
} else if (strcmp(cur_line, "\"\"\"\"") == 0) {
break;
}
temp_node = new_node(make_lower(cur_line));
exclusion_words = add_inorder(exclusion_words, temp_node);
}
free(cur_line);
if (exclusion_words == NULL) {
return 0;
} else {
return exclusion_words;
}
}
/* creates and returns linked-list of node_t storing all index lines from input file */
node_t* loadIndexLines(node_t *index_lines) {
char *cur_line = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
node_t *temp_node = NULL;
while (fgets(cur_line, MAX_LINE_LEN+1, stdin)) {
int line_len = strlen(cur_line);
if (cur_line[line_len-1] == '\n') {
cur_line[line_len-1] = 0;
}
temp_node = new_node(cur_line);
index_lines = add_end(index_lines, temp_node);
}
free(cur_line);
if (index_lines == NULL) {
return 0;
} else {
return index_lines;
}
}
/* creates and returns linked-list of node_t storing all words
in input lines not found in exclusion_words from input file */
node_t* nonExclusionWords(node_t *exclusion_words, node_t *index_lines, node_t *non_exclusion_words) {
char *line = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
char *index_word = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
char *excl_word = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
node_t *curr_excl = NULL;
node_t *curr_lines = NULL;
curr_lines = index_lines;
int appears;
while(curr_lines != NULL) {
strncpy(line, curr_lines->text, MAX_LINE_LEN);
char *word = strtok(line, " ");
while (word != NULL) {
strncpy(index_word, word, MAX_KEYWORD_LEN);
curr_excl = exclusion_words;
appears = 0;
while (curr_excl != NULL) {
if (strcmp(make_lower(index_word), curr_excl->text) == 0) {
appears = 1;
}
curr_excl = curr_excl->next;
}
if (appears == 0) {
if (contains(non_exclusion_words, make_lower(index_word)) == 0) {
non_exclusion_words = add_inorder(non_exclusion_words, new_node(index_word));
}
}
word = strtok(NULL, " ");
}
curr_lines = curr_lines->next;
}
free(line);
free(excl_word);
return non_exclusion_words;
}
/* creates and returns linked-list of node_k storing all index lines with
assosiated key */
node_k *makeKeyAndLine(node_t *non_exclusion_words, node_t *index_lines, node_k *keywords) {
char *line = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
char *linecpy = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
char *word = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
node_t *non_exl = NULL;
node_t *curr_lines = NULL;
non_exl = non_exclusion_words;
while (non_exl != NULL) {
strncpy(word, non_exl->text, MAX_KEYWORD_LEN);
curr_lines = index_lines;
while (curr_lines != NULL) {
strncpy(line, curr_lines->text, MAX_LINE_LEN);
strncpy(linecpy, curr_lines->text, MAX_LINE_LEN);
char *tok_word = strtok(linecpy, " ");
while (tok_word != NULL) {
if (strcmp(word, tok_word) == 0) {
keywords = add_inorder_k_2(keywords, new_node_k(word, line));
}
tok_word = strtok(NULL, " ");
}
curr_lines = curr_lines->next;
}
non_exl = non_exl->next;
}
free(line);
free(word);
return keywords;
}
/* capitalizes keyword in node_k and returns it */
node_k* highlightKeyword(node_k *upper_keyword, node_k *keywords) {
char *key = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
strncpy(key, keywords->keyword, MAX_KEYWORD_LEN);
key = make_upper(key);
node_k *temp = NULL;
temp = new_node_k(key, keywords->line);
upper_keyword = add_inorder_k(upper_keyword, temp);
free(key);
return upper_keyword;
}
/* capitalizes and returns all keywords in lines/keys */
node_k* highlightAllKeywords(node_k *upper_keyword_in_line, node_k *upper_keyword_in_lines_and_key) {
while (upper_keyword_in_line != NULL) {
upper_keyword_in_lines_and_key = highlightKeyword(upper_keyword_in_lines_and_key, upper_keyword_in_line);
upper_keyword_in_line = upper_keyword_in_line->next;
}
return upper_keyword_in_lines_and_key;
}
node_k* highlightKeywordsInLines(node_k *keywords, node_k *upper_keyword_in_line) {
char *space = " ";
while (keywords != NULL) {
char *upper_keyword_line = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
char *word = strtok(keywords->line, " ");
while (word != NULL) {
if (strncmp(make_lower(word), make_lower(keywords->keyword), MAX_KEYWORD_LEN) == 0) {
strncat(upper_keyword_line, make_upper(word), MAX_KEYWORD_LEN);
} else {
strncat(upper_keyword_line, word, MAX_KEYWORD_LEN);
}
strncat(upper_keyword_line, space, MAX_LINE_LEN);
word = strtok(NULL, " ");
}
int line_len = strlen(upper_keyword_line);
if (upper_keyword_line[line_len-1] == ' ') {
upper_keyword_line[line_len-1] = 0;
}
node_k *temp = NULL;
temp = new_node_k(keywords->keyword, upper_keyword_line);
upper_keyword_in_line = add_inorder_k(upper_keyword_in_line, temp);
keywords = keywords->next;
}
return upper_keyword_in_line;
}
node_c* makeLineChars(node_k *upper_keyword_in_lines_and_key) {
char *temp_line = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
strncpy(temp_line, upper_keyword_in_lines_and_key->line, MAX_LINE_LEN);
node_c *line_chars = NULL;
int i = 0;
while(temp_line[i] != '\0') {
node_c *temp = NULL;
temp = new_node_c(temp_line[i]);
line_chars = add_end_c(line_chars, temp);
i++;
}
free(temp_line);
return line_chars;
}
void printOutput(node_k *upper_keyword_in_lines_and_key, node_c *line_chars) {
char *key = (char*)emalloc(MAX_KEYWORD_LEN * sizeof(char));
char *line = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
char *buffer = (char*)emalloc(MAX_LINE_LEN * sizeof(char));
strncpy(key, upper_keyword_in_lines_and_key->keyword, MAX_KEYWORD_LEN);
strncpy(line, upper_keyword_in_lines_and_key->line, MAX_LINE_LEN);
char *result = strstr(line, key);
int index = result - line;
int val = 29 - index;
if (val > 0) {
for (int q = 0; q < val; q++) {
strcat(buffer, " ");
}
} else {
int num = abs(val);
line += num;
}
strcat(buffer, line);
// if length before keyword is > 20: then find first space after 8th column
if (index > 19) {
int pos = 0;
int first_space;
for (pos = 8; pos < 29; pos++) {
if (buffer[pos] == ' ') {
first_space = pos;
break;
}
}
for (int b = 0; b <= first_space; b++) {
buffer[b] = ' ';
}
}
// if length after and including keyword > 30: then find last space before 61 columnm
int temp_len = strlen(buffer);
if (temp_len > 60) {
int idx = 0;
int last_space;
for (idx = 60; idx > 30; idx--) {
if (buffer[idx] == ' ') {
last_space = idx;
break;
}
}
buffer[last_space] = '\0';
}
printf("%s\n", buffer);
}
void makeListLineChars(node_k *upper_keyword_in_lines_and_key, node_c *line_chars) {
while (upper_keyword_in_lines_and_key != NULL) {
line_chars = makeLineChars(upper_keyword_in_lines_and_key);
printOutput(upper_keyword_in_lines_and_key, line_chars);
upper_keyword_in_lines_and_key = upper_keyword_in_lines_and_key->next;
}
}
int main() {
node_t *exclusion_words = NULL;
exclusion_words = loadExclusionWords(exclusion_words);
node_t *index_lines = NULL;
index_lines = loadIndexLines(index_lines);
node_t *non_exclusion_words = NULL;
non_exclusion_words = nonExclusionWords(exclusion_words, index_lines, non_exclusion_words);
node_k *keywords = NULL;
keywords = makeKeyAndLine(non_exclusion_words, index_lines, keywords);
node_k *upper_keyword_in_line = NULL;
upper_keyword_in_line = highlightKeywordsInLines(keywords, upper_keyword_in_line);
node_k *upper_keyword_in_lines_and_key = NULL;
upper_keyword_in_lines_and_key = highlightAllKeywords(upper_keyword_in_line, upper_keyword_in_lines_and_key);
node_c *line_chars = NULL;
makeListLineChars(upper_keyword_in_lines_and_key, line_chars);
freeList(exclusion_words);
freeList(index_lines);
freeList(non_exclusion_words);
freeListK(keywords);
freeListK(upper_keyword_in_line);
freeListK(upper_keyword_in_lines_and_key);
return (0);
}