diff options
Diffstat (limited to 'src/old/staticstring.cpp')
-rw-r--r-- | src/old/staticstring.cpp | 282 |
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 | |||
8 | StaticString::StaticString() | ||
9 | { | ||
10 | lpStr = NULL; | ||
11 | nLen = 0; | ||
12 | } | ||
13 | |||
14 | StaticString::StaticString( int nLength ) | ||
15 | { | ||
16 | lpStr = new char[nLength+1]; | ||
17 | nLen = nLength; | ||
18 | memset( lpStr, 0, nLength+1 ); | ||
19 | } | ||
20 | |||
21 | StaticString::StaticString( const char *lpNewStr, int nNewLen ) | ||
22 | { | ||
23 | lpStr = NULL; | ||
24 | nLen = 0; | ||
25 | setString( lpNewStr, nNewLen ); | ||
26 | } | ||
27 | |||
28 | StaticString::StaticString( const char *lpNewStr ) | ||
29 | { | ||
30 | lpStr = NULL; | ||
31 | nLen = 0; | ||
32 | setString( lpNewStr, -1 ); | ||
33 | } | ||
34 | |||
35 | StaticString::StaticString( StaticString &xSrcStr, int nNewLen ) | ||
36 | { | ||
37 | lpStr = NULL; | ||
38 | nLen = 0; | ||
39 | setString( xSrcStr, nNewLen ); | ||
40 | } | ||
41 | |||
42 | StaticString::StaticString( StaticString &xSrcStr ) | ||
43 | { | ||
44 | lpStr = NULL; | ||
45 | nLen = 0; | ||
46 | setString( xSrcStr, -1 ); | ||
47 | } | ||
48 | |||
49 | StaticString::StaticString( const StaticString &xSrcStr ) | ||
50 | { | ||
51 | nLen = xSrcStr.getLength(); | ||
52 | lpStr = new char[nLen]; | ||
53 | memcpy( lpStr, xSrcStr.getString(), nLen ); | ||
54 | } | ||
55 | |||
56 | StaticString::~StaticString() | ||
57 | { | ||
58 | if( lpStr != NULL ) delete[] lpStr; | ||
59 | } | ||
60 | |||
61 | char *StaticString::getString() | ||
62 | { | ||
63 | return lpStr; | ||
64 | } | ||
65 | |||
66 | const char *StaticString::getString() const | ||
67 | { | ||
68 | return lpStr; | ||
69 | } | ||
70 | |||
71 | int StaticString::getLength() const | ||
72 | { | ||
73 | return nLen; | ||
74 | } | ||
75 | |||
76 | void 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 | |||
92 | void 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 | |||
117 | void 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 | |||
142 | StaticString &StaticString::operator=( StaticString &lpOtherStr ) | ||
143 | { | ||
144 | setString( lpOtherStr ); | ||
145 | |||
146 | return *this; | ||
147 | } | ||
148 | |||
149 | StaticString &StaticString::operator=( std::string &lpOtherStr ) | ||
150 | { | ||
151 | setString( lpOtherStr.c_str() ); | ||
152 | |||
153 | return *this; | ||
154 | } | ||
155 | |||
156 | StaticString &StaticString::operator=( const char *lpNewStr ) | ||
157 | { | ||
158 | setString( lpNewStr ); | ||
159 | |||
160 | return *this; | ||
161 | } | ||
162 | |||
163 | StaticString::operator const char *() | ||
164 | { | ||
165 | return lpStr; | ||
166 | } | ||
167 | |||
168 | char StaticString::getAt( unsigned int nIndex ) | ||
169 | { | ||
170 | if( nIndex < 0 || nIndex >= nLen ) | ||
171 | return '\0'; | ||
172 | |||
173 | return lpStr[nIndex]; | ||
174 | } | ||
175 | |||
176 | void StaticString::setAt( unsigned int nIndex, char cVal ) | ||
177 | { | ||
178 | if( nIndex < 0 || nIndex >= nLen ) | ||
179 | return; | ||
180 | |||
181 | lpStr[nIndex] = cVal; | ||
182 | } | ||
183 | |||
184 | char &StaticString::operator[]( unsigned int nIndex ) | ||
185 | { | ||
186 | if( nIndex < 0 || nIndex >= nLen ) | ||
187 | return lpStr[0]; | ||
188 | |||
189 | return lpStr[nIndex]; | ||
190 | } | ||
191 | |||
192 | StaticString::operator int() | ||
193 | { | ||
194 | return nLen; | ||
195 | } | ||
196 | |||
197 | char *StaticString::operator+( int nAmnt ) | ||
198 | { | ||
199 | return lpStr + nAmnt; | ||
200 | } | ||
201 | |||
202 | char *StaticString::operator-( int nAmnt ) | ||
203 | { | ||
204 | return lpStr - nAmnt; | ||
205 | } | ||
206 | |||
207 | void StaticString::clear() | ||
208 | { | ||
209 | memset( lpStr, 0, nLen+1 ); | ||
210 | } | ||
211 | |||
212 | void 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 | |||
227 | bool 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 | |||
234 | bool 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 | |||
241 | bool 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 | |||
248 | bool 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 | /* | ||
255 | unsigned 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 | |||
266 | bool 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 | */ | ||