• 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
  • poddecoder
poddata.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,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 "poddata.h"
24 
25 
26 namespace Okteta
27 {
28 
29 static inline void copyInvertedBytes( Byte* data, const Byte* sourceData, const int length )
30 {
31  const int last = length-1;
32  for( int i=0,j=last; i<length; ++i,--j )
33  data[i] = sourceData[j];
34 }
35 
36 
37 PODData::PODData()
38  : mCurrentOriginalData( 0 ),
39  mCurrentEndiannessSetData( 0 ),
40  mCurrentSize( 0 ),
41  mByteOrder( thisMachineByteOrder )
42 {
43 }
44 
45 const Byte* PODData::originalData() const { return mCurrentOriginalData; }
46 const Byte* PODData::byteOrderSetData() const { return mCurrentEndiannessSetData; }
47 ByteOrder PODData::byteOrder() const { return mByteOrder; }
48 int PODData::size() const { return mCurrentSize; }
49 
50 
51 Byte* PODData::rawData() { return mOriginalAligned64Bit.mBytes; }
52 
53 void PODData::setByteOrder( ByteOrder byteOrder )
54 {
55  if( mByteOrder == byteOrder )
56  return;
57 
58  mByteOrder = byteOrder;
59 
60  // swap data
61  if( mCurrentOriginalData )
62  copyInvertedBytes( mByteOrderSetAligned64Bit.mBytes, mOriginalAligned64Bit.mBytes, Size );
63  else
64  mByteOrderSetAligned64Bit = mOriginalAligned64Bit;
65 }
66 
67 unsigned long PODData::bitValue( int noOfBitsToRead ) const
68 {
69  static const int BitsPerByte = 8;
70  static const unsigned char BitMask[9] =
71  {
72  0, 1<<7, 3<<6, 7<<5, 15<<4, 31<<3, 63<<2, 127<<1, 255
73  };
74 
75  unsigned long result = 0;
76 
77  if( noOfBitsToRead < 1 )
78  noOfBitsToRead = 1;
79 
80  // TODO: the cursor currently does not go into the byte
81  int noOfUsedBits = 0;//7 - state.cell;
82 
83  const bool isReverse = ( mByteOrder != thisMachineByteOrder );
84  const Byte* data = mByteOrderSetAligned64Bit.mBytes;
85  if( isReverse )
86  data += 7;
87 
88  while( noOfBitsToRead > 0 )
89  {
90  Byte byte = ( *data << noOfUsedBits );
91 
92  const int noOfNextBits =
93  ( (BitsPerByte-noOfUsedBits) >= noOfBitsToRead ) ? noOfBitsToRead : (BitsPerByte-noOfUsedBits);
94 
95  byte &= BitMask[noOfNextBits];
96 
97  byte >>= BitsPerByte - noOfNextBits;
98 
99  result = (result<<noOfNextBits) | byte;
100 
101  noOfBitsToRead -= noOfNextBits;
102  noOfUsedBits += noOfNextBits;
103 
104  if( noOfUsedBits >= BitsPerByte )
105  {
106  noOfUsedBits = 0;
107  if( isReverse )
108  --data;
109  else
110  ++data;
111  }
112  }
113 
114  return result;
115 }
116 
117 
118 bool PODData::updateRawData( int size )
119 {
120  const Byte* oldCurrentOriginalData = mCurrentOriginalData;
121 
122  if( size > 0 )
123  {
124  if( mByteOrder != thisMachineByteOrder )
125  copyInvertedBytes( mByteOrderSetAligned64Bit.mBytes, mOriginalAligned64Bit.mBytes, Size );
126  else
127  mByteOrderSetAligned64Bit = mOriginalAligned64Bit;
128 
129  mCurrentOriginalData = mOriginalAligned64Bit.mBytes;
130  mCurrentEndiannessSetData = mByteOrderSetAligned64Bit.mBytes;
131  mCurrentSize = size;
132  }
133  else
134  {
135  mCurrentOriginalData = 0;
136  mCurrentEndiannessSetData = 0;
137  mCurrentSize = 0;
138  }
139 
140  return ( mCurrentOriginalData || oldCurrentOriginalData );
141 }
142 
143 void PODData::getPointers( const void** P8Bit, const void** P16Bit, const void** P32Bit, const void** P64Bit ) const
144 {
145  static const int MachineOffsets[4] = { 0, 0, 0, 0 };
146  static const int ReversedOffsets[4] = { 7, 6, 4, 0 };
147 
148  const int* offsets = ( mByteOrder == thisMachineByteOrder ) ? MachineOffsets : ReversedOffsets;
149  const Byte* data = mByteOrderSetAligned64Bit.mBytes;
150 
151  *P8Bit = (mCurrentSize>=1) ? data + offsets[0] : 0;
152  *P16Bit = (mCurrentSize>=2) ? data + offsets[1] : 0;
153  *P32Bit = (mCurrentSize>=4) ? data + offsets[2] : 0;
154  *P64Bit = (mCurrentSize>=8) ? data + offsets[3] : 0;
155 }
156 
157 const void* PODData::pointer( int byteCount ) const
158 {
159  if( byteCount > mCurrentSize )
160  byteCount = 0;
161 
162  const int offset = ( mByteOrder == thisMachineByteOrder ) ? 0 : 8-byteCount;
163  const Byte* data = mByteOrderSetAligned64Bit.mBytes;
164 
165  return (byteCount>0) ? data + offset : 0;
166 }
167 
168 }
Okteta::PODData::originalData
const Byte * originalData() const
Definition: poddata.cpp:45
Okteta::PODData::mByteOrder
ByteOrder mByteOrder
Definition: poddata.h:73
Okteta::PODData::mOriginalAligned64Bit
Aligned64Bit mOriginalAligned64Bit
Definition: poddata.h:69
Okteta::PODData::mCurrentOriginalData
Byte * mCurrentOriginalData
Definition: poddata.h:66
Okteta::Byte
unsigned char Byte
Definition: byte.h:29
Okteta::copyInvertedBytes
static void copyInvertedBytes(Byte *data, const Byte *sourceData, const int length)
Definition: poddata.cpp:29
Okteta::PODData::PODData
PODData()
Definition: poddata.cpp:37
Okteta::ByteOrder
ByteOrder
Definition: oktetacore.h:111
Okteta::PODData::setByteOrder
void setByteOrder(ByteOrder byteOrder)
Definition: poddata.cpp:53
Okteta::PODData::pointer
const void * pointer(int byteCount) const
Definition: poddata.cpp:157
Okteta::PODData::byteOrderSetData
const Byte * byteOrderSetData() const
Definition: poddata.cpp:46
Okteta::PODData::Aligned64Bit::mBytes
Byte mBytes[Size]
Definition: poddata.h:62
Okteta::PODData::size
int size() const
Definition: poddata.cpp:48
Okteta::PODData::mCurrentSize
int mCurrentSize
Definition: poddata.h:72
poddata.h
Okteta::PODData::bitValue
unsigned long bitValue(int noOfBitsToRead) const
Definition: poddata.cpp:67
Okteta::PODData::updateRawData
bool updateRawData(int size)
Definition: poddata.cpp:118
Okteta::PODData::mCurrentEndiannessSetData
Byte * mCurrentEndiannessSetData
Definition: poddata.h:67
Okteta::PODData::rawData
Byte * rawData()
Definition: poddata.cpp:51
Okteta::PODData::getPointers
void getPointers(const void **P8Bit, const void **P16Bit, const void **P32Bit, const void **P64Bit) const
Definition: poddata.cpp:143
Okteta::Size
qint32 Size
Definition: size.h:33
Okteta::PODData::byteOrder
ByteOrder byteOrder() const
Definition: poddata.cpp:47
Okteta::PODData::mByteOrderSetAligned64Bit
Aligned64Bit mByteOrderSetAligned64Bit
Definition: poddata.h:70
Okteta::thisMachineByteOrder
static const ByteOrder thisMachineByteOrder
Definition: oktetacore.h:116
This file is part of the KDE documentation.
Documentation copyright © 1996-2014 The KDE developers.
Generated on Tue Oct 14 2014 23:04:08 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