Main Page | Namespace List | Class Hierarchy | Class List | Directories | File List | Namespace Members | Class Members | File Members

# KeosString.h

Go to the documentation of this file.

    00001 /*
    00002  * This source file is part of KEOS (Free 3D Engine)
    00003  * For the latest info, see http://www.keosengine.org/
    00004  * E-mails : thierry.vouriot@keosengine.org, yeri@keosengine.org
    00005  *
    00006  * This program is free software; you can redistribute it and/or modify it under
    00007  * the terms of the GNU Lesser General Public License as published by the Free Software
    00008  * Foundation; either version 2 of the License, or (at your option) any later
    00009  * version.
    00010  *
    00011  * This program is distributed in the hope that it will be useful, but WITHOUT
    00012  * ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
    00013  * FOR A PARTICULAR PURPOSE. See the GNU Lesser General Public License for more details.
    00014  *
    00015  * You should have received a copy of the GNU Lesser General Public License along with
    00016  * this program; if not, write to the Free Software Foundation, Inc., 59 Temple
    00017  * Place - Suite 330, Boston, MA 02111-1307, USA, or go to
    00018  * http://www.gnu.org/copyleft/lesser.txt.
    00019  *
    00020  */
    00021 #ifndef KEOS_STRING_H
    00022 #define KEOS_STRING_H
    00023
    00024 #include "KeosPrerequisites.h"
    00025
    00026 namespace Keos
    00027 {
    00030     class KEOS_EXPORT CStringUtil
    00031     {
    00032         public:
    00033         typedef std::stringstream StrStreamType;
    00034
    00040         static void Split(const String& strString, std::vector<String>& Tokens, const String& strDelim = " \t\n");
    00041
    00045         static void Trim( String& str, bool bLeft = true, bool bRight = true );
    00046
    00049         static void ToLowerCase( String& str );
    00050
    00053         static void ToUpperCase( String& str );
    00054
    00057         static String ToLower(const String& strText);
    00058
    00061         static String ToUpper(const String& strText);
    00062
    00064         static const String BLANK;
    00065     };
    00066
    00069     class KEOS_EXPORT CStringConverter
    00070     {
    00071     public:
    00072
    00075         static String ToString(float fVal, ushort nPrecision = 6,
    00076             ushort nWidth = 0, char fill = ' ',
    00077             std::ios::fmtflags flags = std::ios::fmtflags(0) );
    00078
    00081         static String ToString(int nVal, unsigned short nWidth = 0,
    00082             char fill = ' ',
    00083             std::ios::fmtflags flags = std::ios::fmtflags(0) );
    00084
    00089         static String ToString(bool bVal, bool bYesNo = false);
    00090
    00094         static Real ParseReal(const String& strVal);
    00095
    00099         static int ParseInt(const String& strVal);
    00100
    00104         static uint ParseUnsignedInt(const String& strVal);
    00105
    00109         static long ParseLong(const String& strVal);
    00110
    00114         static ulong ParseUnsignedLong(const String& strVal);
    00115
    00119         static bool ParseBool(const String& strVal);
    00120     };
    00121
    00122     class CStringBuilder
    00123     {
    00124     public :
    00125
    00126         //----------------------------------------------------------------
    00127         // Constructeur par défaut
    00128         //----------------------------------------------------------------
    00129         CStringBuilder();
    00130
    00131         //----------------------------------------------------------------
    00132         // Construit l'injecteur avec une première valeur
    00133         //----------------------------------------------------------------
    00134         template <typename T> CStringBuilder(const T& Value);
    00135
    00136         //----------------------------------------------------------------
    00137         // Injecte une valeur dans la chaîne
    00138         //----------------------------------------------------------------
    00139         template <typename T> CStringBuilder& operator ()(const T& Value);
    00140
    00141         //----------------------------------------------------------------
    00142         // Opérateur de conversion implicite en string
    00143         //----------------------------------------------------------------
    00144         operator std::string();
    00145
    00146     private :
    00147
    00148         //----------------------------------------------------------------
    00149         // Données membres
    00150         //----------------------------------------------------------------
    00151         std::ostringstream m_OutStream;
    00152     };
    00153
    00154
    00158     class CStringExtractor
    00159     {
    00160     public :
    00161
    00162         //----------------------------------------------------------------
    00163         // Construit l'extracteur à partir d'une chaîne
    00164         //----------------------------------------------------------------
    00165         CStringExtractor(const std::string& Text);
    00166
    00167         //----------------------------------------------------------------
    00168         // Extrait une valeur de type T
    00169         //----------------------------------------------------------------
    00170         template <typename T> CStringExtractor& operator ()(T& Value);
    00171
    00172         //----------------------------------------------------------------
    00173         // Vérifie qu'il n'y a plus rien à extraire
    00174         //----------------------------------------------------------------
    00175         void ThrowIfEOF();
    00176
    00177     private :
    00178
    00179         //----------------------------------------------------------------
    00180         // Données membres
    00181         //----------------------------------------------------------------
    00182         std::istringstream m_InStream;
    00183     };
    00184
    00191 template <typename T>
    00192 inline CStringBuilder::CStringBuilder(const T& Value)
    00193 {
    00194     m_OutStream << Value;
    00195 }
    00196
    00197
    00206 template <typename T>
    00207 inline CStringBuilder& CStringBuilder::operator ()(const T& Value)
    00208 {
    00209     m_OutStream << Value;
    00210
    00211     return *this;
    00212 }
    00213
    00214
    00221 inline CStringBuilder::operator std::string()
    00222 {
    00223     return m_OutStream.str();
    00224 }
    00225
    00226
    00233 inline CStringExtractor::CStringExtractor(const std::string& Text) :
    00234 m_InStream(Text)
    00235 {
    00236
    00237 }
    00238
    00239
    00248 template <typename T>
    00249 inline CStringExtractor& CStringExtractor::operator ()(T& Value)
    00250 {
    00251     if (!(m_InStream >> Value))
    00252     {
    00253         if (m_InStream.eof())
    00254             throw CBadConversion(CStringBuilder("Tentative d'extraire un \"")(typeid(T).name())("\" à partir d'une chaîne vide"),"");
    00255         else
    00256             throw CBadConversion(CStringBuilder("Impossible de convertir de \"string\" en \"")(typeid(T).name())("\""),"");
    00257     }
    00258
    00259     return *this;
    00260 }
    00261
    00262
    00267 inline void CStringExtractor::ThrowIfEOF()
    00268 {
    00269     std::string Left;
    00270     if (std::getline(m_InStream, Left))
    00271         throw CBadConversion("Chaîne trop longue, \"" + Left + "\" n'a pas été extrait","");
    00272 }
    00273
    00274
    00275 } // namespace Keos
    00276
    00277 #endif // KEOS_STRING_H
    00278