Validate find-replace value based on field type

Only kUInt64 type has been verified for this commit.

Others are pending.
This commit is contained in:
Srivats P 2021-12-04 12:17:37 +05:30
parent b60aad45d1
commit d3400f0897
9 changed files with 334 additions and 3 deletions

57
client/fieldedit.cpp Normal file
View File

@ -0,0 +1,57 @@
/*
Copyright (C) 2021 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#include "fieldedit.h"
FieldEdit::FieldEdit(QWidget *parent)
: QLineEdit(parent)
{
setType(kUInt64);
}
void FieldEdit::setType(FieldType type)
{
// clear existing contents before changing the validator
clear();
type_ = type;
switch (type_) {
case kUInt64:
setValidator(&uint64Validator_);
break;
case kMacAddress:
setValidator(&macValidator_);
break;
case kIp4Address:
setValidator(&ip4Validator_);
break;
case kIp6Address:
setValidator(&ip6Validator_);
break;
default:
setValidator(nullptr);
break;
}
}
// Applicable only if type is kUInt64
void FieldEdit::setRange(quint64 min, quint64 max)
{
uint64Validator_.setRange(min, max);
}

55
client/fieldedit.h Normal file
View File

@ -0,0 +1,55 @@
/*
Copyright (C) 2021 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _FIELD_EDIT_H
#define _FIELD_EDIT_H
#include "ipv4addressvalidator.h"
#include "ipv6addressvalidator.h"
#include "macaddressvalidator.h"
#include "ulonglongvalidator.h"
#include <QLineEdit>
class FieldEdit: public QLineEdit
{
Q_OBJECT
public:
enum FieldType {
kUInt64,
kMacAddress,
kIp4Address,
kIp6Address
};
FieldEdit(QWidget *parent = 0);
void setType(FieldType type);
void setRange(quint64 min, quint64 max);
private:
FieldType type_{kUInt64};
IPv4AddressValidator ip4Validator_;
IPv6AddressValidator ip6Validator_;
MacAddressValidator macValidator_;
ULongLongValidator uint64Validator_;
};
#endif

View File

@ -72,7 +72,8 @@ void FindReplaceDialog::on_protocol_currentIndexChanged(const QString &name)
FieldAttrib fieldAttrib;
fieldAttrib.index = i; // fieldIndex
fieldAttrib.bitSize = bitSize;
fieldAttrib.max = (1 << bitSize) - 1; // min is always 0
// FIXME: do we need max, since we already have bitSize?
fieldAttrib.max = quint64(~0) >> (64-bitSize); // min is always 0
// field and fieldAttrib_ have same count and order of fields
fieldAttrib_.append(fieldAttrib);
@ -84,6 +85,23 @@ void FindReplaceDialog::on_protocol_currentIndexChanged(const QString &name)
delete protocol;
}
void FindReplaceDialog::on_field_currentIndexChanged(int index)
{
if (index < 0)
return;
FieldAttrib fieldAttrib = fieldAttrib_.at(index);
qDebug("XXXXXX %s bitSize %d max %llx", qPrintable(field->currentText()),
fieldAttrib.bitSize, fieldAttrib.max);
findValue->setType(FieldEdit::kUInt64);
findValue->setRange(0, fieldAttrib.max);
replaceValue->setType(FieldEdit::kUInt64);
replaceValue->setRange(0, fieldAttrib.max);
}
void FindReplaceDialog::on_buttonBox_accepted()
{
FieldAttrib fieldAttrib = fieldAttrib_.at(field->currentIndex());

View File

@ -32,6 +32,7 @@ public:
private slots:
void on_protocol_currentIndexChanged(const QString &name);
void on_field_currentIndexChanged(int index);
void on_buttonBox_accepted();
private:

View File

@ -68,7 +68,7 @@
</widget>
</item>
<item row="2" column="1">
<widget class="QLineEdit" name="findValue"/>
<widget class="FieldEdit" name="findValue"/>
</item>
<item row="1" column="0">
<widget class="QLabel" name="fieldLabel">
@ -107,7 +107,7 @@
</widget>
</item>
<item row="0" column="1">
<widget class="QLineEdit" name="replaceValue"/>
<widget class="FieldEdit" name="replaceValue"/>
</item>
<item row="1" column="0">
<widget class="QCheckBox" name="useReplaceMask">
@ -154,6 +154,13 @@
</item>
</layout>
</widget>
<customwidgets>
<customwidget>
<class>FieldEdit</class>
<extends>QLineEdit</extends>
<header>fieldedit.h</header>
</customwidget>
</customwidgets>
<tabstops>
<tabstop>protocol</tabstop>
<tabstop>field</tabstop>

View File

@ -41,6 +41,7 @@ HEADERS += \
devicemodel.h \
deviceswidget.h \
dumpview.h \
fieldedit.h \
hexlineedit.h \
logsmodel.h \
logswindow.h \
@ -101,6 +102,7 @@ SOURCES += \
hexlineedit.cpp \
logsmodel.cpp \
logswindow.cpp \
fieldedit.cpp \
findreplace.cpp \
main.cpp \
mainwindow.cpp \

View File

@ -0,0 +1,74 @@
/*
Copyright (C) 2021 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _IPV4_ADDRESS_VALIDATOR_H
#define _IPV4_ADDRESS_VALIDATOR_H
#include <QHostAddress>
#include <QValidator>
class IPv4AddressValidator : public QValidator
{
public:
IPv4AddressValidator(QObject *parent = 0)
: QValidator(parent)
{
_ip4ValidChars.setPattern("[0-9]{1,3}(.[0-9]{1,3}){0,4}");
}
~IPv4AddressValidator() {}
virtual QValidator::State validate(QString &input, int& /*pos*/) const
{
QValidator::State state;
QHostAddress addr(input);
qDebug("%s: %s", __FUNCTION__, qPrintable(input));
if (addr.protocol() == QAbstractSocket::IPv4Protocol)
state = Acceptable;
else
if (_ip4ValidChars.exactMatch(input))
state = Intermediate;
else
state = Invalid;
qDebug("%s(%d): %s, ", __FUNCTION__, state, qPrintable(input));
return state;
}
virtual void fixup(QString &input) const
{
input.append(".0.0.0.0");
QHostAddress addr(input);
int len = input.size();
qDebug("%s: %s", __FUNCTION__, qPrintable(input));
while (addr.protocol() != QAbstractSocket::IPv4Protocol)
{
len--;
Q_ASSERT(len >= 0);
addr.setAddress(input.left(len));
}
input = addr.toString();
}
private:
QRegExp _ip4ValidChars;
};
#endif

