Ztring.h

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

Generated on Fri Mar 20 09:24:08 2009 for ZenLib by  doxygen 1.4.7