diff options
| author | 2015-04-04 14:41:04 -0400 | |
|---|---|---|
| committer | 2015-04-04 14:41:04 -0400 | |
| commit | a5b034d4a9c44f9bc1e83b01de82530f8fc63013 (patch) | |
| tree | 7a358206c4aff9c33df1752c92eafec97cee2244 /SQLiteStudio3/guiSQLiteStudio/common/ipvalidator.cpp | |
| parent | 306d6d3ca9c9ad774d19135681a7f9805f77035f (diff) | |
Imported Upstream version 3.0.4upstream/3.0.4
Diffstat (limited to 'SQLiteStudio3/guiSQLiteStudio/common/ipvalidator.cpp')
| -rw-r--r-- | SQLiteStudio3/guiSQLiteStudio/common/ipvalidator.cpp | 95 |
1 files changed, 95 insertions, 0 deletions
diff --git a/SQLiteStudio3/guiSQLiteStudio/common/ipvalidator.cpp b/SQLiteStudio3/guiSQLiteStudio/common/ipvalidator.cpp new file mode 100644 index 0000000..35c8b75 --- /dev/null +++ b/SQLiteStudio3/guiSQLiteStudio/common/ipvalidator.cpp @@ -0,0 +1,95 @@ +#include "ipvalidator.h" +#include <QDebug> + +QString IpValidator::reStr = "^%1(\\d%2)%1\\.%1(\\d%2)%1\\.%1(\\d%2)%1\\.%1(\\d%2)%1$"; + +IpValidator::IpValidator(QObject* parent) : + QValidator(parent) +{ +} + +IpValidator::~IpValidator() +{ +} + +QValidator::State IpValidator::validate(QString& input, int&) const +{ + QString regexp = getPattern(acceptWhiteSpaces, false, whitespaceCharacter); + QRegularExpression re(regexp); + QRegularExpressionMatch match = re.match(input); + if (!match.hasMatch()) + return Invalid; + + QString part; + int value; + bool ok; + for (int i = 1; i <= 4; i++) + { + part = match.captured(i); + if (part.isEmpty()) + { + if (acceptEmptyParts) + continue; + else + return Invalid; + } + + value = part.toInt(&ok); + if (!ok) + return Invalid; + + if (value > 255 || value < 0) + return Invalid; + } + + return Acceptable; +} + +bool IpValidator::getAcceptWhiteSpaces() const +{ + return acceptWhiteSpaces; +} + +void IpValidator::setAcceptWhiteSpaces(bool value) +{ + acceptWhiteSpaces = value; +} +bool IpValidator::getAcceptEmptyParts() const +{ + return acceptEmptyParts; +} + +void IpValidator::setAcceptEmptyParts(bool value) +{ + acceptEmptyParts = value; +} +QChar IpValidator::getWhitespaceCharacter() const +{ + return whitespaceCharacter; +} + +void IpValidator::setWhitespaceCharacter(const QChar& value) +{ + whitespaceCharacter = value; +} + +bool IpValidator::check(const QString& input, bool acceptWhiteSpaces) +{ + QString regexp = getPattern(acceptWhiteSpaces, true, ' '); + QRegularExpression re(regexp); + return re.match(input).hasMatch(); +} + +QString IpValidator::getPattern(bool acceptWhiteSpaces, bool requireFull, QChar whitespaceCharacter) +{ + QString countChar = requireFull ? "+" : "*"; + if (acceptWhiteSpaces) + { + if (whitespaceCharacter == ' ') + return reStr.arg("\\s*", countChar); + else + return reStr.arg(whitespaceCharacter + QString("*"), countChar); + } + else + return reStr.arg("", countChar); +} |
