1 #include "bytearrayprototype.h"
2 #include <QtScript/QScriptEngine>
6 Q_DECLARE_METATYPE(QByteArray*)
13 ByteArrayPrototype::~ByteArrayPrototype()
17 QByteArray *ByteArrayPrototype::thisByteArray()
const
19 return qscriptvalue_cast<QByteArray*>(thisObject().data());
24 thisByteArray()->chop(n);
29 return *thisByteArray() == other;
34 return thisByteArray()->left(len);
40 return thisByteArray()->mid(pos, len);
45 thisByteArray()->remove(pos, len);
52 return thisByteArray()->right(len);
57 thisByteArray()->truncate(pos);
62 return QString::fromLatin1(*thisByteArray());
68 return thisObject().data();
73 T readLE(
const char* p,
int offset)
75 return *(
reinterpret_cast<const T*
>(p + offset));
78 T readBE(
const char* p,
int offset)
84 const char* q = p + offset +
sizeof(T) - 1;
86 while (q>=p) *d++ = *q--;
90 void writeLE(
const T& v,
char* p,
int offset)
92 memcpy(p+offset,&v,
sizeof(T));
95 void writeBE(
const T& v,
char* p,
int offset)
102 char* q = p + offset +
sizeof(T) - 1;
104 while (q>=p) *q-- = *d++;
107 bool ByteArrayPrototype::checkRange(
int offset,
int sz)
const
109 int len = thisByteArray()->length();
110 if (offset<0 || len<sz || offset>len-sz)
112 context()->throwError(QScriptContext::RangeError,tr(
"Index out of range"));
117 double ByteArrayPrototype::readDoubleBE(
int offset)
const
119 if (!checkRange(offset,
sizeof(
double)))
return 0;
120 return readBE<double>(thisByteArray()->constData(),offset);
122 double ByteArrayPrototype::readDoubleLE(
int offset)
const
124 if (!checkRange(offset,
sizeof(
double)))
return 0;
125 return readLE<double>(thisByteArray()->constData(),offset);
127 float ByteArrayPrototype::readFloatBE(
int offset)
const
129 if (!checkRange(offset,
sizeof(
float)))
return 0;
130 return readBE<float>(thisByteArray()->constData(),offset);
132 float ByteArrayPrototype::readFloatLE(
int offset)
const
134 if (!checkRange(offset,
sizeof(
float)))
return 0;
135 return readLE<float>(thisByteArray()->constData(),offset);
138 void ByteArrayPrototype::writeDoubleBE(
double v,
int offset)
140 if (!checkRange(offset,
sizeof(
double)))
return;
141 writeBE(v,thisByteArray()->data(),offset);
143 void ByteArrayPrototype::writeDoubleLE(
double v,
int offset)
145 if (!checkRange(offset,
sizeof(
double)))
return;
146 writeLE(v,thisByteArray()->data(),offset);
148 void ByteArrayPrototype::writeFloatBE(
float v,
int offset)
150 if (!checkRange(offset,
sizeof(
float)))
return;
151 writeBE(v,thisByteArray()->data(),offset);
153 void ByteArrayPrototype::writeFloatLE(
float v,
int offset)
155 if (!checkRange(offset,
sizeof(
float)))
return;
156 writeLE(v,thisByteArray()->data(),offset);
159 int ByteArrayPrototype::readInt32LE(
int offset)
const
161 if (!checkRange(offset,
sizeof(int32_t)))
return 0;
162 return readLE<int32_t>(thisByteArray()->constData(),offset);
164 int ByteArrayPrototype::readInt32BE(
int offset)
const
166 if (!checkRange(offset,
sizeof(int32_t)))
return 0;
167 return readBE<int32_t>(thisByteArray()->constData(),offset);
169 void ByteArrayPrototype::writeInt32LE(
int v,
int offset)
171 if (!checkRange(offset,
sizeof(int32_t)))
return;
172 writeLE((int32_t)v,thisByteArray()->data(),offset);
174 void ByteArrayPrototype::writeInt32BE(
int v,
int offset)
176 if (!checkRange(offset,
sizeof(int32_t)))
return;
177 writeBE((int32_t)v,thisByteArray()->data(),offset);
180 uint ByteArrayPrototype::readUInt32LE(
int offset)
const
182 if (!checkRange(offset,
sizeof(uint32_t)))
return 0;
183 return readLE<uint32_t>(thisByteArray()->constData(),offset);
185 uint ByteArrayPrototype::readUInt32BE(
int offset)
const
187 if (!checkRange(offset,
sizeof(uint32_t)))
return 0;
188 return readBE<uint32_t>(thisByteArray()->constData(),offset);
190 void ByteArrayPrototype::writeUInt32LE(uint v,
int offset)
192 if (!checkRange(offset,
sizeof(uint32_t)))
return;
193 writeLE((uint32_t)v,thisByteArray()->data(),offset);
195 void ByteArrayPrototype::writeUInt32BE(uint v,
int offset)
197 if (!checkRange(offset,
sizeof(uint32_t)))
return;
198 writeBE((uint32_t)v,thisByteArray()->data(),offset);
201 int ByteArrayPrototype::readInt16LE(
int offset)
const
203 if (!checkRange(offset,
sizeof(int16_t)))
return 0;
204 return readLE<int16_t>(thisByteArray()->constData(),offset);
206 int ByteArrayPrototype::readInt16BE(
int offset)
const
208 if (!checkRange(offset,
sizeof(int16_t)))
return 0;
209 return readBE<int16_t>(thisByteArray()->constData(),offset);
211 void ByteArrayPrototype::writeInt16LE(
int v,
int offset)
213 if (!checkRange(offset,
sizeof(int16_t)))
return;
214 writeLE((int16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
216 void ByteArrayPrototype::writeInt16BE(
int v,
int offset)
218 if (!checkRange(offset,
sizeof(int16_t)))
return;
219 writeBE((int16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
222 uint ByteArrayPrototype::readUInt16LE(
int offset)
const
224 if (!checkRange(offset,
sizeof(uint16_t)))
return 0;
225 return readLE<uint16_t>(thisByteArray()->constData(),offset);
227 uint ByteArrayPrototype::readUInt16BE(
int offset)
const
229 if (!checkRange(offset,
sizeof(uint16_t)))
return 0;
230 return readBE<uint16_t>(thisByteArray()->constData(),offset);
232 void ByteArrayPrototype::writeUInt16LE(uint v,
int offset)
234 if (!checkRange(offset,
sizeof(uint16_t)))
return;
235 writeLE((uint16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
237 void ByteArrayPrototype::writeUInt16BE(uint v,
int offset)
239 if (!checkRange(offset,
sizeof(uint16_t)))
return;
240 writeBE((uint16_t)(v & 0xFFFF),thisByteArray()->data(),offset);
243 int ByteArrayPrototype::readInt8(
int offset)
const
245 if (!checkRange(offset,
sizeof(int8_t)))
return 0;
246 return thisByteArray()->constData()[offset];
248 void ByteArrayPrototype::writeInt8(
int v,
int offset)
250 if (!checkRange(offset,
sizeof(int8_t)))
return;
251 thisByteArray()->data()[offset] = (char)(v & 0xFF);
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.