Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add QDltOptmanager unit tests and minor cleanup #564

Merged
merged 5 commits into from
Oct 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions qdlt/qdltexporter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -99,12 +99,12 @@ bool QDltExporter::start()
{
if(!to->open(QIODevice::WriteOnly | QIODevice::Text))
{
if ( true == QDltOptManager::getInstance()->issilentMode() )
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
}
else
;//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
if (QDltOptManager::getInstance()->issilentMode())
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
// QString("Cannot open the export file %1").arg(to->fileName()));
return false;
}
Expand All @@ -113,12 +113,12 @@ bool QDltExporter::start()
{
if(!to->open(QIODevice::WriteOnly))
{
if ( true == QDltOptManager::getInstance()->issilentMode() )
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
}
else
;//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
if (QDltOptManager::getInstance()->issilentMode() )
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
// QString("Cannot open the export file %1").arg(to->fileName()));
return false;
}
Expand All @@ -130,12 +130,12 @@ bool QDltExporter::start()
/* Write the first line of CSV file */
if(!writeCSVHeader(to))
{
if ( true == QDltOptManager::getInstance()->issilentMode() )
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
}
else
;//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
if(QDltOptManager::getInstance()->issilentMode())
{
qDebug() << QString("ERROR - cannot open the export file %1").arg(to->fileName());
}
//else
//QMessageBox::critical(qobject_cast<QWidget *>(parent()), QString("DLT Viewer"),
// QString("Cannot open the export file %1").arg(to->fileName()));
return false;
}
Expand Down
180 changes: 93 additions & 87 deletions qdlt/qdltoptmanager.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -26,30 +26,11 @@
#include <QDebug>
#include <QFileInfo>

// Global static pointer used to ensure a single instance of the class.
QDltOptManager* QDltOptManager::instance;

QDltOptManager::QDltOptManager()
{
project = false;
silent_mode = false;
terminate=false;
convertionmode = e_ASCI;
commandline_mode = false;
delimiter=',';
}

QDltOptManager* QDltOptManager::getInstance()
{
if (!instance)
instance = new QDltOptManager;

return instance;
}

QDltOptManager::QDltOptManager(QDltOptManager const&)
{
static QDltOptManager instance;

return &instance;
}

const QStringList &QDltOptManager::getMf4Files() const
Expand Down Expand Up @@ -81,32 +62,8 @@ void QDltOptManager::printVersion(QString appname)

void QDltOptManager::printUsage()
{
#if (WIN32)
qDebug()<<"Usage: dlt-viewer.exe [OPTIONS] [logfile] [projectfile] [filterfile] [mf4file] [pcapfile]";
#else
qDebug()<<"Usage: dlt-viewer [OPTIONS] [logfile] [projectfile] [filterfile] [mf4file] [pcapfile]";
#endif
qDebug().noquote() << getHelpText();

qDebug()<<"\nOptions:";
qDebug()<<" [logfile]\tLoading one or more logfiles on startup (must end with .dlt)";
qDebug()<<" [projectfile]\tLoading project file on startup (must end with .dlp)";
qDebug()<<" [filterfile]\tLoading filterfile on startup (must end with .dlf)";
qDebug()<<" [pcapfile]\tImporting DLT/IPC from pcap file on startup (must end with .pcap)";
qDebug()<<" [mf4file]\tImporting DLT/IPC from mf4 file on startup (must end with .mf4)";
qDebug()<<" -h or --help\tPrint usage";
qDebug()<<" -c textfile\tConvert logfile file to textfile";
qDebug()<<" -u\tConversion will be done in UTF8 instead of ASCII";
qDebug()<<" -csv\tConversion will be done in CSV format";
qDebug()<<" -d\tConversion will NOT be done, save in dlt file format again instead";
qDebug()<<" -dd\tConversion will NOT be done, save as decoded messages in dlt format";
qDebug()<<" -b \"plugin|command|param1|..|param<n>\"\tExecute a plugin command with <n> parameters before loading log file.";
qDebug()<<" -e \"plugin|command|param1|..|param<n>\"\tExecute a plugin command with <n> parameters after loading log file.";
qDebug()<<" -s or --silent\tEnable silent mode without any GUI. Ideal for commandline usage.";
qDebug()<<" -stream\tTreat the input logfiles as DLT stream instead of DLT files.";
qDebug()<<" -t or --terminate\tTerminate DLT Viewer after command line execution.";
qDebug()<<" -v or --version\tOnly show version and buildtime information";
qDebug()<<" -w workingdirectory\tSet the working directory";
qDebug()<<" -delimiter <character>\tThe used delimiter for CSV export (Default: ,).";
qDebug()<<"\nExamples:";
qDebug()<<" dlt-viewer.exe -t -c output.txt input.dlt";
qDebug()<<" dlt-viewer.exe -t -s -u -c output.txt input.dlt";
Expand All @@ -120,42 +77,37 @@ void QDltOptManager::printUsage()
qDebug()<<" dlt-viewer.exe -t -c output.txt input1.mf4 input2.mf4";
}

