00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
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
00129
00130 CStringBuilder();
00131
00132
00133
00134
00135 template <typename T> CStringBuilder(const T& Value);
00136
00137
00138
00139
00140 template <typename T> CStringBuilder& operator ()(const T& Value);
00141
00142
00143
00144
00145 operator std::string();
00146
00147 private :
00148
00149
00150
00151
00152 std::ostringstream m_OutStream;
00153 };
00154
00155
00159 class CStringExtractor
00160 {
00161 public :
00162
00163
00164
00165
00166 CStringExtractor(const std::string& Text);
00167
00168
00169
00170
00171 template <typename T> CStringExtractor& operator ()(T& Value);
00172
00173
00174
00175
00176 void ThrowIfEOF();
00177
00178 private :
00179
00180
00181
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 }
00275
00276 #endif // KEOS_STRING_H
00277