-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsearch_thread.cpp
131 lines (102 loc) · 3.34 KB
/
search_thread.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
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
#include "search_thread.h"
#include <QFileDialog>
#include <QDebug>
#include <QRegularExpression>
search_thread::search_thread()
{
}
void search_thread::run()
{
doSearch();
}
void search_thread::doSearch(){
emit sendmsg("正在索引...");
QStringList fileList = getFileListUnderDir(url,filter);
emit sendmsg("索引完成,正在查找...");
for(int i=0;i<fileList.size();i++){
QString fileurl=fileList.at(i);
QString inputStr = getFileContentString(fileurl);
QRegularExpression regularExpression0(regularExpression);
QRegularExpressionMatch match;
int startIndex = 0;
int length = 0;
int endIndex = 0;
QStringList result1;
for(;endIndex < inputStr.length();){
match = regularExpression0.match(inputStr, endIndex);
if(match.hasMatch()) {
startIndex = match.capturedStart();
length = match.capturedLength();
endIndex = match.capturedEnd();
QString result0 = inputStr.mid(startIndex,length);
//qDebug()<<result0;
result1.append(result0);
}
else{
break;
}
}
//---------------------------------------------------
QString result1_length = QString::number(result1.length());
QString result2 = "";
if(result1.length()>0){
result2 = "在" + fileurl + "找到 " + result1_length + " 处匹配:\n";
emit sendmsg(result2);
foreach (auto result4, result1) {
emit sendmsg(result4 + "\n");
}
}
int progressBarValue=i*100/(fileList.size()-1);
emit setProgressBar(progressBarValue);
}
emit sendmsg("完成!");
emit setProgressBar(100);
emit finish();
}
QString search_thread::getFileContentString(QString fileurl){
QString fileContentString = "";
QFile file(fileurl);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text))
{
qDebug()<< "Can't open the file!" <<Qt::endl;
}
while (!file.atEnd())
{
QByteArray line = file.readLine();
QString str(line);
//qDebug()<< str;
fileContentString.append(str);
}
return fileContentString;
}
QStringList search_thread::getFileListUnderDir(const QString &dirPath2,const QString &filter2)
{
QStringList fileList;
QDir dir(dirPath2);
QFileInfoList fileInfoList = dir.entryInfoList(QDir::Files | QDir::NoDotAndDotDot | QDir::Dirs);
foreach (auto fileInfo, fileInfoList) {
if(fileInfo.isDir())
{
QStringList fileList2 = getFileListUnderDir(fileInfo.absoluteFilePath(),filter2);
fileList.append(fileList2);
}
if(filter2==""){
if(fileInfo.isFile()){
fileList.append(fileInfo.absoluteFilePath());
}
}else{
QStringList filterlist = filter2.split(",");
if(fileInfo.isFile())
{
QFileInfo fi(fileInfo.absoluteFilePath());
QString ext= fi.suffix();
foreach (auto filter, filterlist) {
if(filter==ext){
fileList.append(fileInfo.absoluteFilePath());
}
}
}
}
}
return fileList;
}