void QDltOptManager::parse(QStringList *opt)
void QDltOptManager::parse(QStringList&& opt)
{
QString str;

qDebug() << "### Starting DLT Viewer";

printVersion(opt->at(0));
printVersion(opt.at(0));

qDebug() << "### Parsing Options";

/* the default parameter - exactly one parameter - should either be
* a dlt or a dlp file, so this enables the "doubleclick" feature
*/
//str = opt->at(0); && ( str.compare("-h)") != 0 || str.compare("-v") !=0 )
if(opt->size()==2 )
{
if(opt->at(1).endsWith(".dlp") || opt->at(1).endsWith(".DLP"))
{
projectFile = QString("%1").arg(opt->at(1));
project = true;
qDebug()<< "Project filename:" << projectFile;
return;
}
if(opt->at(1).endsWith(".dlt") || opt->at(1).endsWith(".DLT"))
{
const QString logFile = QString("%1").arg(opt->at(1));
logFiles += logFile;
qDebug()<< "DLT filename:" << logFile;
return;
}
}
//str = opt.at(0); && ( str.compare("-h)") != 0 || str.compare("-v") !=0 )
if (opt.size() == 2) {
if (opt.at(1).endsWith(".dlp") || opt.at(1).endsWith(".DLP")) {
projectFile = QString("%1").arg(opt.at(1));
project = true;
qDebug() << "Project filename:" << projectFile;
return;
}
if (opt.at(1).endsWith(".dlt") || opt.at(1).endsWith(".DLT")) {
const QString logFile = QString("%1").arg(opt.at(1));
logFiles += logFile;
qDebug() << "DLT filename:" << logFile;
return;
}
}

// 0==Binary 1==First Argument
for (int i = 0; i < opt->size(); ++i)
for (int i = 0; i < opt.size(); ++i)
{
str = opt->at(i);
QString str = opt.at(i);

if(str.compare("-h") == 0 || str.compare("--help") == 0)
{
Expand All @@ -172,7 +124,7 @@ void QDltOptManager::parse(QStringList *opt)
}
else if(str.compare("-v") == 0 || str.compare("--version") == 0)
{
printVersion(opt->at(0));
// version has already been printed above, just exit
exit(0);
}
else if(str.compare("-t") == 0 || str.compare("--terminate") == 0)
Expand All @@ -182,7 +134,7 @@ void QDltOptManager::parse(QStringList *opt)
}
else if(str.compare("-c")==0)
{
QString c1 = opt->value(i+1);
QString c1 = opt.value(i+1);

convertDestFile = QString("%1").arg(c1);
// check here already if the selected file exists
Expand All @@ -194,7 +146,7 @@ void QDltOptManager::parse(QStringList *opt)
}
else if(str.compare("-delimiter")==0)
{
QString c1 = opt->value(i+1);
QString c1 = opt.value(i+1);

delimiter = QString("%1").arg(c1).front().toLatin1();

Expand Down Expand Up @@ -224,30 +176,30 @@ void QDltOptManager::parse(QStringList *opt)
}
else if(str.compare("-e")==0)
{
QString c = opt->value(i+1);
QString c = opt.value(i+1);
postPluginCommands += c;
commandline_mode = true;
++i;
}
else if(str.compare("-b")==0)
{
QString c = opt->value(i+1);
QString c = opt.value(i+1);
prePluginCommands += c;
commandline_mode = true;
++i;
}
else if (str.compare("-w") == 0)
{
workingDirectory = opt->value(i+1);
workingDirectory = opt.value(i+1);
++i;
}
else if(opt->at(i).endsWith(".dlt") || opt->at(i).endsWith(".DLT"))
else if(opt.at(i).endsWith(".dlt") || opt.at(i).endsWith(".DLT"))
{
const QString logFile = QString("%1").arg(opt->at(i));
const QString logFile = QString("%1").arg(opt.at(i));
logFiles += logFile;
qDebug()<< "DLT filename:" << logFile;
}
else if(opt->at(i).endsWith(".dlp") || opt->at(i).endsWith(".DLP"))
else if(opt.at(i).endsWith(".dlp") || opt.at(i).endsWith(".DLP"))
{
if (project == true)
{
Expand All @@ -256,24 +208,24 @@ void QDltOptManager::parse(QStringList *opt)
exit(-1);
}

projectFile = QString("%1").arg(opt->at(i));
projectFile = QString("%1").arg(opt.at(i));
project = true;
qDebug()<< "Project filename:" << projectFile;
}
else if(opt->at(i).endsWith(".dlf") || opt->at(i).endsWith(".DLF"))
else if(opt.at(i).endsWith(".dlf") || opt.at(i).endsWith(".DLF"))
{
filterFiles += QString("%1").arg(opt->at(i));
qDebug()<< "Filter filename:" << QString("%1").arg(opt->at(i));
filterFiles += QString("%1").arg(opt.at(i));
qDebug()<< "Filter filename:" << QString("%1").arg(opt.at(i));
}
else if(opt->at(i).endsWith(".pcap") || opt->at(i).endsWith(".PCAP"))
else if(opt.at(i).endsWith(".pcap") || opt.at(i).endsWith(".PCAP"))
{
const QString pcapFile = QString("%1").arg(opt->at(i));
const QString pcapFile = QString("%1").arg(opt.at(i));
pcapFiles += pcapFile;
qDebug()<< "Pcap filename:" << pcapFile;
}
else if(opt->at(i).endsWith(".mf4") || opt->at(i).endsWith(".MF4"))
else if(opt.at(i).endsWith(".mf4") || opt.at(i).endsWith(".MF4"))
{
const QString mf4File = QString("%1").arg(opt->at(i));
const QString mf4File = QString("%1").arg(opt.at(i));
mf4Files += mf4File;
qDebug()<< "MF4 filename:" << mf4File;
}
Expand Down Expand Up @@ -315,3 +267,57 @@ QString QDltOptManager::getCommandName(){return commandName;}
QStringList QDltOptManager::getCommandParams(){return commandParams;}
QString QDltOptManager::getWorkingDirectory() const { return workingDirectory; }
char QDltOptManager::getDelimiter(){return delimiter;}

QString QDltOptManager::getHelpText() const {
QStringList helpText;
#if (WIN32)
helpText << "Usage: dlt-viewer.exe [OPTIONS] [logfile] [projectfile] [filterfile] [mf4file] [pcapfile]";
#else
helpText << "Usage: dlt-viewer [OPTIONS] [logfile] [projectfile] [filterfile] [mf4file] [pcapfile]";
#endif

helpText << "\nOptions:";
helpText << " [logfile]\tLoading one or more logfiles on startup (must end with .dlt)";
helpText << " [projectfile]\tLoading project file on startup (must end with .dlp)";
helpText << " [filterfile]\tLoading filterfile on startup (must end with .dlf)";
helpText << " [pcapfile]\tImporting DLT/IPC from pcap file on startup (must end with .pcap)";
helpText << " [mf4file]\tImporting DLT/IPC from mf4 file on startup (must end with .mf4)";
helpText << " -h or --help\tPrint usage";
helpText << " -c textfile\tConvert logfile file to textfile";
helpText << " -u\tConversion will be done in UTF8 instead of ASCII";
helpText << " -csv\tConversion will be done in CSV format";
helpText << " -d\tConversion will NOT be done, save in dlt file format again instead";
helpText << " -dd\tConversion will NOT be done, save as decoded messages in dlt format";
helpText << " -b \"plugin|command|param1|..|param<n>\"\tExecute a plugin command with <n> parameters before loading log file.";
helpText << " -e \"plugin|command|param1|..|param<n>\"\tExecute a plugin command with <n> parameters after loading log file.";
helpText << " -s or --silent\tEnable silent mode without any GUI. Ideal for commandline usage.";
helpText << " -stream\tTreat the input logfiles as DLT stream instead of DLT files.";
helpText << " -t or --terminate\tTerminate DLT Viewer after command line execution.";
helpText << " -v or --version\tOnly show version and buildtime information";
helpText << " -w workingdirectory\tSet the working directory";
helpText << " -delimiter <character>\tThe used delimiter for CSV export (Default: ,).";

return helpText.join('\n');
}

void QDltOptManager::reset() {
project = false;
terminate = false;
silent_mode = false;
commandline_mode = false;
convertionmode = e_ASCI;
inputmode = e_inputmode::DLT;
projectFile.clear();
logFiles.clear();
filterFiles.clear();
convertDestFile.clear();
pluginName.clear();
commandName.clear();
commandParams.clear();
prePluginCommands.clear();
postPluginCommands.clear();
workingDirectory.clear();
delimiter = ',';
pcapFiles.clear();
mf4Files.clear();
}
23 changes: 12 additions & 11 deletions qdlt/qdltoptmanager.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,7 +46,7 @@ class QDLT_EXPORT QDltOptManager
static QDltOptManager* getInstance();
void printUsage();
void printVersion(QString appname);
void parse(QStringList *opt);
void parse(QStringList&& opt);

bool isProjectFile();
bool isTerminate();
Expand All @@ -71,16 +71,17 @@ class QDLT_EXPORT QDltOptManager
const QStringList &getMf4Files() const;
char getDelimiter();

QString getHelpText() const;

// only testing relevant
void reset();

private:
QDltOptManager();
QDltOptManager(QDltOptManager const&);
static QDltOptManager *instance;

bool project;
bool terminate;
bool silent_mode;
bool commandline_mode;
e_convertionmode convertionmode;
bool project{false};
bool terminate{false};
bool silent_mode{false};
bool commandline_mode{false};
e_convertionmode convertionmode{e_ASCI};
e_inputmode inputmode{e_inputmode::DLT};

QString projectFile;
Expand All @@ -97,7 +98,7 @@ class QDLT_EXPORT QDltOptManager
QStringList postPluginCommands; // command after loading log file

QString workingDirectory;
char delimiter;
char delimiter{','};
};

#endif //QDLTOPTMANAGER_H
Loading