52
56
mValueCodec = ValueCodec::createCodec( (Okteta::ValueCoding)mCodecId );
59
const QRegExp AddressValidator::expressionRegex = QRegExp("[0-9a-fx\\+/\\s\\-\\*]+",
60
Qt::CaseInsensitive, QRegExp::RegExp2); //FIXME this is way too simple, only there to test
55
62
QValidator::State AddressValidator::validate( QString& string, int& pos ) const
59
66
State result = QValidator::Acceptable;
61
const int stringLength = string.length();
62
for( int i=0; i<stringLength; ++i )
67
if( mCodecId == ExpressionCoding )
64
const QChar c = string.at( i );
65
if( !mValueCodec->isValidDigit(c.toLatin1()) && !c.isSpace() )
69
string = string.trimmed();
70
if( ! expressionRegex.exactMatch(string) )
67
71
result = QValidator::Invalid;
72
//only prefix has been typed:
73
if( string == QLatin1String("+")
74
|| string == QLatin1String("-")
75
|| string.endsWith('x') ) // 0x at end
76
result = QValidator::Intermediate;
80
const int stringLength = string.length();
81
for( int i=0; i<stringLength; ++i )
83
const QChar c = string.at( i );
84
if( !mValueCodec->isValidDigit( c.toLatin1() ) && !c.isSpace() )
86
result = QValidator::Invalid;
91
if( string.isEmpty() )
92
result = QValidator::Intermediate;
76
Address AddressValidator::toAddress( const QString& string ) const
96
Address AddressValidator::toAddress( const QString& string, AddressType* addressType) const
78
const int isHexadecimal = ( mCodecId == HexadecimalCoding );
79
const int base = isHexadecimal ? 16 : 10;
80
const Okteta::Address address = string.toInt( 0, base );
100
QString expression = string.trimmed();
104
const AddressType type =
105
expression.startsWith('+') ? RelativeForwards :
106
expression.startsWith('-') ? RelativeBackwards :
107
/* else */ AbsoluteAddress;
109
if( type != AbsoluteAddress )
110
expression.remove( 0, 1 );
115
if( mCodecId == ExpressionCoding )
117
QScriptEngine evaluator;
118
QScriptValue value = evaluator.evaluate( expression );
119
address = value.toInt32();
120
kDebug() << "expression " << expression << " evaluated to: " << address;
122
if( evaluator.hasUncaughtException() )
124
kWarning() << "evaluation error: "
125
<< evaluator.uncaughtExceptionBacktrace();
127
*addressType = InvalidAddressType;
132
const bool isHexadecimal = ( mCodecId == HexadecimalCoding );
133
const int base = isHexadecimal ? 16 : 10;
134
address = expression.toInt( 0, base );
85
QString AddressValidator::toString( Address address ) const
141
QString AddressValidator::toString( Address address, AddressType addressType ) const
143
//ExpressionCoding just uses base 10 so no need to adjust this code
87
144
const int isHexadecimal = ( mCodecId == HexadecimalCoding );
88
145
const int base = isHexadecimal ? 16 : 10;
89
const QString string = QString::number( address, base );
147
QString string = QString::number( address, base );
149
if( addressType == RelativeForwards )
150
string.prepend( '+' );
151
else if( addressType == RelativeBackwards )
152
string.prepend( '-' );