• 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
  • io
  • streamencoder
  • sourcecode
bytearraysourcecodestreamencoder.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 2007-2008 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 "bytearraysourcecodestreamencoder.h"
24 
25 // Okteta core
26 #include <abstractbytearraymodel.h>
27 // KDE
28 #include <KLocale>
29 // Qt
30 #include <QtCore/QTextStream>
31 
32 
33 namespace Kasten2
34 {
35 
36 static const char* const PrimitiveDataTypeName[] =
37 {
38  "char",
39  "unsigned char",
40  "short",
41  "unsigned short",
42  "int",
43  "unsigned int",
44  "float",
45  "double"
46 };
47 
48 static const int SizeOfPrimitiveDataType[] =
49 {
50  sizeof(char),
51  sizeof(unsigned char),
52  sizeof(short),
53  sizeof(unsigned short),
54  sizeof(int),
55  sizeof(unsigned int),
56  sizeof(float),
57  sizeof(double)
58 };
59 
60 static const int NoOfPrimitiveDataTypes = 8;
61 
62 
63 SourceCodeStreamEncoderSettings::SourceCodeStreamEncoderSettings()
64  : variableName( QString::fromLatin1("array") ), dataType( UnsignedCharType ), elementsPerLine( 4 ), unsignedAsHexadecimal( true )
65 {}
66 
67 
68 ByteArraySourceCodeStreamEncoder::ByteArraySourceCodeStreamEncoder()
69  : AbstractByteArrayStreamEncoder( i18nc("name of the encoding target","C Array"), QString::fromLatin1("text/x-csrc") )
70 {}
71 
72 const char* const* ByteArraySourceCodeStreamEncoder::dataTypeNames() const { return PrimitiveDataTypeName; }
73 int ByteArraySourceCodeStreamEncoder::dataTypesCount() const { return NoOfPrimitiveDataTypes; }
74 
75 bool ByteArraySourceCodeStreamEncoder::encodeDataToStream( QIODevice* device,
76  const ByteArrayView* byteArrayView,
77  const Okteta::AbstractByteArrayModel* byteArrayModel,
78  const Okteta::AddressRange& range )
79 {
80 Q_UNUSED( byteArrayView )
81 
82  bool success = true;
83 
84  // encode
85  QTextStream textStream( device );
86 
87 // textStream << "// from File , selection \n";
88 
89  const int size = range.width();
90  const int dataTypeSize = SizeOfPrimitiveDataType[mSettings.dataType];
91  const int sizeOfArray = (size+dataTypeSize-1) / dataTypeSize;
92 
93  textStream << "const " << QLatin1String(PrimitiveDataTypeName[mSettings.dataType]) << ' '
94  << mSettings.variableName << '[' << sizeOfArray << "] =" << endl
95  << '{' << endl;
96 
97  int elementAddedOnLine = 0;
98  for( Okteta::Address i=range.start(); i<=range.end(); i+=dataTypeSize )
99  {
100  if( elementAddedOnLine == 0 )
101  textStream << " "; // just 3, one space before every datum
102  textStream << ' ' << printFormatted( byteArrayModel, i, range.end()-i+1 );
103  if( i + dataTypeSize <= range.end() )
104  textStream << ',';
105 
106  if( ++elementAddedOnLine >= mSettings.elementsPerLine )
107  {
108  textStream << endl;
109  elementAddedOnLine = 0;
110  }
111  }
112  if( elementAddedOnLine > 0 )
113  textStream << endl;
114 
115  textStream << "};" << endl;
116 
117  return success;
118 }
119 
120 
121 QString ByteArraySourceCodeStreamEncoder::printFormatted( const Okteta::AbstractByteArrayModel* byteArrayModel, Okteta::Address offset,
122  unsigned int dataSize ) const
123 {
124  static const char DecimalFormattedNumberPlaceHolder[] = "%1";
125  static const char HexadecimalFormattedNumberPlaceHolder[] = "0x%1";
126 
127  QString result;
128  switch( mSettings.dataType )
129  {
130  case SourceCodeStreamEncoderSettings::CharType:
131  {
132  char e = 0;
133  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin<size_t>(sizeof(e),dataSize) );
134  static const int fieldWidth = 4;
135  result = QString::fromLatin1(DecimalFormattedNumberPlaceHolder).arg( (int)e, fieldWidth );
136  break;
137  }
138  case SourceCodeStreamEncoderSettings::UnsignedCharType:
139  {
140  unsigned char e = 0;
141  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin(uint(sizeof(e)),dataSize) );
142  const int base = mSettings.unsignedAsHexadecimal ? 16 : 10;
143  const int fieldWidth = mSettings.unsignedAsHexadecimal ? 2 : 3;
144  const char* FormattedNumberPlaceHolder = mSettings.unsignedAsHexadecimal ?
145  HexadecimalFormattedNumberPlaceHolder : DecimalFormattedNumberPlaceHolder;
146  const QChar stuffChar = QLatin1Char( mSettings.unsignedAsHexadecimal ? '0' : ' ' );
147  result = QString::fromLatin1(FormattedNumberPlaceHolder).arg( e, fieldWidth, base, stuffChar );
148  break;
149  }
150  case SourceCodeStreamEncoderSettings::ShortType:
151  {
152  short e = 0;
153  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin(uint(sizeof(e)),dataSize) );
154  static const int fieldWidth = 6;
155  result = QString::fromLatin1(DecimalFormattedNumberPlaceHolder).arg( e, fieldWidth );
156  break;
157  }
158  case SourceCodeStreamEncoderSettings::UnsignedShortType:
159  {
160  unsigned short e = 0;
161  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin(uint(sizeof(e)),dataSize) );
162  const int base = mSettings.unsignedAsHexadecimal ? 16 : 10;
163  const int fieldWidth = mSettings.unsignedAsHexadecimal ? 4 : 5;
164  const char* FormattedNumberPlaceHolder = mSettings.unsignedAsHexadecimal ?
165  HexadecimalFormattedNumberPlaceHolder : DecimalFormattedNumberPlaceHolder;
166  const QChar stuffChar = QLatin1Char( mSettings.unsignedAsHexadecimal ? '0' : ' ' );
167  result = QString::fromLatin1(FormattedNumberPlaceHolder).arg( e, fieldWidth, base, stuffChar );
168  break;
169  }
170  case SourceCodeStreamEncoderSettings::IntegerType:
171  {
172  int e = 0;
173  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin(uint(sizeof(e)),dataSize) );
174  static const int fieldWidth = 11;
175  result = QString::fromLatin1(DecimalFormattedNumberPlaceHolder).arg( e, fieldWidth );
176  break;
177  }
178  case SourceCodeStreamEncoderSettings::UnsignedIntegerType:
179  {
180  unsigned int e = 0;
181  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin(uint(sizeof(e)),dataSize) );
182  const int base = mSettings.unsignedAsHexadecimal ? 16 : 10;
183  const int fieldWidth = mSettings.unsignedAsHexadecimal ? 8 : 10;
184  const char* FormattedNumberPlaceHolder = mSettings.unsignedAsHexadecimal ?
185  HexadecimalFormattedNumberPlaceHolder : DecimalFormattedNumberPlaceHolder;
186  const QChar stuffChar = QLatin1Char( mSettings.unsignedAsHexadecimal ? '0' : ' ' );
187  result = QString::fromLatin1(FormattedNumberPlaceHolder).arg( e, fieldWidth, base, stuffChar );
188  break;
189  }
190  case SourceCodeStreamEncoderSettings::FloatType:
191  {
192  float e = 0;
193  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin(uint(sizeof(e)),dataSize) );
194  static const int fieldWidth = 13;
195  result = QString::fromLatin1(DecimalFormattedNumberPlaceHolder).arg( e, fieldWidth );
196  break;
197  }
198  case SourceCodeStreamEncoderSettings::DoubleType:
199  {
200  double e = 0;
201  byteArrayModel->copyTo( reinterpret_cast<Okteta::Byte*>(&e), offset, qMin(uint(sizeof(e)),dataSize) );
202  static const int fieldWidth = 13;
203  result = QString::fromLatin1(DecimalFormattedNumberPlaceHolder).arg( e, fieldWidth );
204  break;
205  }
206  default:
207  break;
208  }
209 
210  return result;
211 }
212 
213 ByteArraySourceCodeStreamEncoder::~ByteArraySourceCodeStreamEncoder() {}
214 
215 }
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
Kasten2::ByteArraySourceCodeStreamEncoder::mSettings
SourceCodeStreamEncoderSettings mSettings
Definition: bytearraysourcecodestreamencoder.h:89
abstractbytearraymodel.h
KDE::NumberRange< Address, Size >
Kasten2::SourceCodeStreamEncoderSettings::IntegerType
Definition: bytearraysourcecodestreamencoder.h:44
KDE::Range::start
T start() const
Definition: range.h:86
Kasten2::AbstractByteArrayStreamEncoder
Definition: abstractbytearraystreamencoder.h:45
Kasten2::SourceCodeStreamEncoderSettings::dataType
int dataType
Definition: bytearraysourcecodestreamencoder.h:55
Kasten2::SourceCodeStreamEncoderSettings::UnsignedCharType
Definition: bytearraysourcecodestreamencoder.h:41
KDE::NumberRange::width
S width() const
Definition: numberrange.h:141
Kasten2::ByteArraySourceCodeStreamEncoder::dataTypesCount
int dataTypesCount() const
Definition: bytearraysourcecodestreamencoder.cpp:73
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
Kasten2::ByteArraySourceCodeStreamEncoder::~ByteArraySourceCodeStreamEncoder
virtual ~ByteArraySourceCodeStreamEncoder()
Definition: bytearraysourcecodestreamencoder.cpp:213
Kasten2::SourceCodeStreamEncoderSettings::UnsignedShortType
Definition: bytearraysourcecodestreamencoder.h:43
Kasten2::SourceCodeStreamEncoderSettings::elementsPerLine
int elementsPerLine
Definition: bytearraysourcecodestreamencoder.h:56
Kasten2::SourceCodeStreamEncoderSettings::CharType
Definition: bytearraysourcecodestreamencoder.h:40
Kasten2::SourceCodeStreamEncoderSettings::FloatType
Definition: bytearraysourcecodestreamencoder.h:46
Kasten2::ByteArraySourceCodeStreamEncoder::printFormatted
QString printFormatted(const Okteta::AbstractByteArrayModel *byteArrayModel, Okteta::Address offset, unsigned int dataSize) const
Definition: bytearraysourcecodestreamencoder.cpp:121
KDE::Range::end
T end() const
Definition: range.h:88
Kasten2::SizeOfPrimitiveDataType
static const int SizeOfPrimitiveDataType[]
Definition: bytearraysourcecodestreamencoder.cpp:48
Kasten2::PrimitiveDataTypeName
static const char *const PrimitiveDataTypeName[]
Definition: bytearraysourcecodestreamencoder.cpp:36
Kasten2::SourceCodeStreamEncoderSettings::UnsignedIntegerType
Definition: bytearraysourcecodestreamencoder.h:45
bytearraysourcecodestreamencoder.h
Kasten2::SourceCodeStreamEncoderSettings::unsignedAsHexadecimal
bool unsignedAsHexadecimal
Definition: bytearraysourcecodestreamencoder.h:57
Kasten2::ByteArraySourceCodeStreamEncoder::encodeDataToStream
virtual bool encodeDataToStream(QIODevice *device, const ByteArrayView *byteArrayView, const Okteta::AbstractByteArrayModel *byteArrayModel, const Okteta::AddressRange &range)
Definition: bytearraysourcecodestreamencoder.cpp:75
Kasten2::SourceCodeStreamEncoderSettings::DoubleType
Definition: bytearraysourcecodestreamencoder.h:47
Kasten2::ByteArraySourceCodeStreamEncoder::ByteArraySourceCodeStreamEncoder
ByteArraySourceCodeStreamEncoder()
Definition: bytearraysourcecodestreamencoder.cpp:68
Kasten2::SourceCodeStreamEncoderSettings::ShortType
Definition: bytearraysourcecodestreamencoder.h:42
Kasten2::NoOfPrimitiveDataTypes
static const int NoOfPrimitiveDataTypes
Definition: bytearraysourcecodestreamencoder.cpp:60
Kasten2::SourceCodeStreamEncoderSettings::variableName
QString variableName
Definition: bytearraysourcecodestreamencoder.h:54
Kasten2::ByteArraySourceCodeStreamEncoder::dataTypeNames
const char *const * dataTypeNames() const
Definition: bytearraysourcecodestreamencoder.cpp:72
Kasten2::SourceCodeStreamEncoderSettings::SourceCodeStreamEncoderSettings
SourceCodeStreamEncoderSettings()
Definition: bytearraysourcecodestreamencoder.cpp:63
char
QIODevice
Kasten2::ByteArrayView
Definition: bytearrayview.h:51
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