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