cstring.h
Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036
00037
00038
00039
00040
00041
00042
00043
00044
00045
00046
00047
00048 #ifndef _string_hpp
00049 #define _string_hpp
00050
00051 #include <string.h>
00052 #include <assert.h>
00053 #include <iostream.h>
00054
00055 #define VALID(x) assert(x)
00056 #define MAX_STRING_LEN 2000
00057 #define CS_TRUE 1
00058 #define CS_FALSE 0
00059
00060 #pragma interface
00061
00062 typedef int CS_BOOL;
00063
00064 class Cstring
00065 {
00066 public:
00067 friend ostream &operator << (ostream& os, const Cstring& S) {
00068 if (!S.len)
00069 os << "<empty>";
00070 else
00071 os << S.str;
00072 return os;
00073 }
00074
00075
00076
00077
00078 Cstring(int i, char *use_me) :len(0), str((char*) 0) {use(use_me);}
00079 Cstring() : len(0), str((char*) 0) { ; }
00080 Cstring(const char *copy_me) : len(0), str((char*) 0) {set(copy_me);}
00081 Cstring(const Cstring& S) : len(0), str((char*) 0) {set(S);}
00082 ~Cstring() {destroy();}
00083 void destroy() {if (str) delete [] str; len=0; str= (char*) 0;}
00084
00085 CS_BOOL extend (const char *);
00086 CS_BOOL extend (Cstring &S) {return (extend(S.str));}
00087 Cstring operator+ ( const Cstring& S) const;
00088 Cstring operator+ ( const char *s) const;
00089 Cstring& operator= ( const Cstring& S);
00090 Cstring& operator= ( const char *c);
00091 CS_BOOL operator== (const Cstring& S) const { return (*this == S.str);}
00092 CS_BOOL operator== (const char * s) const;
00093 CS_BOOL operator< (const char * s) const;
00094 CS_BOOL operator< (const Cstring& S) const {return ((*this) < S.str);}
00095 CS_BOOL operator> (const char *s) const;
00096 CS_BOOL operator> (const Cstring& S) const {return ((*this) > S.str);}
00097
00098 const char *get() const {return (const char*) str;}
00099 char *get_copy() const;
00100 int getLen() const {return len;}
00101
00102 void set(const Cstring& S);
00103 void set(const char *s);
00104
00105
00106 void use(char *s);
00107
00108 int invariant()
00109 {return (((len>=0) && str) || (!len && !str));}
00110
00111 int empty() {return (len == 0);}
00112
00113 protected:
00114 int len;
00115 char *str;
00116 int prefix_compare (const char *s, int &slen) const;
00117 int prefix_compare (const Cstring& S, int &slen) const
00118 {return prefix_compare(S.str, slen);}
00119 };
00120
00121 void
00122 Cstring::use (char *use_me)
00123 {
00124 if (str)
00125 delete [] str;
00126 str = (char *) 0;
00127
00128 if (!use_me) {
00129 len = 0;
00130 return;
00131 } else {
00132 len = strlen(use_me);
00133 if (len >= 0) {
00134 str = use_me;
00135 return;
00136 } else {
00137 len = 0;
00138 return;
00139 }
00140 }
00141 }
00142
00143 void Cstring::set(const Cstring& S)
00144 {
00145 set(S.str);
00146 }
00147
00148 void
00149 Cstring::set (const char *s)
00150 {
00151 if (str)
00152 delete [] str;
00153 str = (char*) 0;
00154
00155 if (!s) {
00156 len = 0;
00157 return;
00158 }
00159
00160 len = strlen(s);
00161 if (len >= 0) {
00162 str = new char [len + 1];
00163 if (str)
00164 strcpy (str, s);
00165 else
00166 len = 0;
00167 } else
00168 len = 0;
00169
00170 VALID(invariant());
00171 }
00172
00173 int Cstring::operator==(const char *s) const
00174 {
00175 if ((len == strlen(s)) &&
00176 ((!len) || (!strcmp(str, s))))
00177 return CS_TRUE;
00178 else
00179 return 0;
00180 }
00181
00182
00183
00184
00185
00186 int Cstring::prefix_compare(const char *s, int &slen) const
00187 {
00188 if (!len || !s)
00189 return -2;
00190
00191 slen = strlen(s);
00192 return (strncmp(str, s, slen > len ? len : slen));
00193 }
00194
00195 Cstring Cstring::operator+ ( const Cstring& S) const return Ret;
00196 {
00197 Ret = (*this + S.str);
00198 return Ret;
00199 }
00200
00201 Cstring Cstring::operator + (const char *s) const return Ret;
00202 {
00203 Ret = (*this);
00204 Ret.extend(s);
00205 return Ret;
00206 }
00207
00208 CS_BOOL Cstring::extend (const char *s)
00209 {
00210 char *new_str;
00211 int new_len;
00212
00213 if (s) {
00214 if (new_len = strlen(s)) {
00215 if (new_str = new char[len + new_len + 1]) {
00216 if (strcpy(new_str, str) && strcat(new_str, s)) {
00217 if (str)
00218 delete (str);
00219 len += new_len;
00220 str = new_str;
00221 return (CS_TRUE);
00222 }
00223 }
00224 }
00225 } else
00226 return (CS_TRUE);
00227
00228 return (CS_FALSE);
00229 }
00230
00231 Cstring& Cstring::operator= ( const Cstring& S)
00232 {
00233 if (this == &S)
00234 return (*this);
00235 else {
00236 set(S);
00237 return (*this);
00238 }
00239 }
00240
00241 Cstring& Cstring::operator= ( const char *c)
00242 {
00243 if (str == c)
00244 return (*this);
00245 else {
00246 set(c);
00247 return (*this);
00248 }
00249 }
00250
00251 CS_BOOL Cstring::operator< (const char * s) const {
00252 int slen, res;
00253
00254 if ((res = prefix_compare(s, slen)) == -2)
00255 return CS_FALSE;
00256 else if (res < 0)
00257 return CS_TRUE;
00258 else if (res > 0)
00259 return CS_FALSE;
00260 else
00261 return (len < slen);
00262 }
00263
00264 CS_BOOL Cstring::operator> (const char *s) const {
00265 int slen, res;
00266
00267 if ((res = prefix_compare(s, slen)) == -2)
00268 return CS_TRUE;
00269 else if (res < 0)
00270 return (len > slen);
00271 else if (res > 0)
00272 return CS_TRUE;
00273 else
00274 return (len > slen);
00275 }
00276
00277 char *Cstring::get_copy() const
00278 {
00279 char *ret= (char*) 0;
00280 if (str)
00281 ret = strdup(str);
00282 return ret;
00283 }
00284
00285
00286 #endif
00287
00288
00289
00290