QDaq  0.2.6
Qt-based Data Aqcuisition
 All Classes Functions Variables Typedefs Enumerations Enumerator Properties Groups Pages
bytearrayprototype.cpp
1 #include "bytearrayprototype.h"
2 #include <QtScript/QScriptEngine>
3 
4 #include "QDaqTypes.h"
5 
6 Q_DECLARE_METATYPE(QByteArray*)
7 
9  : QObject(parent)
10 {
11 }
12 
13 ByteArrayPrototype::~ByteArrayPrototype()
14 {
15 }
16 
17 QByteArray *ByteArrayPrototype::thisByteArray() const
18 {
19  return qscriptvalue_cast<QByteArray*>(thisObject().data());
20 }
21 
23 {
24  thisByteArray()->chop(n);
25 }
26 
27 bool ByteArrayPrototype::equals(const QByteArray &other)
28 {
29  return *thisByteArray() == other;
30 }
31 
32 QByteArray ByteArrayPrototype::left(int len) const
33 {
34  return thisByteArray()->left(len);
35 }
36 
37 
38 QByteArray ByteArrayPrototype::mid(int pos, int len) const
39 {
40  return thisByteArray()->mid(pos, len);
41 }
42 
43 QScriptValue ByteArrayPrototype::remove(int pos, int len)
44 {
45  thisByteArray()->remove(pos, len);
46  return thisObject();
47 }
48 
49 
50 QByteArray ByteArrayPrototype::right(int len) const
51 {
52  return thisByteArray()->right(len);
53 }
54 
56 {
57  thisByteArray()->truncate(pos);
58 }
59 
61 {
62  return QString::fromLatin1(*thisByteArray());
63 }
64 
65 
66 QScriptValue ByteArrayPrototype::valueOf() const
67 {
68  return thisObject().data();
69 }
70 
71 // Type write/read
72 template<class T>
73 T readLE(const char* p, int offset)
74 {
75  return *(reinterpret_cast<const T*>(p + offset));
76 }
77 template<class T>
78 T readBE(const char* p, int offset)
79 {
80  union {
81  T v;
82  char c[sizeof(T)];
83  } V;
84  const char* q = p + offset + sizeof(T) - 1;
85  char* d = V.c;
86  while (q>=p) *d++ = *q--;
87  return V.v;
88 }
89 template<class T>
90 void writeLE(const T& v, char* p, int offset)
91 {
92  memcpy(p+offset,&v,sizeof(T));
93 }
94 template<class T>
95 void writeBE(const T& v, char* p, int offset)
96 {
97  union {
98  T v;
99  char c[sizeof(T)];
100  } V;
101  V.v = v;
102  char* q = p + offset + sizeof(T) - 1;
103  char* d = V.c;
104  while (q>=p) *q-- = *d++;
105 }
106 
107 bool ByteArrayPrototype::checkRange(int offset, int sz) const
108 {
109  int len = thisByteArray()->length();
110  if (offset<0 || len<sz || offset>len-sz)
111  {
112  context()->throwError(QScriptContext::RangeError,tr("Index out of range"));
113  return false;
114  } else return true;
115 }
116 
117 double ByteArrayPrototype::readDoubleBE(int offset) const
118 {
119  if (!checkRange(offset,sizeof(double))) return 0;
120  return readBE<double>(thisByteArray()->constData(),offset);
121 }
122 double ByteArrayPrototype::readDoubleLE(int offset) const
123 {
124  if (!checkRange(offset,sizeof(double))) return 0;
125  return readLE<double>(thisByteArray()->constData(),offset);
126 }
127 float ByteArrayPrototype::readFloatBE(int offset) const
128 {
129  if (!checkRange(offset,sizeof(float))) return 0;
130  return readBE<float>(thisByteArray()->constData(),offset);
131 }
132 float ByteArrayPrototype::readFloatLE(int offset) const
133 {
134  if (!checkRange(offset,sizeof(float))) return 0;
135  return readLE<float>(thisByteArray()->constData(),offset);
136 }
137 
138 void ByteArrayPrototype::writeDoubleBE(double v, int offset)
139 {
140  if (!checkRange(offset,sizeof(double))) return;
141  writeBE(v,thisByteArray()->data(),offset);
142 }
143 void ByteArrayPrototype::writeDoubleLE(double v, int offset)
144 {
145  if (!checkRange(offset,sizeof(double))) return;
146  writeLE(v,thisByteArray()->data(),offset);
147 }
148 void ByteArrayPrototype::writeFloatBE(float v, int offset)
149 {
150  if (!checkRange(offset,sizeof(float))) return;
151  writeBE(v,thisByteArray()->data(),offset);
152 }
153 void ByteArrayPrototype::writeFloatLE(float v, int offset)
154 {
155  if (!checkRange(offset,sizeof(float))) return;
156  writeLE(v,thisByteArray()->data(),offset);
157 }
158 // Int 32
159 int ByteArrayPrototype::readInt32LE(int offset) const
160 {
161  if (!checkRange(offset,sizeof(int32_t))) return 0;
162  return readLE<int32_t>(thisByteArray()->constData(),offset);
163 }
164 int ByteArrayPrototype::readInt32BE(int offset) const
165 {
166  if (!checkRange(offset,sizeof(int32_t))) return 0;
167  return readBE<int32_t>(thisByteArray()->constData(),offset);
168 }
169 void ByteArrayPrototype::writeInt32LE(int v, int offset)
170 {
171  if (!checkRange(offset,sizeof(int32_t))) return;
172  writeLE((int32_t)v,thisByteArray()->data(),offset);
173 }
174 void ByteArrayPrototype::writeInt32BE(int v, int offset)
175 {
176  if (!checkRange(offset,sizeof(int32_t))) return;
177  writeBE((int32_t)v,thisByteArray()->data(),offset);
178 }
179 // UInt32
180 uint ByteArrayPrototype::readUInt32LE(int offset) const
181 {
182  if (!checkRange(offset,sizeof(uint32_t))) return 0;
183  return readLE<uint32_t>(thisByteArray()->constData(),offset);
184 }
185 uint ByteArrayPrototype::readUInt32BE(int offset) const
186 {
187  if (!checkRange(offset,sizeof(uint32_t))) return 0;
188  return readBE<uint32_t>(thisByteArray()->constData(),offset);
189 }
190 void ByteArrayPrototype::writeUInt32LE(uint v, int offset)
191 {
192  if (!checkRange(offset,sizeof(uint32_t))) return;
193  writeLE((uint32_t)v,thisByteArray()->data(),offset);
194 }
195 void ByteArrayPrototype::writeUInt32BE(uint v, int offset)
196 {
197  if (!checkRange(offset,sizeof(uint32_t))) return;
198  writeBE((uint32_t)v,thisByteArray()->data(),offset);
199 }
200 // Int 16
201 int ByteArrayPrototype::readInt16LE(int offset) const
202 {
203  if (!checkRange(offset,sizeof(int16_t))) return 0;
204  return readLE<int16_t>(thisByteArray()->constData(),offset);
205 }
206 int ByteArrayPrototype::readInt16BE(int offset) const
207 {
208  if (!checkRange(offset,sizeof(int16_t))) return 0;
209  return readBE<int16_t>(thisByteArray()->constData(),offset);
210 }
211 void ByteArrayPrototype::writeInt16LE(int v, int offset)
212 {
213  if (!checkRange(offset,sizeof(int16_t))) return;
214  writeLE((int16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
215 }
216 void ByteArrayPrototype::writeInt16BE(int v, int offset)
217 {
218  if (!checkRange(offset,sizeof(int16_t))) return;
219  writeBE((int16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
220 }
221 // UInt16
222 uint ByteArrayPrototype::readUInt16LE(int offset) const
223 {
224  if (!checkRange(offset,sizeof(uint16_t))) return 0;
225  return readLE<uint16_t>(thisByteArray()->constData(),offset);
226 }
227 uint ByteArrayPrototype::readUInt16BE(int offset) const
228 {
229  if (!checkRange(offset,sizeof(uint16_t))) return 0;
230  return readBE<uint16_t>(thisByteArray()->constData(),offset);
231 }
232 void ByteArrayPrototype::writeUInt16LE(uint v, int offset)
233 {
234  if (!checkRange(offset,sizeof(uint16_t))) return;
235  writeLE((uint16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
236 }
237 void ByteArrayPrototype::writeUInt16BE(uint v, int offset)
238 {
239  if (!checkRange(offset,sizeof(uint16_t))) return;
240  writeBE((uint16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
241 }
242 // Int 8
243 int ByteArrayPrototype::readInt8(int offset) const
244 {
245  if (!checkRange(offset,sizeof(int8_t))) return 0;
246  return thisByteArray()->constData()[offset];
247 }
248 void ByteArrayPrototype::writeInt8(int v, int offset)
249 {
250  if (!checkRange(offset,sizeof(int8_t))) return;
251  thisByteArray()->data()[offset] = (char)(v & 0xFF);
252 }
QByteArray left(int len) const
Returns a ByteArray with the len leftmost bytes.
The prototype for the ByteArray class.
void truncate(int pos)
Truncates the array at pos.
QScriptValue valueOf() const
Return the data stored by the object.
QScriptValue remove(int pos, int len)
Removes len bytes starting at pos.
QString toLatin1String() const
Convert to a String.
QByteArray right(int len) const
Returns the rightmost len bytes.
bool equals(const QByteArray &other)
Returns true if this ByteArray is equal to other.
QByteArray mid(int pos, int len=-1) const
Returns the middle part of the array from pos with length len.
void chop(int n)
Removes n bytes from the end of the byte array.