Main Page   Namespace List   Class Hierarchy   Compound List   File List   Namespace Members   Compound Members   File Members  

Ztring.h

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

Generated on Mon Jul 24 09:22:58 2006 for ZenLib by doxygen1.3-rc3