src/tools/tString.h

Go to the documentation of this file.
00001 
00002 /*
00003 
00004 *************************************************************************
00005 
00006 ArmageTron -- Just another Tron Lightcycle Game in 3D.
00007 Copyright (C) 2000  Manuel Moos (manuel@moosnet.de)
00008 
00009 **************************************************************************
00010 
00011 This program is free software; you can redistribute it and/or
00012 modify it under the terms of the GNU General Public License
00013 as published by the Free Software Foundation; either version 2
00014 of the License, or (at your option) any later version.
00015 
00016 This program is distributed in the hope that it will be useful,
00017 but WITHOUT ANY WARRANTY; without even the implied warranty of
00018 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00019 GNU General Public License for more details.
00020 
00021 You should have received a copy of the GNU General Public License
00022 along with this program; if not, write to the Free Software
00023 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301, USA.
00024 
00025 ***************************************************************************
00026 
00027 */
00028 
00029 #ifndef ArmageTron_STRING_H
00030 #define ArmageTron_STRING_H
00031 
00032 #include "aa_config.h"
00033 
00034 #include "defs.h"
00035 
00036 #include <string>
00037 #include <sstream>
00038 #include <iostream>
00039 #include <iosfwd>
00040 
00041 // macros that help wrapping operators defined for a base class, they probably
00042 // should be refactored into another header. They assume THISCLASS is a typedef to the
00043 // class just being defined and BASE the base class.
00044 
00046 #define WRAP_MUTATING_OPERATOR(op, T) inline THISCLASS & operator op( T t ){ static_cast< BASE & >( *this ) op t; return *this; }
00048 #define WRAP_MUTATING_OPERATOR_ALLTYPES(op) template< class T > WRAP_MUTATING_OPERATOR(op, T const &)
00049 
00051 #define WRAP_OPERATOR(op, T) inline THISCLASS operator op( T t ) const { return THISCLASS( static_cast< const BASE & >( *this ) op  t ); }
00053 #define WRAP_OPERATOR_ALLTYPES(op) template< class T > WRAP_OPERATOR(op, T const &)
00054 
00055 //typedef tArray<char> string;
00056 class tOutput;
00057 
00058 using std::string;
00059 
00061 class tString:public string{
00062 private:
00063 public:
00064     typedef tString          THISCLASS; 
00065     typedef std::string      BASE;      
00066     typedef BASE::value_type CHAR;      
00067 
00068     tString();                         
00069     tString(const BASE &);             
00070     tString(const tString &);          
00071     explicit tString(const CHAR *);    
00072     template<typename T>
00073     tString(T begin, T end) : string(begin,end) {} 
00074     // explicit tString(const char *);    //!< conversion from C string
00075     // explicit tString(const tOutput &); //!< conversion from output
00076 
00077     // wrap base operators so they have the correct type
00078     // if you end up here in the debugger, don't bother to look up
00079     WRAP_OPERATOR_ALLTYPES(+)              // concatenation
00080     WRAP_MUTATING_OPERATOR_ALLTYPES(+=)    // appending
00081     WRAP_MUTATING_OPERATOR(=,CHAR const *) // assignment of C string
00082     WRAP_MUTATING_OPERATOR(=,BASE const &) // assignment from C++ string
00083 
00084     tString & operator =( tOutput const & other ); 
00085 
00086     size_type Size() const;                        
00087 
00089 #ifdef _MSC_VER
00090     bool Convert( int & target, size_type startPos = 0 ) const;
00091     bool Convert( REAL & target, size_type startPos = 0 ) const;
00092 #else
00093     template < class T > bool Convert( T & target, size_type startPos = 0 ) const;
00094 #endif
00095 
00096     void ReadLine(std::istream &s, bool enableEscapeSequences=false); 
00097 
00098     void Clear();                       
00099     void SetPos( int len, bool cut );   
00100 
00101     bool StartsWith( const tString & other ) const; 
00102     bool StartsWith( const CHAR * other ) const;    
00103 
00104     bool EndsWith( const tString & other) const;    
00105     bool EndsWith( const CHAR* other) const;        
00106 
00107     tString LTrim() const;                          
00108     tString RTrim() const;                          
00109     tString Trim() const;                           
00110 
00111     int StrPos( const tString &tofind ) const;      
00112     int StrPos( int start, const tString &tofind ) const; 
00113     int StrPos( const CHAR * tofind ) const;        
00114     int StrPos( int start, const CHAR * tofind ) const; 
00115 
00116     tString SubStr( int start, int len) const;      
00117     tString SubStr( int start ) const;              
00118 
00119     int ToInt( size_type pos = 0 ) const;           
00120     REAL ToFloat( size_type pos = 0 ) const;        
00121 
00123     static int CompareAlphaNumerical( const tString& a, const tString &b);
00124 
00126     tString StripWhitespace( void ) const;
00127 
00129     tString ToLower(void) const;
00131     tString ToUpper(void) const;
00132 
00133     int PosWordRight(int start) const;          
00134     int PosWordLeft(int start) const;           
00135     int RemoveWordRight(int start);             
00136     int RemoveWordLeft(int start);              
00137     void RemoveSubStr(int start, int length);   
00138     tString GetFileMimeExtension() const;       
00139     tString Reverse() const;                    
00140 
00141     tString Truncate( int truncateAt ) const;   
00142 
00143 
00144     // TO BE REPLACED
00145     // auto-expanding subscription operators, will become non-expanding
00146     CHAR operator[]( size_t i ) const;
00147     CHAR & operator[]( size_t i );
00148     CHAR operator[]( int i ) const;
00149     CHAR & operator[]( int i );
00150 
00151     void SetSize( unsigned int size ); 
00152 
00153 #ifdef NOLEGACY
00154 private:
00155 #endif
00156     // non-expanding subscription operators, will disappear
00157     CHAR operator()( size_t i ) const;
00158     CHAR & operator()( size_t i );
00159     CHAR operator()( int i ) const;
00160     CHAR & operator()( int i );
00161 
00162     // comparison operators
00163     int Compare( const CHAR* other ) const;                          
00164     int Compare( const CHAR* other, bool ignoreCase ) const; 
00165 
00166     // automatic conversion will go away, it's dangerous
00167     operator const CHAR*() const;
00168 
00169     int Count(CHAR what) const; 
00170     int LongestLine() const; 
00171 
00172     // LEGACY FUNCTIONS, DON'T USE IN NEW CODE
00173     int Len() const; 
00174 
00175     void SetLen( int len ); 
00176 
00177     void NetFilter();                           
00178 };
00179 
00181 struct tColoredStringProxy
00182 {
00183     REAL r_, g_, b_;
00184 
00185     tColoredStringProxy( REAL r, REAL g, REAL b )
00186             : r_(r), g_(g), b_(b)
00187     {}
00188 };
00189 
00191 class tColoredString: public tString
00192 {
00193 public:
00194     typedef tColoredString   THISCLASS; 
00195     typedef tString          BASE;      
00196 
00197     ~tColoredString();                                      
00198     tColoredString();                                       
00199     tColoredString( const tColoredString& other );          
00200     explicit tColoredString( const tString& other );        
00201     explicit tColoredString( const CHAR * other );          
00202     explicit tColoredString( const tOutput & other );       
00203 
00205     WRAP_MUTATING_OPERATOR(=,CHAR const *)    // assignment of C string
00206     WRAP_MUTATING_OPERATOR(=,BASE const &)    // assignment from C++ string
00207     WRAP_MUTATING_OPERATOR(=,tOutput const &) // assignment from output collector
00208 
00209     static tString RemoveColors( const CHAR *c );           
00210     void SetPos( int len, bool cut=false );                 
00211 
00212     void RemoveTrailingColor();                             
00213 
00214     // void RemoveHex();                                       //!< ?
00215 
00216     int LongestLine() const;
00217 
00219     inline static tColoredStringProxy ColorString( REAL r,REAL g,REAL b )
00220     {
00221         return tColoredStringProxy( r, g, b );
00222     }
00223 };
00224 
00226 bool tIsInList( tString const & list, tString const & item );
00227 
00229 void tToLower( tString & toTransform );
00230 
00232 void tToUpper( tString & toTransform );
00233 
00235 template<class T> tString & operator <<(tString &s,const T &c)
00236 {
00237     std::ostringstream S;
00238 
00239     S << c;
00240 
00241     return s += S.str();
00242 }
00243 
00245 tColoredString & operator <<(tColoredString &s, const tColoredStringProxy &colorCode );
00246 
00248 template<class T> tColoredString & operator <<(tColoredString &s,const T &c)
00249 {
00250     // delegate to basic string function...
00251     static_cast< tString& >( s ) << c;
00252 
00253     // but don't allow overhanging color codes
00254     s.RemoveTrailingColor();
00255 
00256     return s;
00257 }
00258 
00260 std::istream & operator>> (std::istream &s,tString &x);
00261 
00262 #ifndef _MSC_VER
00263 // *******************************************************************************
00264 // *
00265 // *    Convert
00266 // *
00267 // *******************************************************************************
00273 // *******************************************************************************
00274 
00275 template< class T >
00276 bool tString::Convert( T & target, size_type startPos ) const
00277 {
00278     // generate string stream and advance it to the start position
00279     std::istringstream s(*this);
00280     s.seekg(startPos);
00281 
00282     // read it
00283     s >> target;
00284 
00285     // return failure condition
00286     return !s.fail() && s.eof() || isspace(s.get());
00287 }
00288 #endif
00289 
00290 #endif
00291 
00292 

Generated on Sat Mar 15 22:56:02 2008 for Armagetron Advanced by  doxygen 1.5.4