DSL
Buffer.cpp
1 //@AUTOHEADER@BEGIN@
2 /***********************************************************************\
3 | Drift Standard Libraries v1.01 |
4 | Copyright 2010-2023 Drift Solutions / Indy Sams |
5 | Docs and more information available at https://www.driftsolutions.dev |
6 | This file released under the 3-clause BSD license, |
7 | see included DSL.LICENSE.TXT file for details. |
8 \***********************************************************************/
9 //@AUTOHEADER@END@
10 
11 #include <drift/dslcore.h>
12 #include <drift/Buffer.h>
13 #include <assert.h>
14 
15 #pragma warning(disable: 4244)
16 
17 #ifdef DSL_BUFFER_USE_VECTOR
18 
19 void DSL_CC buffer_init(DSL_BUFFER * buf, bool useMutex) {
20  memset(buf, 0, sizeof(DSL_BUFFER));
21  buf->vec = new vector<uint8>;
22  if (useMutex) { buf->hMutex = new DSL_Mutex(); }
23 }
24 
25 void DSL_CC buffer_free(DSL_BUFFER * buf) {
26  delete buf->vec;
27  if (buf->hMutex) { delete buf->hMutex; }
28  memset(buf, 0xFE, sizeof(DSL_BUFFER));
29 }
30 
31 string buffer_as_string(DSL_BUFFER * buf) {
32  string ret;
33  if (buf->hMutex) { buf->hMutex->Lock(); }
34  if (buf->len > 0) {
35  ret.assign(buf->data, buf->len);
36  }
37  if (buf->hMutex) { buf->hMutex->Release(); }
38  return ret;
39 }
40 
41 void DSL_CC buffer_clear(DSL_BUFFER * buf) {
42  if (buf->hMutex) { buf->hMutex->Lock(); }
43  buf->vec->clear();
44  buf->len = 0;
45  buf->data = NULL;
46  if (buf->hMutex) { buf->hMutex->Release(); }
47 }
48 
49 void DSL_CC buffer_set(DSL_BUFFER * buf, const char * ptr, int64 len) {
50  if (buf->hMutex) { buf->hMutex->Lock(); }
51  buf->vec->resize(len);
52  memcpy(buf->vec->data(), ptr, len);
53  buf->data = (char *)buf->vec->data();
54  buf->len = len;
55  if (buf->hMutex) { buf->hMutex->Release(); }
56 }
57 
58 void DSL_CC buffer_resize(DSL_BUFFER * buf, int64 len) {
59  if (buf->hMutex) { buf->hMutex->Lock(); }
60  buf->vec->resize(len);
61  buf->data = (char *)buf->vec->data();
62  buf->len = len;
63  if (buf->hMutex) { buf->hMutex->Release(); }
64 }
65 
66 void DSL_CC buffer_remove_front(DSL_BUFFER * buf, int64 len) {
67  if (len <= 0) { return; }
68  if (buf->hMutex) { buf->hMutex->Lock(); }
69 
70  if (len >= buf->len) {
71  buffer_clear(buf);
72  } else {
73  buf->vec->erase(buf->vec->begin(), buf->vec->begin() + len);
74  buf->data = (char *)buf->vec->data();
75  buf->len -= len;
76  }
77  if (buf->hMutex) { buf->hMutex->Release(); }
78 }
79 
80 void DSL_CC buffer_remove_end(DSL_BUFFER * buf, int64 len) {
81  if (len <= 0) { return; }
82  if (buf->hMutex) { buf->hMutex->Lock(); }
83  if (len >= buf->len) {
84  buffer_clear(buf);
85  } else {
86  buf->vec->resize(buf->vec->size() - len);
87  buf->data = (char *)buf->vec->data();
88  buf->len = buf->vec->size();
89  }
90  if (buf->hMutex) { buf->hMutex->Release(); }
91 }
92 
93 bool DSL_CC buffer_prepend(DSL_BUFFER * buf, const char * ptr, int64 len) {
94  if (len <= 0) { return true; }
95  if (buf->hMutex) { buf->hMutex->Lock(); }
96 
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);
101  buf->len += len;
102 
103  if (buf->hMutex) { buf->hMutex->Release(); }
104  return true;
105 }
106 
107 bool DSL_CC buffer_append(DSL_BUFFER * buf, const char * ptr, int64 len) {
108  if (len <= 0) { return true; }
109  if (buf->hMutex) { buf->hMutex->Lock(); }
110 
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);
115  buf->len += len;
116 
117  if (buf->hMutex) { buf->hMutex->Release(); }
118  return true;
119 }
120 
121 #else
122 
123 void DSL_CC buffer_init(DSL_BUFFER * buf, bool useMutex) {
124  memset(buf, 0, sizeof(DSL_BUFFER));
125  if (useMutex) { buf->hMutex = new DSL_Mutex(); }
126 }
127 
128 void DSL_CC buffer_free(DSL_BUFFER * buf) {
129  dsl_freenn(buf->data);
130  if (buf->hMutex) { delete buf->hMutex; }
131  memset(buf, 0xFE, sizeof(DSL_BUFFER));
132 }
133 
135  string ret;
136  if (buf->hMutex) { buf->hMutex->Lock(); }
137  if (buf->len > 0) {
138  ret.assign(buf->data, buf->len);
139  }
140  if (buf->hMutex) { buf->hMutex->Release(); }
141  return ret;
142 }
143 
144 void DSL_CC buffer_clear(DSL_BUFFER * buf) {
145  if (buf->hMutex) { buf->hMutex->Lock(); }
146  dsl_freenn(buf->data);
147  buf->len = 0;
148  buf->data = NULL;
149  if (buf->hMutex) { buf->hMutex->Release(); }
150 }
151 
152 void DSL_CC buffer_set(DSL_BUFFER * buf, const char * ptr, int64 len) {
153  if (buf->hMutex) { buf->hMutex->Lock(); }
154  buf->data = (char *)dsl_realloc(buf->data, len);
155  buf->len = len;
156  memcpy(buf->data, ptr, len);
157  if (buf->hMutex) { buf->hMutex->Release(); }
158 }
159 
160 void DSL_CC buffer_resize(DSL_BUFFER * buf, int64 len) {
161  if (buf->hMutex) { buf->hMutex->Lock(); }
162  buf->data = (char *)dsl_realloc(buf->data, len);
163  buf->len = len;
164  if (buf->hMutex) { buf->hMutex->Release(); }
165 }
166 
167 void DSL_CC buffer_remove_front(DSL_BUFFER * buf, int64 len) {
168  if (len <= 0) { return; }
169  if (buf->hMutex) { buf->hMutex->Lock(); }
170  if (len >= buf->len) {
171  buffer_clear(buf);
172  } else {
173  buf->len -= len;
174  memmove(buf->data, buf->data + len, buf->len);
175  buf->data = (char *)dsl_realloc(buf->data, buf->len);
176  }
177  if (buf->hMutex) { buf->hMutex->Release(); }
178 }
179 void DSL_CC buffer_remove_end(DSL_BUFFER * buf, int64 len) {
180  if (len <= 0) { return; }
181  if (buf->hMutex) { buf->hMutex->Lock(); }
182  if (len >= buf->len) {
183  buffer_clear(buf);
184  } else {
185  buf->len -= len;
186  buf->data = (char *)dsl_realloc(buf->data, buf->len);
187  }
188  if (buf->hMutex) { buf->hMutex->Release(); }
189 }
190 bool DSL_CC buffer_prepend(DSL_BUFFER * buf, const char * ptr, int64 len) {
191  if (len <= 0) { return true; }
192  if (buf->hMutex) { buf->hMutex->Lock(); }
193 
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);
197  buf->len += len;
198 
199  if (buf->hMutex) { buf->hMutex->Release(); }
200  return true;
201 }
202 bool DSL_CC buffer_append(DSL_BUFFER * buf, const char * ptr, int64 len) {
203  if (len <= 0) { return true; }
204  if (buf->hMutex) { buf->hMutex->Lock(); }
205 
206  buf->data = (char *)dsl_realloc(buf->data, buf->len + len);
207  memcpy(buf->data + buf->len, ptr, len);
208  buf->len += len;
209 
210  if (buf->hMutex) { buf->hMutex->Release(); }
211  return true;
212 }
213 
214 #endif
215 
216 DSL_Buffer::DSL_Buffer() {
217  data = NULL;
218  len = 0;
219 }
220 
221 DSL_Buffer::~DSL_Buffer() { Clear(); }
222 
223 void DSL_Buffer::Clear() {
224  LockMutex(hMutex);
225  if (data) { dsl_free(data); }
226  data = NULL;
227  len = 0;
228  RelMutex(hMutex);
229 }
230 void DSL_Buffer::RemoveFromEnd(uint32 ulen) {
231  if (ulen >= len) {
232  Clear();
233  } else {
234  LockMutex(hMutex);
235  len -= ulen;
236  data = (char *)dsl_realloc(data, len);
237  RelMutex(hMutex);
238  }
239 }
240 
241 void DSL_Buffer::RemoveFromBeginning(uint32 ulen) {
242  if (ulen >= len) {
243  Clear();
244  } else {
245  LockMutex(hMutex);
246  len -= ulen;
247  memmove(data, data+ulen, len);
248  data = (char *)dsl_realloc(data, len);
249  RelMutex(hMutex);
250  }
251 }
252 
253 char * DSL_Buffer::Get() {
254  return data;
255 }
256 
257 uint32 DSL_Buffer::GetLen() {
258  return len;
259 }
260 
261 bool DSL_Buffer::Get(char * buf, uint32 * size) {
262  LockMutex(hMutex);
263  *size = len;
264  if (*size < len) {
265  memcpy(buf, data, *size);
266  RelMutex(hMutex);
267  return false;
268  }
269  memcpy(buf, data, len);
270  RelMutex(hMutex);
271  return false;
272 }
273 
274 bool DSL_Buffer::Get(char * buf, uint32 size) {
275  if (data == NULL) { return size > 0 ? false:true; }
276  LockMutex(hMutex);
277  if (size < len) {
278  memcpy(buf, data, size);
279  RelMutex(hMutex);
280  return false;
281  }
282  memcpy(buf, data, len);
283  RelMutex(hMutex);
284  return false;
285 }
286 
287 void DSL_Buffer::Set(const char * buf, uint32 ulen) {
288  LockMutex(hMutex);
289  if (ulen == 0xFFFFFFFF) { ulen = uint32(strlen(buf)); }
290  data = (char *)dsl_realloc(data, ulen);
291  memcpy(data, buf, ulen);
292  len = ulen;
293  RelMutex(hMutex);
294 }
295 void DSL_Buffer::Append(const char * buf, uint32 ulen) {
296  LockMutex(hMutex);
297  if (ulen == 0xFFFFFFFF) { ulen = uint32(strlen(buf)); }
298  data = (char *)dsl_realloc(data, len+ulen);
299  memcpy(data+len, buf, ulen);
300  len += ulen;
301  RelMutex(hMutex);
302 }
303 void DSL_Buffer::Prepend(const char * buf, uint32 ulen) {
304  LockMutex(hMutex);
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);
309  len += ulen;
310  RelMutex(hMutex);
311 };
312 
313 void DSL_Buffer::Append_int8(int8 val) {
314  Append((char *)&val, sizeof(val));
315 }
316 void DSL_Buffer::Append_uint8(uint8 val) {
317  Append((char *)&val, sizeof(val));
318 }
319 void DSL_Buffer::Append_int16(int16 val) {
320  Append((char *)&val, sizeof(val));
321 }
322 void DSL_Buffer::Append_uint16(uint16 val) {
323  Append((char *)&val, sizeof(val));
324 }
325 void DSL_Buffer::Append_int32(int32 val) {
326  Append((char *)&val, sizeof(val));
327 }
328 void DSL_Buffer::Append_uint32(uint32 val) {
329  Append((char *)&val, sizeof(val));
330 }
331 void DSL_Buffer::Append_int64(int64 val) {
332  Append((char *)&val, sizeof(val));
333 }
334 void DSL_Buffer::Append_uint64(uint64 val) {
335  Append((char *)&val, sizeof(val));
336 }
void DSL_CC buffer_remove_front(DSL_BUFFER *buf, int64 len)
Remove data from the beginning of the buffer.
Definition: Buffer.cpp:167
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 ...
Definition: Buffer.cpp:123
void DSL_CC buffer_set(DSL_BUFFER *buf, const char *ptr, int64 len)
Sets the buffer to the specified data, discarding anything existing.
Definition: Buffer.cpp:152
string buffer_as_string(DSL_BUFFER *buf)
Gets the buffer as a string.
Definition: Buffer.cpp:134
void DSL_CC buffer_free(DSL_BUFFER *buf)
Free the buffer when you are done with it.
Definition: Buffer.cpp:128
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.
Definition: Buffer.cpp:144
void DSL_CC buffer_remove_end(DSL_BUFFER *buf, int64 len)
Remove data from the end of the buffer.
Definition: Buffer.cpp:179
bool DSL_CC buffer_append(DSL_BUFFER *buf, const char *ptr, int64 len)
Add data to the end of the buffer.
Definition: Buffer.cpp:202
bool DSL_CC buffer_prepend(DSL_BUFFER *buf, const char *ptr, int64 len)
Add data to the beginning of the buffer.
Definition: Buffer.cpp:190
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...
Definition: Buffer.cpp:160
DSL_API void DSL_CC dsl_free(void *ptr)
Definition: dsl.cpp:345
#define dsl_freenn(ptr)
Definition: dslcore.h:190
DSL_Mutex * hMutex
Handle to the mutex protecting this buffer, if enabled.
Definition: Buffer.h:31