aboutsummaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorMike Buland <eichlan@xagasoft.com>2006-05-12 05:28:28 +0000
committerMike Buland <eichlan@xagasoft.com>2006-05-12 05:28:28 +0000
commit59cea3a2c49618ec7351028334d468a9204f4027 (patch)
treeed25306797b7549e14498eb2c1ee421d0d1e2e89 /src
parent45b0d6d941c95ab28a143b6a78ac5c2d99b6bc4f (diff)
downloadlibbu++-59cea3a2c49618ec7351028334d468a9204f4027.tar.gz
libbu++-59cea3a2c49618ec7351028334d468a9204f4027.tar.bz2
libbu++-59cea3a2c49618ec7351028334d468a9204f4027.tar.xz
libbu++-59cea3a2c49618ec7351028334d468a9204f4027.zip
Added the StaticString class, which is a slightly modified version of the old
sstring class, who's name is nonsense and now contains code specific to squirrelmud. Anyway, StaticString is more lightweight than the std::string class, but much less versitile. It really is just like a static string, but all nice and classey. Great for use with the HashTable.
Diffstat (limited to 'src')
-rw-r--r--src/staticstring.cpp184
-rw-r--r--src/staticstring.h48
2 files changed, 232 insertions, 0 deletions
diff --git a/src/staticstring.cpp b/src/staticstring.cpp
new file mode 100644
index 0000000..7543178
--- /dev/null
+++ b/src/staticstring.cpp
@@ -0,0 +1,184 @@
1#include "staticstring.h"
2
3#include <stdlib.h>
4#include <stdio.h>
5#include <string.h>
6
7StaticString::StaticString()
8{
9 lpStr = NULL;
10 nLen = 0;
11}
12
13StaticString::StaticString( int nLength )
14{
15 lpStr = new char[nLength+1];
16 nLen = nLength;
17 memset( lpStr, 0, nLength+1 );
18}
19
20StaticString::StaticString( const char *lpNewStr, int nNewLen )
21{
22 lpStr = NULL;
23 nLen = 0;
24 setString( lpNewStr, nNewLen );
25}
26
27StaticString::StaticString( StaticString &xSrcStr, int nNewLen )
28{
29 lpStr = NULL;
30 nLen = 0;
31 setString( xSrcStr, nNewLen );
32}
33
34StaticString::~StaticString()
35{
36 if( lpStr != NULL ) delete[] lpStr;
37}
38
39char *StaticString::getString()
40{
41 return lpStr;
42}
43
44int StaticString::getLength()
45{
46 return nLen;
47}
48
49int StaticString::setLength( int nNewLength )
50{
51 char *lpNewStr = new char[nNewLength+1];
52 if( lpStr != NULL )
53 {
54 strncpy( lpNewStr, lpStr, nNewLength );
55 }
56 lpNewStr[nNewLength] = '\0';
57 if( lpStr )
58 {
59 delete[] lpStr;
60 }
61 lpStr = lpNewStr;
62 nLen = nNewLength;
63}
64
65void StaticString::setString( const char *lpNewStr, int nNewLen )
66{
67 if( lpStr )
68 {
69 delete[] lpStr;
70 lpStr = NULL;
71 nLen = 0;
72 }
73 if( nNewLen < 0 )
74 {
75 if( lpNewStr == NULL ) return;
76 nLen = strlen( lpNewStr );
77 lpStr = new char[nLen+1];
78 strcpy( lpStr, lpNewStr );
79 }
80 else
81 {
82 nLen = nNewLen;
83 lpStr = new char[nLen+1];
84 memset( lpStr, 0, nLen+1 );
85 if( lpNewStr )
86 strncpy( lpStr, lpNewStr, nNewLen );
87 }
88}
89
90void StaticString::setString( StaticString &sNewStr, int nNewLen )
91{
92 if( lpStr )
93 {
94 delete[] lpStr;
95 lpStr = NULL;
96 nLen = 0;
97 }
98 if( nNewLen < 0 )
99 {
100 if( sNewStr.lpStr == NULL ) return;
101 nLen = sNewStr.nLen;
102 lpStr = new char[nLen+1];
103 strcpy( lpStr, sNewStr.lpStr );
104 }
105 else
106 {
107 nLen = nNewLen;
108 lpStr = new char[nLen+1];
109 memset( lpStr, 0, nLen+1 );
110 if( sNewStr.lpStr )
111 strncpy( lpStr, sNewStr.lpStr, nNewLen );
112 }
113}
114
115StaticString &StaticString::operator=( StaticString &lpOtherStr )
116{
117 setString( lpOtherStr );
118
119 return *this;
120}
121
122StaticString &StaticString::operator=( std::string &lpOtherStr )
123{
124 setString( lpOtherStr.c_str() );
125
126 return *this;
127}
128
129StaticString &StaticString::operator=( const char *lpNewStr )
130{
131 setString( lpNewStr );
132
133 return *this;
134}
135
136StaticString::operator const char *()
137{
138 return lpStr;
139}
140
141char StaticString::getAt( int nIndex )
142{
143 if( nIndex < 0 || nIndex >= nLen )
144 return '\0';
145
146 return lpStr[nIndex];
147}
148
149void StaticString::setAt( int nIndex, char cVal )
150{
151 if( nIndex < 0 || nIndex >= nLen )
152 return;
153
154 lpStr[nIndex] = cVal;
155}
156
157char &StaticString::operator[]( int nIndex )
158{
159 if( nIndex < 0 || nIndex >= nLen )
160 return lpStr[0];
161
162 return lpStr[nIndex];
163}
164
165StaticString::operator int()
166{
167 return nLen;
168}
169
170char *StaticString::operator+( int nAmnt )
171{
172 return lpStr + nAmnt;
173}
174
175char *StaticString::operator-( int nAmnt )
176{
177 return lpStr - nAmnt;
178}
179
180void StaticString::clear()
181{
182 memset( lpStr, 0, nLen+1 );
183}
184
diff --git a/src/staticstring.h b/src/staticstring.h
new file mode 100644
index 0000000..88cae70
--- /dev/null
+++ b/src/staticstring.h
@@ -0,0 +1,48 @@
1#ifndef STATIC_STRING_H
2#define STATIC_STRING_H
3
4#include <string>
5
6/**
7 * Simple string managing class. Allows for dynamically allocated string data
8 * along with some minor caching to speed things up when accessing your
9 * string data. Really designed for making copies of strings easy and safe,
10 * and making accessing meta-info like length fast and reliable as well.
11 *@author Mike Buland
12 */
13class StaticString
14{
15public:
16 StaticString();
17 StaticString( const char *lpNewStr, int nNewLen=-1 );
18 StaticString( StaticString &xSrcStr, int nNewLen=-1 );
19 StaticString( int nLength );
20 ~StaticString();
21
22 char *getString();
23 int getLength();
24 int setLength( int nNewLength );
25
26 void setString( const char *lpNewStr, int nNewLen=-1 );
27 void setString( StaticString &sNewStr, int nNewLen=-1 );
28
29 char getAt( int nIndex );
30 void setAt( int nIndex, char cVal );
31
32 class StaticString &operator=( class StaticString &lpOtherStr );
33 class StaticString &operator=( std::string &lpOtherStr );
34 class StaticString &operator=( const char *lpNewStr );
35 operator const char *();
36 char &operator[]( int nIndex );
37 operator int();
38 char *operator+( int nAmnt );
39 char *operator-( int nAmnt );
40
41 void clear();
42
43private:
44 char *lpStr;
45 int nLen;
46};
47
48#endif