Menu

[r804]: / Trunk / Sources / ByteBuffer.h  Maximize  Restore  History

Download this file

268 lines (219 with data), 7.8 kB

  1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
/**
ByteBuffer
ByteBuffer.h
Copyright 2011 - 2013 Ramsey Kant
Licensed under the Apache License, Version 2.0 (the "License");
you may not use this file except in compliance with the License.
You may obtain a copy of the License at
http://www.apache.org/licenses/LICENSE-2.0
Unless required by applicable law or agreed to in writing, software
distributed under the License is distributed on an "AS IS" BASIS,
WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
See the License for the specific language governing permissions and
limitations under the License.
*/
#ifndef _ByteBuffer_H_
#define _ByteBuffer_H_
// Default number of uint8_ts to allocate in the backing buffer if no size is provided
#define DEFAULT_SIZE 4096
// If defined, utility functions within the class are enabled
#define BB_UTILITY
// The byte type from previous versions of ByteBuffer is now obsolete
// This macro is to ensure compatibility, however, it will be removed in future versions
#define byte uint8_t
#include <cstdlib>
#include "stdint.h"
#include <cstring>
#include <vector>
#ifdef BB_UTILITY
#include <iostream>
#include <stdio.h>
#endif
#define RESYNC_INT(bWrite, pBuffer, field) \
{ \
if (bWrite) { pBuffer->putInt((int)field); }\
else { field = pBuffer->getInt(); } \
}
#define RESYNC_FLOAT(bWrite, pBuffer, field) \
{ \
if (bWrite) { pBuffer->putFloat((float)field); }\
else { field = pBuffer->getFloat(); } \
}
#define RESYNC_INT_WITH_CAST(bWrite, pBuffer, field, clazz) \
{ \
if (bWrite) { pBuffer->putInt((int)field); }\
else { field = (clazz)pBuffer->getInt(); } \
}
#define RESYNC_BOOL(bWrite, pBuffer, field) \
{ \
if (bWrite) { pBuffer->put(field ? 1 : 0); }\
else { field = (pBuffer->get() != 0); } \
}
#define RESYNC_STRING(bWrite, pBuffer, field) \
{ \
if (bWrite) { pBuffer->putString(field); }\
else { pBuffer->getString(field); } \
}
#define RESYNC_INT_ARRAY(bWrite, pBuffer, count, aiField) \
{ \
if (bWrite) { for (int i = 0; i < count; i++) { pBuffer->putInt((int)aiField[i]); } } \
else { for (int i = 0; i < count; i++) { aiField[i] = (int)pBuffer->getInt(); } } \
}
#define RESYNC_OPTIONAL_ARRAY(bWrite, pBuffer, count, aiField, arrType) \
{ \
if (bWrite) { \
if (aiField == NULL) { \
pBuffer->put((byte)0); \
} \
else { \
pBuffer->put((byte)1); for (int i = 0; i < count; i++) { pBuffer->putInt((int)aiField[i]); } \
} \
} \
else { \
SAFE_DELETE_ARRAY(aiField); \
if (pBuffer->get() != 0) { \
if (aiField == NULL) aiField = new arrType[count]; \
for (int i = 0; i < count; i++) { aiField[i] = pBuffer->getInt(); } \
} \
else { aiField = NULL; } \
} \
}
#define RESYNC_INT_ARRAY_WITH_CAST(bWrite, pBuffer, count, aiField, clazz) \
{ \
if (bWrite) { for (int i = 0; i < count; i++) { pBuffer->putInt((int)aiField[i]); } } \
else { for (int i = 0; i < count; i++) { aiField[i] = (clazz)pBuffer->getInt(); } } \
}
#define RESYNC_BOOL_ARRAY(bWrite, pBuffer, count, aiField) \
{ \
if (bWrite) { for (int i = 0; i < count; i++) { pBuffer->put(aiField[i] ? 1 : 0); } } \
else { for (int i = 0; i < count; i++) { aiField[i] = (pBuffer->get() != 0); } } \
}
class ByteBuffer {
private:
uint32_t rpos, wpos;
std::vector<uint8_t> buf;
#ifdef BB_UTILITY
std::string name;
#endif
template <typename T> T read() {
T data = read<T>(rpos);
rpos += sizeof(T);
return data;
}
template <typename T> T read(uint32_t index) const {
if (index + sizeof(T) <= buf.size())
return *((T*)&buf[index]);
return 0;
}
template <typename T> void append(T data) {
uint32_t s = sizeof(data);
if (size() < (wpos + s))
buf.resize(wpos + s);
memcpy(&buf[wpos], (uint8_t*)&data, s);
//printf("writing %c to %i\n", (uint8_t)data, wpos);
wpos += s;
}
template <typename T> void insert(T data, uint32_t index) {
if ((index + sizeof(data)) > size())
return;
memcpy(&buf[index], (uint8_t*)&data, sizeof(data));
wpos = index + sizeof(data);
}
public:
ByteBuffer(uint32_t size = DEFAULT_SIZE);
ByteBuffer(uint8_t* arr, uint32_t size);
virtual ~ByteBuffer();
uint32_t bytesRemaining(); // Number of uint8_ts from the current read position till the end of the buffer
void clear(); // Clear our the vector and reset read and write positions
ByteBuffer* clone(); // Return a new instance of a ByteBuffer with the exact same contents and the same state (rpos, wpos)
//ByteBuffer compact(); // TODO?
bool equals(ByteBuffer* other); // Compare if the contents are equivalent
void resize(uint32_t newSize);
uint32_t size(); // Size of internal vector
// Basic Searching (Linear)
template <typename T> int32_t find(T key, uint32_t start = 0) {
int32_t ret = -1;
uint32_t len = buf.size();
for (uint32_t i = start; i < len; i++) {
T data = read<T>(i);
// Wasn't actually found, bounds of buffer were exceeded
if ((key != 0) && (data == 0))
break;
// Key was found in array
if (data == key) {
ret = (int32_t)i;
break;
}
}
return ret;
}
// Replacement
void replace(uint8_t key, uint8_t rep, uint32_t start = 0, bool firstOccuranceOnly = false);
// Read
uint8_t peek(); // Relative peek. Reads and returns the next uint8_t in the buffer from the current position but does not increment the read position
uint8_t get(); // Relative get method. Reads the uint8_t at the buffers current position then increments the position
uint8_t get(uint32_t index); // Absolute get method. Read uint8_t at index
void getBytes(uint8_t* buf, uint32_t len); // Absolute read into array buf of length len
char getChar(); // Relative
char getChar(uint32_t index); // Absolute
double getDouble();
double getDouble(uint32_t index);
float getFloat();
float getFloat(uint32_t index);
uint32_t getInt();
uint32_t getInt(uint32_t index);
uint64_t getLong();
uint64_t getLong(uint32_t index);
uint16_t getShort();
uint16_t getShort(uint32_t index);
void putString(const wchar *szName);
void putString(const char *szName);
void putString(const std::string& szName);
void putString(const std::wstring& szName);
void getString(char *szName);
void getString(wchar *szName);
void getString(std::string& szName);
void getString(std::wstring& szName);
// Write
void put(ByteBuffer* src); // Relative write of the entire contents of another ByteBuffer (src)
void put(uint8_t b); // Relative write
void put(uint8_t b, uint32_t index); // Absolute write at index
void putBytes(uint8_t* b, uint32_t len); // Relative write
void putBytes(uint8_t* b, uint32_t len, uint32_t index); // Absolute write starting at index
void putChar(char value); // Relative
void putChar(char value, uint32_t index); // Absolute
void putDouble(double value);
void putDouble(double value, uint32_t index);
void putFloat(float value);
void putFloat(float value, uint32_t index);
void putInt(uint32_t value);
void putInt(uint32_t value, uint32_t index);
void putLong(uint64_t value);
void putLong(uint64_t value, uint32_t index);
void putShort(uint16_t value);
void putShort(uint16_t value, uint32_t index);
// Buffer Position Accessors & Mutators
void setReadPos(uint32_t r) {
rpos = r;
}
uint32_t getReadPos() {
return rpos;
}
void setWritePos(uint32_t w) {
wpos = w;
}
uint32_t getWritePos() {
return wpos;
}
// Utility Functions
#ifdef BB_UTILITY
void setName(std::string n);
std::string getName();
void printInfo();
void printAH();
void printAscii();
void printHex();
void printPosition();
#endif
};
#endif