61 buffer = (int8*)::malloc (memSize);
63 memset (buffer, initVal, memSize);
77 buffer = (int8*)::malloc (memSize);
91 buffer = (int8*)::malloc (memSize);
93 memcpy (buffer, b, memSize);
104, memSize (bufferR.memSize)
105, fillSize (bufferR.fillSize)
106, delta (bufferR.delta)
111 buffer = (int8*)::malloc (memSize);
113 memcpy (buffer, bufferR.buffer, memSize);
132 if (b2.memSize > 0 && buffer)
133 memcpy (buffer, b2.buffer, b2.memSize);
134 fillSize = b2.fillSize;
152 uint32 maxGet = memSize - fillSize;
156 memcpy (b, buffer + fillSize, size);
164 return put ((
const void*)&c,
sizeof (c));
170 if (
grow (fillSize + 1) ==
false)
173 buffer [fillSize++] = byte;
180 if (
grow (fillSize + 1) ==
false)
183 buffer [fillSize++] = c;
193 if (
grow (fillSize + s) ==
false)
196 memcpy (buffer + fillSize, toPut, s);
204 return put ((
const void*)
str.text () , (
str.length () + 1) * sizeof (tchar));
208bool Buffer::appendString8 (
const char8* s)
213 uint32 len = (uint32) strlen (s);
218bool Buffer::appendString16 (
const char16* s)
223 uint32 len = (uint32)
str.length () *
sizeof (char16);
228bool Buffer::prependString8 (
const char8* s)
233 uint32 len = (uint32) strlen (s);
244bool Buffer::prependString16 (
const char16* s)
250 uint32 len = (uint32)
str.length () *
sizeof (char16);
262bool Buffer::prependString8 (char8 c)
265 char* b = (
char*)buffer;
271bool Buffer::prependString16 (char16 c)
274 char16* b = (char16*)buffer;
282 if (from + bytes > memSize || bytes == 0)
285 if (to + bytes > memSize)
288 if (from + bytes > to && from < to)
290 Buffer tmp (buffer + from, bytes);
291 memcpy (buffer + to, tmp, bytes);
294 memcpy (buffer + to, buffer + from, bytes);
299bool Buffer::makeHexString (
String& result)
304 if (data ==
nullptr || bytes == 0)
307 char8* stringBuffer = (char8*)malloc ((bytes * 2) + 1);
314 unsigned char t1 = ((*data) >> 4) & 0x0F;
315 unsigned char t2 = (*data) & 0x0F;
325 stringBuffer [count++] = t1;
326 stringBuffer [count++] = t2;
330 stringBuffer [count] = 0;
332 result.
take ((
void*)stringBuffer,
false);
337bool Buffer::fromHexString (
const char8*
string)
340 if (
string ==
nullptr)
343 int32 len = strlen8 (
string);
344 if (len == 0 || ((len & 1) == 1) )
354 char c =
string [
count];
357 if (c >=
'0' && c <=
'9') d += c -
'0';
358 else if (c >=
'A' && c <=
'F') d += c -
'A' + 10;
359 else if (c >=
'a' && c <=
'f') d += c -
'a' + 10;
363 data [
count >> 1] =
static_cast<unsigned char> (d << 4);
378 memset (buffer, value, memSize);
395 if (fillSize < memSize)
404 if (newSize > memSize)
407 delta = defaultDelta;
408 uint32 s = ((newSize + delta - 1) / delta) * delta;
419 if (
grow (fillSize + amount))
421 if (position < fillSize)
422 memmove (buffer + amount + position, buffer + position, fillSize - position);
427 else if (amount < 0 && fillSize > 0)
429 uint32 toRemove = -amount;
431 if (toRemove < fillSize)
433 if (position < fillSize)
434 memmove (buffer + position, buffer + toRemove + position, fillSize - position - toRemove);
435 fillSize -= toRemove;
448 if ((uint32)amount < memSize)
450 memmove (buffer + amount, buffer, memSize - amount);
451 memset (buffer, initVal, amount);
454 memset (buffer, initVal, memSize);
458 uint32 toRemove = -amount;
459 if (toRemove < memSize)
461 memmove (buffer, buffer + toRemove, memSize - toRemove);
462 memset (buffer + memSize - toRemove, initVal, toRemove);
465 memset (buffer, initVal, memSize);
472 if (memSize != newSize)
478 int8* newBuffer = (int8*) ::realloc (buffer, newSize);
479 if (newBuffer ==
nullptr)
481 newBuffer = (int8*)::malloc (newSize);
484 uint32 tmp = newSize;
487 memcpy (newBuffer, buffer, tmp);
507 buffer = (int8*)::malloc (newSize);
509 if (newSize > 0 && !buffer)
513 if (fillSize > memSize)
517 return (newSize > 0) == (buffer !=
nullptr);
541 return swap (buffer, memSize, swapSize);
547 if (swapSize != kSwap16 && swapSize != kSwap32 && swapSize != kSwap64)
550 if (swapSize == kSwap16)
552 for (uint32 count = 0 ; count < bufferSize ; count += 2)
554 SWAP_16 ( * (((int16*)buffer) + count) );
557 else if (swapSize == kSwap32)
559 for (uint32 count = 0 ; count < bufferSize ; count += 4)
561 SWAP_32 ( * (((int32*)buffer) + count) );
564 else if (swapSize == kSwap64)
566 for (uint32 count = 0 ; count < bufferSize ; count += 8)
568 SWAP_64 ( * (((int64*)buffer) + count) );
579 memSize = from.memSize;
580 fillSize = from.fillSize;
581 buffer = from.buffer;
582 from.buffer =
nullptr;
623 int32 textLength =
getFillSize () /
sizeof (char16);
624 if (
str16 () [textLength - 1] != 0)
void take(Buffer &from)
takes another Buffer's memory, frees the current Buffer's memory
void shiftStart(int32 amount)
moves all memory by given amount, grows the Buffer if necessary
void flush()
sets fill size to zero
int8 * int8Ptr() const
conversion
bool put(uint8)
append value at end, grows Buffer if necessary
void operator=(const Buffer &buff)
Assignment operator - copies contents from a given Buffer and increases the size if necessary.
bool truncateToFillSize()
virtual bool toWideString(int32 sourceCodePage)
Converts a Buffer's content to UTF-16 from a given multi-byte code page, Buffer must contain char8 of...
bool setFillSize(uint32 c)
sets a new fill size, does not change any memory
void move(int32 amount, uint8 initVal=0)
shifts memory at start without growing the buffer, so data is lost and initialized with init val
void set(uint8 value)
fills complete Buffer with given value
void fillup(uint8 initVal=0)
set from fillSize to end
virtual bool toMultibyteString(int32 destCodePage)
Converts a Buffer's content from UTF-16 to a given multi-byte code page, Buffer must contain UTF-16 e...
bool copy(uint32 from, uint32 to, uint32 bytes)
copies a number of bytes from one position to another, the size may be adapted
tchar * str() const
conversion
char16 * str16() const
conversion
bool operator==(const Buffer &buff) const
Comparison operator - copies contents from a given Buffer and increases the size if necessary.
uint32 get(void *b, uint32 size)
copy to buffer from fillSize, and shift fillSize
uint32 getFillSize() const
uint8 * uint8Ptr() const
conversion
Buffer()
Default constructor, allocates no memory at all.
char8 * str8() const
conversion
virtual ~Buffer()
Destructor - deallocates the internal memory.
bool setSize(uint32 newSize)
Sets a new size for this Buffer, keeping as much content as possible.
bool grow(uint32 memSize)
Increases the Buffer to the next block, block size given by delta.
int8 * operator+(uint32 i)
int8 * pass()
pass the current Buffer's memory
void shiftAt(uint32 position, int32 amount)
moves memory starting at the given position
bool swap(int16 swapSize)
swap all bytes of this Buffer by the given swapSize
static int32 wideStringToMultiByte(char8 *dest, const char16 *source, int32 char8Count, uint32 destCodePage=kCP_Default)
If dest is zero, this returns the maximum number of bytes needed to convert source.
static int32 multiByteToWideString(char16 *dest, const char8 *source, int32 wcharCount, uint32 sourceCodePage=kCP_Default)
If dest is zero, this returns the maximum number of bytes needed to convert source.
void take(String &str)
Take ownership of the string of 'str'.
#define SWAP_32(l)
Byte-order Conversion Macros.