File: qdltoptmanager.cpp

package info (click to toggle)
dlt-viewer 2.27.0%2Bdfsg-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 9,196 kB
  • sloc: cpp: 31,376; ansic: 4,224; xml: 492; sh: 244; makefile: 81
file content (306 lines) | stat: -rw-r--r-- 10,109 bytes parent folder | download
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
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
/**
 * @licence app begin@
 * Copyright (C) 2011-2012  BMW AG
 *
 * This file is part of COVESA Project Dlt Viewer.
 *
 * Contributions are licensed to the COVESA Alliance under one or more
 * Contribution License Agreements.
 *
 * \copyright
 * This Source Code Form is subject to the terms of the
 * Mozilla Public License, v. 2.0. If a  copy of the MPL was not distributed with
 * this file, You can obtain one at http://mozilla.org/MPL/2.0/.
 *
 * \file optmanager.cpp
 * For further information see http://www.covesa.global/.
 * @licence end@
 */

#if (WIN32)
#include <windows.h>
#endif

#include "qdltoptmanager.h"
#include "version.h"

#include <QDebug>
#include <QFileInfo>

QDltOptManager::QDltOptManager()
{
    m_parser.setSingleDashWordOptionMode(QCommandLineParser::ParseAsLongOptions);

    m_parser.addPositionalArgument("logfile", "Loading one or more logfiles on startup (must end with .dlt)");
    m_parser.addPositionalArgument("projectfile", "Loading project file on startup (must end with .dlp)");
    m_parser.addPositionalArgument("filterfile", "Loading filterfile on startup (must end with .dlf)");
    m_parser.addPositionalArgument("pcapfile", "Importing DLT/IPC from pcap file on startup (must end with .pcap)");
    m_parser.addPositionalArgument("mf4file", "Importing DLT/IPC from mf4 file on startup (must end with .mf4)");
    m_parser.addOptions({
        {"c", "Convert logfile file to <textfile>", "textfile"},
        {"u", "Conversion will be done in UTF8 instead of ASCII"},
        {"csv", "Conversion will be done in CSV format"},
        {"d", "Conversion will NOT be done, save in dlt file format again instead"},
        {"dd", "Conversion will NOT be done, save as decoded messages in dlt format"},
        {"b", "Execute a plugin command with <n> parameters before loading log file.", "plugin|command|param1|..|param<n>"},
        {"e", "Execute a plugin command with <n> parameters after loading log file.", "plugin|command|param1|..|param<n>"},
        {QStringList() << "s" << "silent", "Enable silent mode without any GUI. Ideal for commandline usage."},
        {"stream", "Treat the input logfiles as DLT stream instead of DLT files."},
        {QStringList() << "t" << "terminate", "Terminate DLT Viewer after command line execution."},
        {"w", "Set the working directory", "workingdirectory"},
        {"delimiter", "The used delimiter for CSV export (Default: ,).", "character"},
        {QStringList() << "h" << "help", "Print this help message."},
        {QStringList() << "v" << "version", "Print the version."}
    });

}

QDltOptManager* QDltOptManager::getInstance()
{
    static QDltOptManager instance;

    return &instance;
}

const QStringList &QDltOptManager::getMf4Files() const
{
    return mf4Files;
}

const QStringList &QDltOptManager::getPcapFiles() const
{
    return pcapFiles;
}

const QStringList &QDltOptManager::getPostPluginCommands() const
{
    return postPluginCommands;
}

const QStringList &QDltOptManager::getPrePluginCommands() const
{
    return prePluginCommands;
}

void QDltOptManager::printVersion(QString appname)
{
    qDebug() << "Executable Name:" << appname;
    qDebug() << "Build time:" << __DATE__ << __TIME__;
    qDebug() << "Version:" << PACKAGE_VERSION << PACKAGE_VERSION_STATE;
}

void QDltOptManager::printUsage(const QString& helpText)
{
    qDebug().noquote() << helpText;
    qDebug() << "\nExamples:";
    qDebug() << "  dlt-viewer.exe -t -c output.txt input.dlt";
    qDebug() << "  dlt-viewer.exe -t -s -u -c output.txt input.dlt";
    qDebug() << "  dlt-viewer.exe -t -s -d -c output.dlt input.dlt";
    qDebug() << "  dlt-viewer.exe -t -s decoded.dlp -dd -c output.dlt input.dlt ";
    qDebug() << "  dlt-viewer.exe -t -s -csv -c output.csv input.dlt";
    qDebug() << "  dlt-viewer.exe -t -s -d filter.dlf -c output.dlt input.dlt";
    qDebug() << "  dlt-viewer.exe -p export.dlp -e \"Filetransfer Plugin|export|ftransferdir\" input.dlt";
    qDebug() << "  dlt-viewer.exe input1.dlt input2.dlt";
    qDebug() << "  dlt-viewer.exe -t -c output.txt input.pcap";
    qDebug() << "  dlt-viewer.exe -t -c output.txt input1.mf4 input2.mf4";
}

