Text parsing tool developed by Qt

Packet Analysis Tool

Function introduction

  • Support for custom import files
  • modify keywords
  • Multi-threaded parsing of text
  • Save the parsed text to a new file
  • Open file save folder with one click

The core function related code is as follows:

custom import file
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;
}
text file save
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();
}
keyword filtering

This project uses QString.indexof() and QString.mid for keyword positioning and text analysis, mainly for sharing, mainly to provide ideas for everyone

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");
}
open specified folder
#include <QDesktopServices>
//saveFilePath为文件夹路径
QDesktopServices::openUrl(QUrl(saveFilePath, QUrl::TolerantMode));

The github address of the program:

Program running interface:
insert image description here

Guess you like

Origin blog.csdn.net/Wangguang_/article/details/118713932