Warning, file /utilities/okteta/kasten/gui/liboktetawidgets/addressvalidator.cpp was not indexed or was modified since last indexation (in which case cross-reference links may be missing, inaccurate or erroneous).
0001 /* 0002 This file is part of the Okteta Kasten module, made within the KDE community. 0003 0004 SPDX-FileCopyrightText: 2009 Friedrich W. H. Kossebau <kossebau@kde.org> 0005 0006 SPDX-License-Identifier: LGPL-2.1-only OR LGPL-3.0-only OR LicenseRef-KDE-Accepted-LGPL 0007 */ 0008 0009 #include "addressvalidator.hpp" 0010 0011 // lib 0012 #include <oktetakastengui.hpp> 0013 // Okteta core 0014 #include <Okteta/ValueCodec> 0015 // KF 0016 #include <KLocalizedString> 0017 // Qt 0018 #include <QString> 0019 #include <QRegExp> 0020 #include <QJSEngine> 0021 #include <QJSValue> 0022 0023 namespace Okteta { 0024 0025 AddressValidator::AddressValidator(QObject* parent, Coding codecId) 0026 : QValidator(parent) 0027 , mCodecId(InvalidCoding) 0028 , mValueCodec(nullptr) 0029 { 0030 setCodec(codecId); 0031 } 0032 0033 AddressValidator::~AddressValidator() 0034 { 0035 delete mValueCodec; 0036 } 0037 0038 void AddressValidator::setCodec(Coding codecId) 0039 { 0040 if (codecId == mCodecId) { 0041 return; 0042 } 0043 0044 mCodecId = codecId; 0045 0046 delete mValueCodec; 0047 mValueCodec = ValueCodec::createCodec((Okteta::ValueCoding)mCodecId); 0048 } 0049 0050 const QRegExp AddressValidator::expressionRegex = 0051 QRegExp(QStringLiteral("[0-9a-fx\\+/\\s\\-\\*]+"), 0052 Qt::CaseInsensitive, QRegExp::RegExp2); // FIXME this is way too simple, only there to test 0053 0054 QValidator::State AddressValidator::validate(QString& string, int& pos) const 0055 { 0056 Q_UNUSED(pos) 0057 0058 State result = QValidator::Acceptable; 0059 if (mCodecId == ExpressionCoding) { 0060 string = string.trimmed(); 0061 if (!expressionRegex.exactMatch(string)) { 0062 result = QValidator::Invalid; 0063 } 0064 // only prefix has been typed: 0065 if (string == QLatin1String("+") 0066 || string == QLatin1String("-") 0067 || string.endsWith(QLatin1Char('x'))) { // 0x at end 0068 result = QValidator::Intermediate; 0069 } 0070 } else { 0071 const int stringLength = string.length(); 0072 for (int i = 0; i < stringLength; ++i) { 0073 const QChar c = string.at(i); 0074 if (!mValueCodec->isValidDigit(c.toLatin1()) && !c.isSpace()) { 0075 result = QValidator::Invalid; 0076 break; 0077 } 0078 } 0079 } 0080 if (string.isEmpty()) { 0081 result = QValidator::Intermediate; 0082 } 0083 return result; 0084 } 0085 0086 Address AddressValidator::toAddress(const QString& string, AddressType* addressType) const 0087 { 0088 Address address; 0089 0090 QString expression = string.trimmed(); 0091 0092 if (addressType) { 0093 const AddressType type = 0094 expression.startsWith(QLatin1Char('+')) ? RelativeForwards : 0095 expression.startsWith(QLatin1Char('-')) ? RelativeBackwards : 0096 /* else */ AbsoluteAddress; 0097 0098 if (type != AbsoluteAddress) { 0099 expression.remove(0, 1); 0100 } 0101 0102 *addressType = type; 0103 } 0104 0105 if (mCodecId == ExpressionCoding) { 0106 QJSEngine evaluator; 0107 QJSValue value = evaluator.evaluate(expression); 0108 address = value.toInt(); 0109 qCDebug(LOG_KASTEN_OKTETA_GUI) << "expression " << expression << " evaluated to: " << address; 0110 0111 if (value.isError()) { 0112 qCWarning(LOG_KASTEN_OKTETA_GUI) << "evaluation error: " << value.toString(); 0113 if (addressType) { 0114 *addressType = InvalidAddressType; 0115 } 0116 } 0117 } else { 0118 const bool isHexadecimal = (mCodecId == HexadecimalCoding); 0119 const int base = isHexadecimal ? 16 : 10; 0120 address = expression.toInt(nullptr, base); 0121 } 0122 0123 return address; 0124 } 0125 0126 QString AddressValidator::toString(Address address, AddressType addressType) const 0127 { 0128 // ExpressionCoding just uses base 10 so no need to adjust this code 0129 const bool isHexadecimal = (mCodecId == HexadecimalCoding); 0130 const int base = isHexadecimal ? 16 : 10; 0131 0132 QString string = QString::number(address, base); 0133 0134 if (addressType == RelativeForwards) { 0135 string.prepend(QLatin1Char('+')); 0136 } else if (addressType == RelativeBackwards) { 0137 string.prepend(QLatin1Char('-')); 0138 } 0139 0140 return string; 0141 } 0142 0143 } 0144 0145 #include "moc_addressvalidator.cpp"