00001 00016 /*************************************************************************** 00017 * Copyright (C) 2007 by Jan Koci * 00018 * honza.koci@email.cz * 00019 * http://kengine.sourceforge.net/tutorial/ 00020 * * 00021 * This program is free software; you can redistribute it and/or modify * 00022 * it under the terms of the GNU General Public License as published by * 00023 * the Free Software Foundation; either version 2 of the License, or * 00024 * (at your option) any later version. * 00025 * * 00026 * This program is distributed in the hope that it will be useful, * 00027 * but WITHOUT ANY WARRANTY; without even the implied warranty of * 00028 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * 00029 * GNU General Public License for more details. * 00030 * * 00031 * You should have received a copy of the GNU General Public License * 00032 * along with this program; if not, write to the * 00033 * Free Software Foundation, Inc., * 00034 * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. * 00035 ***************************************************************************/ 00036 00037 00038 #pragma once 00039 00040 #include <iostream> 00041 #include <string> 00042 #include <sstream> 00043 #include <algorithm> 00044 using namespace std; 00045 00046 namespace K3d 00047 { 00048 typedef std::string TString; 00049 typedef std::stringstream TStrStream; 00050 00058 class K3dToLower 00059 { 00060 public: 00061 char operator() (char c) const 00062 { 00063 return tolower(c); 00064 } 00065 }; 00066 00074 class K3dString 00075 { 00076 TString m_str; 00077 bool m_bFoundToken; 00078 public: 00079 K3dString() 00080 { 00081 }; 00082 K3dString( const char * _str ) 00083 { 00084 if(_str != NULL) 00085 { 00086 m_str = _str; 00087 } 00088 else 00089 { 00090 cerr << "Error in K3dString() constructor -- *_str is NULL" << endl; 00091 m_str = ""; 00092 } 00093 } 00094 K3dString ( const K3dString & _rStr ) 00095 { 00096 *this = _rStr; 00097 } 00098 K3dString ( const TString & _rStr ) 00099 { 00100 m_str = _rStr; 00101 } 00102 ~K3dString() 00103 { 00104 m_str.clear(); 00105 }; 00106 00107 size_t Find(const char *_str); 00108 size_t RFind(const char *_str); 00109 TString& SetNum ( int _iVal); 00110 TString& SetNum ( bool _bVal); 00111 TString& SetNum ( unsigned int _uiVal); 00112 TString& SetNum ( float _fVal); 00113 K3dString& GetStrTok( K3dString &_strOut,const K3dString &_strSign); 00114 K3dString &Left(size_t _iPos); 00115 K3dString &Right(size_t _iPos); 00116 TString & ToLower(); 00117 void StrLwr(const char * _str); 00118 void AddNumber(const int _iVal); 00119 00120 bool &GetFoundToken() 00121 { 00122 return m_bFoundToken; 00123 } 00124 00127 void GetString(const char * _strOut) 00128 { 00129 _strOut = m_str.c_str(); 00130 } 00131 00133 int ToInt() 00134 { 00135 istringstream ss(m_str); 00136 int i; 00137 ss >> i; 00138 return i; 00139 } 00140 00142 float ToFloat() 00143 { 00144 istringstream ss(m_str); 00145 float f; 00146 ss >> f; 00147 return f; 00148 } 00149 00151 double ToDouble() 00152 { 00153 istringstream ss(m_str); 00154 double d; 00155 ss >> d; 00156 return d; 00157 } 00158 00161 TString& GetString() 00162 { 00163 return m_str; 00164 } 00165 00168 const TString& GetString() const 00169 { 00170 return m_str; 00171 } 00172 00174 void SetString(const char* _str) 00175 { 00176 m_str = _str; 00177 } 00178 00179 K3dString & operator= ( K3dString & _rStr ) 00180 { 00181 m_str = _rStr.GetString(); 00182 return *this; 00183 } 00184 00185 K3dString & operator= (const K3dString & _rStr ) 00186 { 00187 m_str = _rStr.GetString(); 00188 return *this; 00189 } 00190 00191 K3dString & operator= ( const char * _str ) 00192 { 00193 m_str = _str; 00194 return *this; 00195 } 00196 00197 K3dString & operator+= ( const K3dString & _rStr ) 00198 { 00199 m_str = m_str + _rStr.GetString(); 00200 return *this; 00201 } 00202 00203 K3dString & operator+= ( const char * _str ) 00204 { 00205 m_str = m_str + _str; 00206 return *this; 00207 } 00208 00209 char & operator[] ( int _iPos ) 00210 { 00211 return m_str[_iPos]; 00212 } 00213 00214 bool operator!= ( const char * _str ) 00215 { 00216 TString str = _str; 00217 if(m_str != str) 00218 { 00219 return true; 00220 } 00221 else 00222 { 00223 return false; 00224 } 00225 } 00226 00227 bool operator!= ( const char * _str ) const 00228 { 00229 TString str = _str; 00230 if(m_str != str) 00231 { 00232 return true; 00233 } 00234 else 00235 { 00236 return false; 00237 } 00238 } 00239 00240 bool operator!= ( const K3dString &_str ) 00241 { 00242 TString str = _str.GetString( ); 00243 if(m_str != str) 00244 { 00245 return true; 00246 } 00247 else 00248 { 00249 return false; 00250 } 00251 } 00252 00253 bool operator== ( const char * _str ) 00254 { 00255 TString str = _str; 00256 if(m_str == str) 00257 { 00258 return true; 00259 } 00260 else 00261 { 00262 return false; 00263 } 00264 } 00265 00266 bool operator== ( const char * _str ) const 00267 { 00268 TString str = _str; 00269 if(m_str == str) 00270 { 00271 return true; 00272 } 00273 else 00274 { 00275 return false; 00276 } 00277 } 00278 00279 bool operator== ( K3dString &_str ) 00280 { 00281 TString str = _str.GetString(); 00282 if(m_str == str) 00283 { 00284 return true; 00285 } 00286 else 00287 { 00288 return false; 00289 } 00290 } 00291 00292 bool operator== (K3dString &_str ) const 00293 { 00294 TString str = _str.GetString(); 00295 if(m_str == str) 00296 { 00297 return true; 00298 } 00299 else 00300 { 00301 return false; 00302 } 00303 } 00304 00305 void Erase(size_t _uiPos, size_t _uiNumPos) 00306 { 00307 m_str.erase(_uiPos, _uiNumPos); 00308 } 00309 00311 size_t GetSize() 00312 { 00313 return m_str.size(); 00314 } 00315 00317 size_t GetLength() 00318 { 00319 return m_str.length(); 00320 } 00321 00325 size_t Find (const K3dString& _str) 00326 { 00327 return m_str.find(_str.GetString()); 00328 } 00329 00331 void Clear() 00332 { 00333 m_str.clear(); 00334 } 00335 }; 00336 00337 static K3dString g_K3dString; 00338 00340 inline const K3dString& operator+(const K3dString &_rstr1, const K3dString &_rstr2) 00341 { 00342 TString str1 = _rstr1.GetString(); 00343 TString str2 = _rstr2.GetString(); 00344 TString strResult = str1 + str2; 00345 g_K3dString.GetString() = strResult; 00346 return g_K3dString; 00347 } 00348 00350 inline const K3dString& operator+(const char *_str1, const K3dString &_rstr2) 00351 { 00352 K3dString t(_str1); 00353 t += _rstr2; 00354 g_K3dString.GetString() = t.GetString( ); 00355 return g_K3dString; 00356 } 00357 00359 inline const K3dString& operator+(const K3dString &_rstr1, const char *_str2) 00360 { 00361 TString str1 = _rstr1.GetString(); 00362 TString str2 = _str2; 00363 TString strResult = str1 + str2; 00364 g_K3dString.GetString()= strResult; 00365 return g_K3dString; 00366 } 00367 } // End namespace K3d