1 /*
2 * Copyright 2013, Gerasim Troeglazov, 3dEyes@gmail.com. All rights reserved.
3 * Distributed under the terms of the MIT License.
4 */
5
6 #include "DataArray.h"
7
8
BDataArray(int32 blockSize)9 BDataArray::BDataArray(int32 blockSize)
10 {
11 fBlockSize = blockSize;
12 fData = (uint8*)malloc(blockSize);
13 fAllocatedDataSize = blockSize;
14 fDataSize = 0;
15 }
16
17
~BDataArray()18 BDataArray::~BDataArray()
19 {
20 free(fData);
21 }
22
23
24 status_t
_ReallocArrayFor(int32 size)25 BDataArray::_ReallocArrayFor(int32 size)
26 {
27 if (fDataSize + size > fAllocatedDataSize) {
28 int32 blocks = ((fDataSize + size) / fBlockSize) + 1;
29 uint8 *newData = (uint8*)realloc(fData, blocks * fBlockSize);
30 if (newData != NULL) {
31 fData = newData;
32 fAllocatedDataSize = blocks * fBlockSize;
33 }
34 }
35 return fData == NULL ? B_NO_MEMORY : B_OK;
36 }
37
38
39 uint8*
Buffer(void)40 BDataArray::Buffer(void)
41 {
42 return fData;
43 }
44
45
46 int32
Length(void)47 BDataArray::Length(void)
48 {
49 return fDataSize;
50 }
51
52
53 ssize_t
WriteToStream(BPositionIO * stream)54 BDataArray::WriteToStream(BPositionIO *stream)
55 {
56 return stream->Write(fData, fDataSize);
57 }
58
59
60 BDataArray&
Append(uint8 val)61 BDataArray::Append(uint8 val)
62 {
63 status_t status = _ReallocArrayFor(sizeof(val));
64 if (status == B_OK) {
65 fData[fDataSize] = val;
66 fDataSize++;
67 }
68 return *this;
69 }
70
71
72 BDataArray&
Append(int8 val)73 BDataArray::Append(int8 val)
74 {
75 return Append((uint8)val);
76 }
77
78
79 BDataArray&
Append(uint16 val)80 BDataArray::Append(uint16 val)
81 {
82 status_t status = _ReallocArrayFor(sizeof(val));
83 if (status == B_OK) {
84 val = B_HOST_TO_BENDIAN_INT16(val);
85 memcpy(fData + fDataSize, &val, sizeof(val));
86 fDataSize += sizeof(val);
87 }
88 return *this;
89 }
90
91
92 BDataArray&
Append(int16 val)93 BDataArray::Append(int16 val)
94 {
95 return Append((uint16)val);
96 }
97
98
99 BDataArray&
Append(uint32 val)100 BDataArray::Append(uint32 val)
101 {
102 status_t status = _ReallocArrayFor(sizeof(val));
103 if (status == B_OK) {
104 val = B_HOST_TO_BENDIAN_INT32(val);
105 memcpy(fData + fDataSize, &val, sizeof(val));
106 fDataSize += sizeof(val);
107 }
108 return *this;
109 }
110
111
112 BDataArray&
Append(int64 val)113 BDataArray::Append(int64 val)
114 {
115 return Append((uint64)val);
116 }
117
118
119 BDataArray&
Append(uint64 val)120 BDataArray::Append(uint64 val)
121 {
122 status_t status = _ReallocArrayFor(sizeof(val));
123 if (status == B_OK) {
124 val = B_HOST_TO_BENDIAN_INT64(val);
125 memcpy(fData + fDataSize, &val, sizeof(val));
126 fDataSize += sizeof(val);
127 }
128 return *this;
129 }
130
131
132 BDataArray&
Append(int32 val)133 BDataArray::Append(int32 val)
134 {
135 return Append((uint32)val);
136 }
137
138 BDataArray&
Append(const char * str)139 BDataArray::Append(const char *str)
140 {
141 int32 len = strlen(str);
142 status_t status = _ReallocArrayFor(len);
143 if (status == B_OK) {
144 memcpy(fData + fDataSize, str, len);
145 fDataSize += len;
146 }
147 return *this;
148 }
149
150
151 BDataArray&
Append(BString & str)152 BDataArray::Append(BString& str)
153 {
154 return Append(str.String());
155 }
156
157
158 BDataArray&
Append(BDataArray & array)159 BDataArray::Append(BDataArray& array)
160 {
161 return Append(array.Buffer(), array.Length());
162 }
163
164
165 BDataArray&
Append(uint8 * ptr,int32 len)166 BDataArray::Append(uint8 *ptr, int32 len)
167 {
168 status_t status = _ReallocArrayFor(len);
169 if (status == B_OK) {
170 memcpy(fData + fDataSize, ptr, len);
171 fDataSize += len;
172 }
173 return *this;
174 }
175
176
177 BDataArray&
Repeat(uint8 byte,int32 count)178 BDataArray::Repeat(uint8 byte, int32 count)
179 {
180 status_t status = _ReallocArrayFor(count);
181 if (status == B_OK) {
182 memset(fData + fDataSize, byte, count);
183 fDataSize += count;
184 }
185 return *this;
186 }
187
188
189 BDataArray&
operator <<(int8 val)190 BDataArray::operator<<(int8 val)
191 {
192 Append(val);
193 return *this;
194 }
195
196
197 BDataArray&
operator <<(uint8 val)198 BDataArray::operator<<(uint8 val)
199 {
200 Append(val);
201 return *this;
202 }
203
204
205 BDataArray&
operator <<(int16 val)206 BDataArray::operator<<(int16 val)
207 {
208 Append(val);
209 return *this;
210 }
211
212
213 BDataArray&
operator <<(uint16 val)214 BDataArray::operator<<(uint16 val)
215 {
216 Append(val);
217 return *this;
218 }
219
220
221 BDataArray&
operator <<(int32 val)222 BDataArray::operator<<(int32 val)
223 {
224 Append(val);
225 return *this;
226 }
227
228
229 BDataArray&
operator <<(uint32 val)230 BDataArray::operator<<(uint32 val)
231 {
232 Append(val);
233 return *this;
234 }
235
236
237 BDataArray&
operator <<(int64 val)238 BDataArray::operator<<(int64 val)
239 {
240 Append(val);
241 return *this;
242 }
243
244
245 BDataArray&
operator <<(uint64 val)246 BDataArray::operator<<(uint64 val)
247 {
248 Append(val);
249 return *this;
250 }
251
252
253 BDataArray&
operator <<(const char * str)254 BDataArray::operator<<(const char* str)
255 {
256 Append(str);
257 return *this;
258 }
259
260
261 BDataArray&
operator <<(BDataArray & array)262 BDataArray::operator<<(BDataArray& array)
263 {
264 Append(array);
265 return *this;
266 }
267