报文解析工具
功能简介
- 支持自定义导入文件
- 修改关键字
- 多线程解析文本
- 保存解析之后的文本到新的文件
- 一键打开文件保存文件夹
核心功能相关代码如下:
自定义导入文件
bool MainWindow::openLogFile()
{
//文本导入框
QString strPath = QFileDialog::getOpenFileName(NULL,QString::fromUtf8("选择文件"),"",QObject::tr("All(*.*)"));
if(strPath == "")
{
QMessageBox::information(this,QString::fromUtf8("提示"),QString::fromUtf8("选择文件失败,无路径"),"OK");
return false;
}
filePathName = strPath;
ui->lab_filePath->setText(strPath);
return true;
}
文本文件保存
void MainWindow::saveRes2File()
{
QString filePath = QFileDialog::getSaveFileName(NULL,QString::fromUtf8("选择文件"),"",QObject::tr("txt(*.txt)"));
if(filePath == "")
{
QMessageBox::information(this,QString::fromUtf8("提示"),QString::fromUtf8("选择文件失败,无路径"),"OK");
return false;
}
QFile outResFile;
outResFile.setFileName(filePath);
if(!outResFile.exists())
{
outResFile.open(QIODevice::WriteOnly);
outResFile.close();
}
outResFile.open(QIODevice::ReadWrite | QIODevice::Truncate);
QString messCount = QString("This parsing omci Count is: " + QString::number(parsCount) + "\n");
QTextStream outRes(&outResFile);
if(outResFile.isOpen()) {
outResFile.write(messCount.toUtf8());
outRes << ui->out_text->toPlainText();
outRes.flush();
}
outResFile.close();
}
关键字筛选
此项目使用QString.indexof()、QString.mid用于关键字的定位和文本解析,分享为主,主要为大家提供思路
void MainWindow::parsOmci(QString goalMes)
{
//解析目标报文
bool toHex = true;
int attrIndex = 0;
//时间戳
TimeValue = goalMes.mid(1, goalMes.indexOf(':') - 1);
//crc结果
if( goalMes.mid(crcokIndex, 2) == "no")
{
Crc_ok = "no";
}else{
Crc_ok = "yes";
}
//packet_size
Packet_size = goalMes.mid(packetsizeIndex, packSizeLen);
//transaction_id
Transaction_id = goalMes.mid(bufferIndex, TRANSIDLEN);
//message_type
attrIndex = bufferIndex + TRANSIDLEN;
MessageType = goalMes.mid(attrIndex, MESSAGETYPELEN);
int messageType = MessageType.toInt(&toHex, 16) & 0x1F;
bool arBit = ((MessageType.toInt(&toHex, 16) & 0x40) != 0) ? true : false;
bool akBit = ((MessageType.toInt(&toHex, 16) & 0x20) != 0) ? true : false;
QString MessageTypeStr;
if(arBit)
{
MessageFrame = ">>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>>";
MessageTypeStr = " Request";
}
if(akBit)
{
MessageFrame = "<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<<";
MessageTypeStr = " Response";
}
//device identifier
attrIndex = bufferIndex + TRANSIDLEN + MESSAGETYPELEN;
if(goalMes.mid(attrIndex, DEVICEIDTYPELEN) == "0a")
{
DeviceIdentifier = "TypeA";
}else{
DeviceIdentifier = "TypeB";
}
//onu id
Onu_ID = QString::number(pon_ni) + '/' + QString::number(onu_id) + '/' + QString::number(onu_id);
//class id
attrIndex = bufferIndex + TRANSIDLEN + MESSAGETYPELEN + DEVICEIDTYPELEN;
Entity_class = goalMes.mid(attrIndex, CLASSNOLEN);
int classID = Entity_class.toInt(&toHex,16);
//instance id
attrIndex = bufferIndex + TRANSIDLEN + MESSAGETYPELEN + DEVICEIDTYPELEN + CLASSNOLEN;
Entity_instance = goalMes.mid(attrIndex, INSTANCEIDLEN);
//omci trailer
int omci_trailer_len = 0;
if(goalMes.right(2) == "\r\n")
{
omci_trailer_len = 20;
Omci_trailer = goalMes.right(20).left(16);
}else{
omci_trailer_len = 18;
Omci_trailer = goalMes.right(18).left(16);
}
for (int strIndex = 2; strIndex < Omci_trailer.size(); strIndex += 3) {
Omci_trailer.insert(strIndex, ' ');
}
//message content
attrIndex = bufferIndex + TRANSIDLEN + MESSAGETYPELEN + DEVICEIDTYPELEN + CLASSNOLEN + INSTANCEIDLEN;
MessageContent = goalMes.mid(attrIndex, goalMes.size() - attrIndex - omci_trailer_len);
int insertCount = MessageContent.size()/32;
for (int strIndex = 2; strIndex < MessageContent.size(); strIndex += 3) {
MessageContent.insert(strIndex, ' ');
}
for (int messCount = 0; messCount < insertCount-1; messCount++) {
MessageContent.insert((48 + 23*messCount), "\n ");
}
ui->out_text->append(MessageFrame);
ui->out_text->append("Timestamp :" + TimeValue);
ui->out_text->append("CRC ok :" + Crc_ok);
ui->out_text->append("Packet size :" + Packet_size);
ui->out_text->append("Transaction id :0x" + Transaction_id);
ui->out_text->append("Message type :" + QString(OmciMsgTypeNameStr[messageType]) + MessageTypeStr + "(0x" + MessageType + ")");
ui->out_text->append("Device identifier :" + DeviceIdentifier);
ui->out_text->append("ONU id :" + Onu_ID);
ui->out_text->append("Entity class :" + QString(OgSchMeGetNameStr(classID)) + "(0x" + Entity_class + ")");
ui->out_text->append("Entity instance :0x" + Entity_instance);
ui->out_text->append("OMCI message montent:" + MessageContent);
ui->out_text->append("OMCI trailer :" + Omci_trailer);
ui->out_text->append(MessageFrame + "\n");
}
打开指定文件夹
#include <QDesktopServices>
//saveFilePath为文件夹路径
QDesktopServices::openUrl(QUrl(saveFilePath, QUrl::TolerantMode));
程序的github地址:
程序运行界面: