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 #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 
00042 
00043 
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 
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     
00075     
00076 
00077     
00078     
00079     WRAP_OPERATOR_ALLTYPES(+)              
00080     WRAP_MUTATING_OPERATOR_ALLTYPES(+=)    
00081     WRAP_MUTATING_OPERATOR(=,CHAR const *) 
00082     WRAP_MUTATING_OPERATOR(=,BASE const &) 
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     
00145     
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     
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     
00163     int Compare( const CHAR* other ) const;                          
00164     int Compare( const CHAR* other, bool ignoreCase ) const; 
00165 
00166     
00167     operator const CHAR*() const;
00168 
00169     int Count(CHAR what) const; 
00170     int LongestLine() const; 
00171 
00172     
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 *)    
00206     WRAP_MUTATING_OPERATOR(=,BASE const &)    
00207     WRAP_MUTATING_OPERATOR(=,tOutput const &) 
00208 
00209     static tString RemoveColors( const CHAR *c );           
00210     void SetPos( int len, bool cut=false );                 
00211 
00212     void RemoveTrailingColor();                             
00213 
00214     
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     
00251     static_cast< tString& >( s ) << c;
00252 
00253     
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 
00266 
00267 
00273 
00274 
00275 template< class T >
00276 bool tString::Convert( T & target, size_type startPos ) const
00277 {
00278     
00279     std::istringstream s(*this);
00280     s.seekg(startPos);
00281 
00282     
00283     s >> target;
00284 
00285     
00286     return !s.fail() && s.eof() || isspace(s.get());
00287 }
00288 #endif
00289 
00290 #endif
00291 
00292