• 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
  • core
abstractbytearraymodel.cpp
Go to the documentation of this file.
1 /*
2  This file is part of the Okteta Core library, made within the KDE community.
3 
4  Copyright 2003,2008-2009 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 "abstractbytearraymodel.h"
24 
25 // lib
26 #include "charcodec.h"
27 #include "character.h"
28 
29 
30 namespace Okteta
31 {
32 
33 static const int SearchedByteCountSignalLimit = 10000;
34 
35 
36 AbstractByteArrayModel::AbstractByteArrayModel( QObject* parent )
37  : QObject( parent )
38 {}
39 
40 bool AbstractByteArrayModel::isReadOnly() const { return false; }
41 
42 void AbstractByteArrayModel::setReadOnly( bool isReadOnly )
43 {
44  Q_UNUSED( isReadOnly )
45 }
46 
47 Size AbstractByteArrayModel::insert( Address offset, const Byte* insertData, int insertLength )
48 {
49  return replace( offset, 0, insertData, insertLength );
50 }
51 
52 
53 Size AbstractByteArrayModel::remove( const AddressRange& removeRange )
54 {
55  replace( removeRange, 0, 0 );
56  return removeRange.width(); // TODO: check if this is true
57 }
58 
59 
60 Size AbstractByteArrayModel::copyTo( Byte* dest, const AddressRange& _copyRange ) const
61 {
62  AddressRange copyRange( _copyRange );
63  copyRange.restrictEndTo( size()-1 );
64 
65  const Address copyRangeEnd = copyRange.end();
66  for( Address i=copyRange.start(); i<=copyRangeEnd; ++i )
67  *dest++ = byte( i );
68 
69  return copyRange.width();
70 }
71 
72 
73 Address AbstractByteArrayModel::indexOf( const Byte* pattern, int patternLength, Address fromOffset, Address toOffset ) const
74 {
75  Address result = -1;
76 
77  const Address lastOffset = size() - 1;
78  const Address lastFrom = qMin(lastOffset, toOffset) - patternLength + 1;
79  Size nextSignalByteCount = fromOffset + SearchedByteCountSignalLimit;
80 
81  for( Address i=fromOffset; i<=lastFrom ; ++i )
82  {
83  int c = 0;
84  for( ; c<patternLength; ++c )
85  if( pattern[c] != byte(i+c) )
86  break;
87 
88  if( nextSignalByteCount <= i )
89  {
90  nextSignalByteCount += SearchedByteCountSignalLimit;
91  emit searchedBytes( i-fromOffset+1 );
92  }
93 
94  if( c == patternLength )
95  {
96  result = i;
97  break;
98  }
99  }
100 
101  return result;
102 }
103 
104 Address AbstractByteArrayModel::lastIndexOf( const Byte* pattern, int patternLength, Address fromOffset, Address toOffset ) const
105 {
106  Address result = -1;
107 
108  const Address lastFrom = size() - patternLength;
109 
110  if( fromOffset < 0 )
111  fromOffset = lastFrom + 1 + fromOffset;
112  else if( fromOffset > lastFrom )
113  fromOffset = lastFrom;
114 
115  if( toOffset < 0 )
116  toOffset = 0;
117 
118  Size nextSignalByteCount = fromOffset - SearchedByteCountSignalLimit;
119 
120  for( Address i=fromOffset; i>=toOffset ; --i )
121  {
122  int c = 0;
123  for( ; c<patternLength; ++c )
124  if( pattern[c] != byte(i+c) )
125  break;
126 
127  if( nextSignalByteCount >= i )
128  {
129  nextSignalByteCount -= SearchedByteCountSignalLimit;
130  emit searchedBytes( i-fromOffset+1 );
131  }
132 
133  if( c == patternLength )
134  {
135  result = i;
136  break;
137  }
138  }
139 
140  return result;
141 }
142 
143 static QByteArray toLower( const QByteArray& _pattern, const CharCodec* charCodec )
144 {
145  QByteArray result( _pattern );
146 
147  const int patternLength = result.size();
148  char* pattern = result.data();
149  for( int i = 0; i<patternLength; ++i )
150  {
151  const Character decodedChar = charCodec->decode( pattern[i] );
152 
153  if( decodedChar.isUndefined() )
154  continue;
155 
156  charCodec->encode( reinterpret_cast<Byte*>(&pattern[i]), decodedChar.toLower() );
157  }
158 
159  return result;
160 }
161 
162 Address AbstractByteArrayModel::indexOfCaseInsensitive( const CharCodec* charCodec, const QByteArray& _pattern, Address fromOffset, Address toOffset ) const
163 {
164  Address result = -1;
165 
166  const QByteArray lowerPattern = toLower( _pattern, charCodec );
167  const Byte* const pattern = reinterpret_cast<const Byte*>( lowerPattern.constData() );
168  const int patternLength = lowerPattern.size();
169  const Address lastOffset = size() - 1;
170  const Address lastFrom = qMin(lastOffset, toOffset) - patternLength + 1;
171 
172  Address nextSignalByteCount = fromOffset + SearchedByteCountSignalLimit;
173 
174  for( Address i=fromOffset; i<=lastFrom ; ++i )
175  {
176  int c = 0;
177  for( ; c<patternLength; ++c )
178  {
179  Byte _byte = byte( i+c );
180 
181  // turn to lowercase if possible
182  // TODO: optimize, like caching and not reencoding chars without a lower letter
183  const Okteta::Character decodedChar = charCodec->decode( _byte );
184  if( ! decodedChar.isUndefined() )
185  charCodec->encode( &_byte, decodedChar.toLower() );
186 
187  if( _byte != pattern[c] )
188  break;
189  }
190 
191  if( nextSignalByteCount <= i )
192  {
193  nextSignalByteCount += SearchedByteCountSignalLimit;
194  searchedBytes( i-fromOffset+1 );
195  }
196 
197  if( c == patternLength )
198  {
199  result = i;
200  break;
201  }
202  }
203 
204  return result;
205 }
206 
207 Address AbstractByteArrayModel::lastIndexOfCaseInsensitive( const CharCodec* charCodec, const QByteArray& _pattern, Address fromOffset, Address toOffset ) const
208 {
209  Address result = -1;
210 
211  const QByteArray lowerPattern = toLower( _pattern, charCodec );
212  const Byte* const pattern = reinterpret_cast<const Byte*>( lowerPattern.constData() );
213  const int patternLength = lowerPattern.size();
214  const Address lastFrom = size() - patternLength;
215 
216  if( fromOffset < 0 )
217  fromOffset = lastFrom + 1 + fromOffset;
218  else if( fromOffset > lastFrom )
219  fromOffset = lastFrom;
220 
221  if( toOffset < 0 )
222  toOffset = 0;
223 
224  Address nextSignalByteCount = fromOffset - SearchedByteCountSignalLimit;
225 
226  for( Address i=fromOffset; i>=toOffset ; --i )
227  {
228  int c = 0;
229  for( ; c<patternLength; ++c )
230  {
231  Byte _byte = byte( i+c );
232 
233  // turn to lowercase if possible
234  // TODO: optimize, like caching and not reencoding chars without a lower letter
235  const Okteta::Character decodedChar = charCodec->decode( _byte );
236  if( ! decodedChar.isUndefined() )
237  charCodec->encode( &_byte, decodedChar.toLower() );
238 
239  if( _byte != pattern[c] )
240  break;
241  }
242 
243  if( nextSignalByteCount >= i )
244  {
245  nextSignalByteCount -= SearchedByteCountSignalLimit;
246  searchedBytes( i-fromOffset+1 );
247  }
248 
249  if( c == patternLength )
250  {
251  result = i;
252  break;
253  }
254  }
255 
256  return result;
257 }
258 
259 
260 AbstractByteArrayModel::~AbstractByteArrayModel() {}
261 
262 }
character.h
Okteta::AbstractByteArrayModel::replace
virtual Size replace(const AddressRange &removeRange, const Byte *insertData, int insertLength)=0
replaces as much as possible
Okteta::Address
qint32 Address
Definition: address.h:34
abstractbytearraymodel.h
Okteta::AbstractByteArrayModel::insert
virtual Size insert(Address offset, const Byte *insertData, int insertLength)
inserts bytes copied from the given source at Position.
Definition: abstractbytearraymodel.cpp:47
KDE::NumberRange< Address, Size >
Okteta::toLower
static QByteArray toLower(const QByteArray &_pattern, const CharCodec *charCodec)
Definition: abstractbytearraymodel.cpp:143
KDE::Range::start
T start() const
Definition: range.h:86
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
KDE::NumberRange::width
S width() const
Definition: numberrange.h:141
Okteta::AbstractByteArrayModel::copyTo
virtual Size copyTo(Byte *dest, const AddressRange &copyRange) const
copies the data of the section into a given array Dest.
Definition: abstractbytearraymodel.cpp:60
Okteta::AbstractByteArrayModel::lastIndexOfCaseInsensitive
Address lastIndexOfCaseInsensitive(const CharCodec *charCodec, const QByteArray &pattern, Address fromOffset=-1, Address toOffset=0) const
Definition: abstractbytearraymodel.cpp:207
Okteta::AbstractByteArrayModel::size
virtual Size size() const =0
Okteta::AbstractByteArrayModel::lastIndexOf
virtual Address lastIndexOf(const Byte *pattern, int patternLength, Address fromOffset=-1, Address toOffset=0) const
searches for a given data string The section limits the data within which the key has to be found If ...
Definition: abstractbytearraymodel.cpp:104
KDE::Range::restrictEndTo
void restrictEndTo(T Limit)
restricts the end to Limit.
Definition: range.h:69
Okteta::CharCodec
Definition: charcodec.h:42
Okteta::AbstractByteArrayModel::AbstractByteArrayModel
AbstractByteArrayModel(QObject *parent=0)
Definition: abstractbytearraymodel.cpp:36
KDE::Range::end
T end() const
Definition: range.h:88
Okteta::AbstractByteArrayModel::setReadOnly
virtual void setReadOnly(bool isReadOnly)
sets the readonly flag for the byte array if this is possible.
Definition: abstractbytearraymodel.cpp:42
Okteta::AbstractByteArrayModel::indexOfCaseInsensitive
Address indexOfCaseInsensitive(const CharCodec *charCodec, const QByteArray &pattern, Address fromOffset=0, Address toOffset=-1) const
Definition: abstractbytearraymodel.cpp:162
Okteta::AbstractByteArrayModel::byte
virtual Byte byte(Address offset) const =0
locates working range The idea behind is to tell buffer which range will be requested in the followin...
charcodec.h
Okteta::AbstractByteArrayModel::searchedBytes
void searchedBytes(Okteta::Size bytes) const
Okteta::AbstractByteArrayModel::~AbstractByteArrayModel
virtual ~AbstractByteArrayModel()
Definition: abstractbytearraymodel.cpp:260
Okteta::CharCodec::encode
virtual bool encode(Byte *byte, const QChar &_char) const =0
Okteta::AbstractByteArrayModel::isReadOnly
virtual bool isReadOnly() const
Default returns true.
Definition: abstractbytearraymodel.cpp:40
Okteta::Size
qint32 Size
Definition: size.h:33
Okteta::SearchedByteCountSignalLimit
static const int SearchedByteCountSignalLimit
Definition: abstractbytearraymodel.cpp:33
Okteta::AbstractByteArrayModel::indexOf
virtual Address indexOf(const Byte *pattern, int patternLength, Address fromOffset=0, Address toOffset=-1) const
searches beginning with byte at Pos.
Definition: abstractbytearraymodel.cpp:73
Okteta::Character
Definition: character.h:35
Okteta::AbstractByteArrayModel::remove
virtual Size remove(const AddressRange &removeRange)
removes beginning with position as much as possible
Definition: abstractbytearraymodel.cpp:53
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:06 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