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:
parent
b60aad45d1
commit
d3400f0897
57
client/fieldedit.cpp
Normal file
57
client/fieldedit.cpp
Normal 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
55
client/fieldedit.h
Normal 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
|
||||||
|
|
@ -72,7 +72,8 @@ void FindReplaceDialog::on_protocol_currentIndexChanged(const QString &name)
|
|||||||
FieldAttrib fieldAttrib;
|
FieldAttrib fieldAttrib;
|
||||||
fieldAttrib.index = i; // fieldIndex
|
fieldAttrib.index = i; // fieldIndex
|
||||||
fieldAttrib.bitSize = bitSize;
|
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
|
// field and fieldAttrib_ have same count and order of fields
|
||||||
fieldAttrib_.append(fieldAttrib);
|
fieldAttrib_.append(fieldAttrib);
|
||||||
@ -84,6 +85,23 @@ void FindReplaceDialog::on_protocol_currentIndexChanged(const QString &name)
|
|||||||
delete protocol;
|
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()
|
void FindReplaceDialog::on_buttonBox_accepted()
|
||||||
{
|
{
|
||||||
FieldAttrib fieldAttrib = fieldAttrib_.at(field->currentIndex());
|
FieldAttrib fieldAttrib = fieldAttrib_.at(field->currentIndex());
|
||||||
|
@ -32,6 +32,7 @@ public:
|
|||||||
|
|
||||||
private slots:
|
private slots:
|
||||||
void on_protocol_currentIndexChanged(const QString &name);
|
void on_protocol_currentIndexChanged(const QString &name);
|
||||||
|
void on_field_currentIndexChanged(int index);
|
||||||
void on_buttonBox_accepted();
|
void on_buttonBox_accepted();
|
||||||
|
|
||||||
private:
|
private:
|
||||||
|
@ -68,7 +68,7 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="2" column="1">
|
<item row="2" column="1">
|
||||||
<widget class="QLineEdit" name="findValue"/>
|
<widget class="FieldEdit" name="findValue"/>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="1" column="0">
|
||||||
<widget class="QLabel" name="fieldLabel">
|
<widget class="QLabel" name="fieldLabel">
|
||||||
@ -107,7 +107,7 @@
|
|||||||
</widget>
|
</widget>
|
||||||
</item>
|
</item>
|
||||||
<item row="0" column="1">
|
<item row="0" column="1">
|
||||||
<widget class="QLineEdit" name="replaceValue"/>
|
<widget class="FieldEdit" name="replaceValue"/>
|
||||||
</item>
|
</item>
|
||||||
<item row="1" column="0">
|
<item row="1" column="0">
|
||||||
<widget class="QCheckBox" name="useReplaceMask">
|
<widget class="QCheckBox" name="useReplaceMask">
|
||||||
@ -154,6 +154,13 @@
|
|||||||
</item>
|
</item>
|
||||||
</layout>
|
</layout>
|
||||||
</widget>
|
</widget>
|
||||||
|
<customwidgets>
|
||||||
|
<customwidget>
|
||||||
|
<class>FieldEdit</class>
|
||||||
|
<extends>QLineEdit</extends>
|
||||||
|
<header>fieldedit.h</header>
|
||||||
|
</customwidget>
|
||||||
|
</customwidgets>
|
||||||
<tabstops>
|
<tabstops>
|
||||||
<tabstop>protocol</tabstop>
|
<tabstop>protocol</tabstop>
|
||||||
<tabstop>field</tabstop>
|
<tabstop>field</tabstop>
|
||||||
|
@ -41,6 +41,7 @@ HEADERS += \
|
|||||||
devicemodel.h \
|
devicemodel.h \
|
||||||
deviceswidget.h \
|
deviceswidget.h \
|
||||||
dumpview.h \
|
dumpview.h \
|
||||||
|
fieldedit.h \
|
||||||
hexlineedit.h \
|
hexlineedit.h \
|
||||||
logsmodel.h \
|
logsmodel.h \
|
||||||
logswindow.h \
|
logswindow.h \
|
||||||
@ -101,6 +102,7 @@ SOURCES += \
|
|||||||
hexlineedit.cpp \
|
hexlineedit.cpp \
|
||||||
logsmodel.cpp \
|
logsmodel.cpp \
|
||||||
logswindow.cpp \
|
logswindow.cpp \
|
||||||
|
fieldedit.cpp \
|
||||||
findreplace.cpp \
|
findreplace.cpp \
|
||||||
main.cpp \
|
main.cpp \
|
||||||
mainwindow.cpp \
|
mainwindow.cpp \
|
||||||
|
74
common/ipv4addressvalidator.h
Normal file
74
common/ipv4addressvalidator.h
Normal 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
|
37
common/macaddressvalidator.h
Normal file
37
common/macaddressvalidator.h
Normal 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
|
80
common/ulonglongvalidator.h
Normal file
80
common/ulonglongvalidator.h
Normal 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
|
Loading…
Reference in New Issue
Block a user