ZenLib
|
00001 // ZenLib::Ztring - More methods for std::(w)string 00002 // Copyright (C) 2002-2011 MediaArea.net SARL, Info@MediaArea.net 00003 // 00004 // This software is provided 'as-is', without any express or implied 00005 // warranty. In no event will the authors be held liable for any damages 00006 // arising from the use of this software. 00007 // 00008 // Permission is granted to anyone to use this software for any purpose, 00009 // including commercial applications, and to alter it and redistribute it 00010 // freely, subject to the following restrictions: 00011 // 00012 // 1. The origin of this software must not be misrepresented; you must not 00013 // claim that you wrote the original software. If you use this software 00014 // in a product, an acknowledgment in the product documentation would be 00015 // appreciated but is not required. 00016 // 2. Altered source versions must be plainly marked as such, and must not be 00017 // misrepresented as being the original software. 00018 // 3. This notice may not be removed or altered from any source distribution. 00019 // 00020 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00021 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00022 // 00023 // More methods for std::(w)string 00024 // 00025 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 00026 00027 //--------------------------------------------------------------------------- 00028 #ifndef ZenLib_ZtringH 00029 #define ZenLib_ZtringH 00030 //--------------------------------------------------------------------------- 00031 00032 //--------------------------------------------------------------------------- 00033 #include "ZenLib/Conf.h" 00034 #include "ZenLib/Utils.h" 00035 #include "ZenLib/int128u.h" 00036 #include <string> 00037 #include <sstream> 00038 //--------------------------------------------------------------------------- 00039 00040 namespace ZenLib 00041 { 00042 00043 //--------------------------------------------------------------------------- 00044 typedef std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > tstring; 00045 //--------------------------------------------------------------------------- 00046 00047 //--------------------------------------------------------------------------- 00048 /// @brief Options for Ztring methods 00049 enum ztring_t 00050 { 00051 Ztring_Nothing, 00052 Ztring_Rounded = 1, ///< if >.5, upper, else lower 00053 Ztring_CaseSensitive = 2, ///< Case sensitive ("A" and "a" are different) 00054 Ztring_AddLastItem = 4, ///< if Begin is found and End is not found, return between Begin and end of string 00055 Ztring_Recursive = 8, ///< Do all strings 00056 Ztring_NoZero =16 ///> Doesn't keep Zero in the float number 00057 }; 00058 00059 //--------------------------------------------------------------------------- 00060 00061 //*************************************************************************** 00062 /// @brief String manipulation (based on std::(w)string) 00063 //*************************************************************************** 00064 00065 class Ztring : public tstring //for details about undocumented methods see http://www.sgi.com/tech/stl/basic_string.html 00066 { 00067 public : 00068 //Constructor/destructor 00069 Ztring () : tstring(){}; 00070 Ztring (const tstring& str) : tstring(str){}; 00071 Ztring (const tstring& str, size_type pos, size_type n=npos) : tstring(str, pos, n){}; 00072 Ztring (const Char* s, size_type n) : tstring(s, n){}; 00073 Ztring (const Char* s) : tstring(s){}; 00074 Ztring (size_type n, Char c) : tstring(n, c){}; 00075 #ifdef UNICODE 00076 Ztring (const char* S) : tstring(){From_UTF8(S);}; 00077 Ztring (const char* S, size_type n) : tstring(){From_UTF8(S, 0, n);}; 00078 #endif //UNICODE 00079 00080 //Operators 00081 ///Same as [], but resize the string if Pos doesn't exist yet 00082 Char &operator () (size_type Pos); 00083 00084 //Assign 00085 bool Assign_FromFile (const Ztring &FileName); 00086 00087 //Conversions - From 00088 #ifndef WSTRING_MISSING 00089 /// @brief convert an Unicode encoded string into Ztring 00090 Ztring& From_Unicode (const std::wstring &S) {return From_Unicode(S.c_str());}; 00091 #endif //WSTRING_MISSING 00092 /// @brief convert an Unicode encoded string into Ztring 00093 Ztring& From_Unicode (const wchar_t *S); 00094 /// @brief convert an Unicode encoded string into Ztring 00095 Ztring& From_Unicode (const wchar_t *S, size_type Start, size_type Length); 00096 /// @brief convert an Unicode encoded string into Ztring 00097 Ztring& From_Unicode (const wchar_t *S, size_type Length) {return From_Unicode(S, 0, Length);}; 00098 /// @brief convert an UTF-8 encoded string into Ztring 00099 Ztring& From_UTF8 (const std::string &S) {return From_UTF8(S.c_str());}; 00100 /// @brief convert an UTF-8 encoded string into Ztring 00101 Ztring& From_UTF8 (const char *S); 00102 /// @brief convert an UTF-8 encoded string into Ztring 00103 Ztring& From_UTF8 (const char *S, size_type Start, size_type Length); 00104 /// @brief convert an UTF-8 encoded string into Ztring 00105 Ztring& From_UTF8 (const char *S, size_type Length) {return From_UTF8(S, 0, Length);}; 00106 /// @brief convert an UTF-16 encoded string into Ztring 00107 Ztring& From_UTF16 (const char *S); 00108 /// @brief convert an UTF-16 encoded string into Ztring 00109 Ztring& From_UTF16 (const char *S, size_type Start, size_type Length); 00110 /// @brief convert an UTF-16 encoded string into Ztring 00111 Ztring& From_UTF16 (const char *S, size_type Length) {return From_UTF16(S, 0, Length);}; 00112 /// @brief convert an UTF-16BE encoded string into Ztring 00113 Ztring& From_UTF16BE (const char *S); 00114 /// @brief convert an UTF-16BE encoded string into Ztring 00115 Ztring& From_UTF16BE (const char *S, size_type Start, size_type Length); 00116 /// @brief convert an UTF-16BE encoded string into Ztring 00117 Ztring& From_UTF16BE (const char *S, size_type Length) {return From_UTF16BE(S, 0, Length);}; 00118 /// @brief convert an UTF-16LE encoded string into Ztring 00119 Ztring& From_UTF16LE (const char *S); 00120 /// @brief convert an UTF-16LE encoded string into Ztring 00121 Ztring& From_UTF16LE (const char *S, size_type Start, size_type Length); 00122 /// @brief convert an UTF-16LE encoded string into Ztring 00123 Ztring& From_UTF16LE (const char *S, size_type Length) {return From_UTF16LE(S, 0, Length);}; 00124 /// @brief convert an Locael encoded string into Ztring 00125 Ztring& From_Local (const std::string &S) {return From_Local(S.c_str());}; 00126 /// @brief convert an Local encoded string into Ztring 00127 Ztring& From_Local (const char *S); 00128 /// @brief convert an Local encoded string into Ztring 00129 Ztring& From_Local (const char *S, size_type Start, size_type Length); 00130 /// @brief convert an Local encoded string into Ztring 00131 Ztring& From_Local (const char *S, size_type Length) {return From_Local(S, 0, Length);}; 00132 00133 /// @brief convert an ISO-8859-1 encoded string into Ztring 00134 Ztring& From_ISO_8859_1 (const char *S); 00135 /// @brief convert an ISO-8859-1 encoded string into Ztring 00136 Ztring& From_ISO_8859_1 (const char *S, size_type Start, size_type Length); 00137 /// @brief convert an ISO-8859-1 encoded string into Ztring 00138 Ztring& From_ISO_8859_1 (const char *S, size_type Length) {return From_ISO_8859_1(S, 0, Length);}; 00139 00140 /// @brief convert an ISO-8859-2 encoded string into Ztring 00141 Ztring& From_ISO_8859_2 (const char *S); 00142 /// @brief convert an ISO-8859-1 encoded string into Ztring 00143 Ztring& From_ISO_8859_2 (const char *S, size_type Start, size_type Length); 00144 /// @brief convert an ISO-8859-1 encoded string into Ztring 00145 Ztring& From_ISO_8859_2 (const char *S, size_type Length) {return From_ISO_8859_2(S, 0, Length);}; 00146 00147 /// @brief convert an 16 byte GUID into Ztring 00148 Ztring& From_GUID (const int128u S); 00149 /// @brief convert an 16 byte UUID into Ztring 00150 Ztring& From_UUID (const int128u S); 00151 /// @brief convert an 4 Character Code into Ztring 00152 Ztring& From_CC4 (const char *S) {return From_Local(S, 0, 4);}; 00153 /// @brief convert an 4 Character Code into Ztring 00154 Ztring& From_CC4 (const int8u *S) {return From_Local((const char*)S, 0, 4);}; 00155 /// @brief convert an 4 Character Code into Ztring 00156 Ztring& From_CC4 (const int32u S); 00157 /// @brief convert an 2 Character Code into Ztring 00158 Ztring& From_CC3 (const char *S) {return From_Local(S, 0, 3);}; 00159 /// @brief convert an 4 Character Code into Ztring 00160 Ztring& From_CC3 (const int8u *S) {return From_Local((const char*)S, 0, 3);}; 00161 /// @brief convert an 4 Character Code into Ztring 00162 Ztring& From_CC3 (const int32u S); 00163 /// @brief convert an 2 Character Code into Ztring 00164 Ztring& From_CC2 (const char *S) {return From_CC2(ZenLib::CC2(S));}; 00165 /// @brief convert an 2 Character Code into Ztring 00166 Ztring& From_CC2 (const int8u *S) {return From_CC2(ZenLib::CC2(S));}; 00167 /// @brief convert an 2 Character Code into Ztring 00168 Ztring& From_CC2 (const int16u S); 00169 /// @brief convert an 1 Character Code into Ztring 00170 Ztring& From_CC1 (const char *S) {return From_CC1(ZenLib::CC1(S));}; 00171 /// @brief convert an 1 Character Code into Ztring 00172 Ztring& From_CC1 (const int8u *S) {return From_CC1(ZenLib::CC1(S));}; 00173 /// @brief convert an 1 Character Code into Ztring 00174 Ztring& From_CC1 (const int8u S); 00175 /// @brief convert number into Ztring 00176 Ztring& From_Number (const int8s, int8u Radix=10); 00177 /// @brief convert number into Ztring 00178 Ztring& From_Number (const int8u, int8u Radix=10); 00179 /// @brief convert number into Ztring 00180 Ztring& From_Number (const int16s, int8u Radix=10); 00181 /// @brief convert number into Ztring 00182 Ztring& From_Number (const int16u, int8u Radix=10); 00183 /// @brief convert number into Ztring 00184 Ztring& From_Number (const int32s, int8u Radix=10); 00185 /// @brief convert number into Ztring 00186 Ztring& From_Number (const int32u, int8u Radix=10); 00187 /// @brief convert number into Ztring 00188 Ztring& From_Number (const int64s, int8u Radix=10); 00189 /// @brief convert number into Ztring 00190 Ztring& From_Number (const int64u, int8u Radix=10); 00191 /// @brief convert number into Ztring 00192 Ztring& From_Number (const int128u, int8u Radix=10); 00193 /// @brief convert number into Ztring 00194 Ztring& From_Number (const float32, int8u AfterComma=3, ztring_t Options=Ztring_Nothing); 00195 /// @brief convert number into Ztring 00196 Ztring& From_Number (const float64, int8u AfterComma=3, ztring_t Options=Ztring_Nothing); 00197 /// @brief convert number into Ztring 00198 Ztring& From_Number (const float80, int8u AfterComma=3, ztring_t Options=Ztring_Nothing); 00199 #ifdef NEED_SIZET 00200 /// @brief convert number into Ztring 00201 Ztring& From_Number (const size_t, int8u Radix=10); 00202 #endif //NEED_SIZET 00203 /// @brief convert number (BCD coded) into Ztring 00204 Ztring& From_BCD (const int8u); 00205 /// @brief convert count of milliseconds into a readable and sortable string 00206 Ztring& Duration_From_Milliseconds (const int64s Milliseconds); 00207 /// @deprecated replaced by the int64s version 00208 Ztring& Duration_From_Milliseconds (const int64u Milliseconds); 00209 /// @brief convert count of seconds since 1601 into a readable and sortable string 00210 Ztring& Date_From_Milliseconds_1601 (const int64u Milliseconds); 00211 /// @brief convert count of seconds since 1601 into a readable and sortable string 00212 Ztring& Date_From_Seconds_1601 (const int64u Seconds); 00213 /// @brief convert count of seconds since 1970 into a readable and sortable string 00214 Ztring& Date_From_Seconds_1904 (const int64u Seconds); 00215 /// @brief convert count of seconds since 1970 into a readable and sortable string 00216 Ztring& Date_From_Seconds_1970 (const int32u Seconds); 00217 /// @brief convert count of seconds since 1970 into a readable and sortable string (in local time) 00218 Ztring& Date_From_Seconds_1970_Local (const int32u Seconds); 00219 /// @brief convert a free formated string into a readable and sortable string 00220 Ztring& Date_From_String (const char* Date, size_type Value_Size=Error); 00221 /// @brief convert numbers into a readable and sortable string 00222 Ztring& Date_From_Numbers (const int8u Year, const int8u Month, const int8u Day, const int8u Hour, const int8u Minute, const int8u Second); 00223 00224 //Conversions - To 00225 #ifndef WSTRING_MISSING 00226 /// @brief Convert into Unicode chars 00227 /// @return the string corresponding \n 00228 std::wstring To_Unicode () const; 00229 #endif //WSTRING_MISSING 00230 /// @brief Convert into char* (UTF-8 encoded) 00231 /// @return the string corresponding \n 00232 std::string To_UTF8 () const; 00233 /// @brief Convert into char* (Local encoded) 00234 /// @return the string corresponding \n 00235 std::string To_Local () const; 00236 /// @brief Convert into 16 byte UUID number 00237 /// @return the value corresponding \n 00238 /// 0 if there is a problem 00239 int128u To_UUID () const; 00240 /// @brief Convert into a 4 Character Code 00241 /// @return the value corresponding \n 00242 /// 0 if there is a problem 00243 int32u To_CC4 () const; 00244 /// @brief Convert into Int (8 bits) 00245 /// @return the value corresponding \n 00246 /// 0 if there is a problem 00247 int8s To_int8s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00248 /// @brief Convert into unsigned Int (8 bits) 00249 /// @return the value corresponding 00250 /// 0 if there is a problem 00251 int8u To_int8u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00252 /// @brief Convert into Int (16 bits) 00253 /// @return the value corresponding \n 00254 /// 0 if there is a problem 00255 int16s To_int16s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00256 /// @brief Convert into unsigned Int (16 bits) 00257 /// @return the value corresponding 00258 /// 0 if there is a problem 00259 int16u To_int16u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00260 /// @brief Convert into Int (32 bits) 00261 /// @return the value corresponding \n 00262 /// 0 if there is a problem 00263 int32s To_int32s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00264 /// @brief Convert into unsigned Int (32 bits) 00265 /// @return the value corresponding 00266 /// 0 if there is a problem 00267 int32u To_int32u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00268 /// @brief Convert into Int (64 bits) 00269 /// @return the value corresponding \n 00270 /// 0 if there is a problem 00271 int64s To_int64s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00272 /// @brief Convert into unsigned Int (64 bits) 00273 /// @return the value corresponding \n 00274 /// 0 if there is a problem 00275 int64u To_int64u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00276 /// @brief Convert into unsigned Int (64 bits) 00277 /// @warning only hexadecimal and no rounding are currenlty supported \n 00278 /// @return the value corresponding \n 00279 /// 0 if there is a problem 00280 int128u To_int128u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00281 /// @brief Convert into float 00282 /// @return the value corresponding \n 00283 /// 0 if there is a problem 00284 float32 To_float32 (ztring_t Options=Ztring_Nothing) const; 00285 float64 To_float64 (ztring_t Options=Ztring_Nothing) const; 00286 float80 To_float80 (ztring_t Options=Ztring_Nothing) const; 00287 00288 //Static versions 00289 static Ztring ToZtring_From_Local(const std::string &S) {return Ztring().From_Local(S);}; 00290 static Ztring ToZtring_From_Local(const char *S) {return Ztring().From_Local(S);}; 00291 static Ztring ToZtring_From_Local(const char *S, size_type Start, size_type Length) {return Ztring().From_Local(S, Start, Length);}; 00292 static Ztring ToZtring_From_Local(const char *S, size_type Length) {return Ztring().From_Local(S, Length);}; 00293 static Ztring ToZtring_From_CC4 (const char *S) {return Ztring().From_CC4(S);}; 00294 static Ztring ToZtring_From_CC4 (const int8u *S) {return Ztring().From_CC4(S);}; 00295 static Ztring ToZtring_From_CC4 (const int32u S) {return Ztring().From_CC4(S);}; 00296 static Ztring ToZtring_From_CC3 (const char *S) {return Ztring().From_CC3(S);}; 00297 static Ztring ToZtring_From_CC3 (const int8u *S) {return Ztring().From_CC3(S);}; 00298 static Ztring ToZtring_From_CC3 (const int32u S) {return Ztring().From_CC3(S);}; 00299 static Ztring ToZtring_From_CC2 (const char *S) {return Ztring().From_CC2(S);}; 00300 static Ztring ToZtring_From_CC2 (const int8u *S) {return Ztring().From_CC2(S);}; 00301 static Ztring ToZtring_From_CC2 (const int16u S) {return Ztring().From_CC2(S);}; 00302 static Ztring ToZtring_From_CC1 (const char *S) {return Ztring().From_CC1(S);}; 00303 static Ztring ToZtring_From_CC1 (const int8u *S) {return Ztring().From_CC1(S);}; 00304 static Ztring ToZtring_From_CC1 (const int8u S) {return Ztring().From_CC1(S);}; 00305 static Ztring ToZtring (const int8s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00306 static Ztring ToZtring (const int8u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00307 static Ztring ToZtring (const int16s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00308 static Ztring ToZtring (const int16u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00309 static Ztring ToZtring (const int32s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00310 static Ztring ToZtring (const int32u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00311 static Ztring ToZtring (const int64s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00312 static Ztring ToZtring (const int64u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00313 static Ztring ToZtring (const int128u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00314 static Ztring ToZtring (const float32 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);}; 00315 static Ztring ToZtring (const float64 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);}; 00316 static Ztring ToZtring (const float80 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);}; 00317 #ifdef NEED_SIZET 00318 static Ztring ToZtring (const size_t I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00319 #endif //NEED_SIZET 00320 00321 //Edition 00322 /// @brief test if it is a number 00323 bool IsNumber() const; 00324 /// @brief convert into lowercase 00325 Ztring &MakeLowerCase(); 00326 /// @brief convert into uppercase 00327 Ztring &MakeUpperCase(); 00328 /// @brief Remove leading whitespaces from a string 00329 Ztring &TrimLeft(Char ToTrim=_T(' ')); 00330 /// @brief Remove trailing whitespaces from a string 00331 Ztring &TrimRight(Char ToTrim=_T(' ')); 00332 /// @brief Remove leading and trailing whitespaces from a string 00333 Ztring &Trim(Char ToTrim=_T(' ')); 00334 /// @brief Quotes a string 00335 Ztring &Quote(Char ToTrim=_T('\"')); 00336 /// @brief return a string between two strings 00337 /// @param Begin First string 00338 /// @param End Second string 00339 /// @param Pos Position to begin to scan string 00340 /// @param Options Options for searching \n 00341 /// Available : Ztring_CaseSensitive 00342 /// @return The substring \n 00343 /// "" if not found 00344 Ztring SubString (const tstring &Begin, const tstring &End, size_type Pos=0, ztring_t Options=Ztring_Nothing) const; 00345 /// @brief replace a string by another one 00346 /// @param ToFind string to find 00347 /// @param ToReplace string wich replace the string found 00348 /// @param Pos Position to begin to scan string 00349 /// @param Options Options for searching \n 00350 /// Available : Ztring_CaseSensitive, Ztring_Recursive 00351 /// @return The count of replacements 00352 size_type FindAndReplace (const tstring &ToFind, const tstring &ReplaceBy, size_type Pos=0, ztring_t Options=Ztring_Nothing); //Remplace une chaine par une autre 00353 /// @brief Count the number of occurencies of a string in the string 00354 /// @param ToCount string to count 00355 /// @param Options Options for count \n 00356 /// Available : Ztring_CaseSensitive 00357 /// @return the count 00358 00359 //Information 00360 size_type Count (const Ztring &ToCount, ztring_t Options=Ztring_Nothing) const; 00361 /// @brief compare with another string 00362 /// @param ToCompare string to compare with 00363 /// @param Options Options for comaparing \n 00364 /// Available : Ztring_CaseSensitive 00365 /// @return The result of comparasion 00366 bool Compare (const Ztring &ToCompare, const Ztring &Comparator=_T("=="), ztring_t Options=Ztring_Nothing) const; 00367 }; 00368 00369 } //NameSpace 00370 00371 #endif 00372