00001 /* 00002 00003 Astring.h : aString class definition file. 00004 00005 Purpose: The aString class is a generic string class intended to be portable 00006 across operating environments. It provides generic string operations 00007 such as assignment, copy construction substring, concatenation, 00008 replacement, and character removal. This class is intended to be used 00009 as a component in the construction of filters, parsers etc. 00010 It is also intended as a base for more specialized string classes. 00011 00012 copyright (c) 1996 - 2001 00013 00014 David McCombs davidmc@newcottage.com 00015 00016 Nuclear Core Class Library 00017 00018 freely re-usable, redistributable. 00019 00020 */ 00021 00022 #ifndef ASTRING_H 00023 #define ASTRING_H 00024 00025 #include <string> 00026 #include "Vector.h" // for backtrack pointers 00027 00028 class aString 00029 { 00030 00031 public: 00032 // for trimming 00033 // 00034 enum end { left, right, both }; 00035 00036 // Returns the character that ended the last token operation. 00037 // 00038 inline char EndDelimiter( void ){ return endDelimiter; } 00039 char startDelimiter( void ); 00040 00041 00042 // default constructor 00043 // 00044 aString(); 00045 00046 // constructor using a passed char pointer 00047 // 00048 aString( const char* ); 00049 00050 // for better compatibility with STL string class 00051 // 00052 aString( const string & input ); 00053 00054 // copy constructor 00055 // 00056 aString( const aString & input ); 00057 00058 // destructor 00059 // 00060 ~aString(){ delete[]buffer; delete[]tokened; } 00061 00062 // method returns the buffer 00063 // 00064 const char * str() const { return buffer?buffer:""; } 00065 00066 // for better compatibility with STL string class 00067 // 00068 const char * c_str() const { return str(); } 00069 00070 // method returns the remainder of 00071 // the string undergoing token operations. 00072 // 00073 char * remainder() const { return tokenleft; } 00074 00075 // cast operator casts aString to char* 00076 // 00077 operator char * () const { return buffer; } 00078 00079 // for better compatibility with STL string class 00080 // 00081 operator string () const { return string(buffer); } 00082 00083 void attach( char * input ); 00084 00085 // overloaded assignment operators. 00086 // 00087 aString & operator = ( char * input ); 00088 aString & operator = ( const char * input ); 00089 aString & operator = ( const aString & input ); 00090 00091 // for better compatibility with STL string class 00092 // 00093 aString & operator = ( const string & input ); 00094 00095 00096 // subString methods 00097 // These methods perform the function of specialized assignment. 00098 // 00099 aString & subString( const char * input, int start ); 00100 aString & subString( const char * input, int start, int end ); 00101 00102 // operator performs string concatenation. 00103 // 00104 aString & operator += ( const char * input ); 00105 aString & operator += ( char * input ); 00106 aString & operator += ( char input ); 00107 00108 00109 // method that returns the size of the string (as set by new[]). 00110 // 00111 int Size( void ) const { return size; } 00112 int length( void ) const { return buffer?strlen( buffer ):0; } 00113 00114 // method sets the buffer to a new size. 00115 // 00116 aString & setSize( int newSize ); 00117 00118 // method replaces any chars in 'find' with the replacement char 'replace' 00119 // defaulted parameter provides for searching in alternate string, though 00120 // replacement still occurs in the internal buffer. 00121 // 00122 aString & replaceFoundWith( const char * find, const char replace, const char * search = NULL ); 00123 00124 // method replaces any match of 'find' with the replacement text 'replace' 00125 // 00126 aString & replaceFoundWith( const char * find, const char * replace ); 00127 00128 // method replaces the text at position 'pos' with text 'replace'. 00129 // 'pos' is zero based. i.e. the first character position is '0'. 00130 // 00131 aString & replaceAt( unsigned int pos, char * replace ); 00132 00133 // This method removes all characters found in char list 'find' 00134 // 00135 aString & removeChars( const char * find ); 00136 00137 // This method encapsulates buffer in 'boundary', 'boundary' by default. 00138 // if 'end' is specified, the buffer is encapsulated in 'boundary', 'end' 00139 // 00140 aString & encapsulateIn( const char * boundary, const char * end = NULL ); 00141 00142 // This method upper cases the first character of the string. 00143 // 00144 aString & upperS(void); 00145 00146 // This method lower cases the first character of the string. 00147 // 00148 aString & lowerS(void); 00149 00150 // This method upper cases the string. 00151 // 00152 aString & upper(void); 00153 00154 // This method lower cases the string. 00155 // 00156 aString & lower(void); 00157 00158 // This method trims spaces off of the end of the string. 00159 // 00160 aString & trim( aString::end end = right ); 00161 00162 // This method returns true if the 'input' string is found anywhere 00163 // in the buffer 00164 // 00165 bool matchIn( const char * input )const; 00166 00167 // return the point in the buffer where the input string is found 00168 char * find( const char * input ) const; 00169 00170 // This returns true if the buffer exactly matches the start of the 00171 // input string. 00172 // 00173 bool matchesStartOf( const char * input )const; 00174 00175 // This returns true if the input string exactly matches the start of the 00176 // buffer. 00177 // 00178 bool aString::startOfMatches( const char * input ) const; 00179 00180 // This method returns true only if the 'input' string is an exact match to 00181 // buffer. 00182 // 00183 bool match( const char * input )const; 00184 // associated comparison operators 00185 bool operator == ( aString & input ){ return match(input.str()); } 00186 bool operator == ( const char * input ){ return match(input); } 00187 00188 // Slide over the copy of the buffer, one 00189 // or more characters at a time, 00190 // returning the current position. 00191 // 00192 char * slide( int iAmount = 1 ); 00193 00194 // This method returns tokens until the end of the buffer is reached. 00195 // The 'separator' string holds a list of characters that the buffer is tokenized 00196 // around. The original buffer string is not changed by this method. 00197 // the token method should be called until it returns NULL unless tokenReset() 00198 // is called. 00199 // Also, each token should be used before the next invocation of token(). 00200 // previous tokens are invalid after calling token() for the current token. 00201 // 00202 char * token( const char * separator, aString * delimiterHolder = NULL ); 00203 00204 // This method resets the token buffer to NULL, insuring subsequent calls to 00205 // token() start at the beginning of the string. 00206 // 00207 void tokenReset( void ); 00208 00209 // indicates the position of the current valid token 00210 int tokenPosition( void ); 00211 00212 // indicates the position of the remainder of the token buffer 00213 int remainderPosition( void ); 00214 00215 00216 // returns the current valid token 00217 char * currentToken( void ); 00218 00219 // back the token string to the previous token 00220 char * backTrack( void ); 00221 00222 // returns true if the string evaluates to a number 00223 // ( decimal, with or without decimal point) 00224 bool isNumber( void ); 00225 00226 // the following method is an exclusive filter. 00227 // an input range is an overload of the 00228 // method. 00229 // 00230 aString & exclusive( char * input ); 00231 aString & exclusive( char low, char high ); 00232 00233 // This method converts the hexadecimal escaped string to the actual characters, 00234 // input is the starting token that signifies the begining of the hex escape. 00235 // two characters are read after the escape, since that is all that is needed to represent 00236 // a character in a hex number representation. 00237 // 00238 aString & deHexify( char hexToken ); 00239 00240 protected: 00241 00242 int oneToken( const char * separator, aString * delimiterHolder ); 00243 bool allocate( int sizeRequseted, bool bCopy = false ); 00244 00245 private: 00246 00247 char * buffer; // pointer to buffer 00248 char endDelimiter; // holds the ending delimiter of the last returned token. 00249 int size; // current size of buffer 00250 enum { incSize=512}; // increment size by this amount with each allocation 00251 char * tokened; // pointer to new[] allocated tokenized string. 00252 char * tokenleft; // string remaining after token operation. 00253 char * pchToken; // the current token 00254 pointerVector< char > allTokens; 00255 00256 }; 00257 00258 /* stream operator for aString class */ 00259 std::ostream & operator << (std::ostream & outStream, const aString & inString ); 00260 00261 00262 #endif
1.2.18