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 16 byte GUID into Ztring 00141 Ztring& From_GUID (const int128u S); 00142 /// @brief convert an 16 byte UUID into Ztring 00143 Ztring& From_UUID (const int128u S); 00144 /// @brief convert an 4 Character Code into Ztring 00145 Ztring& From_CC4 (const char *S) {return From_Local(S, 0, 4);}; 00146 /// @brief convert an 4 Character Code into Ztring 00147 Ztring& From_CC4 (const int8u *S) {return From_Local((const char*)S, 0, 4);}; 00148 /// @brief convert an 4 Character Code into Ztring 00149 Ztring& From_CC4 (const int32u S); 00150 /// @brief convert an 2 Character Code into Ztring 00151 Ztring& From_CC3 (const char *S) {return From_Local(S, 0, 3);}; 00152 /// @brief convert an 4 Character Code into Ztring 00153 Ztring& From_CC3 (const int8u *S) {return From_Local((const char*)S, 0, 3);}; 00154 /// @brief convert an 4 Character Code into Ztring 00155 Ztring& From_CC3 (const int32u S); 00156 /// @brief convert an 2 Character Code into Ztring 00157 Ztring& From_CC2 (const char *S) {return From_CC2(ZenLib::CC2(S));}; 00158 /// @brief convert an 2 Character Code into Ztring 00159 Ztring& From_CC2 (const int8u *S) {return From_CC2(ZenLib::CC2(S));}; 00160 /// @brief convert an 2 Character Code into Ztring 00161 Ztring& From_CC2 (const int16u S); 00162 /// @brief convert an 1 Character Code into Ztring 00163 Ztring& From_CC1 (const char *S) {return From_CC1(ZenLib::CC1(S));}; 00164 /// @brief convert an 1 Character Code into Ztring 00165 Ztring& From_CC1 (const int8u *S) {return From_CC1(ZenLib::CC1(S));}; 00166 /// @brief convert an 1 Character Code into Ztring 00167 Ztring& From_CC1 (const int8u S); 00168 /// @brief convert number into Ztring 00169 Ztring& From_Number (const int8s, int8u Radix=10); 00170 /// @brief convert number into Ztring 00171 Ztring& From_Number (const int8u, int8u Radix=10); 00172 /// @brief convert number into Ztring 00173 Ztring& From_Number (const int16s, int8u Radix=10); 00174 /// @brief convert number into Ztring 00175 Ztring& From_Number (const int16u, int8u Radix=10); 00176 /// @brief convert number into Ztring 00177 Ztring& From_Number (const int32s, int8u Radix=10); 00178 /// @brief convert number into Ztring 00179 Ztring& From_Number (const int32u, int8u Radix=10); 00180 /// @brief convert number into Ztring 00181 Ztring& From_Number (const int64s, int8u Radix=10); 00182 /// @brief convert number into Ztring 00183 Ztring& From_Number (const int64u, int8u Radix=10); 00184 /// @brief convert number into Ztring 00185 Ztring& From_Number (const int128u, int8u Radix=10); 00186 /// @brief convert number into Ztring 00187 Ztring& From_Number (const float32, int8u AfterComma=3, ztring_t Options=Ztring_Nothing); 00188 /// @brief convert number into Ztring 00189 Ztring& From_Number (const float64, int8u AfterComma=3, ztring_t Options=Ztring_Nothing); 00190 /// @brief convert number into Ztring 00191 Ztring& From_Number (const float80, int8u AfterComma=3, ztring_t Options=Ztring_Nothing); 00192 #ifdef NEED_SIZET 00193 /// @brief convert number into Ztring 00194 Ztring& From_Number (const size_t, int8u Radix=10); 00195 #endif //NEED_SIZET 00196 /// @brief convert number (BCD coded) into Ztring 00197 Ztring& From_BCD (const int8u); 00198 /// @brief convert count of milliseconds into a readable and sortable string 00199 Ztring& Duration_From_Milliseconds (const int64s Milliseconds); 00200 /// @deprecated replaced by the int64s version 00201 Ztring& Duration_From_Milliseconds (const int64u Milliseconds); 00202 /// @brief convert count of seconds since 1601 into a readable and sortable string 00203 Ztring& Date_From_Milliseconds_1601 (const int64u Milliseconds); 00204 /// @brief convert count of seconds since 1601 into a readable and sortable string 00205 Ztring& Date_From_Seconds_1601 (const int64u Seconds); 00206 /// @brief convert count of seconds since 1970 into a readable and sortable string 00207 Ztring& Date_From_Seconds_1904 (const int64u Seconds); 00208 /// @brief convert count of seconds since 1970 into a readable and sortable string 00209 Ztring& Date_From_Seconds_1970 (const int32u Seconds); 00210 /// @brief convert count of seconds since 1970 into a readable and sortable string (in local time) 00211 Ztring& Date_From_Seconds_1970_Local (const int32u Seconds); 00212 /// @brief convert a free formated string into a readable and sortable string 00213 Ztring& Date_From_String (const char* Date, size_type Value_Size=Error); 00214 /// @brief convert numbers into a readable and sortable string 00215 Ztring& Date_From_Numbers (const int8u Year, const int8u Month, const int8u Day, const int8u Hour, const int8u Minute, const int8u Second); 00216 00217 //Conversions - To 00218 #ifndef WSTRING_MISSING 00219 /// @brief Convert into Unicode chars 00220 /// @return the string corresponding \n 00221 std::wstring To_Unicode () const; 00222 #endif //WSTRING_MISSING 00223 /// @brief Convert into char* (UTF-8 encoded) 00224 /// @return the string corresponding \n 00225 std::string To_UTF8 () const; 00226 /// @brief Convert into char* (Local encoded) 00227 /// @return the string corresponding \n 00228 std::string To_Local () const; 00229 /// @brief Convert into 16 byte UUID number 00230 /// @return the value corresponding \n 00231 /// 0 if there is a problem 00232 int128u To_UUID () const; 00233 /// @brief Convert into a 4 Character Code 00234 /// @return the value corresponding \n 00235 /// 0 if there is a problem 00236 int32u To_CC4 () const; 00237 /// @brief Convert into Int (8 bits) 00238 /// @return the value corresponding \n 00239 /// 0 if there is a problem 00240 int8s To_int8s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00241 /// @brief Convert into unsigned Int (8 bits) 00242 /// @return the value corresponding 00243 /// 0 if there is a problem 00244 int8u To_int8u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00245 /// @brief Convert into Int (16 bits) 00246 /// @return the value corresponding \n 00247 /// 0 if there is a problem 00248 int16s To_int16s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00249 /// @brief Convert into unsigned Int (16 bits) 00250 /// @return the value corresponding 00251 /// 0 if there is a problem 00252 int16u To_int16u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00253 /// @brief Convert into Int (32 bits) 00254 /// @return the value corresponding \n 00255 /// 0 if there is a problem 00256 int32s To_int32s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00257 /// @brief Convert into unsigned Int (32 bits) 00258 /// @return the value corresponding 00259 /// 0 if there is a problem 00260 int32u To_int32u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00261 /// @brief Convert into Int (64 bits) 00262 /// @return the value corresponding \n 00263 /// 0 if there is a problem 00264 int64s To_int64s (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00265 /// @brief Convert into unsigned Int (64 bits) 00266 /// @return the value corresponding \n 00267 /// 0 if there is a problem 00268 int64u To_int64u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00269 /// @brief Convert into unsigned Int (64 bits) 00270 /// @warning only hexadecimal and no rounding are currenlty supported \n 00271 /// @return the value corresponding \n 00272 /// 0 if there is a problem 00273 int128u To_int128u (int8u Radix=10, ztring_t Options=Ztring_Rounded) const; 00274 /// @brief Convert into float 00275 /// @return the value corresponding \n 00276 /// 0 if there is a problem 00277 float32 To_float32 (ztring_t Options=Ztring_Nothing) const; 00278 float64 To_float64 (ztring_t Options=Ztring_Nothing) const; 00279 float80 To_float80 (ztring_t Options=Ztring_Nothing) const; 00280 00281 //Static versions 00282 static Ztring ToZtring_From_Local(const std::string &S) {return Ztring().From_Local(S);}; 00283 static Ztring ToZtring_From_Local(const char *S) {return Ztring().From_Local(S);}; 00284 static Ztring ToZtring_From_Local(const char *S, size_type Start, size_type Length) {return Ztring().From_Local(S, Start, Length);}; 00285 static Ztring ToZtring_From_Local(const char *S, size_type Length) {return Ztring().From_Local(S, Length);}; 00286 static Ztring ToZtring_From_CC4 (const char *S) {return Ztring().From_CC4(S);}; 00287 static Ztring ToZtring_From_CC4 (const int8u *S) {return Ztring().From_CC4(S);}; 00288 static Ztring ToZtring_From_CC4 (const int32u S) {return Ztring().From_CC4(S);}; 00289 static Ztring ToZtring_From_CC3 (const char *S) {return Ztring().From_CC3(S);}; 00290 static Ztring ToZtring_From_CC3 (const int8u *S) {return Ztring().From_CC3(S);}; 00291 static Ztring ToZtring_From_CC3 (const int32u S) {return Ztring().From_CC3(S);}; 00292 static Ztring ToZtring_From_CC2 (const char *S) {return Ztring().From_CC2(S);}; 00293 static Ztring ToZtring_From_CC2 (const int8u *S) {return Ztring().From_CC2(S);}; 00294 static Ztring ToZtring_From_CC2 (const int16u S) {return Ztring().From_CC2(S);}; 00295 static Ztring ToZtring_From_CC1 (const char *S) {return Ztring().From_CC1(S);}; 00296 static Ztring ToZtring_From_CC1 (const int8u *S) {return Ztring().From_CC1(S);}; 00297 static Ztring ToZtring_From_CC1 (const int8u S) {return Ztring().From_CC1(S);}; 00298 static Ztring ToZtring (const int8s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00299 static Ztring ToZtring (const int8u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00300 static Ztring ToZtring (const int16s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00301 static Ztring ToZtring (const int16u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00302 static Ztring ToZtring (const int32s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00303 static Ztring ToZtring (const int32u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00304 static Ztring ToZtring (const int64s I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00305 static Ztring ToZtring (const int64u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00306 static Ztring ToZtring (const int128u I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00307 static Ztring ToZtring (const float32 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);}; 00308 static Ztring ToZtring (const float64 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);}; 00309 static Ztring ToZtring (const float80 F, int8u AfterComma=3) {return Ztring().From_Number(F, AfterComma);}; 00310 #ifdef NEED_SIZET 00311 static Ztring ToZtring (const size_t I, int8u Radix=10) {return Ztring().From_Number(I, Radix);}; 00312 #endif //NEED_SIZET 00313 00314 //Edition 00315 /// @brief test if it is a number 00316 bool IsNumber() const; 00317 /// @brief convert into lowercase 00318 Ztring &MakeLowerCase(); 00319 /// @brief convert into uppercase 00320 Ztring &MakeUpperCase(); 00321 /// @brief Remove leading whitespaces from a string 00322 Ztring &TrimLeft(Char ToTrim=_T(' ')); 00323 /// @brief Remove trailing whitespaces from a string 00324 Ztring &TrimRight(Char ToTrim=_T(' ')); 00325 /// @brief Remove leading and trailing whitespaces from a string 00326 Ztring &Trim(Char ToTrim=_T(' ')); 00327 /// @brief Quotes a string 00328 Ztring &Quote(Char ToTrim=_T('\"')); 00329 /// @brief return a string between two strings 00330 /// @param Begin First string 00331 /// @param End Second string 00332 /// @param Pos Position to begin to scan string 00333 /// @param Options Options for searching \n 00334 /// Available : Ztring_CaseSensitive 00335 /// @return The substring \n 00336 /// "" if not found 00337 Ztring SubString (const tstring &Begin, const tstring &End, size_type Pos=0, ztring_t Options=Ztring_Nothing) const; 00338 /// @brief replace a string by another one 00339 /// @param ToFind string to find 00340 /// @param ToReplace string wich replace the string found 00341 /// @param Pos Position to begin to scan string 00342 /// @param Options Options for searching \n 00343 /// Available : Ztring_CaseSensitive, Ztring_Recursive 00344 /// @return The count of replacements 00345 size_type FindAndReplace (const tstring &ToFind, const tstring &ReplaceBy, size_type Pos=0, ztring_t Options=Ztring_Nothing); //Remplace une chaine par une autre 00346 /// @brief Count the number of occurencies of a string in the string 00347 /// @param ToCount string to count 00348 /// @param Options Options for count \n 00349 /// Available : Ztring_CaseSensitive 00350 /// @return the count 00351 00352 //Information 00353 size_type Count (const Ztring &ToCount, ztring_t Options=Ztring_Nothing) const; 00354 /// @brief compare with another string 00355 /// @param ToCompare string to compare with 00356 /// @param Options Options for comaparing \n 00357 /// Available : Ztring_CaseSensitive 00358 /// @return The result of comparasion 00359 bool Compare (const Ztring &ToCompare, const Ztring &Comparator=_T("=="), ztring_t Options=Ztring_Nothing) const; 00360 }; 00361 00362 } //NameSpace 00363 00364 #endif 00365