aboutsummaryrefslogtreecommitdiff
path: root/src/flexbuf.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-05-01 17:11:04 +0000
committerMike Buland <eichlan@xagasoft.com>2006-05-01 17:11:04 +0000
commitf7a9549bd6ad83f2e0bceec9cddacfa5e3f84a54 (patch)
tree53cec4864776e07950e3c72f2a990a1017d08045 /src/flexbuf.cpp
downloadlibbu++-f7a9549bd6ad83f2e0bceec9cddacfa5e3f84a54.tar.gz
libbu++-f7a9549bd6ad83f2e0bceec9cddacfa5e3f84a54.tar.bz2
libbu++-f7a9549bd6ad83f2e0bceec9cddacfa5e3f84a54.tar.xz
libbu++-f7a9549bd6ad83f2e0bceec9cddacfa5e3f84a54.zip
libbu++ is finally laid out the way it should be, trunk, branches, and tags.
Diffstat (limited to '')
-rw-r--r--src/flexbuf.cpp206
1 files changed, 206 insertions, 0 deletions
diff --git a/src/flexbuf.cpp b/src/flexbuf.cpp
new file mode 100644
index 0000000..acd55a7
--- /dev/null
+++ b/src/flexbuf.cpp
@@ -0,0 +1,206 @@
1#include "flexbuf.h"
2#include <string.h>
3
4FlexBuf::FlexBuf()
5{
6 lpBuf = new char[1024];
7 nLastChar = 0;
8 nFirstChar = 0;
9 nSize = 1024;
10 nFill = 0;
11 clearData();
12}
13
14FlexBuf::~FlexBuf()
15{
16 delete[] lpBuf;
17}
18
19bool FlexBuf::appendData( const char *lpData, int nDSize )
20{
21 int nStrLen;
22 if( nDSize < 0 )
23 {
24 nStrLen = strlen( lpData );
25 }
26 else
27 {
28 nStrLen = nDSize;
29 }
30
31 if( nLastChar + nStrLen + 1 > nSize )
32 {
33 if( nFill + nStrLen + 1 < nSize )
34 {
35 memcpy( lpBuf, lpBuf+nFirstChar, nFill );
36 nLastChar -= nFirstChar;
37 nFirstChar = 0;
38 }
39 else
40 {
41 nSize += nStrLen+1;
42 char *lpNewBuf = new char[nSize];
43 memcpy( lpNewBuf, lpBuf+nFirstChar, nFill );
44 delete[] lpBuf;
45 lpBuf = lpNewBuf;
46 nLastChar -= nFirstChar;
47 nFirstChar = 0;
48 }
49 }
50
51 memcpy( &lpBuf[nLastChar], lpData, nStrLen );
52 nLastChar += nStrLen;
53 nFill += nStrLen;
54 lpBuf[nLastChar] = '\0';
55
56 return true;
57}
58
59bool FlexBuf::appendData( const char lData )
60{
61 if( nLastChar + 2 > nSize )
62 {
63 if( nFill+2 < nSize )
64 {
65 memcpy( lpBuf, lpBuf+nFirstChar, nFill );
66 nLastChar -= nFirstChar;
67 nFirstChar = 0;
68 }
69 else
70 {
71 nSize += 1024;
72 char *lpNewBuf = new char[nSize];
73 memcpy( lpNewBuf, lpBuf+nFirstChar, nFill );
74 delete[] lpBuf;
75 lpBuf = lpNewBuf;
76 nLastChar -= nFirstChar;
77 nFirstChar = 0;
78 }
79 }
80
81 lpBuf[nLastChar] = lData;
82 nLastChar++;
83 nFill++;
84 lpBuf[nLastChar] = '\0';
85
86 return true;
87}
88
89bool FlexBuf::appendData( const short lData )
90{
91 return appendData( (const char *)&lData, sizeof(short) );
92}
93
94bool FlexBuf::appendData( const int lData )
95{
96 return appendData( (const char *)&lData, sizeof(int) );
97}
98
99bool FlexBuf::appendData( const long lData )
100{
101 return appendData( (const char *)&lData, sizeof(long) );
102}
103
104bool FlexBuf::appendData( const float lData )
105{
106 return appendData( (const char *)&lData, sizeof(float) );
107}
108
109bool FlexBuf::appendData( const double lData )
110{
111 return appendData( (const char *)&lData, sizeof(double) );
112}
113
114bool FlexBuf::appendData( const unsigned char lData )
115{
116 return appendData( (const char)lData );
117}
118
119bool FlexBuf::appendData( const unsigned short lData )
120{
121 return appendData( (const char *)&lData, sizeof(short) );
122}
123
124bool FlexBuf::appendData( const unsigned long lData )
125{
126 return appendData( (const char *)&lData, sizeof(long) );
127}
128
129bool FlexBuf::appendData( const unsigned int lData )
130{
131 return appendData( (const char *)&lData, sizeof(int) );
132}
133
134bool FlexBuf::clearData()
135{
136 nFirstChar = nLastChar = nFill = 0;
137 lpBuf[nLastChar] = '\0';
138
139 return true;
140}
141
142const char *FlexBuf::getData()
143{
144 return (lpBuf+nFirstChar);
145}
146
147int FlexBuf::getLength()
148{
149 return nFill;
150}
151
152int FlexBuf::getCapacity()
153{
154 return nSize;
155}
156
157bool FlexBuf::usedData( int nAmount )
158{
159 // Remove from the end if negative
160 if( nAmount < 0 )
161 {
162 if( nFill+nAmount < 0 )
163 {
164 nFill = nFirstChar = nLastChar = 0;
165 return true;
166 }
167 nLastChar += nAmount;
168 nFill += nAmount;
169 return true;
170 }
171 if( nAmount > nFill )
172 {
173 nAmount = nSize;
174// return false;
175 }
176
177 //nLastChar -= nAmount;
178 nFirstChar += nAmount;
179 nFill -= nAmount;
180
181 if( nFill == 0 )
182 {
183 nFirstChar = nLastChar = 0;
184 }
185
186 //if( nLastChar > 0 )
187 //{
188 //memmove( lpBuf, &lpBuf[nAmount], nLastChar );
189 //}
190
191 return true;
192}
193
194int FlexBuf::findChar( char cTarget )
195{
196 for( int j = nFirstChar; j < nLastChar; j++ )
197 {
198 if( lpBuf[j] == cTarget )
199 {
200 return j;
201 }
202 }
203
204 return -1;
205}
206