-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscreen.cpp
More file actions
executable file
·508 lines (491 loc) · 10.8 KB
/
Copy pathscreen.cpp
File metadata and controls
executable file
·508 lines (491 loc) · 10.8 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
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
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
#include "screen.h"
#include <curses.h>
#include <iostream>
#include <string>
#include <fstream>
#include <sstream>
#include <algorithm>
#include <windows.h>
#include "rk.h"
#include "kmp.h"
#include "MooreSearch.h"
using namespace std;
int CHECKSPEED = 150;
char *choices[] = {
"Rabin-Karp",
"Knuth-Morris-Pratt",
"Booyre-Moore",
"Exit",
};
/********* colorString Class *********/
class colorString
{
private:
char* cString;
int* formatArray;
size_t searchPos;
size_t patPos;
size_t length;
size_t patternLength;
public:
colorString(char* stringIn, size_t lengthIn, size_t patternLengthIn)
{
length = lengthIn+1;
cString = new char[length];
strcpy(cString, stringIn);
patternLength = patternLengthIn;
formatArray = new int[length] {0};
searchPos = -1;
patPos = -1;
}
~colorString()
{
delete[] cString;
delete[] formatArray;
}
void print(WINDOW *winIn, int posY, int posX)
{
wmove(winIn, posY, posX);
for (size_t i = 0; i < length; i++)
{
if (i == searchPos && cString[i] != '\n')
waddch(winIn, '_' | A_STANDOUT);
else if (i >= patPos && i < patPos + patternLength && cString[i] != '\n')
waddch(winIn, '_');
else if (formatArray[i] == 0)
waddch(winIn, cString[i]);
else
waddch(winIn, cString[i] | A_BOLD | A_UNDERLINE);
}
}
void printBM(WINDOW *winIn, int posY, int posX)
{
wmove(winIn, posY, posX);
for (size_t i = 0; i < length; i++)
{
if (i == patPos && cString[i] != '\n')
waddch(winIn, '_' | A_STANDOUT);
else if (i >= searchPos - patternLength && i < searchPos && cString[i] != '\n')
waddch(winIn, '_');
else if (formatArray[i] == 0)
waddch(winIn, cString[i]);
else
waddch(winIn, cString[i] | A_BOLD | A_UNDERLINE);
}
}
void setColor(int elementIn, bool state)
{
if (state)
formatArray[elementIn] = 1;
else
formatArray[elementIn] = 0;
}
void setSearchPos(int posIn)
{
searchPos = posIn;
}
void setPatPos(int patPosIn)
{
patPos = patPosIn;
}
};
/********* Parent Screen Class *********/
Screen::Screen(const int& numIn)
{
screenNum = numIn;
screenChange = numIn;
}
int Screen::changeScreen()
{
int temp = screenChange;
screenChange = screenNum;
return temp;
}
string Screen::readFile(string fileName)
{
ifstream file(fileName);
ostringstream ss;
if (file.is_open())
{
ss << file.rdbuf();
}
else
{
cerr << "Unable to open file: " << fileName << endl;
return "";
}
file.close();
return ss.str();
}
/********* Main Screen Class *********/
MainScreen::MainScreen(const int& numIn) : Screen(numIn)
{
n_choices = sizeof(choices) / sizeof(char *);
}
void MainScreen::run()
{
int WIDTH = 25;
int HEIGHT = 6;
WINDOW *menu_win;
int highlight = 1;
int choice = 0;
int c;
string printBuffer;
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
menu_win = newwin(HEIGHT, WIDTH, maxY/2 - HEIGHT/2, maxX/2 - WIDTH/2);
keypad(menu_win, TRUE);
clear();
noecho();
cbreak(); /* Line buffering disabled. pass on everything */
curs_set(0);
// Print Title
printBuffer = "|| | | || BARCODES ||| || ||";
mvprintw(maxY / 2 - HEIGHT / 2 - 3, maxX / 2 - printBuffer.length() / 2, printBuffer.c_str());
printBuffer = "String Matching Presentation";
mvprintw(maxY / 2 - HEIGHT / 2 - 2, maxX / 2 - printBuffer.length() / 2, printBuffer.c_str());
// Print Footer
printBuffer = "Nima Vasseghi | Xince Shi | David Park | Damon Birtola";
mvprintw(maxY / 2 + n_choices / 2 + 2, maxX / 2 - printBuffer.length() / 2, printBuffer.c_str());
refresh();
// Print Interactive Menu
print_menu(menu_win, highlight);
// Keyboard Interaction
while (1)
{
c = wgetch(menu_win);
switch (c)
{
case KEY_UP:
if (highlight == 1)
highlight = n_choices;
else
--highlight;
break;
case KEY_DOWN:
if (highlight == n_choices)
highlight = 1;
else
++highlight;
break;
case '\r': case '\n': case KEY_ENTER:
choice = highlight;
screenChange = choice;
break;
default:
break;
}
print_menu(menu_win, highlight);
if (screenChange != screenNum) /* User did a choice come out of the infinite loop */
break;
}
delwin(menu_win);
}
void MainScreen::print_menu(WINDOW *menu_win, int highlight)
{
int maxY, maxX, i;
getmaxyx(menu_win, maxY, maxX);
box(menu_win, 0, 0);
// Print Options
int y = maxY / 2 - n_choices / 2;
int x;
for (i = 0; i < n_choices; ++i)
{
x = maxX / 2 - strlen(choices[i]) / 2;
if (highlight == i + 1) /* High light the present choice */
{
wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", choices[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", choices[i]);
++y;
}
wrefresh(menu_win);
}
/********* ScreenRK Class *********/
ScreenRK::ScreenRK(const int& numIn) : Screen(numIn)
{
}
void ScreenRK::run()
{
//Pad Creation
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
WINDOW *rkWin;
rkWin = newpad(1000, maxX);
//Read file to string
text = readFile("Speech.txt");
//count lines to be displayed from text
int textLines = 0;
int linePos = 0;
for (char& c : text) {
if (c == '\n')
{
textLines++;
linePos = 0;
}
else
{
if (linePos < maxX)
linePos++;
else
{
textLines++;
linePos = 0;
}
}
}
if (textLines > maxY)
textLines -= maxY;
textLines += 5;
// Rabin Karp Algorithm Object
char* txt = _strdup(text.c_str());
char* pat = "the";
size_t M = strlen(pat);
size_t N = strlen(txt);
RabinKarp<char> rk;
rk.add(txt, pat, M, N);
// Create
colorString colorText(txt, N, M);
colorText.setSearchPos(0);
colorText.print(rkWin, 0, 0);
prefresh(rkWin, 0, 0, 0, 0, maxY-1, maxX-1);
//Input Loop
keypad(rkWin, TRUE);
nodelay(rkWin, TRUE);
int padPos = 0;
size_t textIndex = 0;
int c;
bool exitFlag = false;
while (1)
{
c = wgetch(rkWin);
switch (c)
{
case KEY_UP:
if (padPos >0)
padPos -= 1;
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
case KEY_DOWN:
if (padPos < textLines)
padPos += 1;
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
case '\r': case '\n': case KEY_ENTER:
screenChange = 0;
exitFlag = true;
default:
textIndex = rk.getIndex();
colorText.setSearchPos(textIndex);
if (rk.searchNext())
{
for (size_t i = textIndex; i < M + textIndex; i++)
colorText.setColor(i, true);
}
colorText.print(rkWin, 0, 0);
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
}
if (exitFlag)
break;
Sleep(CHECKSPEED);
}
delete[] txt;
delwin(rkWin);
}
/********* ScreenKMP Class *********/
ScreenKMP::ScreenKMP(const int& numIn) : Screen(numIn)
{
}
void ScreenKMP::run()
{
//Pad Creation
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
WINDOW *rkWin;
rkWin = newpad(1000, maxX);
//Read file to string
text = readFile("Speech.txt");
//count lines to be displayed from text
int textLines = 0;
int linePos = 0;
for (char& c : text) {
if (c == '\n')
{
textLines++;
linePos = 0;
}
else
{
if (linePos < maxX)
linePos++;
else
{
textLines++;
linePos = 0;
}
}
}
if (textLines > maxY)
textLines -= maxY;
textLines += 5;
//KMP object
char* txt = _strdup(text.c_str());
char* pat = "temporary";
size_t M = strlen(pat);
size_t N = strlen(txt);
KMP kmp(pat, strlen(pat), txt, text.length());
// Create
colorString colorText(txt, N, M);
colorText.setSearchPos(0);
colorText.print(rkWin, 0, 0);
prefresh(rkWin, 0, 0, 0, 0, maxY - 1, maxX - 1);
//Input Loop
keypad(rkWin, TRUE);
nodelay(rkWin, TRUE);
int padPos = 0;
size_t textIndex = 0;
size_t patIndex = 0;
int c;
bool exitFlag = false;
while (1)
{
c = wgetch(rkWin);
switch (c)
{
case KEY_UP:
if (padPos >0)
padPos -= 1;
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
case KEY_DOWN:
if (padPos < textLines)
padPos += 1;
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
case '\r': case '\n': case KEY_ENTER:
screenChange = 0;
exitFlag = true;
default:
textIndex = kmp.getIndex();
patIndex = kmp.getPatIndex();
colorText.setSearchPos(textIndex);
colorText.setPatPos(patIndex);
if (kmp.findNext())
{
for (size_t i = textIndex; i < M + textIndex+1; i++)
colorText.setColor(i-M, true);
}
colorText.print(rkWin, 0, 0);
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
}
if (exitFlag)
break;
Sleep(CHECKSPEED);
}
delete[] txt;
delwin(rkWin);
}
/********* ScreenBM Class *********/
ScreenBM::ScreenBM(const int& numIn) : Screen(numIn)
{
}
void ScreenBM::run()
{
//Pad Creation
int maxY, maxX;
getmaxyx(stdscr, maxY, maxX);
WINDOW *rkWin;
rkWin = newpad(1000, maxX);
//Read file to string
text = readFile("Speech.txt");
//count lines to be displayed from text
int textLines = 0;
int linePos = 0;
for (char& c : text) {
if (c == '\n')
{
textLines++;
linePos = 0;
}
else
{
if (linePos < maxX)
linePos++;
else
{
textLines++;
linePos = 0;
}
}
}
if (textLines > maxY)
textLines -= maxY;
textLines += 5;
//BM object
string patString = "the";
const char * cText = text.c_str();
const char * cPat = patString.c_str();
int n = text.size();//size of text
int m = patString.size();//size of pattern
BoyerMoore bm(cPat, m);
bm.searchstarter(cText, n);
char *txt;
txt = new char[n+1];
strcpy(txt, cText);
// Create
colorString colorText(txt, n, m);
colorText.setSearchPos(0);
colorText.printBM(rkWin, 0, 0);
prefresh(rkWin, 0, 0, 0, 0, maxY - 1, maxX - 1);
//Input Loop
keypad(rkWin, TRUE);
nodelay(rkWin, TRUE);
int padPos = 0;
size_t textIndex = 0;
size_t patIndex = 0;
int c;
bool exitFlag = false;
while (1)
{
c = wgetch(rkWin);
switch (c)
{
case KEY_UP:
if (padPos >0)
padPos -= 1;
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
case KEY_DOWN:
if (padPos < textLines)
padPos += 1;
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
case '\r': case '\n': case KEY_ENTER:
screenChange = 0;
exitFlag = true;
default:
textIndex = bm.getIndex();
patIndex = bm.getPatIndex();
colorText.setSearchPos(textIndex);
colorText.setPatPos(patIndex);
if (bm.searchstep())
{
for (size_t i = textIndex - m; i < textIndex; i++)
colorText.setColor(i, true);
}
colorText.printBM(rkWin, 0, 0);
prefresh(rkWin, padPos, 0, 0, 0, maxY - 1, maxX - 1);
break;
}
if (exitFlag)
break;
Sleep(CHECKSPEED);
}
delwin(rkWin);
delete[] txt;
}