mirror of
https://github.com/swift-project/pilotclient.git
synced 2026-04-26 02:35:38 +08:00
refs #848, added (utility) functions to parser
This commit is contained in:
committed by
Mathew Sutcliffe
parent
c20b54466f
commit
0c720c7c71
@@ -8,6 +8,7 @@
|
|||||||
*/
|
*/
|
||||||
|
|
||||||
#include "blackmisc/pq/pqstring.h"
|
#include "blackmisc/pq/pqstring.h"
|
||||||
|
#include "blackmisc/stringutils.h"
|
||||||
#include "blackmisc/simplecommandparser.h"
|
#include "blackmisc/simplecommandparser.h"
|
||||||
|
|
||||||
#include <QList>
|
#include <QList>
|
||||||
@@ -18,7 +19,6 @@ using namespace BlackMisc::PhysicalQuantities;
|
|||||||
|
|
||||||
namespace BlackMisc
|
namespace BlackMisc
|
||||||
{
|
{
|
||||||
|
|
||||||
CSimpleCommandParser::CSimpleCommandParser(const QStringList &knownCommands)
|
CSimpleCommandParser::CSimpleCommandParser(const QStringList &knownCommands)
|
||||||
{
|
{
|
||||||
this->setCheckedCommandList(knownCommands);
|
this->setCheckedCommandList(knownCommands);
|
||||||
@@ -55,7 +55,7 @@ namespace BlackMisc
|
|||||||
QString CSimpleCommandParser::remainingStringAfter(int index) const
|
QString CSimpleCommandParser::remainingStringAfter(int index) const
|
||||||
{
|
{
|
||||||
if (index < 0) { return this->m_originalLine.trimmed(); }
|
if (index < 0) { return this->m_originalLine.trimmed(); }
|
||||||
QString p = this->part(index);
|
const QString p = this->part(index);
|
||||||
int fi = this->m_originalLine.indexOf(p, 0, Qt::CaseInsensitive);
|
int fi = this->m_originalLine.indexOf(p, 0, Qt::CaseInsensitive);
|
||||||
if (fi < 0) { return ""; }
|
if (fi < 0) { return ""; }
|
||||||
return this->m_originalLine.mid(fi).trimmed();
|
return this->m_originalLine.mid(fi).trimmed();
|
||||||
@@ -66,6 +66,11 @@ namespace BlackMisc
|
|||||||
return this->m_splitParts.count();
|
return this->m_splitParts.count();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CSimpleCommandParser::hasPart(int index) const
|
||||||
|
{
|
||||||
|
return index >= 0 && index < this->countParts();
|
||||||
|
}
|
||||||
|
|
||||||
int CSimpleCommandParser::countPartsWithoutCommand() const
|
int CSimpleCommandParser::countPartsWithoutCommand() const
|
||||||
{
|
{
|
||||||
int c = countParts();
|
int c = countParts();
|
||||||
@@ -75,8 +80,8 @@ namespace BlackMisc
|
|||||||
bool CSimpleCommandParser::isInt(int index) const
|
bool CSimpleCommandParser::isInt(int index) const
|
||||||
{
|
{
|
||||||
const QString p = this->part(index);
|
const QString p = this->part(index);
|
||||||
if (p.isEmpty()) return false;
|
if (p.isEmpty()) { return false; }
|
||||||
bool ok;
|
bool ok = false;
|
||||||
p.toInt(&ok);
|
p.toInt(&ok);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
@@ -84,8 +89,8 @@ namespace BlackMisc
|
|||||||
bool CSimpleCommandParser::isDouble(int index) const
|
bool CSimpleCommandParser::isDouble(int index) const
|
||||||
{
|
{
|
||||||
const QString p = this->part(index);
|
const QString p = this->part(index);
|
||||||
if (p.isEmpty()) return false;
|
if (p.isEmpty()) { return false; }
|
||||||
bool ok;
|
bool ok = false;
|
||||||
CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
||||||
return ok;
|
return ok;
|
||||||
}
|
}
|
||||||
@@ -93,21 +98,38 @@ namespace BlackMisc
|
|||||||
int CSimpleCommandParser::toInt(int index, int def) const
|
int CSimpleCommandParser::toInt(int index, int def) const
|
||||||
{
|
{
|
||||||
const QString p = this->part(index);
|
const QString p = this->part(index);
|
||||||
if (p.isEmpty()) return def;
|
if (p.isEmpty()) { return def; }
|
||||||
bool ok;
|
bool ok = false;
|
||||||
int i = p.toInt(&ok);
|
int i = p.toInt(&ok);
|
||||||
return ok ? i : def;
|
return ok ? i : def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CSimpleCommandParser::toBool(int index, bool def) const
|
||||||
|
{
|
||||||
|
const QString p = this->part(index);
|
||||||
|
if (p.isEmpty()) { return def; }
|
||||||
|
const bool b = stringToBool(p);
|
||||||
|
return b;
|
||||||
|
}
|
||||||
|
|
||||||
double CSimpleCommandParser::toDouble(int index, double def) const
|
double CSimpleCommandParser::toDouble(int index, double def) const
|
||||||
{
|
{
|
||||||
const QString p = this->part(index);
|
const QString p = this->part(index);
|
||||||
if (p.isEmpty()) { return def; }
|
if (p.isEmpty()) { return def; }
|
||||||
bool ok;
|
bool ok = false;
|
||||||
double d = CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
double d = CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess);
|
||||||
return ok ? d : def;
|
return ok ? d : def;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
bool CSimpleCommandParser::matchesPart(int index, const QString &toMatch, Qt::CaseSensitivity cs) const
|
||||||
|
{
|
||||||
|
if (toMatch.isEmpty()) { return false; }
|
||||||
|
if (!this->hasPart(index)) { return false; }
|
||||||
|
const QString p(this->part(index));
|
||||||
|
if (p.isEmpty()) { return false; }
|
||||||
|
return (p.length() == toMatch.length() && p.startsWith(toMatch, cs));
|
||||||
|
}
|
||||||
|
|
||||||
QString CSimpleCommandParser::removeLeadingDot(const QString &candidate)
|
QString CSimpleCommandParser::removeLeadingDot(const QString &candidate)
|
||||||
{
|
{
|
||||||
if (!candidate.startsWith('.')) { return candidate; }
|
if (!candidate.startsWith('.')) { return candidate; }
|
||||||
@@ -147,10 +169,9 @@ namespace BlackMisc
|
|||||||
|
|
||||||
void CSimpleCommandParser::setCheckedCommandList(const QStringList &commands)
|
void CSimpleCommandParser::setCheckedCommandList(const QStringList &commands)
|
||||||
{
|
{
|
||||||
foreach(QString c, commands)
|
for (const QString &c : commands)
|
||||||
{
|
{
|
||||||
this->m_knownCommands.append(formatCommand(c));
|
this->m_knownCommands.append(formatCommand(c));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|||||||
@@ -13,7 +13,6 @@
|
|||||||
#define BLACKMISC_SIMPLECOMMANDPARSER_H
|
#define BLACKMISC_SIMPLECOMMANDPARSER_H
|
||||||
|
|
||||||
#include "blackmisc/blackmiscexport.h"
|
#include "blackmisc/blackmiscexport.h"
|
||||||
|
|
||||||
#include <QString>
|
#include <QString>
|
||||||
#include <QStringList>
|
#include <QStringList>
|
||||||
|
|
||||||
@@ -24,7 +23,7 @@ namespace BlackMisc
|
|||||||
class BLACKMISC_EXPORT CSimpleCommandParser
|
class BLACKMISC_EXPORT CSimpleCommandParser
|
||||||
{
|
{
|
||||||
public:
|
public:
|
||||||
//! No Constructor
|
//! Constructor
|
||||||
CSimpleCommandParser(const QStringList &knownCommands);
|
CSimpleCommandParser(const QStringList &knownCommands);
|
||||||
|
|
||||||
//! Known command
|
//! Known command
|
||||||
@@ -51,6 +50,9 @@ namespace BlackMisc
|
|||||||
//! Count parts
|
//! Count parts
|
||||||
int countParts() const;
|
int countParts() const;
|
||||||
|
|
||||||
|
//! Existing part
|
||||||
|
bool hasPart(int index) const;
|
||||||
|
|
||||||
//! Count parts, command excluded
|
//! Count parts, command excluded
|
||||||
int countPartsWithoutCommand() const;
|
int countPartsWithoutCommand() const;
|
||||||
|
|
||||||
@@ -63,11 +65,17 @@ namespace BlackMisc
|
|||||||
//! Part as integer
|
//! Part as integer
|
||||||
int toInt(int index, int def = -1) const;
|
int toInt(int index, int def = -1) const;
|
||||||
|
|
||||||
|
//! Part as bool
|
||||||
|
bool toBool(int index, bool def = false) const;
|
||||||
|
|
||||||
//! Part as double
|
//! Part as double
|
||||||
double toDouble(int index, double def = -1.0) const;
|
double toDouble(int index, double def = -1.0) const;
|
||||||
|
|
||||||
|
//! Matches given part
|
||||||
|
bool matchesPart(int index, const QString &toMatch, Qt::CaseSensitivity cs = Qt::CaseInsensitive) const;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
QString m_originalLine;
|
QString m_originalLine; //!< line as entered by user
|
||||||
QString m_cleanedLine; //!< trimmed, no double spaces etc.
|
QString m_cleanedLine; //!< trimmed, no double spaces etc.
|
||||||
QString m_commandPart; //!< command part (e.g. ".msg", if any)
|
QString m_commandPart; //!< command part (e.g. ".msg", if any)
|
||||||
QStringList m_splitParts; //!< split parts (split by " ")
|
QStringList m_splitParts; //!< split parts (split by " ")
|
||||||
|
|||||||
Reference in New Issue
Block a user