-
Notifications
You must be signed in to change notification settings - Fork 1
/
Copy pathfile_reader.h
58 lines (48 loc) · 1.6 KB
/
file_reader.h
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
#include <QThread>
#include <QFile>
#include <QByteArray>
#include <QStringList>
#include <QDebug>
class FileReaderThread : public QThread {
Q_OBJECT
public:
FileReaderThread(const QString &filePath, QObject *parent = nullptr)
: QThread(parent), m_filePath(filePath) {}
signals:
void dataReady(const QList <QByteArray> &segments);
protected:
void run() override {
QFile file(m_filePath);
if (!file.open(QIODevice::ReadOnly)) {
qWarning() << "Failed to open file:" << m_filePath;
return;
}
QByteArray data = file.readAll();
file.close();
// 定义要查找的字符串
const QByteArray delimiter = QByteArray::fromHex("000001BA");
QList<QByteArray> segments;
int lastIndex = 0;
int index;
// 查找并分割数据
while ((index = data.indexOf(delimiter, lastIndex)) != -1) {
QByteArray segment = data.mid(lastIndex, index - lastIndex);
if (!segment.isEmpty()) {
segments.append(delimiter + segment);
// qDebug() << segment;
// qDebug() << "size " << segment.size();
}
lastIndex = index + delimiter.size();
}
// 处理最后一个段
if (lastIndex < data.size()) {
QByteArray segment = data.mid(lastIndex);
if (!segment.isEmpty()) {
segments.append(delimiter + segment);
}
}
emit dataReady(segments);
}
private:
QString m_filePath;
};