• 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
  • controllers
  • view
  • structures
  • datatypes
  • strings
utf32stringdata.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of the Okteta Kasten Framework, made within the KDE community.
3  *
4  * Copyright 2011 Alex Richardson <alex.richardson@gmx.de>
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 #include "utf32stringdata.h"
23 
24 #include <QVarLengthArray>
25 
26 #include <KLocale>
27 #include <KDebug> //TODO remove
28 
29 #include <abstractbytearraymodel.h>
30 
31 #include "../topleveldatainformation.h"
32 #include "../dummydatainformation.h"
33 #include "stringdatainformation.h"
34 
35 Utf32StringData::Utf32StringData(StringDataInformation* parent)
36  : StringData(parent), mNonBMPCount(0)
37 {
38 }
39 
40 Utf32StringData::~Utf32StringData()
41 {
42 }
43 
44 QString Utf32StringData::charType() const
45 {
46  return mLittleEndian ? i18n("UTF32-LE char") : i18n("UTF32-BE char");
47 }
48 
49 QString Utf32StringData::typeName() const
50 {
51  return mLittleEndian ? i18n("UTF32-LE string") : i18n("UTF32-BE string");
52 }
53 
54 uint Utf32StringData::count() const
55 {
56  return mCodePoints.size();
57 }
58 
59 QString Utf32StringData::stringValue(int row) const
60 {
61  //TODO details
62  Q_ASSERT((uint)row < count());
63  //TODO show invalid values
64  uint val = mCodePoints.at(row);
65  QString number = QString::number(val, 16).toUpper();
66  if (number.length() == 1)
67  number = QLatin1String("0") + number;
68  if (val > UNICODE_MAX)
69  return i18n("Value too big: 0x%1", number);
70  else if (val > BMP_MAX) {
71  QString ret(2, Qt::Uninitialized);
72  ret[0] = QChar::highSurrogate(val);
73  ret[1] = QChar::lowSurrogate(val);
74  return i18n("%1 (U+%2)", ret, number);
75  }
76  else
77  return i18n("%1 (U+%2)", QString(QChar(mCodePoints.at(row))), number);
78 }
79 
80 QString Utf32StringData::completeString(bool skipInvalid) const
81 {
82  QVarLengthArray<QChar> data(mCodePoints.size() + mNonBMPCount);
83  int codePointCount = mCodePoints.size();
84  int i = 0;
85  for (int idx = 0; idx < codePointCount; ++idx) {
86  uint val = mCodePoints.at(idx);
87  if (val > UNICODE_MAX)
88  {
89  if (skipInvalid)
90  continue;
91  else
92  data[i] = QChar::ReplacementCharacter;
93  }
94  else if (val > BMP_MAX) {
95  data[i] = QChar::highSurrogate(val);
96  i++;
97  data[i] = QChar::lowSurrogate(val);
98  }
99  else
100  {
101  data[i] = QChar((ushort)val);
102  }
103  i++;
104  }
105  return QString(data.constData(), i);
106 }
107 
108 qint64 Utf32StringData::read(Okteta::AbstractByteArrayModel* input, Okteta::Address address,
109  BitCount64 bitsRemaining)
110 {
111  const int oldSize = count();
112  mNonBMPCount = 0;
113  if (mMode == CharCount)
114  {
115  mCodePoints.reserve(mLength.maxChars);
116  }
117  else if (mMode == ByteCount)
118  {
119  mCodePoints.reserve(mLength.maxBytes / 4);
120  }
121 
122  mParent->topLevelDataInformation()->_childCountAboutToChange(mParent, oldSize, 0);
123  mParent->topLevelDataInformation()->_childCountChanged(mParent, oldSize, 0);
124 
125  const uint oldMax = mCodePoints.size();
126  quint64 remaining = bitsRemaining;
127  Okteta::Address addr = address;
128  uint count = 0;
129  mEofReached = false;
130  if (((mMode & CharCount) && mLength.maxChars == 0)
131  || ((mMode & ByteCount) && mLength.maxBytes < 2))
132  return 0;
133 
134  bool eofAtStart = false;
135  if (remaining < 32)
136  eofAtStart = true;
137 
138  while (true)
139  {
140  if (remaining < 32)
141  {
142  mEofReached = true;
143  break;
144  }
145  uint codePoint;
146  bool terminate = false;
147 
148  if (mLittleEndian)
149  codePoint = input->byte(addr) | (input->byte(addr + 1) << 8)
150  | (input->byte(addr + 2) << 16) | (input->byte(addr + 3) << 24);
151  else
152  codePoint = (input->byte(addr) << 24) | (input->byte(addr + 1) << 16)
153  | (input->byte(addr + 2) << 8) | input->byte(addr + 3);
154 
155 
156  if (count < oldMax)
157  mCodePoints[count] = codePoint;
158  else
159  mCodePoints.append(codePoint);
160 
161  remaining -= 32;
162  addr += 4;
163  count++;
164 
165  //now check if we have to terminate
166  if (mMode & Sequence)
167  {
168  if (codePoint == mTerminationCodePoint)
169  terminate = true;
170  }
171  if (mMode & ByteCount)
172  {
173  // divide by two in case someone set length to an odd number of bytes
174  if ((addr - address) / 4 >= Okteta::Address(mLength.maxBytes / 4))
175  terminate = true;
176  }
177  if (mMode & CharCount)
178  {
179  if (count >= mLength.maxChars)
180  terminate = true;
181  }
182  if (mMode == None) {
183  kDebug() << "no termination mode set!!";
184  Q_ASSERT(false);
185  }
186  if (terminate)
187  break;
188  }
189  mCodePoints.resize(count);
190  mParent->topLevelDataInformation()->_childCountAboutToChange(mParent, 0, count);
191  mParent->topLevelDataInformation()->_childCountChanged(mParent, 0, count);
192 
193  if (eofAtStart)
194  return -1;
195  return (addr - address) * 8;
196 }
197 
198 BitCount32 Utf32StringData::size() const
199 {
200  return mCodePoints.size() * 32;
201 }
202 
203 BitCount32 Utf32StringData::sizeAt(uint i) const
204 {
205  Q_ASSERT(i <= count());
206  Q_UNUSED(i)
207  return 32;
208 }
Okteta::Address
qint32 Address
Definition: address.h:34
Okteta::AbstractByteArrayModel
could it be useful to hide the data access behind an iterator? * class KDataBufferIterator { public: ...
Definition: abstractbytearraymodel.h:79
abstractbytearraymodel.h
StringData
Definition: stringdata.h:36
Utf32StringData::sizeAt
virtual BitCount32 sizeAt(uint i) const
Definition: utf32stringdata.cpp:203
DataInformation::topLevelDataInformation
TopLevelDataInformation * topLevelDataInformation() const
Definition: datainformation.cpp:240
StringData::mLittleEndian
bool mLittleEndian
Definition: stringdata.h:89
TopLevelDataInformation::_childCountAboutToChange
void _childCountAboutToChange(DataInformation *sender, uint oldCount, uint newCount)
Definition: topleveldatainformation.h:184
BitCount64
quint64 BitCount64
Definition: datainformationbase.h:42
TopLevelDataInformation::_childCountChanged
void _childCountChanged(DataInformation *sender, uint oldCount, uint newCount)
Definition: topleveldatainformation.h:192
StringData::ByteCount
Definition: stringdata.h:45
StringData::BMP_MAX
static const uint BMP_MAX
Definition: stringdata.h:79
stringdatainformation.h
BitCount32
quint32 BitCount32
Definition: datainformationbase.h:37
Utf32StringData::typeName
virtual QString typeName() const
Definition: utf32stringdata.cpp:49
StringData::mTerminationCodePoint
quint32 mTerminationCodePoint
Definition: stringdata.h:87
Utf32StringData::size
virtual BitCount32 size() const
Definition: utf32stringdata.cpp:198
StringData::Sequence
Definition: stringdata.h:43
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...
Utf32StringData::~Utf32StringData
virtual ~Utf32StringData()
Definition: utf32stringdata.cpp:40
StringData::mLength
union StringData::@5 mLength
Utf32StringData::read
virtual qint64 read(Okteta::AbstractByteArrayModel *input, Okteta::Address address, BitCount64 bitsRemaining)
Definition: utf32stringdata.cpp:108
Utf32StringData::stringValue
virtual QString stringValue(int row) const
Definition: utf32stringdata.cpp:59
StringData::mMode
uint mMode
Definition: stringdata.h:88
StringData::mParent
StringDataInformation * mParent
Definition: stringdata.h:82
utf32stringdata.h
Utf32StringData::charType
virtual QString charType() const
Definition: utf32stringdata.cpp:44
Utf32StringData::completeString
virtual QString completeString(bool skipInvalid=false) const
Definition: utf32stringdata.cpp:80
StringData::None
Definition: stringdata.h:42
Utf32StringData::count
virtual uint count() const
Definition: utf32stringdata.cpp:54
StringData::mEofReached
bool mEofReached
Definition: stringdata.h:90
Utf32StringData::Utf32StringData
Utf32StringData(StringDataInformation *parent)
Definition: utf32stringdata.cpp:35
StringData::CharCount
Definition: stringdata.h:44
StringDataInformation
Definition: stringdatainformation.h:39
StringData::UNICODE_MAX
static const uint UNICODE_MAX
Definition: stringdata.h:78
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:09 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