-
Notifications
You must be signed in to change notification settings - Fork 0
/
kmp.c
217 lines (184 loc) · 4.57 KB
/
kmp.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
#include <unistd.h>
#include <stdlib.h>
#include <string.h>
#include <fcntl.h>
#include <signal.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdio.h>
#include <time.h>
int kmparr[30];
#define MAX_SIZE 3000
#define SLIDE_SIZE 20
/* Color Schemes.*/
#define RED "\x1B[31m"
#define GRN "\x1B[32m"
#define YEL "\x1B[33m"
#define BLU "\x1B[34m"
#define MAG "\x1B[35m"
#define CYN "\x1B[36m"
#define WHT "\x1B[37m"
#define RESET "\x1B[0m"
struct results
{
char st[200];
};
/*************** Kmp Functions. ***************/
void compute(int * kmparr, char * pattern, int patternlength) {
int m = patternlength;
kmparr[0] = 0;
int len = 0, i;
for (i = 1; i < m; ++i)
{
if (pattern[i] == pattern[len])
{
len++;
kmparr[i] = len;
}
else {
if (len != 0)
{
len = kmparr[len - 1];
i--;
}
else {
kmparr[len] = 0;
}
}
}
}
/************************ End functions. *********************/
void updateSlidingWindow(char * slide, char newChar) {
int i = 0, len = strlen(slide);
if (len < SLIDE_SIZE) {
i = len;
}
else {
for (; i < len - 1; i++)
slide[i] = slide[i+1];
}
slide[i] = newChar;
slide[i+1] = '\0';
}
void getLastCharsFromString(char * slide, char * result, int cut_off_length) {
int i = 0, j = 0;
int current_length = strlen(slide);
while(i < current_length - cut_off_length) i++;
while(slide[i] != '\0'){
if (slide[i] == '\n') {
j = 0;
i++;
}
result[j++] = slide[i++];
}
result[j] = '\0';
}
void getNextCharactersFromString(char * text, char * result, int current_position, int cut_off_length) {
int current_length = strlen(result);
int i = current_position;
printf("%d\n", i);
int t = 0;
while(text[i++] != '\0') {
//if(t++ == 10)
// break;
if (text[i] == '\n' || cut_off_length-- == 0)
break;
result[current_length++] = text[i];
}
result[current_length] = '\0';
}
int stateStore = 0, matches;
void findResultInBuffer(struct results * res, int tillFound, char text[], char * pattern, char * slide, int * kmpArray, int patternlength, int textlength) {
int i = 0, j = 0;
j = stateStore;
for (i = 0; i < textlength; ++i)
{
updateSlidingWindow(slide, text[i]);
while(1) {
if (text[i] == pattern[j]) {
j++;
if (j == patternlength) {
matches++;
j = kmparr[j];
char result[150];
getLastCharsFromString(slide, result, 20);
printf("Before = %s\n", result);
getNextCharactersFromString(text, result, i, 20);
//printf("%s\n", result);
printf("After = %s\n", result);
//res[tillFound++].st = result;
}
break;
} else if (j == 0) break;
else j = kmparr[j];
}
}
stateStore = j;
}
// implement all the argument validations.
int check_args(int argc, char * argv[]) {
}
int main(int argc, char *argv[]) {
if (argc == 3)
{
struct results res[100];
clock_t begin = clock();
if(access(argv[2], F_OK ) != -1 && access(argv[2], R_OK) != -1) {
int file_descriptor = open(argv[2], O_RDWR, S_IREAD);
char buffer[MAX_SIZE], SLIDING_WINDOW[MAX_SIZE];
int buffer_size;
int i = 0;
char pattern[150];
const int patternlength= strlen(argv[1]);
for (i = 0; i < patternlength; ++i)
pattern[i] = argv[1][i];
pattern[strlen(argv[1])] = '\0';
int kmparray[patternlength];
compute(kmparr, pattern, patternlength);
while((buffer_size = read(file_descriptor, buffer, MAX_SIZE)) > 0) {
buffer[buffer_size] = '\0';
findResultInBuffer(res, 0, buffer, pattern, SLIDING_WINDOW, kmparr, patternlength, buffer_size);
}
//const int buffer_size = read(file_descriptor, buffer, MAX_SIZE);
// take from sysargs.
clock_t end = clock();
double time_spent = (double)(end - begin) / CLOCKS_PER_SEC;
printf("Time : %lf\n", time_spent);
}
else {
printf("Specify the file with corrent name and permissions.\n");
}
}
else {
// write help code.
printf("The correct usage is - ./grep 'pattern' 'filename'\n");
printf("Without quotes.\n");
printf("For help, ./grep help\n");
}
return 0;
}
/*
char * text = "AAACAAAAACAACAAAAACAAACAAAAACAAACAAAAACAAACAAAAAC";
while()
//SLIDING_WINDOW = updateSlidingWindow(SLIDING_WINDOW, 'D');
updateSlidingWindow(SLIDING_WINDOW, 'D');
printf("%s\n", SLIDING_WINDOW);
return 0;
// int matches = 0;
// compute();
//
// for (int i = 0; i < strlen(pattern); ++i)
// {
// printf("%d\n", kmparr[i]);
// }
//
// int i = 0, j;
// while(i < strlen(text)) {
// for(j = 0; pattern[j] != '\0' && text[i+j] != '\0' && pattern[j] == text[i+j]; j++);
// if(pattern[j] == '\0')
// matches++;
// i += max(1, j-kmparr[j]);
// }
// printf("matches - %d\n", matches);
// return 0;
}*/