00001
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028
00029
00030
00031
00032
00033
00034
00035
00036 #pragma once
00037
00038 #include <iostream>
00039 #include <string>
00040 #include <sstream>
00041 #include <algorithm>
00042 using namespace std;
00043
00044 typedef std::string TString;
00045 typedef std::stringstream TStrStream;
00046
00054 class K3dToLower
00055 {
00056 public:
00057 char operator() ( char c ) const
00058 {
00059 return tolower ( c );
00060 }
00061 };
00069 class K3dString
00070 {
00071 TString m_str;
00072 bool m_bFoundToken;
00073 public:
00074 K3dString() {};
00075 K3dString ( const char * _str )
00076 {
00077 if ( _str != NULL )
00078 {
00079 m_str = _str;
00080 }
00081 else
00082 {
00083 cerr << "Error in K3dString() constructor -- *_str is NULL" << endl;
00084 m_str = "";
00085 }
00086 }
00087 K3dString ( const K3dString & _rStr )
00088 {
00089 *this = _rStr;
00090 }
00091 K3dString ( const TString & _rStr )
00092 {
00093 m_str = _rStr;
00094 }
00095 ~K3dString() {};
00096
00097 size_t Find ( const char *_str );
00098 size_t RFind ( const char *_str );
00099 TString& SetNum ( int _iVal );
00100 TString& SetNum ( bool _bVal );
00101 TString& SetNum ( unsigned int _uiVal );
00102 TString& SetNum ( float _fVal );
00103 K3dString& GetStrTok ( K3dString &_strOut,const K3dString &_strSign );
00104 K3dString &Left ( size_t _iPos );
00105 K3dString &Right ( size_t _iPos );
00106 TString & ToLower();
00107 void StrLwr ( const char * _str );
00108 void AddNumber ( const int _iVal );
00109
00110 bool &GetFoundToken()
00111 {
00112 return m_bFoundToken;
00113 }
00114
00117 void GetString ( const char * _strOut )
00118 {
00119 _strOut = m_str.c_str();
00120 }
00121
00123 int ToInt()
00124 {
00125 istringstream ss ( m_str );
00126 int i;
00127 ss >> i;
00128 return i;
00129 }
00130
00132 float ToFloat()
00133 {
00134 istringstream ss ( m_str );
00135 float f;
00136 ss >> f;
00137 return f;
00138 }
00139
00141 double ToDouble()
00142 {
00143 istringstream ss ( m_str );
00144 double d;
00145 ss >> d;
00146 return d;
00147 }
00148
00151 TString& GetString()
00152 {
00153 return m_str;
00154 }
00155
00158 const TString& GetString() const
00159 {
00160 return m_str;
00161 }
00162
00164 void SetString ( const char* _str )
00165 {
00166 m_str = _str;
00167 }
00168
00169 K3dString & operator= ( K3dString & _rStr )
00170 {
00171 m_str = _rStr.GetString();
00172 return *this;
00173 }
00174
00175 K3dString & operator= ( const K3dString & _rStr )
00176 {
00177 m_str = _rStr.GetString();
00178 return *this;
00179 }
00180
00181 K3dString & operator= ( const char * _str )
00182 {
00183 m_str = _str;
00184 return *this;
00185 }
00186
00187 K3dString & operator+= ( const K3dString & _rStr )
00188 {
00189 m_str = m_str + _rStr.GetString();
00190 return *this;
00191 }
00192
00193 K3dString & operator+= ( const char * _str )
00194 {
00195 m_str = m_str + _str;
00196 return *this;
00197 }
00198
00199 char & operator[] ( int _iPos )
00200 {
00201 return m_str[_iPos];
00202 }
00203
00204 bool operator!= ( const char * _str )
00205 {
00206 TString str = _str;
00207 if ( m_str != str )
00208 {
00209 return true;
00210 }
00211 else
00212 {
00213 return false;
00214 }
00215 }
00216
00217 bool operator!= ( const char * _str ) const
00218 {
00219 TString str = _str;
00220 if ( m_str != str )
00221 {
00222 return true;
00223 }
00224 else
00225 {
00226 return false;
00227 }
00228 }
00229
00230 bool operator!= ( const K3dString &_str )
00231 {
00232 TString str = _str.GetString( );
00233 if ( m_str != str )
00234 {
00235 return true;
00236 }
00237 else
00238 {
00239 return false;
00240 }
00241 }
00242
00243 bool operator== ( const char * _str )
00244 {
00245 TString str = _str;
00246 if ( m_str == str )
00247 {
00248 return true;
00249 }
00250 else
00251 {
00252 return false;
00253 }
00254 }
00255
00256 bool operator== ( const char * _str ) const
00257 {
00258 TString str = _str;
00259 if ( m_str == str )
00260 {
00261 return true;
00262 }
00263 else
00264 {
00265 return false;
00266 }
00267 }
00268
00269 bool operator== ( K3dString &_str )
00270 {
00271 TString str = _str.GetString();
00272 if ( m_str == str )
00273 {
00274 return true;
00275 }
00276 else
00277 {
00278 return false;
00279 }
00280 }
00281
00282 bool operator== ( K3dString &_str ) const
00283 {
00284 TString str = _str.GetString();
00285 if ( m_str == str )
00286 {
00287 return true;
00288 }
00289 else
00290 {
00291 return false;
00292 }
00293 }
00294
00295 void Erase ( size_t _uiPos, size_t _uiNumPos )
00296 {
00297 m_str.erase ( _uiPos, _uiNumPos );
00298 }
00299
00301 size_t GetSize()
00302 {
00303 return m_str.size();
00304 }
00305
00307 size_t GetLength()
00308 {
00309 return m_str.length();
00310 }
00311
00315 size_t Find ( const K3dString& _str )
00316 {
00317 return m_str.find ( _str.GetString() );
00318 }
00319
00321 void AddString ( char *_strOut, const unsigned int _iStrLen, const char *_strIn, const int _i )
00322 {
00323 snprintf ( _strOut, _iStrLen, "%s %i" , _strIn, _i );
00324 }
00325
00327 void AddString ( char *_strOut, const unsigned int _iStrLen, const char *_strIn, const int _iVal0, const int _iVal1 )
00328 {
00329 snprintf ( _strOut, _iStrLen, "%s %i, %i" , _strIn, _iVal0, _iVal1 );
00330 }
00331
00333 void AddString ( char *_strOut, const unsigned int _iStrLen, const char *_strIn, const char *_strAdd )
00334 {
00335 snprintf ( _strOut, _iStrLen, "%s %s" , _strIn, _strAdd );
00336 }
00337
00339 void AddString ( char *_strOut,const unsigned int _iStrLen, const char *_strIn, const float _f )
00340 {
00341 snprintf ( _strOut, _iStrLen, "%s %f" , _strIn, _f );
00342 }
00343
00345 void AddStringNumber ( char *_strOut, const unsigned int _iStrLen, const char *_strIn, const int _i )
00346 {
00347 snprintf ( _strOut, _iStrLen, "%s%i" , _strIn, _i );
00348 }
00349
00351 void Clear()
00352 {
00353 m_str.clear();
00354 }
00355 };
00356
00357 static K3dString g_K3dString;
00358
00360 inline const K3dString& operator+ ( const K3dString &_rstr1, const K3dString &_rstr2 )
00361 {
00362 TString str1 = _rstr1.GetString();
00363 TString str2 = _rstr2.GetString();
00364 TString strResult = str1 + str2;
00365 g_K3dString.GetString() = strResult;
00366 return g_K3dString;
00367 }
00368
00370 inline const K3dString& operator+ ( const char *_str1, const K3dString &_rstr2 )
00371 {
00372 K3dString t ( _str1 );
00373 t += _rstr2;
00374 g_K3dString.GetString() = t.GetString( );
00375 return g_K3dString;
00376 }
00377
00379 inline const K3dString& operator+ ( const K3dString &_rstr1, const char *_str2 )
00380 {
00381 TString str1 = _rstr1.GetString();
00382 TString str2 = _str2;
00383 TString strResult = str1 + str2;
00384 g_K3dString.GetString() = strResult;
00385 return g_K3dString;
00386 }
00387