• Skip to content
  • Skip to link menu
KDE API Reference
  • KDE API Reference
  • kdesdk API Reference
  • KDE Home
  • Contact Us
 

okteta

  • sources
  • kde-4.12
  • kdesdk
  • okteta
  • kasten
  • gui
  • liboktetawidgets
bytearrayvalidator.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Kasten module, made within the KDE community.
3 
4  Copyright 2006,2009,2011 Friedrich W. H. Kossebau <kossebau@kde.org>
5 
6  This library is free software; you can redistribute it and/or
7  modify it under the terms of the GNU Lesser General Public
8  License as published by the Free Software Foundation; either
9  version 2.1 of the License, or (at your option) version 3, or any
10  later version accepted by the membership of KDE e.V. (or its
11  successor approved by the membership of KDE e.V.), which shall
12  act as a proxy defined in Section 6 of version 3 of the license.
13 
14  This library is distributed in the hope that it will be useful,
15  but WITHOUT ANY WARRANTY; without even the implied warranty of
16  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17  Lesser General Public License for more details.
18 
19  You should have received a copy of the GNU Lesser General Public
20  License along with this library. If not, see <http://www.gnu.org/licenses/>.
21 */
22 
23 #include "bytearrayvalidator.h"
24 
25 // Okteta core
26 #include <valuecodec.h>
27 #include <character.h>
28 #include <charcodec.h>
29 
30 
31 namespace Okteta
32 {
33 
34 ByteArrayValidator::ByteArrayValidator( QObject* parent, Coding codecId, int charCodecId )
35  : QValidator( parent ),
36  mCodecId( InvalidCoding ),
37  mValueCodec( 0 ),
38  mCharCodec( CharCodec::createCodec(Okteta::LocalEncoding) ),
39  mMaxLength( 32767 ),
40  mMinLength( 0 )
41 {
42 Q_UNUSED(charCodecId)
43  setCodec( codecId );
44 }
45 
46 
47 void ByteArrayValidator::setCharCodec( const QString& charCodecName )
48 {
49  if( charCodecName == mCharCodec->name() )
50  return;
51 
52  delete mCharCodec;
53  mCharCodec = CharCodec::createCodec( charCodecName );
54 }
55 
56 void ByteArrayValidator::setCodec( Coding codecId )
57 {
58  if( codecId == mCodecId )
59  return;
60 
61  mCodecId = codecId;
62 
63  if( mCodecId != CharCoding
64  && mCodecId != Utf8Coding )
65  {
66  delete mValueCodec;
67  mValueCodec = ValueCodec::createCodec( (Okteta::ValueCoding)mCodecId );
68  }
69 }
70 
71 void ByteArrayValidator::setMaxLength( int maxLength )
72 {
73  mMaxLength = maxLength;
74  if( maxLength < mMinLength )
75  mMinLength = maxLength;
76 }
77 
78 void ByteArrayValidator::setMinLength( int minLength )
79 {
80  mMinLength = minLength;
81  if( minLength > mMaxLength )
82  mMaxLength = minLength;
83 }
84 
85 QValidator::State ByteArrayValidator::validate( QString& string, int& pos ) const
86 {
87  Q_UNUSED( pos )
88 
89  State result = QValidator::Acceptable;
90 
91  int stringLength = string.length();
92 
93  if( mCodecId == CharCoding )
94  {
95  if( stringLength > mMaxLength )
96  {
97  string.truncate( mMaxLength );
98  stringLength = mMaxLength;
99  }
100 
101  for( int i=0; i<stringLength; ++i )
102  {
103  const QChar c = string.at( i );
104  if( !mCharCodec->canEncode(c) )
105  {
106  result = QValidator::Invalid;
107  break;
108  }
109  }
110  }
111  else if( mCodecId != Utf8Coding )
112  {
113  const int encodingWidth = mValueCodec->encodingWidth();
114  int byteCount = 0;
115  for( int i = 0; i < stringLength; )
116  {
117  Okteta::Byte dummyByte;
118  const int usedCharCount = mValueCodec->decode( &dummyByte, string, i );
119 
120  // could not decode?
121  if( usedCharCount == 0 )
122  {
123  result = QValidator::Invalid;
124  break;
125  }
126  i += usedCharCount;
127  ++byteCount;
128 
129  if( byteCount >= mMaxLength )
130  {
131  string.truncate( i );
132  break;
133  }
134  }
135  if( byteCount < mMinLength )
136  {
137  const int paddingCount = (mMinLength-byteCount) * encodingWidth;
138  string += QString( paddingCount, QLatin1Char('0') );
139  }
140  }
141 
142  return result;
143 }
144 
145 
146 QByteArray ByteArrayValidator::toByteArray( const QString& string ) const
147 {
148  QByteArray result;
149 
150  const int stringLength = string.length();
151  if( mCodecId == CharCoding )
152  {
153  result.resize( stringLength );
154  for( int i=0; i<stringLength; ++i )
155  {
156  Byte byte;
157  const bool success = mCharCodec->encode( &byte, string[i] );
158  result[i] = success ? byte : '?'; // TODO: define unknown symbol
159  }
160  }
161  else if( mCodecId == Utf8Coding )
162  result = string.toUtf8();
163  else
164  {
165  int i = 0;
166  while( i < stringLength )
167  {
168  Byte byte;
169  const int readChars = mValueCodec->decode( &byte, string, i );
170  if( readChars > 0 )
171  {
172  i += readChars;
173  result.append( byte );
174  }
175  else
176  while( i < stringLength && !mValueCodec->isValidDigit(string[i].toLatin1()) )
177  ++i;
178  }
179  }
180 
181  return result;
182 }
183 
184 
185 QString ByteArrayValidator::toString( const QByteArray& byteArray ) const
186 {
187  QString result;
188 
189  const int byteArraySize = byteArray.size();
190  if( mCodecId == Utf8Coding )
191  result = QString::fromUtf8( byteArray.constData(), byteArraySize );
192  else if( mCodecId == CharCoding )
193  {
194  result.resize( byteArraySize );
195  for( int i=0; i<byteArraySize; ++i )
196  {
197  Character c = mCharCodec->decode( byteArray[i] );
198  result[i] = c.isUndefined() ? QChar::fromLatin1('?') : c; // TODO: define unknown symbol
199  }
200  }
201  else
202  {
203  const int encodingWidth = mValueCodec->encodingWidth();
204  result.resize( byteArraySize * encodingWidth );
205  int r = 0;
206  for( int i=0; i<byteArraySize; ++i,r+=encodingWidth )
207  mValueCodec->encode( result, r, byteArray[i] );
208  }
209  return result;
210 }
211 
212 
213 ByteArrayValidator::~ByteArrayValidator()
214 {
215  delete mValueCodec;
216  delete mCharCodec;
217 }
218 
219 }
character.h
Okteta::InvalidCoding
Definition: oktetacore.h:35
Okteta::ValueCodec::createCodec
static ValueCodec * createCodec(ValueCoding valueCoding)
Definition: valuecodec.cpp:36
Okteta::ByteArrayValidator::setCharCodec
void setCharCodec(const QString &charCodecName)
Sets the char codec to use. Does not change the current codec.
Definition: bytearrayvalidator.cpp:47
Okteta::ValueCodec::decode
unsigned int decode(Byte *byte, const QString &text, unsigned int pos=0) const
Tries to decode the digits in the text into a byte.
Definition: valuecodec.cpp:50
Okteta::ByteArrayValidator::setMaxLength
void setMaxLength(int maxLength)
Sets the maximal length of the edited bytearray to maxLength.
Definition: bytearrayvalidator.cpp:71
Okteta::ByteArrayValidator::toString
QString toString(const QByteArray &byteArray) const
Definition: bytearrayvalidator.cpp:185
Okteta::ByteArrayValidator::maxLength
int maxLength() const
Definition: bytearrayvalidator.h:101
Okteta::CharCoding
CharCoding
Definition: oktetacore.h:39
Okteta::ByteArrayValidator::minLength
int minLength() const
Definition: bytearrayvalidator.h:102
Okteta::Character::isUndefined
bool isUndefined() const
Definition: character.h:52
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::CharCodec::decode
virtual Character decode(Byte byte) const =0
QObject
Okteta::ByteArrayValidator::~ByteArrayValidator
virtual ~ByteArrayValidator()
Definition: bytearrayvalidator.cpp:213
QValidator
Okteta::CharCodec
Definition: charcodec.h:42
Okteta::ByteArrayValidator::ByteArrayValidator
ByteArrayValidator(QObject *parent=0, Coding codecId=CharCoding, int charCodecId=LocalEncoding)
Definition: bytearrayvalidator.cpp:34
Okteta::ByteArrayValidator::Utf8Coding
Definition: bytearrayvalidator.h:51
Okteta::CharCodec::createCodec
static CharCodec * createCodec(CharCoding charCoding)
Definition: charcodec.cpp:68
valuecodec.h
Okteta::ByteArrayValidator::setCodec
void setCodec(Coding codecId)
Sets one of the value codecs or the current char codec.
Definition: bytearrayvalidator.cpp:56
charcodec.h
Okteta::ByteArrayValidator::validate
virtual QValidator::State validate(QString &input, int &pos) const
Definition: bytearrayvalidator.cpp:85
Okteta::CharCodec::canEncode
virtual bool canEncode(const QChar &_char) const =0
bytearrayvalidator.h
Okteta::CharCodec::encode
virtual bool encode(Byte *byte, const QChar &_char) const =0
Okteta::ValueCoding
ValueCoding
Definition: oktetacore.h:34
Okteta::CharCodec::name
virtual const QString & name() const =0
Okteta::LocalEncoding
the coding of your shell
Definition: oktetacore.h:42
Okteta::ByteArrayValidator::setMinLength
void setMinLength(int minLength)
Sets the maximal length of the edited bytearray to minLength.
Definition: bytearrayvalidator.cpp:78
Okteta::ByteArrayValidator::Coding
Coding
Definition: bytearrayvalidator.h:44
Okteta::ValueCodec::encodingWidth
virtual unsigned int encodingWidth() const =0
Okteta::Character
Definition: character.h:35
Okteta::ByteArrayValidator::toByteArray
QByteArray toByteArray(const QString &string) const
Definition: bytearrayvalidator.cpp:146
Okteta::ValueCodec::encode
virtual void encode(QString &digits, unsigned int pos, Byte byte) const =0
Encodes the byte using full coding width, prefixing with 0s if needed, and writes the result to digit...
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:07 by doxygen 1.8.7 written by Dimitri van Heesch, © 1997-2006

KDE's Doxygen guidelines are available online.

okteta

Skip menu "okteta"
  • Main Page
  • Namespace List
  • Namespace Members
  • Alphabetical List
  • Class List
  • Class Hierarchy
  • Class Members
  • File List
  • File Members
  • Related Pages

kdesdk API Reference

Skip menu "kdesdk API Reference"
  • kapptemplate
  • kcachegrind
  • kompare
  • lokalize
  • okteta
  • umbrello
  •   umbrello

Search



Report problems with this website to our bug tracking system.
Contact the specific authors with questions and comments about the page contents.

KDE® and the K Desktop Environment® logo are registered trademarks of KDE e.V. | Legal