View File

@ -0,0 +1,37 @@
/*
Copyright (C) 2021 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _MAC_ADDRESS_VALIDATOR_H
#define _MAC_ADDRESS_VALIDATOR_H
#include <QRegExpValidator>
class MacAddressValidator : public QRegExpValidator
{
public:
MacAddressValidator(QObject *parent = 0)
: QRegExpValidator(parent)
{
// Allow : or - as separator
setRegExp(QRegExp("([0-9,a-f,A-F]{2,2}[:-]){5,5}[0-9,a-f,A-F]{2,2}"));
}
~MacAddressValidator() {}
};
#endif

View File

@ -0,0 +1,80 @@
/*
Copyright (C) 2021 Srivats P.
This file is part of "Ostinato"
This is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <http://www.gnu.org/licenses/>
*/
#ifndef _ULONGLONG_VALIDATOR_H
#define _ULONGLONG_VALIDATOR_H
#include <QValidator>
class ULongLongValidator : public QValidator
{
public:
ULongLongValidator(QObject *parent = 0)
: QValidator(parent)
{
}
~ULongLongValidator() {}
void setRange(qulonglong min, qulonglong max)
{
min_ = min;
max_ = max;
}
virtual QValidator::State validate(QString &input, int& /*pos*/) const
{
if (input.isEmpty())
return Intermediate;
if (input.compare("0x", Qt::CaseInsensitive) == 0)
return Intermediate;
bool isOk;
qulonglong v = input.toULongLong(&isOk, 0);
//qDebug("input: %s, ok: %d, %llu", qPrintable(input), isOk, v);
if (!isOk)
return Invalid;
if (v > max_)
return Invalid;
if (v < min_)
return Intermediate;
return Acceptable;
}
virtual void fixup(QString &input) const
{
int dummyPos = 0;
State state = validate(input, dummyPos);
if (state == Acceptable)
return;
input.setNum(min_);
}
private:
qulonglong min_{0};
qulonglong max_{~0ULL};
};
#endif