-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathscanworker.cpp
More file actions
118 lines (103 loc) · 4.04 KB
/
Copy pathscanworker.cpp
File metadata and controls
118 lines (103 loc) · 4.04 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
#include "scanworker.h"
#include <QFile>
#include <QFileInfo>
#include <QDateTime>
#include <QTextStream>
#include <QDirIterator>
#include <QDir>
#include <QMap>
#include <QDebug>
ScanWorker::ScanWorker(const QString &src, const QString &out, const std::vector<QString> &words)
: sourceDir(src), outputDir(out)
{
// Normalize wordlist to lowercase upfront
wordlist.reserve(words.size());
for (const auto &w : words) {
wordlist.push_back(w.trimmed().toLower());
}
}
void ScanWorker::requestStop() {
stopRequested.store(true);
}
void ScanWorker::startScan() {
foundFiles.clear();
// Gather all .txt and .ini files
qDebug() << "Scanning directory:" << sourceDir;
QDirIterator it(sourceDir, {"*.txt", "*.ini"}, QDir::Files, QDirIterator::Subdirectories);
int current = 0;
QStringList allFiles;
while (it.hasNext()) {
QString file = it.next();
allFiles.append(file);
qDebug() << "Found file:" << file;
}
int total = allFiles.size();
qDebug() << "Total files found:" << total;
emit totalFilesCounted(total);
for (const QString &filePath : allFiles) {
if (stopRequested.load())
break;
emit currentFile(filePath);
emit progress(++current, total);
QFile file(filePath);
if (!file.open(QIODevice::ReadOnly | QIODevice::Text)) {
qDebug() << "Cannot open file:" << filePath;
continue;
}
QTextStream in(&file);
QStringList matchedWords;
QMap<QString, QList<int>> wordLineNumbers; // Track which lines each word appears on
// Scan file line-by-line
int lineNumber = 0;
while (!in.atEnd() && !stopRequested.load()) {
lineNumber++;
QString line = in.readLine().toLower();
for (const auto &word : wordlist) {
if (line.contains(word)) {
if (!matchedWords.contains(word)) {
matchedWords.append(word);
}
wordLineNumbers[word].append(lineNumber);
// Emit detailed signal for each word found
emit wordFoundInFile(word, filePath, lineNumber);
qDebug() << "Match found:" << word << "in" << filePath << "at line" << lineNumber;
}
}
}
file.close();
// Copy file if matches are found
if (!matchedWords.isEmpty()) {
QFileInfo fi(filePath);
QString timeStamp = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss");
QString destPath = outputDir + "/" + timeStamp + "_" + fi.fileName();
if (QFile::copy(filePath, destPath)) {
MatchEntry entry{filePath, destPath, matchedWords.join(", ")};
foundFiles.append(entry);
emit matchFound(filePath);
// Emit detailed completion signal
emit fileMatchComplete(filePath, matchedWords.size(), matchedWords);
} else {
qDebug() << "Failed to copy:" << filePath << "to" << destPath;
}
}
}
// Write results CSV
QString csvTimeStamp = QDateTime::currentDateTime().toString("yyyyMMdd_HHmmss");
QString csvPath = outputDir + "/scan_results_" + csvTimeStamp + ".csv";
QFile csvFile(csvPath);
if (csvFile.open(QIODevice::WriteOnly | QIODevice::Text)) {
QTextStream out(&csvFile);
out << "file_path,copied_to,matched_words\n";
for (const auto &entry : foundFiles) {
out << "\"" << entry.originalPath << "\","
<< "\"" << entry.copiedPath << "\","
<< "\"" << entry.matchedWords << "\"\n";
}
out << "\"TOTAL_FILES_SCANNED\"," << total << ",\n";
qDebug() << "CSV written to:" << csvPath << "with" << foundFiles.size() << "entries.";
emit csvGenerated(csvPath); // <---- NEW: Notify MainWindow
} else {
qDebug() << "Failed to write CSV to:" << csvPath;
}
emit finished();
}