forked from soutade/libgourou
Rework ByteArray::resize() to keep buffer data
This commit is contained in:
parent
9f62cf3447
commit
f33891ef1c
|
@ -156,10 +156,12 @@ namespace gourou
|
||||||
unsigned int size() const {return length();}
|
unsigned int size() const {return length();}
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* @brief Create a new internal buffer of length bytes
|
* @brief Increase or decrease internal buffer
|
||||||
* All previous data is lost
|
* @param length New length of internal buffer
|
||||||
|
* @param keepData If true copy old data on new buffer, if false,
|
||||||
|
* create a new buffer with random data
|
||||||
*/
|
*/
|
||||||
void resize(unsigned int length);
|
void resize(unsigned int length, bool keepData=true);
|
||||||
|
|
||||||
ByteArray& operator=(const ByteArray& other);
|
ByteArray& operator=(const ByteArray& other);
|
||||||
|
|
||||||
|
|
|
@ -172,33 +172,44 @@ namespace gourou
|
||||||
|
|
||||||
void ByteArray::append(const unsigned char* data, unsigned int length)
|
void ByteArray::append(const unsigned char* data, unsigned int length)
|
||||||
{
|
{
|
||||||
unsigned char* oldData = _data;
|
if (!length)
|
||||||
unsigned char* newData;
|
return;
|
||||||
|
|
||||||
if (_useMalloc)
|
unsigned int oldLength = _length;
|
||||||
newData = (unsigned char*)malloc(_length+length);
|
|
||||||
else
|
|
||||||
newData = new unsigned char[_length+length];
|
|
||||||
|
|
||||||
memcpy(newData, oldData, _length);
|
resize(_length+length, true);
|
||||||
|
|
||||||
delRef();
|
memcpy(&_data[oldLength], data, length);
|
||||||
|
|
||||||
memcpy(&newData[_length], data, length);
|
|
||||||
_length += length;
|
|
||||||
|
|
||||||
_data = newData;
|
|
||||||
|
|
||||||
addRef();
|
|
||||||
}
|
}
|
||||||
|
|
||||||
void ByteArray::append(unsigned char c) { append(&c, 1);}
|
void ByteArray::append(unsigned char c) { append(&c, 1);}
|
||||||
void ByteArray::append(const char* str) { append((const unsigned char*)str, strlen(str));}
|
void ByteArray::append(const char* str) { append((const unsigned char*)str, strlen(str));}
|
||||||
void ByteArray::append(const std::string& str) { append((const unsigned char*)str.c_str(), str.length()); }
|
void ByteArray::append(const std::string& str) { append((const unsigned char*)str.c_str(), str.length()); }
|
||||||
|
|
||||||
void ByteArray::resize(unsigned length)
|
void ByteArray::resize(unsigned length, bool keepData)
|
||||||
{
|
{
|
||||||
delRef();
|
if (length == _length)
|
||||||
initData(0, length);
|
return;
|
||||||
|
else if (length < _length)
|
||||||
|
_length = length ; // Don't touch data
|
||||||
|
else // New size >
|
||||||
|
{
|
||||||
|
unsigned char* newData;
|
||||||
|
|
||||||
|
if (_useMalloc)
|
||||||
|
newData = (unsigned char*)malloc(_length+length);
|
||||||
|
else
|
||||||
|
newData = new unsigned char[_length+length];
|
||||||
|
|
||||||
|
if (keepData)
|
||||||
|
memcpy(newData, _data, _length);
|
||||||
|
|
||||||
|
delRef();
|
||||||
|
|
||||||
|
_length = length;
|
||||||
|
_data = newData;
|
||||||
|
|
||||||
|
addRef();
|
||||||
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in New Issue
Block a user