From 2bda8f7717adf28da4af0d34fb82f63d2868c31d Mon Sep 17 00:00:00 2001 From: toma Date: Wed, 25 Nov 2009 17:56:58 +0000 Subject: Copy the KDE 3.5 branch to branches/trinity for new KDE 3.5 features. BUG:215923 git-svn-id: svn://anonsvn.kde.org/home/kde/branches/trinity/kdeutils@1054174 283d02a7-25f6-0310-bc7c-ecb5cbfe19da --- khexedit/hexvalidator.cc | 329 +++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 329 insertions(+) create mode 100644 khexedit/hexvalidator.cc (limited to 'khexedit/hexvalidator.cc') diff --git a/khexedit/hexvalidator.cc b/khexedit/hexvalidator.cc new file mode 100644 index 0000000..e8c1731 --- /dev/null +++ b/khexedit/hexvalidator.cc @@ -0,0 +1,329 @@ +/* + * khexedit - Versatile hex editor + * Copyright (C) 1999 Espen Sand, espensa@online.no + * + * This program 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 2 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, write to the Free Software + * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. + * + */ + +#include +#include +#include +#include "hexvalidator.h" + +CHexValidator::CHexValidator( QWidget *parent, EState state, + const char *name ) + :QValidator( parent, name ) +{ + setState( state ); +} + +CHexValidator::~CHexValidator( void ) +{ +} + +QValidator::State CHexValidator::validate( QString &string, int &/*pos*/ ) const +{ + if( mState == hexadecimal ) + { + for( uint i=0; i < string.length(); i++ ) + { + int val = string[i].latin1(); + if( isxdigit( val ) == 0 && isspace( val ) == 0 ) + { + return( QValidator::Invalid ); + } + } + return( QValidator::Valid ); + } + if( mState == decimal ) + { + for( uint i=0; i < string.length(); i++ ) + { + int val = string[i].latin1(); + if( isdigit( val ) == 0 && isspace( val ) == 0 ) + { + return( QValidator::Invalid ); + } + } + return( QValidator::Valid ); + } + else if( mState == octal ) + { + for( uint i=0; i < string.length(); i++ ) + { + int val = string[i].latin1(); + if( (isdigit( val ) == 0 || val == '8' || val == '9') && + isspace( val ) == 0 ) + { + return( QValidator::Invalid ); + } + } + return( QValidator::Valid ); + } + else if( mState == binary ) + { + for( uint i=0; i < string.length(); i++ ) + { + int val = string[i].latin1(); + if( val != '0' && val != '1' && isspace( val ) == 0 ) + { + return( QValidator::Invalid ); + } + } + return( QValidator::Valid ); + } + else if( mState == regularText ) + { + return( QValidator::Valid ); + } + else + { + return( QValidator::Invalid ); + } + +} + + +void CHexValidator::setState( EState state ) +{ + mState = state; +} + + +void CHexValidator::convert( QByteArray &dest, const QString &src ) +{ + uint value; + uint k=0; + + if( mState == hexadecimal ) + { + dest.resize(0); + + char buf[3]; + for( uint i=0; i < src.length(); i++ ) + { + int val = src[i].latin1(); + if( isxdigit(val) ) + { + buf[k++] = val; + if( k == 2 ) + { + buf[k] = 0; + sscanf( buf, "%X", &value ); + + dest.resize( dest.size()+1 ); + dest[ dest.size()-1 ] = value; + k = 0; + } + } + } + + if( k == 1 ) + { + buf[1] = buf[0]; + buf[0] = '0'; + buf[2] = 0; + sscanf( buf, "%X", &value ); + + dest.resize( dest.size()+1 ); + dest[ dest.size()-1 ] = value; + } + + } + else if( mState == decimal ) + { + dest.resize(0); + + char buf[4]; + for( uint i=0; i < src.length(); i++ ) + { + int val = src[i].latin1(); + if( isdigit(val) ) + { + buf[k++] = val; + if( k == 3 ) + { + buf[k] = 0; + sscanf( buf, "%u", &value ); + + dest.resize( dest.size()+1 ); + dest[ dest.size()-1 ] = value; + k = 0; + } + } + } + + if( k == 1 || k == 2 ) + { + if( k == 1 ) + { + buf[2] = buf[0]; + buf[0] = buf[1] = '0'; + } + else + { + buf[2] = buf[1]; + buf[1] = buf[0]; + buf[0] = '0'; + } + buf[3] = 0; + sscanf( buf, "%u", &value ); + + dest.resize( dest.size()+1 ); + dest[ dest.size()-1 ] = value; + } + } + + else if( mState == octal ) + { + dest.resize(0); + + char buf[4]; + for( uint i=0; i < src.length(); i++ ) + { + int val = src[i].latin1(); + if( isdigit(val) ) + { + buf[k++] = val; + if( k == 3 ) + { + if( buf[0] > '3' ) { buf[0] = '3'; } + buf[k] = 0; + sscanf( buf, "%o", &value ); + + dest.resize( dest.size()+1 ); + dest[ dest.size()-1 ] = value; + k = 0; + } + } + } + + if( k == 1 || k == 2 ) + { + if( k == 1 ) + { + buf[2] = buf[0]; + buf[0] = buf[1] = '0'; + } + else + { + buf[2] = buf[1]; + buf[1] = buf[0]; + buf[0] = '0'; + } + buf[3] = 0; + sscanf( buf, "%o", &value ); + + dest.resize( dest.size()+1 ); + dest[ dest.size()-1 ] = value; + } + } + else if( mState == binary ) + { + dest.resize(0); + + char buf[9]; + for( uint i=0; i < src.length(); i++ ) + { + int val = src[i].latin1(); + if( isdigit(val) ) + { + buf[k++] = val; + if( k == 8 ) + { + value = 0; + for( uint j=0; j < 8; j++ ) + { + value |= (buf[8-j-1] == '1') ? 1< 0 ) + { + value = 0; + for( uint j=0; j < k; j++ ) + { + value |= (buf[k-j-1] == '1') ? 1<= destLen ) + return src; + + QString zeroes; + zeroes.fill( '0', destLen - src.length() ); + return zeroes + src; +} +#include "hexvalidator.moc" -- cgit v1.2.1