-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
109 lines (85 loc) · 3.84 KB
/
main.cpp
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
#include <iostream>
#include <time.h>
#include <string.h>
#include "PathHelper.h"
#include "SearchEngine.h"
#include "Analyzer.h"
#include "Tokenizer.h"
#include "String.h"
#include "Vector.h"
#include "Map.h"
#define ARGC_REQUIRED 2
inline bool haveValidArgsNumber(int argc) {
return argc == ARGC_REQUIRED;
}
inline char* getPath(char *argv[]) {
return argv[1];
}
inline void displayAppName() {
std::cout << "" << std::endl;
std::cout << "-------------------------------------------------------------------------------------------------------------" << std::endl;
std::cout << " " << std::endl;
std::cout << " _ _ _ _ ____ _ ____ _ " << std::endl;
std::cout << "| | (_) __ _| |__ | |_ | _ \\ ___ ___ _ _ _ __ ___ ___ _ __ | |_ / ___| ___ __ _ _ __ ___| |__ " << std::endl;
std::cout << "| | | |/ _` | '_ \\| __| | | | |/ _ \\ / __| | | | '_ ` _ \\ / _ \\ '_ \\| __| \\___ \\ / _ \\/ _` | '__/ __| '_ \\ " << std::endl;
std::cout << "| |___| | (_| | | | | |_ | |_| | (_) | (__| |_| | | | | | | __/ | | | |_ ___) | __/ (_| | | | (__| | | |" << std::endl;
std::cout << "|_____|_|\\__, |_| |_|\\__| |____/ \\___/ \\___|\\__,_|_| |_| |_|\\___|_| |_|\\__| |____/ \\___|\\__,_|_| \\___|_| |_|" << std::endl;
std::cout << " |___/ " << std::endl;
std::cout << " " << std::endl;
std::cout << "-------------------------------------------------------------------------------------------------------------" << std::endl;
std::cout << "" << std::endl;
}
void wait(int seconds)
{
clock_t endwait;
endwait = clock() + seconds * CLOCKS_PER_SEC;
while (clock() < endwait) {}
}
template class ylib::Vector<ylib::String>;
int main(int argc, char *argv[]) {
// Display app informations
displayAppName();
std::cout << "Author: yroseau" << std::endl;
std::cout << "Licence MIT" << std::endl;
std::cout << "v0.0.1" << std::endl;
std::cout << "" << std::endl;
// check args number (argc)
if (!haveValidArgsNumber(argc)) {
std::cout << "[ERROR] Invalid args number. Required: " << ARGC_REQUIRED << " - Got: " << argc << std::endl;
for (int i = 0; i != argc; ++i) {
std::cout << i << ": " << argv[i] << std::endl;
}
return 0;
}
// get path in argv
char* path = getPath(argv);
// check path is a valid directory
if (!PathHelper::isDir(path)) {
std::cout << "[ERROR] '" << path << "' is not a valid directory !" << std::endl;
return 0;
}
std::cout << "Directory: " << path << std::endl;
Tokenizer *tokenizer = new Tokenizer();
Analyzer *analyzer = new Analyzer(tokenizer);
//Analyzer *analyzer = new ExtensionAnalyzer(tokenizer, '.txt');
SearchEngine *searchEngine = new SearchEngine(path);
searchEngine->analyze(analyzer, ".TXT");
searchEngine->analyze(analyzer, ".txt");
std::cout << "Search engine has indexed " << searchEngine->getPaths()->size() << " document(s)." << std::endl;
const char* stopWord = ":q";
std::cout << "\nWrite '" << stopWord << "' to quit." << std::endl;
char term[1024];
do {
std::cin >> term;
if (strcmp(term, stopWord) == 0) {
continue;
}
std::cout << "\nTerm to search: " << term << std::endl;
StringVector result = searchEngine->search(term);
std::cout << " Find " << result.size() << " result(s)." << std::endl;
for (ylib::size_t i = 0; i != result.size(); ++i) {
std::cout << " - " << (*result[i])->toCharArray() << std::endl;
}
} while (strcmp(term, stopWord) != 0);
return 0;
}