]> git.bts.cx Git - benzene.git/blob - third_party/soloud_speech/darray.cpp
Sprites
[benzene.git] / third_party / soloud_speech / darray.cpp
1 #include <stdlib.h>
2 #include <string.h>
3 #include "darray.h"
4
5 darray::darray()
6 {
7 mAllocChunk = 128;
8 mAllocated = mUsed = 0;
9 mData = NULL;
10 }
11
12 void darray::clear()
13 {
14 free(mData);
15 mAllocChunk = 128;
16 mAllocated = mUsed = 0;
17 mData = NULL;
18 }
19
20 darray::~darray()
21 {
22 clear();
23 }
24
25 char * darray::getDataInPos(int aPosition)
26 {
27 if (aPosition < mAllocated && aPosition < mUsed)
28 return mData + aPosition;
29
30 if (aPosition >= mAllocated)
31 {
32 int newsize = mAllocated;
33
34 while (newsize <= aPosition)
35 {
36 newsize += mAllocChunk;
37 mAllocChunk *= 2;
38 }
39
40 char *newdata = (char*)realloc(mData, newsize);
41 if (!newdata)
42 {
43 free(mData);
44 mData = NULL;
45 mAllocated = mUsed = 0;
46 return NULL;
47 }
48 else
49 {
50 memset(newdata + mAllocated, 0, newsize - mAllocated);
51 }
52
53 mData = newdata;
54 mAllocated = newsize;
55 }
56
57 if (aPosition >= mUsed)
58 {
59 mUsed = aPosition + 1;
60 }
61
62 return mData + aPosition;
63 }
64
65 void darray::put(int aData)
66 {
67 char *s = getDataInPos(mUsed);
68
69 *s = (char)aData;
70 }