void QDltOptManager::parse(const QStringList& args)
{
    m_parser.parse(args);

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

    printVersion(args.at(0));

     /* the default parameter - exactly one parameter - should either be
      * a dlt or a dlp file, so this enables the "doubleclick" feature
      */

    if (m_parser.optionNames().isEmpty() && m_parser.positionalArguments().size() == 1)
    {
        const QString& arg = m_parser.positionalArguments().at(0);
        if(arg.endsWith(".dlp") || arg.endsWith(".DLP"))
        {
            projectFile = arg;
            project = true;
            qDebug()<< "Project filename:" << projectFile;
            return;
        }
        if (arg.endsWith(".dlt") || arg.endsWith(".DLT"))
        {
            const QString logFile = arg;
            logFiles += logFile;
            qDebug()<< "DLT filename:" << logFile;
            return;
        }
    }

    if (m_parser.isSet("help")) {
        printUsage(m_parser.helpText());
        exit(0);
    }

    if (m_parser.isSet("silent")) {
        silent_mode = true;
        qDebug() << "Silent mode enabled";
    }

    if (m_parser.isSet("version")) {
        // version is already printed above
        exit(0);
    }

    if (m_parser.isSet("terminate")) {
        terminate = true;
        commandline_mode = true;
    }

    if (m_parser.isSet("c")) {
        convertDestFile = m_parser.value("c");
        qDebug() << "Convert filename:" << convertDestFile;
        commandline_mode = true;
    }

    if (m_parser.isSet("delimiter")) {
        delimiter = m_parser.value("delimiter").front().toLatin1();
        qDebug() << "Delimiter:" << delimiter;
    }

    if (m_parser.isSet("u")) {
        convertionmode = e_UTF8;
    }

    if (m_parser.isSet("csv")) {
        convertionmode = e_CSV;
    }

    if (m_parser.isSet("d")) {
        convertionmode = e_DLT;
    }

    if (m_parser.isSet("dd")) {
        convertionmode = e_DDLT;
    }

    if (m_parser.isSet("stream")) {
        inputmode = e_inputmode::STREAM;
    }

    if (m_parser.isSet("e")) {
        postPluginCommands += m_parser.values("e");
        commandline_mode = true;
    }

    if (m_parser.isSet("b")) {
        prePluginCommands += m_parser.values("b");
        commandline_mode = true;
    }

    if (m_parser.isSet("w")) {
        workingDirectory = m_parser.value("w");
    }

    QStringList positionalArguments = m_parser.positionalArguments();
    for (const QString &arg : positionalArguments)
    {
        if(arg.endsWith(".dlt") || arg.endsWith(".DLT"))
        {
            const QString logFile = arg;
            logFiles += logFile;
            qDebug()<< "DLT filename:" << logFile;
        }
        else if(arg.endsWith(".dlp") || arg.endsWith(".DLP"))        {
            if (project == true)
            {
                qDebug() << "\nError: Can only load one project file\n";
                printUsage(m_parser.helpText());
                exit(-1);
            }

            projectFile = arg;
            project = true;
            qDebug()<< "Project filename:" << projectFile;
        }
        else if(arg.endsWith(".dlf") || arg.endsWith(".DLF"))
        {
            filterFiles += arg;
            qDebug()<< "Filter filename:" << arg;
        }
        else if(arg.endsWith(".pcap") || arg.endsWith(".PCAP"))
        {
            const QString pcapFile = arg;
            pcapFiles += pcapFile;
            qDebug()<< "Pcap filename:" << pcapFile;
        }
        else if(arg.endsWith(".mf4") || arg.endsWith(".MF4"))
        {
            const QString mf4File = arg;
            mf4Files += mf4File;
            qDebug()<< "MF4 filename:" << mf4File;
        }
    }

    /* On Windows we do not want to open a console in case
      * we start the application e.g. from file explorer.
      * Unfortunateley Windows opens a console anyway.
      * So we have to close it in this case
     */
    #if (WIN32)
        if (!commandline_mode)
        {
            HWND consoleWnd = GetConsoleWindow();
            DWORD dwProcessId;
            GetWindowThreadProcessId(consoleWnd, &dwProcessId);
            if (GetCurrentProcessId() == dwProcessId)
            {
                // user launched the application with a double click from explorer: we do not need console
                FreeConsole();
            }
        }
    #endif
}

bool QDltOptManager::isProjectFile(){ return project;}
bool QDltOptManager::isTerminate(){return terminate;}
bool QDltOptManager::issilentMode(){return silent_mode;}
bool QDltOptManager::isCommandlineMode(){return commandline_mode;}
e_convertionmode QDltOptManager::get_convertionmode(){return convertionmode;}
e_inputmode QDltOptManager::get_inputmode(){return inputmode;}
QString QDltOptManager::getProjectFile(){return projectFile;}
QStringList QDltOptManager::getLogFiles(){return logFiles;}
QStringList QDltOptManager::getFilterFiles(){return filterFiles;}
QString QDltOptManager::getConvertDestFile(){return convertDestFile;}
QString QDltOptManager::getPluginName(){return pluginName;}
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
{
    return m_parser.helpText();
}

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();
}