完成文件读取
This commit is contained in:
130
readthread.cpp
Normal file
130
readthread.cpp
Normal file
@ -0,0 +1,130 @@
|
||||
#include <QTextStream>
|
||||
#include "readthread.h"
|
||||
|
||||
ReadThread::ReadThread(QObject *parent)
|
||||
: QThread{parent} {
|
||||
/*正则表达式*/
|
||||
//时间
|
||||
dateTimeRegex.setPattern("([0-9]{4}-[0-9]{2}-[0-9]{2} [0-9]{2}:[0-9]{2}:[0-9]{2}:[0-9]{3})");
|
||||
//基带
|
||||
jdRegex.setPattern(R"(\[.*(\d)\])");
|
||||
//匹配数字和可选的正负号
|
||||
pattern.setPattern("^[+-]?([1-9][0-9]*|0)[.]?[0-9]*$");
|
||||
//科学计数法
|
||||
kexue.setPattern("^[0-9].[0-9]e[+-][0-9][0-9]");
|
||||
}
|
||||
|
||||
bool ReadThread::isNumeric(QString data) {
|
||||
bool flag = false;
|
||||
patternMatch = pattern.match(data);
|
||||
if (patternMatch.hasMatch()) {
|
||||
flag = true;
|
||||
}
|
||||
kexueMatch = kexue.match(data);
|
||||
if (kexueMatch.hasMatch()) {
|
||||
flag = true;
|
||||
}
|
||||
return flag;
|
||||
}
|
||||
|
||||
void ReadThread::run() {
|
||||
//初始化数据
|
||||
int bytesRead = 0; //进度条数值
|
||||
for (auto &i: g_param) {
|
||||
i.clear();
|
||||
}
|
||||
QMap<QString, QString> temData;
|
||||
bool isBeginTime = true;
|
||||
//清除下拉框数据
|
||||
emit clearCombo();
|
||||
if (!namelist.empty()) {
|
||||
namelist.clear();
|
||||
completer->deleteLater();
|
||||
}
|
||||
|
||||
//设置状态栏
|
||||
emit showStatusbar("正在打开文件");
|
||||
|
||||
//读取文件
|
||||
QTextStream in(&file);
|
||||
while (!in.atEnd()) {
|
||||
//读取一行
|
||||
QString line = in.readLine();
|
||||
|
||||
//更新进度条
|
||||
bytesRead += line.size();
|
||||
emit setProgressBar(bytesRead * 2);
|
||||
|
||||
/* 匹配数据 */
|
||||
//匹配日期时间
|
||||
QMap<QString, QString> temMap;
|
||||
int JD;
|
||||
QString temtime;
|
||||
QString temjd;
|
||||
matchDateTime = dateTimeRegex.match(line);
|
||||
if (matchDateTime.hasMatch()) {
|
||||
temtime = matchDateTime.captured(1);
|
||||
temMap["时间"] = temtime;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
//匹配基带
|
||||
matchLogLevel = jdRegex.match(line);
|
||||
if (matchLogLevel.hasMatch()) {
|
||||
temjd = matchLogLevel.captured(1);
|
||||
temMap["基带"] = temjd;
|
||||
JD = temjd.toInt() - 1;
|
||||
} else {
|
||||
continue;
|
||||
}
|
||||
|
||||
//设置开始时间
|
||||
if (isBeginTime) {
|
||||
emit setBeginTime(QDateTime::fromString(temtime, "yyyy-MM-dd hh:mm:ss:zzz"));
|
||||
isBeginTime = false;
|
||||
}
|
||||
temData.clear();
|
||||
temData["时间"] = temtime;
|
||||
//将左右拆分
|
||||
QStringList dataParts = line.split(']');
|
||||
//判断右侧是否有数据
|
||||
if (dataParts[1].isEmpty()) {
|
||||
continue;
|
||||
}
|
||||
//将右侧数据拆分
|
||||
QStringList data = dataParts[1].split(',');
|
||||
for (int i = 0; i < data.size(); i++) {
|
||||
//将每一条数据拆为数据名和值
|
||||
QStringList tem = data[i].split('=');
|
||||
if (tem.size() < 2) {
|
||||
continue;
|
||||
}
|
||||
if (!namelist.contains(tem[0])) {
|
||||
namelist[tem[0]] = isNumeric(tem[1]);
|
||||
if (tem[0].contains("版本号")) {
|
||||
namelist[tem[0]] = false;
|
||||
}
|
||||
}
|
||||
temData[tem[0]] = tem[1];
|
||||
}
|
||||
//记录数据
|
||||
g_param[JD].append(temData);
|
||||
|
||||
}
|
||||
|
||||
//设置结束时间
|
||||
emit setEndTime(QDateTime::fromString(temData["时间"], "yyyy-MM-dd hh:mm:ss:zzz"));
|
||||
|
||||
//设置状态栏
|
||||
emit showStatusbar("打开成功:" + file.fileName());
|
||||
|
||||
//添加下拉框数据
|
||||
emit addCombo(namelist.keys());
|
||||
|
||||
//关闭
|
||||
emit closeProg();
|
||||
file.close();
|
||||
temData.clear();
|
||||
quit();
|
||||
}
|
||||
|
Reference in New Issue
Block a user