StringHelper.h
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2006-2011, SRI International (R)
00003  *
00004  * This program is free software: you can redistribute it and/or modify
00005  * it under the terms of the GNU Lesser General Public License as published by
00006  * the Free Software Foundation, either version 3 of the License, or
00007  * (at your option) any later version.
00008  *
00009  * This program is distributed in the hope that it will be useful,
00010  * but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  * GNU Lesser General Public License for more details.
00013  *
00014  * You should have received a copy of the GNU Lesser General Public License
00015  * along with this program.  If not, see <http://www.gnu.org/licenses/>.
00016  */
00017 
00018 #pragma once
00019 
00020 #ifndef __OpenKarto_StringHelper_h__
00021 #define __OpenKarto_StringHelper_h__
00022 
00023 #include <stdio.h>
00024 #include <ostream>
00025 #include <sstream>
00026 
00027 #include <OpenKarto/String.h>
00028 
00029 namespace karto
00030 {
00031 
00033 
00034 
00038 
00039   class Color;
00040 
00041   class Pose2;
00042   class Pose3;
00043   class Quaternion;
00044 
00045   template<typename T> class Size2;
00046 
00047   template<typename T> class Vector2;
00048   template<typename T> class Vector3;
00049   template<typename T> class Vector4;
00050 
00054   class KARTO_EXPORT StringHelper
00055   {
00056   public:
00062     static String ToString(const char* value);
00063 
00069     static String ToString(kt_bool value);
00070 
00076     static String ToString(kt_int16s value);
00077 
00083     static String ToString(kt_int16u value);
00084 
00090     static String ToString(kt_int32s value);
00091 
00097     static String ToString(kt_int32u value);
00098 
00104     static String ToString(kt_int64s value);
00105 
00111     static String ToString(kt_int64u value);
00112 
00113 #if defined(__APPLE__) && !defined(__LP64__)
00114     static String ToString(kt_size_t value);
00115 #endif
00116   
00122     static String ToString(kt_float value);
00123 
00129     static String ToString(kt_double value);
00130           
00137     static String ToString(kt_float value, kt_int32u precision);
00138 
00145     static String ToString(kt_double value, kt_int32u precision);
00146 
00152     static String ToString(const String& rValue);
00153 
00159     template<typename T>
00160     inline static String ToString(const Size2<T>& rValue);
00161 
00167     template<typename T>
00168     inline static String ToString(const Vector2<T>& rValue);
00169 
00175     template<typename T>
00176     inline static String ToString(const Vector3<T>& rValue);
00177 
00183     template<typename T>
00184     inline static String ToString(const Vector4<T>& rValue);
00185 
00191     static String ToString(const Quaternion& rValue);
00192 
00198     static String ToString(const Color& rValue);
00199 
00205     static String ToString(const Pose2& rValue);
00206 
00212     static String ToString(const Pose3& rValue);
00213 
00220     static kt_bool FromString(const String& rStringValue, kt_bool& rValue);
00221 
00228     static kt_bool FromString(const String& rStringValue, kt_int16s& rValue);
00229     
00236     static kt_bool FromString(const String& rStringValue, kt_int16u& rValue);
00237 
00244     static kt_bool FromString(const String& rStringValue, kt_int32s& rValue);
00245     
00252     static kt_bool FromString(const String& rStringValue, kt_int32u& rValue);
00253 
00260     static kt_bool FromString(const String& rStringValue, kt_int64s& rValue);
00261     
00268     static kt_bool FromString(const String& rStringValue, kt_int64u& rValue);
00269 
00276     static kt_bool FromString(const String& rStringValue, kt_float& rValue);
00277     
00284     static kt_bool FromString(const String& rStringValue, kt_double& rValue);
00285 
00292     static kt_bool FromString(const String& rStringValue, String& rValue);
00293 
00300     template<typename T>
00301     static kt_bool FromString(const String& rStringValue, Size2<T>& rValue)
00302     {
00303       kt_size_t index = rStringValue.FindFirstOf(" ");
00304       if (index != -1)
00305       {
00306         karto::String stringValue;
00307         T value;
00308 
00309         stringValue = rStringValue.SubString(0, index);
00310 
00311         value = 0;
00312         FromString(stringValue, value);
00313         rValue.SetWidth(value);
00314 
00315         stringValue = rStringValue.SubString(index + 1, rStringValue.Size());
00316 
00317         value = 0;
00318         FromString(stringValue, value);
00319         rValue.SetHeight(value);
00320 
00321         return true;
00322       }
00323 
00324       return false;
00325     }
00326 
00333     template<typename T>
00334     static kt_bool FromString(const String& rStringValue, Vector2<T>& rValue)
00335     {
00336       kt_size_t index = rStringValue.FindFirstOf(" ");
00337       if (index != -1)
00338       {
00339         karto::String stringValue;
00340         T value;
00341         
00342         stringValue = rStringValue.SubString(0, index);
00343 
00344         value = 0;
00345         FromString(stringValue, value);
00346         rValue.SetX(value);
00347 
00348         stringValue = rStringValue.SubString(index + 1, rStringValue.Size());
00349 
00350         value = 0;
00351         FromString(stringValue, value);
00352         rValue.SetY(value);
00353 
00354         return true;
00355       }
00356 
00357       return false;
00358     }
00359 
00366     template<typename T>
00367     inline static kt_bool FromString(const String& rStringValue, Vector3<T>& rValue)
00368     {
00369       karto::String tempString = rStringValue;
00370       kt_size_t index = tempString.FindFirstOf(" ");
00371       if (index != -1)
00372       {
00373         karto::String stringValue;
00374         T value;
00375 
00376         // Get X
00377         stringValue = tempString.SubString(0, index);
00378 
00379         value = 0;
00380         FromString(stringValue, value);
00381         rValue.SetX(value);
00382 
00383         // Get Y
00384         tempString = rStringValue.SubString(index + 1, rStringValue.Size());
00385         index = tempString.FindFirstOf(" ");
00386 
00387         stringValue = tempString.SubString(0, index);
00388 
00389         value = 0;
00390         FromString(stringValue, value);
00391         rValue.SetY(value);
00392 
00393         // Get Z
00394         tempString = tempString.SubString(index + 1, rStringValue.Size());
00395         index = tempString.FindFirstOf(" ");
00396 
00397         stringValue = tempString.SubString(index + 1, rStringValue.Size());
00398 
00399         value = 0;
00400         FromString(stringValue, value);
00401         rValue.SetZ(value);
00402 
00403         return true;
00404       }
00405 
00406       return false;
00407     }
00408 
00415     template<typename T>
00416     static kt_bool FromString(const String& rStringValue, Vector4<T>& rValue)
00417     {
00418       karto::String tempString = rStringValue;
00419       kt_size_t index = tempString.FindFirstOf(" ");
00420       if (index != -1)
00421       {
00422         karto::String stringValue;
00423         T value;
00424 
00425         // Get X
00426         stringValue = tempString.SubString(0, index);
00427 
00428         value = 0;
00429         FromString(stringValue, value);
00430         rValue.SetX(value);
00431 
00432         // Get Y
00433         tempString = rStringValue.SubString(index + 1, rStringValue.Size());
00434         index = tempString.FindFirstOf(" ");
00435 
00436         stringValue = tempString.SubString(0, index);
00437 
00438         value = 0;
00439         FromString(stringValue, value);
00440         rValue.SetY(value);
00441 
00442         // Get Z
00443         tempString = tempString.SubString(index + 1, rStringValue.Size());
00444         index = tempString.FindFirstOf(" ");
00445 
00446         stringValue = tempString.SubString(index + 1, rStringValue.Size());
00447 
00448         value = 0;
00449         FromString(stringValue, value);
00450         rValue.SetZ(value);
00451 
00452         // Get W
00453         tempString = tempString.SubString(index + 1, rStringValue.Size());
00454         index = tempString.FindFirstOf(" ");
00455 
00456         stringValue = tempString.SubString(index + 1, rStringValue.Size());
00457 
00458         value = 0;
00459         FromString(stringValue, value);
00460         rValue.SetW(value);
00461 
00462         return true;
00463       }
00464 
00465       return false;
00466     }
00467 
00474     static kt_bool FromString(const String& rStringValue, Quaternion& rValue);
00475 
00482     static kt_bool FromString(const String& rStringValue, Color& rValue);
00483 
00490     static kt_bool FromString(const String& rStringValue, Pose2& rValue);
00491 
00498     static kt_bool FromString(const String& rStringValue, Pose3& rValue);
00499 
00505     static String Trim(const String& rValue);
00506 
00514     static String Replace(const String& rSource, const String& rFind, const String& rReplace);
00515 
00521     static kt_bool IsLetter(char ch);
00522 
00528     static String ToLowerCase(const String &rValue);
00529     
00535     static String ToUpperCase(const String &rValue);
00536   }; // class StringHelper
00537 
00541 
00545   class KARTO_EXPORT StringBuilder
00546   {
00547   public:
00552     const String& ToString() const;
00553 
00557     void Clear();
00558 
00559   public:
00564 //    StringBuilder& operator << (char value);
00565 
00571     StringBuilder& operator << (kt_int8u value);
00572 
00578     StringBuilder& operator << (kt_int16s value);
00584     StringBuilder& operator << (kt_int16u value);
00585 
00591     StringBuilder& operator << (kt_int32s value);
00597     StringBuilder& operator << (kt_int32u value);
00598 
00604     StringBuilder& operator << (kt_int64s value);
00610     StringBuilder& operator << (kt_int64u value);
00611 
00612 #if defined(__APPLE__) && !defined(__LP64__)
00613 
00618     StringBuilder& operator << (kt_size_t value);
00619 #endif
00620 
00626     StringBuilder& operator << (kt_float value);
00632     StringBuilder& operator << (kt_double value);
00633 
00639     StringBuilder& operator << (const String& rValue);
00640 
00646     StringBuilder& operator << (const StringBuilder& rValue);
00647 
00654     friend KARTO_FORCEINLINE std::ostream& operator << (std::ostream& rStream, const StringBuilder& rStringBuilder)
00655     {
00656       rStream << rStringBuilder.ToString();
00657       return rStream;
00658     }
00659 
00660   private:
00661     String m_String;
00662   }; // class StringBuilder
00663 
00665 
00666 }
00667 
00668 #endif // __OpenKarto_StringHelper_h__


nav2d_karto
Author(s): Sebastian Kasperski
autogenerated on Mon Oct 6 2014 02:44:17