getByte
setByte
size
clear
removeFront
append
appendByte
appendString
insert
insertByte
insertString
appendVariable
insertVariable
extractVariable
getAsString
crc32
compress
decompress
getErrMsg
The DyBytes class contains a double-ended queue of unsigned characters, and stores data to be sent or received from remote hosts. DyBytes has built-in compression and CRC functions, and can safely handle binary data.
Example:
DyBytes dtest;
dtest.appendString("Hello World");
cout << dtest << " CRC: " << dtest.crc32() << endl;
DyBytes member functions:
unsigned char getByte(long int index);
Retrieve the byte at the specified index.
void setByte(long int index, unsigned char newByte);
Changes the byte at the specified index to the new value.
Returns the number of bytes being stored in the DyBytes.
Clears all data within the object. The size is set to zero.
void removeFront(long int numRemovals);
Deletes numRemovals elements from the front of the DyBytes. If the size of the DyBytes was less than numRemovals, the object is completely cleared.
void append(DyBytes *source);
void append(DyBytes &source);
void appendByte(unsigned char source);
void appendString(const string &source);
bool append(DyBytes *source, long int index, long int length);
bool append(DyBytes &source, long int index, long int length);
Append source to the end of this DyBytes object. The last two will only copy length bytes starting at position index, and will return false if index is out of bounds.
void insert(DyBytes *source);
void insert(DyBytes &source);
void insertByte(unsigned char source);
void insertString(const string &source);
bool insert(DyBytes *source, long int index, long int length);
bool insert(DyBytes &source, long int index, long int length);
Insert source at the beginning of this DyBytes object. The last two will only copy length bytes starting at position index, and will return false if index is out of bounds.
template <class T>
unsigned int appendVariable(T theVariable);
Appends theVariable to the end of the DyBytes. The return value is the number of bytes that were added. (This will be equal to the size of theVariable). You can use sizeof() to determine variable lengths.
template <class T>
unsigned int insertVariable(T theVariable);
Similar to appendVariable, but it inserts theVariable at the beginning of the DyBytes.
template <class T>
bool extractVariable(long int index, T &theVariable);
Copies binary data from the DyBytes object, and uses it to assign a value to theVariable. It copies the first byte from position index, and continues until enough bytes to fill theVariable have been copied. So if theVariable is 4 bytes long, and index is 10, then the bytes from positions 10, 11, 12 and 13 will be used to fill theVariable. You can use the sizeof() function to determine variable sizes:
DyBytes theData;
int32_t myNumber = 12345;
int32_t numberTest = 0;
cout << "Number of bytes in myNumber: " << sizeof(myNumber) << endl;
theData.appendVariable(myNumber);
theData.extractVariable(0, numberTest);
cout << "Extracted variable: " << numberTest << endl;
Note that no check is being made for endianess here. extractVariable returns false if index is out of bounds.
Return the entire contents of the DyBytes object as a string. If the object contains any binary data, it won't be correctly retrieved. In this case, use getByte instead.
Returns a 32-bit CRC hash for the data in this object, using the 'fast' CRC algorithm. This is a good way to verify the data's integrity. Note: crc32 must be called after startup, or you'll get incorrect results.
bool compress(int compressionLevel);
Compresses the data currently stored in the DyBytes object. The original data is gone; the object now contains compressed binary data. The compression level should be between 0 (no compression) and 9 (maximum compression).
Returns false if compression errors occurred, and sets errMsg appropriately.
Decompresses the data within the object. If the DyBytes object contained a single valid, compressed file, it should now contain the uncompressed version. Returns false if errors occurred, and sets errMsg appropriately.
Return a description of the last compression or decompression error for this DyBytes.