Ztring.h

Go to the documentation of this file.
00001 // ZenLib::Ztring - More methods for std::(w)string
00002 // Copyright (C) 2002-2007 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 <string>
00036 #include <sstream>
00037 //---------------------------------------------------------------------------
00038 
00039 namespace ZenLib
00040 {
00041 
00042 //---------------------------------------------------------------------------
00043 typedef std::basic_string<Char, std::char_traits<Char>, std::allocator<Char> > tstring;
00044 //---------------------------------------------------------------------------
00045 
00046 //---------------------------------------------------------------------------
00047 /// @brief Options for Ztring methods
00048 enum ztring_t
00049 {
00050     Ztring_Nothing,
00051     Ztring_Rounded              = 1,            ///< if >.5, upper, else lower
00052     Ztring_CaseSensitive        = 2,            ///< Case sensitive ("A" and "a" are different)
00053     Ztring_AddLastItem          = 4,            ///< if Begin is found and End is not found, return between Begin and end of string
00054     Ztring_Recursive            = 8,            ///< Do all strings
00055     Ztring_NoZero               =16             ///> Doesn't keep Zero in the float number
00056 };
00057 
00058 //---------------------------------------------------------------------------
00059 
00060 //***************************************************************************
00061 /// @brief String manipulation (based on std::(w)string)
00062 //***************************************************************************
00063 
00064 class Ztring : public tstring  //for details about undocumented methods see http://www.sgi.com/tech/stl/basic_string.html
00065 {
00066 public :
00067     //Constructor/destructor
00068     Ztring ()                                                                   : tstring(){};
00069     Ztring (const tstring& S)                                                   : tstring(S.c_str()){};
00070     Ztring (const tstring& S, size_type Pos,   size_type n=npos)                : tstring(S.c_str(),  Pos, n){};
00071     Ztring (const tstring* S)                                                   : tstring(S->c_str()){};
00072     Ztring (const tstring* S, size_type Pos, size_type n=npos)                  : tstring(S->c_str(), Pos, n){};
00073     Ztring (const Char* S)                                                      : tstring(S){};
00074     Ztring (const Char* S, size_type n)                                         : tstring(S, n){};
00075     Ztring (size_type n, const Char &S)                                         : tstring(n, S){};
00076     #ifdef UNICODE
00077     Ztring (const char* S)                                                      : tstring(){From_UTF8(S);};
00078     Ztring (const char* S, size_type n)                                         : tstring(){From_UTF8(S, 0, n);};
00079     #endif //UNICODE
00080 
00081     //Operators
00082         ///Same as [], but resize the string if Pos doesn't exist yet
00083     Char &operator () (size_type Pos);
00084 
00085     //Conversions - From
00086         /// @brief convert an Unicode encoded string into Ztring
00087     Ztring& From_Unicode (const std::wstring &S)                                {return From_Unicode(S.c_str());};
00088         /// @brief convert an Unicode encoded string into Ztring
00089     Ztring& From_Unicode (const wchar_t *S);
00090         /// @brief convert an Unicode encoded string into Ztring
00091     Ztring& From_Unicode (const wchar_t *S, size_type Start, size_type Length);
00092         /// @brief convert an Unicode encoded string into Ztring
00093     Ztring& From_Unicode (const wchar_t *S, size_type Length)                   {return From_Unicode(S, 0, Length);};
00094         /// @brief convert an UTF-8 encoded string into Ztring
00095     Ztring& From_UTF8    (const std::string &S)                                 {return From_UTF8(S.c_str());};
00096         /// @brief convert an UTF-8 encoded string into Ztring
00097     Ztring& From_UTF8    (const char *S);
00098         /// @brief convert an UTF-8 encoded string into Ztring
00099     Ztring& From_UTF8    (const char *S, size_type Start, size_type Length);
00100         /// @brief convert an UTF-8 encoded string into Ztring
00101     Ztring& From_UTF8    (const char *S, size_type Length)                      {return From_UTF8(S, 0, Length);};
00102         /// @brief convert an UTF-16 encoded string into Ztring
00103     Ztring& From_UTF16   (const char *S);
00104         /// @brief convert an UTF-16 encoded string into Ztring
00105     Ztring& From_UTF16   (const char *S, size_type Start, size_type Length);
00106         /// @brief convert an UTF-16 encoded string into Ztring
00107     Ztring& From_UTF16   (const char *S, size_type Length)                      {return From_UTF16(S, 0, Length);};
00108         /// @brief convert an UTF-16BE encoded string into Ztring
00109     Ztring& From_UTF16BE (const char *S);
00110         /// @brief convert an UTF-16BE encoded string into Ztring
00111     Ztring& From_UTF16BE (const char *S, size_type Start, size_type Length);
00112         /// @brief convert an UTF-16BE encoded string into Ztring
00113     Ztring& From_UTF16BE (const char *S, size_type Length)                      {return From_UTF16BE(S, 0, Length);};
00114         /// @brief convert an UTF-16LE encoded string into Ztring
00115     Ztring& From_UTF16LE (const char *S);
00116         /// @brief convert an UTF-16LE encoded string into Ztring
00117     Ztring& From_UTF16LE (const char *S, size_type Start, size_type Length);
00118         /// @brief convert an UTF-16LE encoded string into Ztring
00119     Ztring& From_UTF16LE (const char *S, size_type Length)                      {return From_UTF16LE(S, 0, Length);};
00120         /// @brief convert an Locael encoded string into Ztring
00121     Ztring& From_Local   (const std::string &S)                                 {return From_Local(S.c_str());};
00122         /// @brief convert an Local encoded string into Ztring
00123     Ztring& From_Local   (const char *S);
00124         /// @brief convert an Local encoded string into Ztring
00125     Ztring& From_Local   (const char *S, size_type Start,  size_type Length);
00126         /// @brief convert an Local encoded string into Ztring
00127     Ztring& From_Local   (const char *S, size_type Length)                      {return From_Local(S, 0, Length);};
00128         /// @brief convert an 4 Character Code into Ztring
00129     Ztring& From_CC4     (const char *S)                                        {return From_Local(S, 0, 4);};
00130         /// @brief convert an 4 Character Code into Ztring
00131     Ztring& From_CC4     (const int8u *S)                                       {return From_Local((const char*)S, 0, 4);};
00132         /// @brief convert an 4 Character Code into Ztring
00133     Ztring& From_CC4     (const int32u S);
00134         /// @brief convert an 2 Character Code into Ztring
00135     Ztring& From_CC2     (const char *S)                                        {return From_Number(ZenLib::CC2(S), 16);};
00136         /// @brief convert an 2 Character Code into Ztring
00137     Ztring& From_CC2     (const int8u *S)                                       {return From_Number(ZenLib::CC2(S), 16);};
00138         /// @brief convert an 2 Character Code into Ztring
00139     Ztring& From_CC2     (const int16u S)                                       {return From_Number(S, 16);};
00140         /// @brief convert number into Ztring
00141     Ztring& From_Number  (const int8s,    int8u Radix=10);
00142         /// @brief convert number into Ztring
00143     Ztring& From_Number  (const int8u,    int8u Radix=10);
00144         /// @brief convert number into Ztring
00145     Ztring& From_Number  (const int16s,   int8u Radix=10);
00146         /// @brief convert number into Ztring
00147     Ztring& From_Number  (const int16u,   int8u Radix=10);
00148         /// @brief convert number into Ztring
00149     Ztring& From_Number  (const int32s,   int8u Radix=10);
00150         /// @brief convert number into Ztring
00151     Ztring& From_Number  (const int32u,   int8u Radix=10);
00152         /// @brief convert number into Ztring
00153     Ztring& From_Number  (const int64s,   int8u Radix=10);
00154         /// @brief convert number into Ztring
00155     Ztring& From_Number  (const int64u,   int8u Radix=10);
00156         /// @brief convert number into Ztring
00157     Ztring& From_Number  (const float32,  int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
00158         /// @brief convert number into Ztring
00159     Ztring& From_Number  (const float64,  int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
00160         /// @brief convert number into Ztring
00161     Ztring& From_Number  (const float80,  int8u AfterComma=3, ztring_t Options=Ztring_Nothing);
00162     #ifdef NEED_SIZET
00163         /// @brief convert number into Ztring
00164     Ztring& From_Number  (const size_t,   int8u Radix=10);
00165     #endif //NEED_SIZET
00166         /// @brief convert count of milliseconds into a readable and sortable string
00167     Ztring& Duration_From_Milliseconds (const int64u Milliseconds);
00168         /// @brief convert count of seconds since 1601 into a readable and sortable string
00169     Ztring& Date_From_Seconds_1601 (const int64u Seconds);
00170         /// @brief convert count of seconds since 1970 into a readable and sortable string
00171     Ztring& Date_From_Seconds_1904 (const int64u Seconds);
00172         /// @brief convert count of seconds since 1970 into a readable and sortable string
00173     Ztring& Date_From_Seconds_1970 (const int32u Seconds);
00174         /// @brief convert a free formated string into a readable and sortable string
00175     Ztring& Date_From_String (const char* Date, size_type Value_Size=Error);
00176 
00177     //Conversions - To
00178         /// @brief Convert into Unicode chars
00179         /// @return the string corresponding \n
00180     std::wstring To_Unicode () const;
00181         /// @brief Convert into char* (UTF-8 encoded)
00182         /// @return the string corresponding \n
00183     std::string To_UTF8     () const;
00184         /// @brief Convert into char* (Local encoded)
00185         /// @return the string corresponding \n
00186     std::string To_Local    () const;
00187         /// @brief Convert into Int (32 bits)
00188         /// @return the value corresponding \n
00189         ///         0 if there is a problem
00190     int32s      To_int32s   (ztring_t Options=Ztring_Rounded) const;
00191         /// @brief Convert into unsigned Int (32 bits)
00192         /// @return the value corresponding
00193         ///         0 if there is a problem
00194     int32u      To_int32u   (ztring_t Options=Ztring_Rounded) const;
00195         /// @brief Convert into Int (64 bits)
00196         /// @return the value corresponding \n
00197         ///         0 if there is a problem
00198     int64s      To_int64s   (ztring_t Options=Ztring_Rounded) const;
00199         /// @brief Convert into unsigned Int (64 bits)
00200         /// @return the value corresponding \n
00201         ///         0 if there is a problem
00202     int64u      To_int64u   (ztring_t Options=Ztring_Rounded) const;
00203         /// @brief Convert into float
00204         /// @return the value corresponding \n
00205         ///         0 if there is a problem
00206     float32     To_float32  (ztring_t Options=Ztring_Nothing) const;
00207     float64     To_float64  (ztring_t Options=Ztring_Nothing) const;
00208     float80     To_float80  (ztring_t Options=Ztring_Nothing) const;
00209 
00210     //Static versions
00211     static Ztring ToZtring  (const int8s    I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00212     static Ztring ToZtring  (const int8u    I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00213     static Ztring ToZtring  (const int16s   I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00214     static Ztring ToZtring  (const int16u   I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00215     static Ztring ToZtring  (const int32s   I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00216     static Ztring ToZtring  (const int32u   I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00217     static Ztring ToZtring  (const int64s   I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00218     static Ztring ToZtring  (const int64u   I, int8u Radix=10)                   {return Ztring().From_Number(I, Radix);};
00219     static Ztring ToZtring  (const float32  F, int8u AfterComma=3)               {return Ztring().From_Number(F, AfterComma);};
00220     static Ztring ToZtring  (const float64  F, int8u AfterComma=3)               {return Ztring().From_Number(F, AfterComma);};
00221     static Ztring ToZtring  (const float80  F, int8u AfterComma=3)               {return Ztring().From_Number(F, AfterComma);};
00222     #ifdef NEED_SIZET
00223     static Ztring ToZtring  (const size_t   I,  int8u Radix=10)                  {return Ztring().From_Number(I, Radix);};
00224     #endif //NEED_SIZET
00225 
00226     //Edition
00227         /// @brief convert into lowercase
00228     Ztring &MakeLowerCase();
00229         /// @brief convert into uppercase
00230     Ztring &MakeUpperCase();
00231         /// @brief Remove leading whitespaces from a string
00232     Ztring &TrimLeft(Char ToTrim=_T(' '));
00233         /// @brief Remove trailing whitespaces from a string
00234     Ztring &TrimRight(Char ToTrim=_T(' '));
00235         /// @brief Remove leading and trailing whitespaces from a string
00236     Ztring &Trim(Char ToTrim=_T(' '));
00237         /// @brief Quotes a string
00238     Ztring &Quote(Char ToTrim=_T('\"'));
00239         /// @brief return a string between two strings
00240         /// @param Begin First string
00241         /// @param End Second string
00242         /// @param Pos Position to begin to scan string
00243         /// @param Options Options for searching \n
00244         ///                Available : Ztring_CaseSensitive
00245         /// @return The substring \n
00246         ///         "" if not found
00247     Ztring SubString (const tstring &Begin, const tstring &End, size_type Pos=0, ztring_t Options=Ztring_Nothing) const;
00248         /// @brief replace a string by another one
00249         /// @param ToFind string to find
00250         /// @param ToReplace string wich replace the string found
00251         /// @param Pos Position to begin to scan string
00252         /// @param Options Options for searching \n
00253         ///                Available : Ztring_CaseSensitive, Ztring_Recursive
00254         /// @return The count of replacements
00255     size_type FindAndReplace (const tstring &ToFind, const tstring &ReplaceBy, size_type Pos=0, ztring_t Options=Ztring_Nothing); //Remplace une chaine par une autre
00256         /// @brief Count the number of occurencies of a string in the string
00257         /// @param ToCount string to count
00258         /// @param Options Options for count \n
00259         ///                Available : Ztring_CaseSensitive
00260         /// @return the count
00261 
00262     //Information
00263     size_type Count (const Ztring &ToCount, ztring_t Options=Ztring_Nothing) const;
00264         /// @brief compare with another string
00265         /// @param ToCompare string to compare with
00266         /// @param Options Options for comaparing \n
00267         ///                Available : Ztring_CaseSensitive
00268         /// @return The result of comparasion
00269     bool Compare (const Ztring &ToCompare, const Ztring &Comparator=_T("=="), ztring_t Options=Ztring_Nothing) const;
00270 };
00271 
00272 } //NameSpace
00273 
00274 #endif

Generated on Mon May 14 15:50:56 2007 for ZenLib by  doxygen 1.5.1-p1