11 #include <drift/dslcore.h>
12 #include <drift/Buffer.h>
15 #pragma warning(disable: 4244)
17 #ifdef DSL_BUFFER_USE_VECTOR
21 buf->vec =
new vector<uint8>;
22 if (useMutex) { buf->
hMutex =
new DSL_Mutex(); }
35 ret.assign(buf->data, buf->len);
51 buf->vec->resize(len);
52 memcpy(buf->vec->data(), ptr, len);
53 buf->data = (
char *)buf->vec->data();
60 buf->vec->resize(len);
61 buf->data = (
char *)buf->vec->data();
67 if (len <= 0) {
return; }
70 if (len >= buf->len) {
73 buf->vec->erase(buf->vec->begin(), buf->vec->begin() + len);
74 buf->data = (
char *)buf->vec->data();
81 if (len <= 0) {
return; }
83 if (len >= buf->len) {
86 buf->vec->resize(buf->vec->size() - len);
87 buf->data = (
char *)buf->vec->data();
88 buf->len = buf->vec->size();
94 if (len <= 0) {
return true; }
97 buf->vec->resize(buf->len + len);
98 buf->data = (
char *)buf->vec->data();
99 memmove(buf->data + len, buf->data, buf->len);
100 memcpy(buf->data, ptr, len);
108 if (len <= 0) {
return true; }
111 buf->vec->resize(buf->len + len);
112 assert(buf->vec->size() == buf->len + len);
113 buf->data = (
char *)buf->vec->data();
114 memcpy(buf->data + buf->len, ptr, len);
125 if (useMutex) { buf->
hMutex =
new DSL_Mutex(); }
138 ret.assign(buf->data, buf->len);
154 buf->data = (
char *)dsl_realloc(buf->data, len);
156 memcpy(buf->data, ptr, len);
162 buf->data = (
char *)dsl_realloc(buf->data, len);
168 if (len <= 0) {
return; }
170 if (len >= buf->len) {
174 memmove(buf->data, buf->data + len, buf->len);
175 buf->data = (
char *)dsl_realloc(buf->data, buf->len);
180 if (len <= 0) {
return; }
182 if (len >= buf->len) {
186 buf->data = (
char *)dsl_realloc(buf->data, buf->len);
191 if (len <= 0) {
return true; }
194 buf->data = (
char *)dsl_realloc(buf->data, buf->len + len);
195 memmove(buf->data + len, buf->data, buf->len);
196 memcpy(buf->data, ptr, len);
203 if (len <= 0) {
return true; }
206 buf->data = (
char *)dsl_realloc(buf->data, buf->len + len);
207 memcpy(buf->data + buf->len, ptr, len);
216 DSL_Buffer::DSL_Buffer() {
221 DSL_Buffer::~DSL_Buffer() { Clear(); }
223 void DSL_Buffer::Clear() {
230 void DSL_Buffer::RemoveFromEnd(uint32 ulen) {
236 data = (
char *)dsl_realloc(data, len);
241 void DSL_Buffer::RemoveFromBeginning(uint32 ulen) {
247 memmove(data, data+ulen, len);
248 data = (
char *)dsl_realloc(data, len);
253 char * DSL_Buffer::Get() {
257 uint32 DSL_Buffer::GetLen() {
261 bool DSL_Buffer::Get(
char * buf, uint32 * size) {
265 memcpy(buf, data, *size);
269 memcpy(buf, data, len);
274 bool DSL_Buffer::Get(
char * buf, uint32 size) {
275 if (data == NULL) {
return size > 0 ?
false:
true; }
278 memcpy(buf, data, size);
282 memcpy(buf, data, len);
287 void DSL_Buffer::Set(
const char * buf, uint32 ulen) {
289 if (ulen == 0xFFFFFFFF) { ulen = uint32(strlen(buf)); }
290 data = (
char *)dsl_realloc(data, ulen);
291 memcpy(data, buf, ulen);
295 void DSL_Buffer::Append(
const char * buf, uint32 ulen) {
297 if (ulen == 0xFFFFFFFF) { ulen = uint32(strlen(buf)); }
298 data = (
char *)dsl_realloc(data, len+ulen);
299 memcpy(data+len, buf, ulen);
303 void DSL_Buffer::Prepend(
const char * buf, uint32 ulen) {
305 if (ulen == 0xFFFFFFFF) { ulen = uint32(strlen(buf)); }
306 data = (
char *)dsl_realloc(data, len+ulen);
307 memmove(data+ulen, data, len);
308 memcpy(data, buf, ulen);
313 void DSL_Buffer::Append_int8(int8 val) {
314 Append((
char *)&val,
sizeof(val));
316 void DSL_Buffer::Append_uint8(uint8 val) {
317 Append((
char *)&val,
sizeof(val));
319 void DSL_Buffer::Append_int16(int16 val) {
320 Append((
char *)&val,
sizeof(val));
322 void DSL_Buffer::Append_uint16(uint16 val) {
323 Append((
char *)&val,
sizeof(val));
325 void DSL_Buffer::Append_int32(int32 val) {
326 Append((
char *)&val,
sizeof(val));
328 void DSL_Buffer::Append_uint32(uint32 val) {
329 Append((
char *)&val,
sizeof(val));
331 void DSL_Buffer::Append_int64(int64 val) {
332 Append((
char *)&val,
sizeof(val));
334 void DSL_Buffer::Append_uint64(uint64 val) {
335 Append((
char *)&val,
sizeof(val));
void DSL_CC buffer_remove_front(DSL_BUFFER *buf, int64 len)
Remove data from the beginning of the buffer.
void DSL_CC buffer_init(DSL_BUFFER *buf, bool useMutex)
Initialize the buffer, optionally with a mutex protecting it. If you don't use the mutex you need to ...
void DSL_CC buffer_set(DSL_BUFFER *buf, const char *ptr, int64 len)
Sets the buffer to the specified data, discarding anything existing.
string buffer_as_string(DSL_BUFFER *buf)
Gets the buffer as a string.
void DSL_CC buffer_free(DSL_BUFFER *buf)
Free the buffer when you are done with it.
void DSL_CC buffer_clear(DSL_BUFFER *buf)
Sets the buffer length to 0 and clears the data. It is still ready to be used unlike buffer_free.
void DSL_CC buffer_remove_end(DSL_BUFFER *buf, int64 len)
Remove data from the end of the buffer.
bool DSL_CC buffer_append(DSL_BUFFER *buf, const char *ptr, int64 len)
Add data to the end of the buffer.
bool DSL_CC buffer_prepend(DSL_BUFFER *buf, const char *ptr, int64 len)
Add data to the beginning of the buffer.
void DSL_CC buffer_resize(DSL_BUFFER *buf, int64 len)
Resize the buffer, if the length is longer then the existing data the added byte values are undefined...
DSL_API void DSL_CC dsl_free(void *ptr)
DSL_Mutex * hMutex
Handle to the mutex protecting this buffer, if enabled.