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
|
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
typedef struct lineData{
int lineNumber;
int occurrences;
int *positionList;
} lineData;
typedef struct Data{
char *name;
int lines;
lineData *lineList;
} Data;
typedef struct dataNode{
Data *data;
struct dataNode *next;
} dataNode;
typedef dataNode* dataHead;
lineData *createLineData(int lineNumber, int occurrences, int *positionList){
lineData *newLineData = malloc(sizeof(lineData));
newLineData->lineNumber = lineNumber;
newLineData->occurrences = occurrences;
newLineData->positionList = positionList;
return newLineData;
}
Data *createData(char *name, int lines, lineData *lineList){
Data *newData = malloc(sizeof(Data));
newData->name = name;
newData->lines = lines;
newData->lineList = lineList;
return newData;
}
dataNode * createNode(Data *data, dataNode *next){
dataNode *newNode = malloc(sizeof(dataNode));
newNode->data = data;
newNode->next = next;
return newNode;
}
void addNode(dataHead *head, Data *data){
dataNode *newNode = createNode(data, NULL);
if(*head){
dataNode *temp = *head;
while(temp->next){
temp = temp->next;
}
temp->next = newNode;
}else{
(*head) = newNode;
}
}
void printLineData(lineData lineData){
for(int i = 0; i < lineData.occurrences; i++){
printf("\t\t-At position %d\n", lineData.positionList[i]);
}
}
void printData(Data data){
printf("Found %s in %d lines:\n", data.name, data.lines);
for(int i = 0; i < data.lines; i++){
printf("\t-At line %d:\n", data.lineList[i].lineNumber);
printLineData(data.lineList[i]);
}
}
void printNodes(dataHead head){
dataNode *node = head;
while(node){
printData(*node->data);
node = node->next;
}
free(node);
}
size_t getLineCount(FILE *fp){
size_t lines = 0;
while(!feof(fp)){
char ch = fgetc(fp);
if(ch == '\n') lines++;
}
rewind(fp);
return ++lines;
}
char **fileToLineArray(FILE *fp, int *fileLines){
size_t len, l = 0;
(*fileLines) = getLineCount(fp);
char **lineArray = (char**)malloc((*fileLines)*sizeof(char*)), *line = NULL;
while((len = getline(&line, &len, fp)) != -1){
if(len > 0 && line[len-1] == '\n'){
line[len-1] = '\0';
}else if(line[len-1] != '\n'){
line = (char*)realloc(line, len+1);
line[len] = '\0';
}
lineArray[l++] = line;
line = NULL;
}
rewind(fp);
return lineArray;
}
int main(int argc, char **argv){
if(argc < 3){
return printf("Usage : wordfinder <document> <list>");
}
char *docName = argv[1], *listName = argv[2];
FILE *document = fopen(docName, "r"), *list = fopen(listName, "r");
if(!document) printf("Error: \"%s\" not found\n", document);
if(!list) printf("Error: \"%s\" not found\n", list);
if(!document || !list){
return 0;
}
size_t docLines = 0, listLines = 0;
char **fdocument = fileToLineArray(document, &docLines);
char **flist = fileToLineArray(list, &listLines);
fclose(document);
fclose(list);
dataNode *head = NULL;
for(int i = 0; i < listLines; i++){
int lineCount = 0;
lineData *lines = malloc(sizeof(lines));
for(int j = 0; j < docLines; j++){
char *p1 = fdocument[j], *p2 = fdocument[j];
int positionCount = 0;
int *positions = malloc(sizeof(int));
while(p2 = strstr(p2, flist[i])){
positions = realloc(positions, (positionCount+1)*sizeof(int));
int pos = p2 - fdocument[j];
positions[positionCount++] = pos;
p2 += strlen(flist[i]);
}
if(positionCount){
lineData *line = createLineData(j+1, positionCount, positions);
lines = realloc(lines, (lineCount+1)*sizeof(lineData));
lines[lineCount++] = (*line);
}
}
if(lineCount){
Data *data = createData(flist[i], lineCount, lines);
addNode(&head, data);
}
}
printNodes(head);
free(head);
free(fdocument);
free(flist);
return 0;
}
|