aboutsummaryrefslogtreecommitdiff
path: root/src/old/staticstring.cpp
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2007-04-03 03:49:53 +0000
committerMike Buland <eichlan@xagasoft.com>2007-04-03 03:49:53 +0000
commitf4c20290509d7ed3a8fd5304577e7a4cc0b9d974 (patch)
tree13cdf64f7cf134f397a7165b7a3fe0807e37026b /src/old/staticstring.cpp
parent74d4c8cd27334fc7204d5a8773deb3d424565778 (diff)
downloadlibbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.gz
libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.bz2
libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.tar.xz
libbu++-f4c20290509d7ed3a8fd5304577e7a4cc0b9d974.zip
Ok, no code is left in src, it's all in src/old. We'll gradually move code back
into src as it's fixed and re-org'd. This includes tests, which, I may write a unit test system into libbu++ just to make my life easier.
Diffstat (limited to 'src/old/staticstring.cpp')
-rw-r--r--src/old/staticstring.cpp282
1 files changed, 282 insertions, 0 deletions
diff --git a/src/old/staticstring.cpp b/src/old/staticstring.cpp
new file mode 100644
index 0000000..60f130f
--- /dev/null
+++ b/src/old/staticstring.cpp
@@ -0,0 +1,282 @@
1#include "staticstring.h"
2#include "serializer.h"
3
4#include <stdlib.h>
5#include <stdio.h>
6#include <string.h>
7
8StaticString::StaticString()
9{
10 lpStr = NULL;
11 nLen = 0;
12}
13
14StaticString::StaticString( int nLength )
15{
16 lpStr = new char[nLength+1];
17 nLen = nLength;
18 memset( lpStr, 0, nLength+1 );
19}
20
21StaticString::StaticString( const char *lpNewStr, int nNewLen )
22{
23 lpStr = NULL;
24 nLen = 0;
25 setString( lpNewStr, nNewLen );
26}
27
28StaticString::StaticString( const char *lpNewStr )
29{
30 lpStr = NULL;
31 nLen = 0;
32 setString( lpNewStr, -1 );
33}
34
35StaticString::StaticString( StaticString &xSrcStr, int nNewLen )
36{
37 lpStr = NULL;
38 nLen = 0;
39 setString( xSrcStr, nNewLen );
40}
41
42StaticString::StaticString( StaticString &xSrcStr )
43{
44 lpStr = NULL;
45 nLen = 0;
46 setString( xSrcStr, -1 );
47}
48
49StaticString::StaticString( const StaticString &xSrcStr )
50{
51 nLen = xSrcStr.getLength();
52 lpStr = new char[nLen];
53 memcpy( lpStr, xSrcStr.getString(), nLen );
54}
55
56StaticString::~StaticString()
57{
58 if( lpStr != NULL ) delete[] lpStr;
59}
60
61char *StaticString::getString()
62{
63 return lpStr;
64}
65
66const char *StaticString::getString() const
67{
68 return lpStr;
69}
70
71int StaticString::getLength() const
72{
73 return nLen;
74}
75
76void StaticString::setLength( int nNewLength )
77{
78 char *lpNewStr = new char[nNewLength+1];
79 if( lpStr != NULL )
80 {
81 strncpy( lpNewStr, lpStr, nNewLength );
82 }
83 lpNewStr[nNewLength] = '\0';
84 if( lpStr )
85 {
86 delete[] lpStr;
87 }
88 lpStr = lpNewStr;
89 nLen = nNewLength;
90}
91
92void StaticString::setString( const char *lpNewStr, int nNewLen )
93{
94 if( lpStr )
95 {
96 delete[] lpStr;
97 lpStr = NULL;
98 nLen = 0;
99 }
100 if( nNewLen < 0 )
101 {
102 if( lpNewStr == NULL ) return;
103 nLen = strlen( lpNewStr );
104 lpStr = new char[nLen+1];
105 strcpy( lpStr, lpNewStr );
106 }
107 else
108 {
109 nLen = nNewLen;
110 lpStr = new char[nLen+1];
111 memset( lpStr, 0, nLen+1 );
112 if( lpNewStr )
113 strncpy( lpStr, lpNewStr, nNewLen );
114 }
115}
116
117void StaticString::setString( StaticString &sNewStr, int nNewLen )
118{
119 if( lpStr )
120 {
121 delete[] lpStr;
122 lpStr = NULL;
123 nLen = 0;
124 }
125 if( nNewLen < 0 )
126 {
127 if( sNewStr.lpStr == NULL ) return;
128 nLen = sNewStr.nLen;
129 lpStr = new char[nLen+1];
130 strcpy( lpStr, sNewStr.lpStr );
131 }
132 else
133 {
134 nLen = nNewLen;
135 lpStr = new char[nLen+1];
136 memset( lpStr, 0, nLen+1 );
137 if( sNewStr.lpStr )
138 strncpy( lpStr, sNewStr.lpStr, nNewLen );
139 }
140}
141
142StaticString &StaticString::operator=( StaticString &lpOtherStr )
143{
144 setString( lpOtherStr );
145
146 return *this;
147}
148
149StaticString &StaticString::operator=( std::string &lpOtherStr )
150{
151 setString( lpOtherStr.c_str() );
152
153 return *this;
154}
155
156StaticString &StaticString::operator=( const char *lpNewStr )
157{
158 setString( lpNewStr );
159
160 return *this;
161}
162
163StaticString::operator const char *()
164{
165 return lpStr;
166}
167
168char StaticString::getAt( unsigned int nIndex )
169{
170 if( nIndex < 0 || nIndex >= nLen )
171 return '\0';
172
173 return lpStr[nIndex];
174}
175
176void StaticString::setAt( unsigned int nIndex, char cVal )
177{
178 if( nIndex < 0 || nIndex >= nLen )
179 return;
180
181 lpStr[nIndex] = cVal;
182}
183
184char &StaticString::operator[]( unsigned int nIndex )
185{
186 if( nIndex < 0 || nIndex >= nLen )
187 return lpStr[0];
188
189 return lpStr[nIndex];
190}
191
192StaticString::operator int()
193{
194 return nLen;
195}
196
197char *StaticString::operator+( int nAmnt )
198{
199 return lpStr + nAmnt;
200}
201
202char *StaticString::operator-( int nAmnt )
203{
204 return lpStr - nAmnt;
205}
206
207void StaticString::clear()
208{
209 memset( lpStr, 0, nLen+1 );
210}
211
212void StaticString::serialize( Serializer &ar )
213{
214 if( ar.isLoading() )
215 {
216 ar >> nLen;
217 setLength( nLen );
218 ar.read( lpStr, nLen );
219 }
220 else
221 {
222 ar << nLen;
223 ar.write( lpStr, nLen );
224 }
225}
226
227bool StaticString::operator==( const char *str )
228{
229 const char *a = str, *b = lpStr;
230 for(; *a == *b; a++, b++ ) if( *a == '\0' && *b == '\0' ) return true;
231 return false;
232}
233
234bool StaticString::operator==( StaticString &str )
235{
236 const char *a = str.lpStr, *b = lpStr;
237 for(; *a == *b; a++, b++ ) if( *a == '\0' && *b == '\0' ) return true;
238 return false;
239}
240
241bool StaticString::operator!=( const char *str )
242{
243 const char *a = str, *b = lpStr;
244 for(; *a == *b; a++, b++ ) if( *a == '\0' && *b == '\0' ) return false;
245 return true;
246}
247
248bool StaticString::operator!=( StaticString &str )
249{
250 const char *a = str.lpStr, *b = lpStr;
251 for(; *a == *b; a++, b++ ) if( *a == '\0' && *b == '\0' ) return false;
252 return true;
253}
254/*
255unsigned long int StaticString::getHashCode()
256{
257 unsigned long int nPos = nLen;
258 unsigned long int j = 0;
259 for( const char *s = (const char *)lpStr; j< nLen; s++, j++ )
260 {
261 nPos = *s + (nPos << 6) + (nPos << 16) - nPos;
262 }
263 return nPos;
264}
265
266bool StaticString::compareForHash( Hashable &other )
267{
268 if( ((StaticString &)other).nLen != nLen )
269 return false;
270
271 const char *a = ((StaticString &)other).lpStr;
272 const char *b = lpStr;
273 if( a == b )
274 return true;
275
276 for( unsigned long j = 0; j < nLen; j++, a++, b++ )
277 if( *a != *b )
278 return false;
279
280 return true;
281}
282*/