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