Last active
August 29, 2015 13:57
-
-
Save buzzySmile/9756058 to your computer and use it in GitHub Desktop.
QByteArray Chunk Cutter
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <QByteArray> | |
/// Extension of class QByteArray: | |
/// allows cut off a chunk from begining of byte array | |
/// chunk is equivalent quint8, quint16 or quint32 by length | |
class qBA_cutter: public QByteArray | |
{ | |
private: | |
union { | |
quint16 sshort; | |
quint8 sbyte[2]; | |
} SHORT_16_BYTE; | |
union { | |
quint32 slong; | |
quint16 sshort[2]; | |
quint8 sbyte[4]; | |
} LONG_32_BYTE; | |
public: | |
qBA_cutter(); | |
qBA_cutter(const char *_array, int _size = -1); | |
~qBA_cutter(); | |
void assign(const char *_array, int _size) { this->clear(); this->append(_array, _size); } | |
void skipBytes(int n) { this->remove(0, n); } | |
quint8 cutoffByte() { | |
if(!this->isEmpty()) { | |
quint8 iRet = this->at(0); | |
this->remove(0, 1); | |
return iRet; | |
} | |
else | |
return 0; | |
} | |
quint16 cutoffShort() { | |
if(!this->isEmpty() && this->size() >= 2) { | |
SHORT_16_BYTE.sbyte[0] = this->at(0); | |
SHORT_16_BYTE.sbyte[1] = this->at(1); | |
this->remove(0, 2); | |
} | |
else SHORT_16_BYTE.sshort = 0; | |
return SHORT_16_BYTE.sshort; | |
} | |
quint32 cutoffLong() { | |
if(!this->isEmpty() && this->size() >= 4) { | |
LONG_32_BYTE.sbyte[0] = this->at(0); | |
LONG_32_BYTE.sbyte[1] = this->at(1); | |
LONG_32_BYTE.sbyte[2] = this->at(2); | |
LONG_32_BYTE.sbyte[3] = this->at(3); | |
this->remove(0, 4); | |
} | |
else LONG_32_BYTE.slong = 0; | |
return LONG_32_BYTE.slong; | |
} | |
}; |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment