diff options
Diffstat (limited to 'src/unstable/blob.cpp')
-rw-r--r-- | src/unstable/blob.cpp | 403 |
1 files changed, 403 insertions, 0 deletions
diff --git a/src/unstable/blob.cpp b/src/unstable/blob.cpp new file mode 100644 index 0000000..51a3b93 --- /dev/null +++ b/src/unstable/blob.cpp | |||
@@ -0,0 +1,403 @@ | |||
1 | /* | ||
2 | * Copyright (C) 2007-2019 Xagasoft, All rights reserved. | ||
3 | * | ||
4 | * This file is part of the libbu++ library and is released under the | ||
5 | * terms of the license contained in the file LICENSE. | ||
6 | */ | ||
7 | |||
8 | #include "bu/blob.h" | ||
9 | #include "bu/exceptioninvaliditerator.h" | ||
10 | |||
11 | #include <string.h> | ||
12 | |||
13 | Bu::Blob::Blob() : | ||
14 | pData( 0 ), | ||
15 | iSize( -1 ) | ||
16 | { | ||
17 | } | ||
18 | |||
19 | Bu::Blob::Blob( const Bu::Blob &rSrc ) : | ||
20 | pData( 0 ), | ||
21 | iSize( rSrc.iSize ) | ||
22 | { | ||
23 | pData = new char[rSrc.iSize+1]; | ||
24 | memcpy( pData, rSrc.pData, iSize+1 ); | ||
25 | } | ||
26 | |||
27 | Bu::Blob::Blob( const char *pSrc ) : | ||
28 | pData( 0 ), | ||
29 | iSize( 0 ) | ||
30 | { | ||
31 | for(; iSize < __INT_MAX__ && pSrc[iSize]; iSize++ ) { } | ||
32 | pData = new char[iSize+1]; | ||
33 | memcpy( pData, pSrc, iSize+1 ); | ||
34 | } | ||
35 | |||
36 | Bu::Blob::Blob( const void *pSrc, int32_t iSize ) | ||
37 | { | ||
38 | this->iSize = iSize; | ||
39 | pData = new char[iSize+1]; | ||
40 | memcpy( pData, pSrc, iSize ); | ||
41 | pData[iSize] = '\0'; | ||
42 | } | ||
43 | |||
44 | Bu::Blob::~Blob() | ||
45 | { | ||
46 | delete[] pData; | ||
47 | pData = 0; | ||
48 | iSize = -1; | ||
49 | } | ||
50 | |||
51 | int32_t Bu::Blob::getSize() const | ||
52 | { | ||
53 | return iSize; | ||
54 | } | ||
55 | |||
56 | char *Bu::Blob::getData() const | ||
57 | { | ||
58 | return pData; | ||
59 | } | ||
60 | |||
61 | char *Bu::Blob::c_str() const | ||
62 | { | ||
63 | return pData; | ||
64 | } | ||
65 | |||
66 | Bu::Blob &Bu::Blob::operator=( const Bu::Blob &rRhs ) | ||
67 | { | ||
68 | delete[] pData; | ||
69 | |||
70 | iSize = rRhs.iSize; | ||
71 | pData = new char[iSize+1]; | ||
72 | memcpy( pData, rRhs.pData, iSize+1 ); | ||
73 | |||
74 | return *this; | ||
75 | } | ||
76 | |||
77 | Bu::Blob &Bu::Blob::operator=( const char *pRhs ) | ||
78 | { | ||
79 | delete[] pData; | ||
80 | |||
81 | for(iSize = 0; iSize < __INT_MAX__ && pRhs[iSize]; iSize++ ) { } | ||
82 | pData = new char[iSize+1]; | ||
83 | memcpy( pData, pRhs, iSize+1 ); | ||
84 | |||
85 | return *this; | ||
86 | } | ||
87 | |||
88 | Bu::Blob::iterator::iterator( Bu::Blob *pBlob, bool bForward ) : | ||
89 | pBlob( pBlob ), | ||
90 | iIndex( bForward?0:pBlob->iSize-1 ), | ||
91 | bForward( bForward ) | ||
92 | { | ||
93 | } | ||
94 | |||
95 | Bu::Blob::iterator::iterator() : | ||
96 | pBlob( NULL ), | ||
97 | iIndex( -1 ), | ||
98 | bForward( true ) | ||
99 | { | ||
100 | } | ||
101 | |||
102 | Bu::Blob::iterator::iterator( const Bu::Blob::iterator &rSrc ) : | ||
103 | pBlob( rSrc.pBlob ), | ||
104 | iIndex( rSrc.iIndex ), | ||
105 | bForward( rSrc.bForward ) | ||
106 | { | ||
107 | } | ||
108 | |||
109 | bool Bu::Blob::iterator::isValid() const | ||
110 | { | ||
111 | if( !pBlob ) | ||
112 | return false; | ||
113 | |||
114 | if( iIndex < 0 || iIndex >= pBlob->iSize ) | ||
115 | return false; | ||
116 | |||
117 | return true; | ||
118 | } | ||
119 | |||
120 | Bu::Blob::iterator::operator bool() const | ||
121 | { | ||
122 | return isValid(); | ||
123 | } | ||
124 | |||
125 | char &Bu::Blob::iterator::operator *() | ||
126 | { | ||
127 | if( !isValid() ) | ||
128 | throw Bu::ExceptionInvalidIterator(); | ||
129 | |||
130 | return pBlob->pData[iIndex]; | ||
131 | } | ||
132 | |||
133 | Bu::Blob::iterator &Bu::Blob::iterator::operator++( int ) | ||
134 | { | ||
135 | if( bForward ) | ||
136 | ++iIndex; | ||
137 | else | ||
138 | --iIndex; | ||
139 | |||
140 | return *this; | ||
141 | } | ||
142 | |||
143 | Bu::Blob::iterator &Bu::Blob::iterator::operator++() | ||
144 | { | ||
145 | if( bForward ) | ||
146 | ++iIndex; | ||
147 | else | ||
148 | --iIndex; | ||
149 | |||
150 | return *this; | ||
151 | } | ||
152 | |||
153 | Bu::Blob::iterator &Bu::Blob::iterator::operator--( int ) | ||
154 | { | ||
155 | if( bForward ) | ||
156 | --iIndex; | ||
157 | else | ||
158 | ++iIndex; | ||
159 | |||
160 | return *this; | ||
161 | } | ||
162 | |||
163 | Bu::Blob::iterator &Bu::Blob::iterator::operator--() | ||
164 | { | ||
165 | if( bForward ) | ||
166 | --iIndex; | ||
167 | else | ||
168 | ++iIndex; | ||
169 | |||
170 | return *this; | ||
171 | } | ||
172 | |||
173 | bool Bu::Blob::iterator::operator==( const Bu::Blob::iterator &rRhs ) | ||
174 | { | ||
175 | if( isValid() == false && rRhs.isValid() == false ) | ||
176 | return true; | ||
177 | |||
178 | return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; | ||
179 | } | ||
180 | |||
181 | bool Bu::Blob::iterator::operator==( const Bu::Blob::const_iterator &rRhs ) | ||
182 | { | ||
183 | if( isValid() == false && rRhs.isValid() == false ) | ||
184 | return true; | ||
185 | |||
186 | return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; | ||
187 | } | ||
188 | |||
189 | bool Bu::Blob::iterator::operator!=( const Bu::Blob::iterator &rRhs ) | ||
190 | { | ||
191 | if( isValid() != rRhs.isValid() ) | ||
192 | return true; | ||
193 | |||
194 | return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; | ||
195 | } | ||
196 | |||
197 | bool Bu::Blob::iterator::operator!=( const Bu::Blob::const_iterator &rRhs ) | ||
198 | { | ||
199 | if( isValid() != rRhs.isValid() ) | ||
200 | return true; | ||
201 | |||
202 | return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; | ||
203 | } | ||
204 | |||
205 | Bu::Blob::iterator &Bu::Blob::iterator::operator=( Bu::Blob::iterator &rRhs ) | ||
206 | { | ||
207 | pBlob = rRhs.pBlob; | ||
208 | iIndex = rRhs.iIndex; | ||
209 | bForward = rRhs.bForward; | ||
210 | |||
211 | return *this; | ||
212 | } | ||
213 | |||
214 | |||
215 | Bu::Blob::const_iterator::const_iterator( const Blob *pBlob, bool bForward ) : | ||
216 | pBlob( pBlob ), | ||
217 | iIndex( bForward?0:pBlob->iSize-1 ), | ||
218 | bForward( bForward ) | ||
219 | { | ||
220 | } | ||
221 | |||
222 | Bu::Blob::const_iterator::const_iterator() : | ||
223 | pBlob( NULL ), | ||
224 | iIndex( -1 ), | ||
225 | bForward( true ) | ||
226 | { | ||
227 | } | ||
228 | |||
229 | Bu::Blob::const_iterator::const_iterator( | ||
230 | const Bu::Blob::const_iterator &rSrc ) : | ||
231 | pBlob( rSrc.pBlob ), | ||
232 | iIndex( rSrc.iIndex ), | ||
233 | bForward( rSrc.bForward ) | ||
234 | { | ||
235 | } | ||
236 | |||
237 | Bu::Blob::const_iterator::const_iterator( const Bu::Blob::iterator &rSrc ) : | ||
238 | pBlob( rSrc.pBlob ), | ||
239 | iIndex( rSrc.iIndex ), | ||
240 | bForward( rSrc.bForward ) | ||
241 | { | ||
242 | } | ||
243 | |||
244 | bool Bu::Blob::const_iterator::isValid() const | ||
245 | { | ||
246 | if( !pBlob ) | ||
247 | return false; | ||
248 | |||
249 | if( iIndex < 0 || iIndex >= pBlob->iSize ) | ||
250 | return false; | ||
251 | |||
252 | return true; | ||
253 | } | ||
254 | |||
255 | Bu::Blob::const_iterator::operator bool() const | ||
256 | { | ||
257 | return isValid(); | ||
258 | } | ||
259 | |||
260 | char Bu::Blob::const_iterator::operator *() const | ||
261 | { | ||
262 | if( !isValid() ) | ||
263 | throw Bu::ExceptionInvalidIterator(); | ||
264 | |||
265 | return pBlob->pData[iIndex]; | ||
266 | } | ||
267 | |||
268 | Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator++( int ) | ||
269 | { | ||
270 | if( bForward ) | ||
271 | ++iIndex; | ||
272 | else | ||
273 | --iIndex; | ||
274 | |||
275 | return *this; | ||
276 | } | ||
277 | |||
278 | Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator++() | ||
279 | { | ||
280 | if( bForward ) | ||
281 | ++iIndex; | ||
282 | else | ||
283 | --iIndex; | ||
284 | |||
285 | return *this; | ||
286 | } | ||
287 | |||
288 | Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--( int ) | ||
289 | { | ||
290 | if( bForward ) | ||
291 | ++iIndex; | ||
292 | else | ||
293 | --iIndex; | ||
294 | |||
295 | return *this; | ||
296 | } | ||
297 | |||
298 | Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator--() | ||
299 | { | ||
300 | if( bForward ) | ||
301 | ++iIndex; | ||
302 | else | ||
303 | --iIndex; | ||
304 | |||
305 | return *this; | ||
306 | } | ||
307 | |||
308 | bool Bu::Blob::const_iterator::operator==( const Bu::Blob::iterator &rRhs ) | ||
309 | { | ||
310 | if( isValid() == false && rRhs.isValid() == false ) | ||
311 | return true; | ||
312 | |||
313 | return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; | ||
314 | } | ||
315 | |||
316 | bool Bu::Blob::const_iterator::operator==( | ||
317 | const Bu::Blob::const_iterator &rRhs ) | ||
318 | { | ||
319 | if( isValid() == false && rRhs.isValid() == false ) | ||
320 | return true; | ||
321 | |||
322 | return pBlob == rRhs.pBlob && iIndex == rRhs.iIndex; | ||
323 | } | ||
324 | |||
325 | bool Bu::Blob::const_iterator::operator!=( const Bu::Blob::iterator &rRhs ) | ||
326 | { | ||
327 | if( isValid() != rRhs.isValid() ) | ||
328 | return true; | ||
329 | |||
330 | return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; | ||
331 | } | ||
332 | |||
333 | bool Bu::Blob::const_iterator::operator!=( | ||
334 | const Bu::Blob::const_iterator &rRhs ) | ||
335 | { | ||
336 | if( isValid() != rRhs.isValid() ) | ||
337 | return true; | ||
338 | |||
339 | return pBlob != rRhs.pBlob || iIndex != rRhs.iIndex; | ||
340 | } | ||
341 | |||
342 | Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( | ||
343 | Bu::Blob::iterator &rRhs ) | ||
344 | { | ||
345 | pBlob = rRhs.pBlob; | ||
346 | iIndex = rRhs.iIndex; | ||
347 | bForward = rRhs.bForward; | ||
348 | |||
349 | return *this; | ||
350 | } | ||
351 | |||
352 | Bu::Blob::const_iterator &Bu::Blob::const_iterator::operator=( | ||
353 | Bu::Blob::const_iterator &rRhs ) | ||
354 | { | ||
355 | pBlob = rRhs.pBlob; | ||
356 | iIndex = rRhs.iIndex; | ||
357 | bForward = rRhs.bForward; | ||
358 | |||
359 | return *this; | ||
360 | } | ||
361 | |||
362 | |||
363 | Bu::Blob::iterator Bu::Blob::begin() | ||
364 | { | ||
365 | return iterator( this, true ); | ||
366 | } | ||
367 | |||
368 | Bu::Blob::const_iterator Bu::Blob::begin() const | ||
369 | { | ||
370 | return const_iterator( this, true ); | ||
371 | } | ||
372 | |||
373 | Bu::Blob::iterator Bu::Blob::end() | ||
374 | { | ||
375 | return iterator(); | ||
376 | } | ||
377 | |||
378 | Bu::Blob::const_iterator Bu::Blob::end() const | ||
379 | { | ||
380 | return const_iterator(); | ||
381 | } | ||
382 | |||
383 | Bu::Blob::iterator Bu::Blob::rbegin() | ||
384 | { | ||
385 | return iterator( this, false ); | ||
386 | } | ||
387 | |||
388 | Bu::Blob::const_iterator Bu::Blob::rbegin() const | ||
389 | { | ||
390 | return const_iterator( this, false ); | ||
391 | } | ||
392 | |||
393 | Bu::Blob::iterator Bu::Blob::rend() | ||
394 | { | ||
395 | return iterator(); | ||
396 | } | ||
397 | |||
398 | Bu::Blob::const_iterator Bu::Blob::rend() const | ||
399 | { | ||
400 | return const_iterator(); | ||
401 | } | ||
402 | |||
403 | |||