-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathfinderOfStrings.cpp
More file actions
99 lines (85 loc) · 3 KB
/
finderOfStrings.cpp
File metadata and controls
99 lines (85 loc) · 3 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
#include "finderOfStrings.h"
#include "experimental/algorithm"
#include "experimental/functional"
struct cancellation_exception : std::exception {
const char *what() const noexcept override {
return "Stop thread";
}
};
finderSub::finderSub(QString dir, std::string strSub, std::vector<fileTrigram> filestemp) {
curDir = std::move(dir);
sub = std::move(strSub);
files = std::move(filestemp);
}
finderSub::~finderSub() = default;
void finderSub::process() {
scan_directory();
}
void finderSub::scan_directory() {
try {
auto cancellation_point = [thread = QThread::currentThread(), this]() {
if (thread->isInterruptionRequested()) {
throw cancellation_exception();
}
};
std::vector<uint32_t> subTrig;
if (sub.size() > 2) {
for (int i = 0; i < sub.size() - 3 + 1; ++i) {
cancellation_point();
uint32_t trig = 0;
auto a = (uint8_t) sub[i];
trig |= a;
trig <<= 8;
a = (uint8_t) sub[i + 1];
trig |= a;
trig <<= 8;
a = (uint8_t) sub[i + 2];
trig |= a;
subTrig.push_back(trig);
}
}
cancellation_point();
for (auto &file : files) {
cancellation_point();
bool contain = true;
for (uint32_t j : subTrig) {
if (file.trigrams.end() == file.trigrams.find(j)) {
contain = false;
break;
}
}
if (contain) {
std::ifstream fin(file.file.toStdString(), std::ios::binary);
std::string text;
std::string pat = sub;
int number = 0;
std::vector<std::pair<int, int>> thisLine;
while (!fin.eof()) {
cancellation_point();
number++;
std::getline(fin, text);
int ans = 0;
auto it = std::experimental::search(text.begin(), text.end(),
std::experimental::boyer_moore_searcher(
pat.begin(), pat.end()));
while (it != text.end()) {
ans++;
it = std::experimental::search(it + 1, text.end(),
std::experimental::boyer_moore_searcher(
pat.begin(), pat.end()));
}
if (ans != 0) {
thisLine.emplace_back(number, ans);
}
}
if (!thisLine.empty()) {
emit addToTree({file.file, thisLine});
}
}
emit updateProgressBar();
}
emit finished();
} catch (std::exception &ex) {
emit error();
}
}