Ztring.h Source File

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