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 
00123   class CStringBuilder
00124   {
00125   public :
00126 
00127     //----------------------------------------------------------------
00128     // Constructeur par défaut
00129     //----------------------------------------------------------------
00130     CStringBuilder();
00131 
00132     //----------------------------------------------------------------
00133     // Construit l'injecteur avec une première valeur
00134     //----------------------------------------------------------------
00135     template <typename T> CStringBuilder(const T& Value);
00136 
00137     //----------------------------------------------------------------
00138     // Injecte une valeur dans la chaîne
00139     //----------------------------------------------------------------
00140     template <typename T> CStringBuilder& operator ()(const T& Value);
00141 
00142     //----------------------------------------------------------------
00143     // Opérateur de conversion implicite en string
00144     //----------------------------------------------------------------
00145     operator std::string();
00146 
00147   private :
00148 
00149     //----------------------------------------------------------------
00150     // Données membres
00151     //----------------------------------------------------------------
00152     std::ostringstream m_OutStream; 
00153   };
00154 
00155 
00159   class CStringExtractor
00160   {
00161   public :
00162 
00163     //----------------------------------------------------------------
00164     // Construit l'extracteur à partir d'une chaîne
00165     //----------------------------------------------------------------
00166     CStringExtractor(const std::string& Text);
00167 
00168     //----------------------------------------------------------------
00169     // Extrait une valeur de type T
00170     //----------------------------------------------------------------
00171     template <typename T> CStringExtractor& operator ()(T& Value);
00172 
00173     //----------------------------------------------------------------
00174     // Vérifie qu'il n'y a plus rien à extraire
00175     //----------------------------------------------------------------
00176     void ThrowIfEOF();
00177 
00178   private :
00179 
00180     //----------------------------------------------------------------
00181     // Données membres
00182     //----------------------------------------------------------------
00183     std::istringstream m_InStream; 
00184   };
00185 
00192   template <typename T>
00193   inline CStringBuilder::CStringBuilder(const T& Value)
00194   {
00195     m_OutStream << Value;
00196   }
00197 
00198 
00207   template <typename T>
00208   inline CStringBuilder& CStringBuilder::operator ()(const T& Value)
00209   {
00210     m_OutStream << Value;
00211 
00212     return *this;
00213   }
00214 
00215 
00222   inline CStringBuilder::operator std::string()
00223   {
00224     return m_OutStream.str();
00225   }
00226 
00227 
00234   inline CStringExtractor::CStringExtractor(const std::string& Text) :
00235       m_InStream(Text)
00236   {}
00237 
00238 
00247   template <typename T>
00248   inline CStringExtractor& CStringExtractor::operator ()(T& Value)
00249   {
00250     if (!(m_InStream >> Value))
00251     {
00252       if (m_InStream.eof())
00253         throw CBadConversion(CStringBuilder("Tentative d'extraire un \"")(typeid(T).name())("\" à partir d'une chaîne vide"), "");
00254       else
00255         throw CBadConversion(CStringBuilder("Impossible de convertir de \"string\" en \"")(typeid(T).name())("\""), "");
00256     }
00257 
00258     return *this;
00259   }
00260 
00261 
00266   inline void CStringExtractor::ThrowIfEOF()
00267   {
00268     std::string Left;
00269     if (std::getline(m_InStream, Left))
00270       throw CBadConversion("Chaîne trop longue, \"" + Left + "\" n'a pas été extrait", "");
00271   }
00272 
00273 
00274 } // namespace Keos
00275 
00276 #endif // KEOS_STRING_H
00277 

Generated on Fri Mar 9 14:29:03 2007 for Keos by  doxygen 1.5.1-p1