NXEngine v1.0.0.6
[NXEngine.git] / common / DBuffer.cpp
blobf10c6f714566df969a9eb57c983ca286beace074
2 #include <stdlib.h>
3 #include <stdint.h>
4 #include <string.h>
6 #include "DBuffer.h"
7 #include "DBuffer.fdh"
10 DBuffer::DBuffer()
12 fData = &fBuiltInData[0];
13 fAllocSize = DBUFFER_BUILTIN_SIZE;
14 fAllocdExternal = false;
15 fLength = 0;
18 DBuffer::~DBuffer()
20 if (fAllocdExternal)
21 free(fData);
25 void c------------------------------() {}
28 // append data to the end of the buffer
29 void DBuffer::AppendData(const uint8_t *data, int length)
31 if (length <= 0) return;
32 EnsureAlloc(fLength + length);
34 memcpy(&fData[fLength], data, length);
35 fLength += length;
38 // append a string, along with it's null-terminator.
39 void DBuffer::AppendString(const char *str)
41 AppendData((uint8_t *)str, strlen(str) + 1);
44 // append a string, without it's null-terminator.
45 void DBuffer::AppendStringNoNull(const char *str)
47 AppendData((uint8_t *)str, strlen(str));
51 void DBuffer::AppendBool(bool value)
53 uint8_t ch = (uint8_t)value;
54 AppendData((uchar *)&ch, 1);
57 void DBuffer::Append16(uint16_t value)
59 AppendData((uchar *)&value, 2);
62 void DBuffer::Append32(uint32_t value)
64 AppendData((uchar *)&value, 4);
67 void DBuffer::Append24(uint32_t value)
69 Append16(value);
70 Append8(value >> 16);
74 void c------------------------------() {}
77 // real SetTo code is in DBuffer.h
79 void DBuffer::SetTo(const char *string)
81 SetTo((const uint8_t *)string, strlen(string) + 1);
84 void DBuffer::SetTo(DBuffer *other)
86 SetTo(other->Data(), other->Length());
89 void DBuffer::SetTo(DBuffer &other)
91 SetTo(other.Data(), other.Length());
95 void c------------------------------() {}
98 void DBuffer::ReplaceUnprintableChars()
100 char *data = (char *)fData;
101 int length = fLength;
102 int i;
104 for(i=0;i<length;i++)
106 if (data[i] == '\n' || data[i] == '\r')
108 data[i] = '+';
110 else if (((uchar)data[i] < 32 || (uchar)data[i] > 127) && data[i] != 0)
112 data[i] = '`';
118 void c------------------------------() {}
121 DBuffer& DBuffer::operator= (const DBuffer &other)
123 SetTo((DBuffer *)&other);
124 return *this;
127 // return the data contained in the buffer
128 uint8_t *DBuffer::Data()
130 return (uint8_t *)fData;
133 // return the data contained in the buffer, and "steal" the pointer from the DBuffer
134 // so that the caller obtains ownership of it and the DBuffer contents are lost.
135 // the contents of the DBuffer are undefined after this function returns.
136 // It is intended for quickly returning C-style pointers to data from functions which use
137 // DBuffer internally to build the data.
138 uint8_t *DBuffer::TakeData()
140 if (!fAllocdExternal)
141 { // we can't give them ownership of the data, because it's still small enough
142 // that it's located within our own object. So give them a copy instead.
143 uint8_t *copy = (uint8_t *)malloc(fLength);
144 memcpy(copy, fData, fLength);
145 return copy;
148 uint8_t *data = fData; // save our pointer
149 fData = NULL; // now forget it, so it's not freed in the destructor
150 fAllocdExternal = false; // revert to internal data buffer
152 return data;
155 // return the data, along with a trailing null-terminator
156 char *DBuffer::String()
158 // ensure the data returned is null-terminated
159 if (fLength == 0 || fData[fLength - 1] != 0)
161 EnsureAlloc(fLength + 1);
162 fData[fLength] = '\0';
165 return (char *)fData;
168 // return the length of the buffer. note that this will include
169 // any null-terminators.
170 int DBuffer::Length()
172 return fLength;