Continues migration with DDS, Common
This commit is contained in:
1 parent
9b15cee841
commit
fa48db111e
668 files changed
+111841
-2
No files matched your search
+21
@@ -0,0 +1,21 @@
|
||||
#
|
||||
# $Id$
|
||||
# $HeadURL$
|
||||
# $Author$
|
||||
#
|
||||
|
||||
NGL_ADD_LIBRARY2(ALL LtsCommandLineParser SHARED
|
||||
LtsCommandLineParser.cpp
|
||||
LtsCommandLineParser.h
|
||||
)
|
||||
NGL_TARGET_INCLUDE_DIRECTORIES(LtsCommandLineParser
|
||||
INTERFACE
|
||||
${CMAKE_CURRENT_SOURCE_DIR}/../
|
||||
)
|
||||
NGL_TARGET_LINK_LIBRARIES(LtsCommandLineParser
|
||||
PRIVATE
|
||||
ngltools
|
||||
Qt5::Network
|
||||
PUBLIC
|
||||
Qt5::Core
|
||||
)
|
||||
+183
@@ -0,0 +1,183 @@
|
||||
|
||||
|
||||
|
||||
#include "LtsCommandLineParser.h"
|
||||
#include <ngltools/ngltools_filesystem.h>
|
||||
#include <QNetworkInterface>
|
||||
#include <QFlags>
|
||||
#include <QSettings>
|
||||
#include <QUrl>
|
||||
|
||||
namespace LtsCommandLineParser
|
||||
{
|
||||
static constexpr int s_maxDomainId = 232;
|
||||
|
||||
LtsCommandLineParser::LtsCommandLineParser(ProvideCommandLineParameterOptions opts)
|
||||
: QCommandLineParser()
|
||||
, m_enabledOptions(opts)
|
||||
{
|
||||
if (NGL::isAppInBuildBasePath() || m_enabledOptions.testFlag(CreateDomainIdFromIp))
|
||||
{
|
||||
const QList<QHostAddress> addresses = QNetworkInterface::allAddresses();
|
||||
for (const QHostAddress &address : addresses)
|
||||
{
|
||||
if (address.isLoopback() || address.isNull())
|
||||
continue;
|
||||
if (address.protocol() != QAbstractSocket::IPv4Protocol)
|
||||
continue;
|
||||
|
||||
|
||||
m_generatedDomainId = (address.toIPv4Address() & 0x000000ff) % 232;
|
||||
if (m_generatedDomainId == 0)
|
||||
m_generatedDomainId = s_maxDomainId;
|
||||
break;
|
||||
}
|
||||
}
|
||||
|
||||
if (m_enabledOptions.testFlag(ExerciseId))
|
||||
addOption({{QStringLiteral("e"), QStringLiteral("exerciseid")},
|
||||
QStringLiteral("Default: -1 (invalid, get from lts-cc.conf)"),
|
||||
QStringLiteral("exerciseId"),
|
||||
QStringLiteral("-1")
|
||||
});
|
||||
if (m_enabledOptions.testFlag(DomainId))
|
||||
addOption({{QStringLiteral("d"), QStringLiteral("domainid")},
|
||||
QStringLiteral("Default: ") + QString::number(m_generatedDomainId),
|
||||
QStringLiteral("domainId"),
|
||||
QString::number(m_generatedDomainId)
|
||||
});
|
||||
if (m_enabledOptions.testFlag(CustomConfigFile))
|
||||
{
|
||||
const QString cf(NGL::findProjectConfigFile());
|
||||
addOption({{QStringLiteral("i"), QStringLiteral("ini")},
|
||||
QStringLiteral("Default: ") + cf,
|
||||
QStringLiteral("filename"),
|
||||
cf
|
||||
});
|
||||
}
|
||||
if (m_enabledOptions.testFlag(TcpConnectionUrl))
|
||||
{
|
||||
const QString defaultTcpUrl(QStringLiteral("EXCTDS01:7400"));
|
||||
addOption({{QStringLiteral("t"), QStringLiteral("tcpurl")},
|
||||
QStringLiteral("Default: ") + defaultTcpUrl,
|
||||
QStringLiteral("hostname:port"),
|
||||
defaultTcpUrl
|
||||
});
|
||||
}
|
||||
addHelpOption();
|
||||
|
||||
if (!m_enabledOptions.testFlag(NoVersion))
|
||||
addVersionOption();
|
||||
}
|
||||
|
||||
int LtsCommandLineParser::domainId() const
|
||||
{
|
||||
if (m_domainId == 0 && m_generatedDomainId != 0 && !m_domainIdSetByFunction)
|
||||
{
|
||||
#ifdef WIN32
|
||||
|
||||
fprintf(stdout, "\n\nAttention: using a default domain id != 0 because executable is run inside builddir - default domainid is %d\n\n", m_generatedDomainId);
|
||||
#else
|
||||
fprintf(stdout, "\n\n\e[31mAttention: using a default domain id != 0 because executable is run inside builddir - default domainid is %d\e[0m\n\n", m_generatedDomainId);
|
||||
#endif
|
||||
return m_generatedDomainId;
|
||||
}
|
||||
return m_domainId;
|
||||
}
|
||||
|
||||
void LtsCommandLineParser::setDomainId(int id)
|
||||
{
|
||||
m_domainIdSetByFunction = true;
|
||||
m_domainId = id;
|
||||
}
|
||||
|
||||
bool LtsCommandLineParser::iniFileSet() const
|
||||
{
|
||||
if (!m_enabledOptions.testFlag(CustomConfigFile))
|
||||
return false;
|
||||
return isSet(QStringLiteral("i"));
|
||||
}
|
||||
|
||||
bool LtsCommandLineParser::useTcpConnection() const
|
||||
{
|
||||
if (!m_enabledOptions.testFlag(TcpConnectionUrl))
|
||||
return false;
|
||||
return isSet(QStringLiteral("t"));
|
||||
}
|
||||
|
||||
QUrl LtsCommandLineParser::tcpConnectionUrl() const
|
||||
{
|
||||
if (!m_enabledOptions.testFlag(TcpConnectionUrl))
|
||||
return QUrl();
|
||||
if (!useTcpConnection())
|
||||
return QUrl();
|
||||
QString str = value(QStringLiteral("t"));
|
||||
|
||||
str.replace(QLatin1String("tcpv4_lan"), QLatin1String("tcpv4lan"));
|
||||
QUrl url = QUrl::fromUserInput(str);
|
||||
if (url.scheme() == QLatin1String("http") ||
|
||||
url.scheme() == QLatin1String("file"))
|
||||
url.setScheme(QStringLiteral("tcpv4lan"));
|
||||
return url;
|
||||
}
|
||||
|
||||
bool LtsCommandLineParser::init(bool bParseHelp)
|
||||
{
|
||||
if (!parse(QCoreApplication::arguments()))
|
||||
{
|
||||
fprintf(stdout, "\nBad option: %s \n", qPrintable(errorText()));
|
||||
if (!m_unitModuleTestMode)
|
||||
return false;
|
||||
}
|
||||
|
||||
if (m_enabledOptions.testFlag(ExerciseId))
|
||||
m_exerciseId = value(QStringLiteral("e")).toInt();
|
||||
if (m_enabledOptions.testFlag(DomainId))
|
||||
m_domainId = value(QStringLiteral("d")).toInt();
|
||||
if (m_enabledOptions.testFlag(CustomConfigFile))
|
||||
m_iniFile = value(QStringLiteral("i"));
|
||||
|
||||
if (bParseHelp && isSet(QStringLiteral("h")))
|
||||
showHelp();
|
||||
if (!m_unitModuleTestMode && isSet(QStringLiteral("v")))
|
||||
showVersion();
|
||||
if (!domainIdProvided())
|
||||
{
|
||||
const QString iniFile = m_iniFile.isEmpty() ? NGL::findProjectConfigFile() : m_iniFile;
|
||||
if (!iniFile.isEmpty())
|
||||
{
|
||||
QSettings s(iniFile, QSettings::IniFormat);
|
||||
bool bOk;
|
||||
int domainId = s.value(QStringLiteral("Global/DomainId"), m_domainId).toInt(&bOk);
|
||||
if (bOk && domainId > 0 && domainId <= s_maxDomainId)
|
||||
m_domainId = domainId;
|
||||
}
|
||||
}
|
||||
return true;
|
||||
}
|
||||
|
||||
void LtsCommandLineParser::showHelp(int exitCode)
|
||||
{
|
||||
if (!m_additionalHelpStr.isEmpty())
|
||||
fprintf(stdout, "%s", qPrintable(m_additionalHelpStr));
|
||||
|
||||
|
||||
QCommandLineParser::showHelp(exitCode);
|
||||
}
|
||||
|
||||
bool LtsCommandLineParser::domainIdProvided() const
|
||||
{
|
||||
if (!m_enabledOptions.testFlag(DomainId))
|
||||
return false;
|
||||
return isSet(QStringLiteral("d"));
|
||||
}
|
||||
|
||||
bool LtsCommandLineParser::exerciseIdProvided() const
|
||||
{
|
||||
if (!m_enabledOptions.testFlag(ExerciseId))
|
||||
return false;
|
||||
return isSet(QStringLiteral("e"));
|
||||
}
|
||||
|
||||
|
||||
}
|
||||
+56
@@ -0,0 +1,56 @@
|
||||
|
||||
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <QCommandLineParser>
|
||||
#include <LtsCommandLineParser/ltscommandlineparser_export.h>
|
||||
|
||||
class QUrl;
|
||||
namespace LtsCommandLineParser
|
||||
{
|
||||
class LTSCOMMANDLINEPARSER_EXPORT LtsCommandLineParser : public QCommandLineParser
|
||||
{
|
||||
public:
|
||||
enum ProvideCommandLineParameterOptions
|
||||
{
|
||||
None = 0x0000,
|
||||
CreateDomainIdFromIp = 0x0001,
|
||||
DomainId = 0x0002,
|
||||
ExerciseId = 0x0004,
|
||||
CustomConfigFile = 0x0008,
|
||||
TcpConnectionUrl = 0x0010,
|
||||
NoVersion = 0x0020
|
||||
};
|
||||
|
||||
explicit LtsCommandLineParser(ProvideCommandLineParameterOptions opts = ProvideCommandLineParameterOptions(CreateDomainIdFromIp|DomainId|ExerciseId|CustomConfigFile));
|
||||
virtual ~LtsCommandLineParser() = default;
|
||||
int domainId() const;
|
||||
void setDomainId(int id);
|
||||
int exerciseId() const { return m_exerciseId; }
|
||||
QString iniFile() const { return m_iniFile; }
|
||||
bool iniFileSet() const;
|
||||
bool useTcpConnection() const;
|
||||
QUrl tcpConnectionUrl() const;
|
||||
virtual bool init(bool bParseHelp = true);
|
||||
|
||||
void showHelp(int exitCode = 0);
|
||||
bool domainIdProvided() const;
|
||||
bool exerciseIdProvided() const;
|
||||
|
||||
void setUnitModuleTestMode(bool onOff) { m_unitModuleTestMode = onOff; }
|
||||
|
||||
protected:
|
||||
QString m_additionalHelpStr;
|
||||
|
||||
private:
|
||||
QFlags<ProvideCommandLineParameterOptions> m_enabledOptions;
|
||||
int m_exerciseId{0};
|
||||
int m_domainId{0};
|
||||
int m_generatedDomainId{0};
|
||||
bool m_domainIdSetByFunction{false};
|
||||
bool m_unitModuleTestMode{false};
|
||||
QString m_iniFile;
|
||||
};
|
||||
|
||||
}
|
||||
Reference in new issue
Block a user