diff --git a/src/blackmisc/simplecommandparser.cpp b/src/blackmisc/simplecommandparser.cpp index 01f4957c2..71fef6878 100644 --- a/src/blackmisc/simplecommandparser.cpp +++ b/src/blackmisc/simplecommandparser.cpp @@ -8,6 +8,7 @@ */ #include "blackmisc/pq/pqstring.h" +#include "blackmisc/stringutils.h" #include "blackmisc/simplecommandparser.h" #include @@ -18,7 +19,6 @@ using namespace BlackMisc::PhysicalQuantities; namespace BlackMisc { - CSimpleCommandParser::CSimpleCommandParser(const QStringList &knownCommands) { this->setCheckedCommandList(knownCommands); @@ -55,7 +55,7 @@ namespace BlackMisc QString CSimpleCommandParser::remainingStringAfter(int index) const { 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); if (fi < 0) { return ""; } return this->m_originalLine.mid(fi).trimmed(); @@ -66,6 +66,11 @@ namespace BlackMisc return this->m_splitParts.count(); } + bool CSimpleCommandParser::hasPart(int index) const + { + return index >= 0 && index < this->countParts(); + } + int CSimpleCommandParser::countPartsWithoutCommand() const { int c = countParts(); @@ -75,8 +80,8 @@ namespace BlackMisc bool CSimpleCommandParser::isInt(int index) const { const QString p = this->part(index); - if (p.isEmpty()) return false; - bool ok; + if (p.isEmpty()) { return false; } + bool ok = false; p.toInt(&ok); return ok; } @@ -84,8 +89,8 @@ namespace BlackMisc bool CSimpleCommandParser::isDouble(int index) const { const QString p = this->part(index); - if (p.isEmpty()) return false; - bool ok; + if (p.isEmpty()) { return false; } + bool ok = false; CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess); return ok; } @@ -93,21 +98,38 @@ namespace BlackMisc int CSimpleCommandParser::toInt(int index, int def) const { const QString p = this->part(index); - if (p.isEmpty()) return def; - bool ok; + if (p.isEmpty()) { return def; } + bool ok = false; int i = p.toInt(&ok); 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 { const QString p = this->part(index); if (p.isEmpty()) { return def; } - bool ok; + bool ok = false; double d = CPqString::parseNumber(p, ok, CPqString::SeparatorsBestGuess); 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) { if (!candidate.startsWith('.')) { return candidate; } @@ -147,10 +169,9 @@ namespace BlackMisc void CSimpleCommandParser::setCheckedCommandList(const QStringList &commands) { - foreach(QString c, commands) + for (const QString &c : commands) { this->m_knownCommands.append(formatCommand(c)); } } - } // namespace diff --git a/src/blackmisc/simplecommandparser.h b/src/blackmisc/simplecommandparser.h index 150090b21..be47c99dd 100644 --- a/src/blackmisc/simplecommandparser.h +++ b/src/blackmisc/simplecommandparser.h @@ -13,7 +13,6 @@ #define BLACKMISC_SIMPLECOMMANDPARSER_H #include "blackmisc/blackmiscexport.h" - #include #include @@ -24,7 +23,7 @@ namespace BlackMisc class BLACKMISC_EXPORT CSimpleCommandParser { public: - //! No Constructor + //! Constructor CSimpleCommandParser(const QStringList &knownCommands); //! Known command @@ -51,6 +50,9 @@ namespace BlackMisc //! Count parts int countParts() const; + //! Existing part + bool hasPart(int index) const; + //! Count parts, command excluded int countPartsWithoutCommand() const; @@ -63,11 +65,17 @@ namespace BlackMisc //! Part as integer int toInt(int index, int def = -1) const; + //! Part as bool + bool toBool(int index, bool def = false) const; + //! Part as double 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: - QString m_originalLine; + QString m_originalLine; //!< line as entered by user QString m_cleanedLine; //!< trimmed, no double spaces etc. QString m_commandPart; //!< command part (e.g. ".msg", if any) QStringList m_splitParts; //!< split parts (split by " ")