pstring.h

Go to the documentation of this file.
00001 /*
00002  * pstring.h
00003  *
00004  * Character string class.
00005  *
00006  * Portable Windows Library
00007  *
00008  * Copyright (c) 1993-1998 Equivalence Pty. Ltd.
00009  *
00010  * The contents of this file are subject to the Mozilla Public License
00011  * Version 1.0 (the "License"); you may not use this file except in
00012  * compliance with the License. You may obtain a copy of the License at
00013  * http://www.mozilla.org/MPL/
00014  *
00015  * Software distributed under the License is distributed on an "AS IS"
00016  * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See
00017  * the License for the specific language governing rights and limitations
00018  * under the License.
00019  *
00020  * The Original Code is Portable Windows Library.
00021  *
00022  * The Initial Developer of the Original Code is Equivalence Pty. Ltd.
00023  *
00024  * Portions are Copyright (C) 1993 Free Software Foundation, Inc.
00025  * All Rights Reserved.
00026  *
00027  * Contributor(s): ______________________________________.
00028  *
00029  * $Revision: 20428 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-06-10 11:59:48 +0000 (Tue, 10 Jun 2008) $
00032  */
00033 #ifndef __PSTRING__
00034 #define __PSTRING__
00035 
00036 #ifdef P_USE_PRAGMA
00037 #pragma interface
00038 #endif
00039 
00040 #include <string>
00041 #include <vector>
00042 #include <ptlib/array.h>
00043 
00045 // PString class
00046 
00047 class PStringArray;
00048 class PRegularExpression;
00049 
00050 #if (defined(_WIN32) || defined(_WIN32_WCE)) && (!defined(_NATIVE_WCHAR_T_DEFINED)) && (!defined(__MINGW32__))
00051 PBASEARRAY(PWCharArray, unsigned short);
00052 #else
00053 PBASEARRAY(PWCharArray, wchar_t);
00054 #endif
00055 
00090 class PString : public PCharArray {
00091   PCLASSINFO(PString, PCharArray);
00092 
00093 //  using namespace std;
00094 
00095   public:
00101     PINLINE PString();
00102 
00106     PINLINE PString(
00107       const PString & str  
00108     );
00109 
00112     PINLINE PString(
00113       const std::string & str
00114     );
00115 
00124     PString(
00125       const char * cstr 
00126     );
00127 
00132     PString(
00133       const wchar_t * ustr 
00134     );
00135 
00149     PString(
00150       const char * cstr,  
00151       PINDEX len          
00152     );
00153 
00164     PString(
00165       const wchar_t * ustr,  
00166       PINDEX len          
00167     );
00168 
00179     PString(
00180       const PWCharArray & ustr 
00181     );
00182 
00191     PString(
00192       char ch    
00193     );
00194 
00199     PString(
00200       short n   
00201     );
00202 
00207     PString(
00208       unsigned short n   
00209     );
00210 
00215     PString(
00216       int n   
00217     );
00218 
00223     PString(
00224       unsigned int n   
00225     );
00226 
00231     PString(
00232       long n   
00233     );
00234 
00239     PString(
00240       unsigned long n   
00241     );
00242 
00247     PString(
00248       PInt64 n   
00249     );
00250 
00255     PString(
00256       PUInt64 n   
00257     );
00258 
00259  
00260     enum ConversionType {
00261       Pascal,   // Data is a length byte followed by characters.
00262       Basic,    // Data is two length bytes followed by characters.
00263       Literal,  // Data is C language style string with \ escape codes.
00264       Signed,   // Convert a signed integer to a string.
00265       Unsigned, // Convert an unsigned integer to a string.
00266       Decimal,  // Convert a real number to a string in decimal format.
00267       Exponent, // Convert a real number to a string in exponent format.
00268       Printf,   // Formatted output, sprintf() style function.
00269       NumConversionTypes
00270     };
00271     /* Type of conversion to make in the conversion constructors.
00272      */
00273 
00274     /* Contruct a new string converting from the spcified data source into
00275        a string array.
00276      */
00277     PString(
00278       ConversionType type,  
00279       const char * str,    
00280       ...                 
00281     );
00282     PString(
00283       ConversionType type,  
00284       long value,           
00285       unsigned base = 10    
00286     );
00287     PString(
00288       ConversionType type,  
00289       double value,         
00290       unsigned places       
00291     );
00292 
00300     PString & operator=(
00301       const PString & str  
00302     );
00303 
00313     PString & operator=(
00314       const char * cstr  
00315     );
00316 
00325     PString & operator=(
00326       char ch            
00327     );
00328 
00333     PString & operator=(
00334       short n   
00335     );
00336 
00341     PString & operator=(
00342       unsigned short n   
00343     );
00344 
00349     PString & operator=(
00350       int n   
00351     );
00352 
00357     PString & operator=(
00358       unsigned int n   
00359     );
00360 
00365     PString & operator=(
00366       long n   
00367     );
00368 
00373     PString & operator=(
00374       unsigned long n   
00375     );
00376 
00381     PString & operator=(
00382       PInt64 n   
00383     );
00384 
00389     PString & operator=(
00390       PUInt64 n   
00391     );
00392 
00395     virtual PString & MakeEmpty();
00396 
00399     static PString Empty();
00401 
00408     virtual PObject * Clone() const;
00409 
00419     virtual Comparison Compare(
00420       const PObject & obj   
00421     ) const;
00422 
00425     virtual void PrintOn(
00426       ostream & strm  
00427     ) const;
00428 
00434     virtual void ReadFrom(
00435       istream & strm  
00436     );
00437 
00451     virtual PINDEX HashFunction() const;
00453 
00468     virtual PBoolean SetSize(
00469       PINDEX newSize  
00470     );
00471 
00480     virtual PBoolean IsEmpty() const;
00481 
00490     virtual PBoolean MakeUnique();
00492 
00493 
00506     PBoolean MakeMinimumSize();
00507 
00516     PINLINE PINDEX GetLength() const;
00517 
00524     bool operator!() const;
00526 
00535     PString operator+(
00536       const PString & str   
00537     ) const;
00538 
00550     PString operator+(
00551       const char * cstr  
00552     ) const;
00553 
00565     PString operator+(
00566       char ch   
00567     ) const;
00568 
00580     friend PString operator+(
00581       const char * cstr,    
00582       const PString & str   
00583     );
00584 
00596     friend PString operator+(
00597       char  c,              
00598       const PString & str   
00599     );
00600 
00606     PString & operator+=(
00607       const PString & str   
00608     );
00609 
00619     PString & operator+=(
00620       const char * cstr  
00621     );
00622 
00632     PString & operator+=(
00633       char ch   
00634     );
00635 
00636 
00643     PString operator&(
00644       const PString & str   
00645     ) const;
00646 
00663     PString operator&(
00664       const char * cstr  
00665     ) const;
00666 
00683     PString operator&(
00684       char ch   
00685     ) const;
00686 
00703     friend PString operator&(
00704       const char * cstr,    
00705       const PString & str   
00706     );
00707 
00724     friend PString operator&(
00725       char  ch,              
00726       const PString & str   
00727     );
00728 
00734     PString & operator&=(
00735       const PString & str   
00736     );
00737 
00752     PString & operator&=(
00753       const char * cstr  
00754     );
00755 
00756 
00771     PString & operator&=(
00772       char ch  
00773     );
00775 
00776 
00784     bool operator*=(
00785       const PString & str  
00786     ) const;
00787 
00795     bool operator==(
00796       const PObject & str  
00797     ) const;
00798 
00806     bool operator!=(
00807       const PObject & str  
00808     ) const;
00809 
00817     bool operator<(
00818       const PObject & str  
00819     ) const;
00820 
00828     bool operator>(
00829       const PObject & str  
00830     ) const;
00831 
00839     bool operator<=(
00840       const PObject & str  
00841     ) const;
00842 
00850     bool operator>=(
00851       const PObject & str  
00852     ) const;
00853 
00854 
00865     bool operator*=(
00866       const char * cstr  
00867     ) const;
00868 
00879     bool operator==(
00880       const char * cstr  
00881     ) const;
00882 
00893     bool operator!=(
00894       const char * cstr  
00895     ) const;
00896 
00907     bool operator<(
00908       const char * cstr  
00909     ) const;
00910 
00921     bool operator>(
00922       const char * cstr  
00923     ) const;
00924 
00935     bool operator<=(
00936       const char * cstr  
00937     ) const;
00938 
00949     bool operator>=(
00950       const char * cstr  
00951     ) const;
00952 
00964     Comparison NumCompare(
00965       const PString & str,        
00966       PINDEX count = P_MAX_INDEX, 
00967       PINDEX offset = 0           
00968     ) const;
00969 
00981     Comparison NumCompare(
00982       const char * cstr,          
00983       PINDEX count = P_MAX_INDEX, 
00984       PINDEX offset = 0           
00985     ) const;
00987 
00988 
00992     PINDEX Find(
00993       char ch,              
00994       PINDEX offset = 0     
00995     ) const;
00996 
00998     PINDEX Find(
00999       const PString & str,  
01000       PINDEX offset = 0     
01001     ) const;
01002 
01003     /* Locate the position within the string of the character or substring. The
01004        search will begin at the character offset provided.
01005        
01006        If #offset# is beyond the length of the string, then the
01007        function will always return #P_MAX_INDEX#.
01008        
01009        The matching will be for identical character or string. If a search
01010        ignoring case is required then the string should be converted to a
01011        #PCaselessString# before the search is made.
01012 
01013        @return
01014        position of character or substring in the string, or P_MAX_INDEX if the
01015        character or substring is not in the string.
01016      */
01017     PINDEX Find(
01018       const char * cstr,    
01019       PINDEX offset = 0     
01020     ) const;
01021 
01023     PINDEX FindLast(
01024       char ch,                     
01025       PINDEX offset = P_MAX_INDEX  
01026     ) const;
01027 
01029     PINDEX FindLast(
01030       const PString & str,         
01031       PINDEX offset = P_MAX_INDEX  
01032     ) const;
01033 
01051     PINDEX FindLast(
01052       const char * cstr,           
01053       PINDEX offset = P_MAX_INDEX  
01054     ) const;
01055 
01057     PINDEX FindOneOf(
01058       const PString & set,  
01059       PINDEX offset = 0     
01060     ) const;
01061 
01076     PINDEX FindOneOf(
01077       const char * cset,    
01078       PINDEX offset = 0     
01079     ) const;
01080 
01082     PINDEX FindSpan(
01083       const PString & set,  
01084       PINDEX offset = 0     
01085     ) const;
01086 
01102     PINDEX FindSpan(
01103       const char * cset,    
01104       PINDEX offset = 0     
01105     ) const;
01106 
01117     PINDEX FindRegEx(
01118       const PRegularExpression & regex, 
01119       PINDEX offset = 0                 
01120     ) const;
01121 
01132     PBoolean FindRegEx(
01133       const PRegularExpression & regex, 
01134       PINDEX & pos,                     
01135       PINDEX & len,                     
01136       PINDEX offset = 0,                
01137       PINDEX maxPos = P_MAX_INDEX       
01138     ) const;
01139 
01140 
01151     void Replace(
01152       const PString & target,   
01153       const PString & subs,     
01154       PBoolean all = PFalse,         
01155       PINDEX offset = 0         
01156     );
01157 
01165     void Splice(
01166       const PString & str,  
01167       PINDEX pos,           
01168       PINDEX len = 0        
01169     );
01170 
01178     void Splice(
01179       const char * cstr,    
01180       PINDEX pos,           
01181       PINDEX len = 0        
01182     );
01183 
01190     void Delete(
01191       PINDEX start,   
01192       PINDEX len      
01193     );
01195 
01196 
01216     PString operator()(
01217       PINDEX start,  
01218       PINDEX end     
01219     ) const;
01220 
01235     PString Left(
01236       PINDEX len   
01237     ) const;
01238 
01253     PString Right(
01254       PINDEX len   
01255     ) const;
01256 
01273     PString Mid(
01274       PINDEX start,             
01275       PINDEX len = P_MAX_INDEX  
01276     ) const;
01277 
01278 
01286     PString LeftTrim() const;
01287 
01295     PString RightTrim() const;
01296 
01305     PString Trim() const;
01306 
01307 
01316     PString ToLower() const;
01317 
01326     PString ToUpper() const;
01327 
01328 
01330     PStringArray Tokenise(
01331       const PString & separators,
01333       PBoolean onePerSeparator = PTrue
01335     ) const;
01360     PStringArray Tokenise(
01361       const char * cseparators,
01363       PBoolean onePerSeparator = PTrue
01365     ) const;
01366 
01380     PStringArray Lines() const;
01382 
01399     PString & sprintf(
01400       const char * cfmt,   
01401       ...                  
01402     );
01403 
01418     friend PString psprintf(
01419       const char * cfmt,   
01420       ...                  
01421     );
01422 
01424     PString & vsprintf(
01425       const PString & fmt, 
01426       va_list args         
01427     );
01442     PString & vsprintf(
01443       const char * cfmt,   
01444       va_list args         
01445     );
01446 
01448     friend PString pvsprintf(
01449       const char * cfmt,   
01450       va_list args         
01451     );
01466     friend PString pvsprintf(
01467       const PString & fmt, 
01468       va_list args         
01469     );
01470 
01471 
01484     long AsInteger(
01485       unsigned base = 10    
01486     ) const;
01499     DWORD AsUnsigned(
01500       unsigned base = 10    
01501     ) const;
01515     PInt64 AsInt64(
01516       unsigned base = 10    
01517     ) const;
01531     PUInt64 AsUnsigned64(
01532       unsigned base = 10    
01533     ) const;
01534 
01545     double AsReal() const;
01546      
01550     PWCharArray AsUCS2() const;
01551 
01562     PBYTEArray ToPascal() const;
01563 
01572     PString ToLiteral() const;
01573 
01581     operator const unsigned char *() const;
01582 
01585     operator std::string () const
01586     { return std::string(theArray); }
01588 
01589 
01590   protected:
01591     void InternalFromUCS2(
01592       const wchar_t * ptr,
01593       PINDEX len
01594     );
01595     virtual Comparison InternalCompare(
01596       PINDEX offset,      // Offset into string to compare.
01597       char c              // Character to compare against.
01598     ) const;
01599     virtual Comparison InternalCompare(
01600       PINDEX offset,      // Offset into string to compare.
01601       PINDEX length,      // Number of characters to compare.
01602       const char * cstr   // C string to compare against.
01603     ) const;
01604     /* Internal function to compare the current string value against the
01605        specified C string.
01606 
01607        @return
01608        relative rank of the two strings.
01609      */
01610 
01611     PString(int dummy, const PString * str);
01612 };
01613 
01614 
01615 inline ostream & operator<<(ostream & stream, const PString & string)
01616 {
01617   string.PrintOn(stream);
01618   return stream;
01619 }
01620 
01621 
01622 #ifdef _WIN32
01623   class PWideString : public PWCharArray {
01624     PCLASSINFO(PWideString, PWCharArray);
01625 
01626     public:
01627       PWideString() { }
01628       PWideString(const PWCharArray & arr) : PWCharArray(arr) { }
01629       PWideString(const PString     & str) : PWCharArray(str.AsUCS2()) { }
01630       PWideString(const char        * str) : PWCharArray(PString(str).AsUCS2()) { }
01631       PWideString & operator=(const PWideString & str) { PWCharArray::operator=(str); return *this; }
01632       PWideString & operator=(const PString     & str) { PWCharArray::operator=(str.AsUCS2()); return *this; }
01633       PWideString & operator=(const char        * str) { PWCharArray::operator=(PString(str).AsUCS2()); return *this; }
01634       friend inline ostream & operator<<(ostream & stream, const PWideString & string) { return stream << PString(string); }
01635   };
01636 
01637   #ifdef UNICODE
01638     typedef PWideString PVarString;
01639   #else
01640     typedef PString PVarString;
01641   #endif
01642 #endif
01643 
01644 
01646 
01656 class PCaselessString : public PString
01657 {
01658   PCLASSINFO(PCaselessString, PString);
01659 
01660   public:
01663     PCaselessString();
01664 
01668     PCaselessString(
01669       const char * cstr   
01670     );
01671 
01676     PCaselessString(
01677       const PString & str  
01678     );
01679 
01680 
01683     PCaselessString(
01684       const std::string & str  
01685       ) : PString(str)
01686     { }
01687 
01695     PCaselessString & operator=(
01696       const PString & str  
01697     );
01698 
01708     PCaselessString & operator=(
01709       const char * cstr  
01710     );
01711 
01720     PCaselessString & operator=(
01721       char ch            
01722     );
01723 
01724 
01725   // Overrides from class PObject
01730     virtual PObject * Clone() const;
01731 
01732   protected:
01733   // Overrides from class PString
01734     virtual Comparison InternalCompare(
01735       PINDEX offset,      // Offset into string to compare.
01736       char c              // Character to compare against.
01737     ) const;
01738     virtual Comparison InternalCompare(
01739       PINDEX offset,      // Offset into string to compare.
01740       PINDEX length,      // Number of characters to compare.
01741       const char * cstr   // C string to compare against.
01742     ) const;
01743     /* Internal function to compare the current string value against the
01744        specified C string.
01745 
01746        @return
01747        relative rank of the two strings or characters.
01748      */
01749 
01750     PCaselessString(int dummy, const PCaselessString * str);
01751 };
01752 
01754 
01755 class PStringStream;
01756 
01763 class PStringStream : public PString, public iostream
01764 {
01765   PCLASSINFO(PStringStream, PString);
01766 
01767   public:
01773     PStringStream();
01774 
01779     PStringStream(
01780       PINDEX fixedBufferSize
01781     );
01782 
01789     PStringStream(
01790       const PString & str   
01791     );
01792 
01797     PStringStream(
01798       const char * cstr   
01799     );
01800 
01803     virtual PString & MakeEmpty();
01804 
01816     PStringStream & operator=(
01817       const PStringStream & strm
01818     );
01819 
01831     PStringStream & operator=(
01832       const PString & str  
01833     );
01834 
01850     PStringStream & operator=(
01851       const char * cstr  
01852     );
01853 
01862     PStringStream & operator=(
01863       char ch            
01864     );
01865 
01866 
01868     virtual ~PStringStream();
01869 
01870 
01871   protected:
01872     virtual void AssignContents(const PContainer & cont);
01873 
01874   private:
01875     PStringStream(int, const PStringStream &) : iostream(cout.rdbuf()) { }
01876 
01877     class Buffer : public streambuf {
01878       public:
01879         Buffer(PStringStream & str, PINDEX size);
01880         Buffer(const Buffer & sbuf);
01881         Buffer & operator=(const Buffer & sbuf);
01882         virtual int_type overflow(int_type = EOF);
01883         virtual int_type underflow();
01884         virtual int sync();
01885         virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
01886         virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
01887         PStringStream & string;
01888         PBoolean            fixedBufferSize;
01889     };
01890 };
01891 
01892 
01893 class PStringList;
01894 class PSortedStringList;
01895 
01908 #ifdef DOC_PLUS_PLUS
01909 class PStringArray : public PArray {
01910 #endif
01911   PDECLARE_ARRAY(PStringArray, PString);
01912   public:
01919     PStringArray(
01920       PINDEX count,                 
01921       char const * const * strarr,  
01922       PBoolean caseless = PFalse         
01923     );
01926     PStringArray(
01927       const PString & str  
01928     );
01931     PStringArray(
01932       const PStringList & list  
01933     );
01936     PStringArray(
01937       const PSortedStringList & list  
01938     );
01939 
01943     PStringArray(
01944       const std::vector<PString> & vec
01945     )
01946     {
01947       for (std::vector<PString>::const_iterator r = vec.begin(); r != vec.end(); ++r)
01948         AppendString(*r);
01949     }
01950 
01954     PStringArray(
01955       const std::vector<std::string> & vec
01956     )
01957     {
01958       for (std::vector<std::string>::const_iterator r = vec.begin(); r != vec.end(); ++r)
01959         AppendString(PString(*r));
01960     }
01961 
01965     template <typename stlContainer>
01966     static PStringArray container(
01967       const stlContainer & vec
01968     )
01969     {
01970       PStringArray list;
01971       for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
01972         list.AppendString(PString(*r));
01973       return list;
01974     }
01975 
01977 
01985     virtual void ReadFrom(
01986       istream &strm   // Stream to read the objects contents from.
01987     );
01989 
01998     PINDEX GetStringsIndex(
01999       const PString & str 
02000     ) const;
02001 
02002     PString operator[](
02003       PINDEX index  
02004     ) const;
02005 
02013     PString & operator[](
02014       PINDEX index  
02015     );
02016 
02019     PINDEX AppendString(
02020       const PString & str 
02021     );
02022 
02028     PStringArray & operator +=(const PStringArray & array);
02029     PStringArray & operator +=(const PString & str);
02030 
02031 
02038     PStringArray operator + (const PStringArray & array);
02039     PStringArray operator + (const PString & str);
02040 
02048     char ** ToCharArray(
02049       PCharArray * storage = NULL
02050     ) const;
02052 };
02053 
02054 
02067 #ifdef DOC_PLUS_PLUS
02068 class PStringList : public PList {
02069 #endif
02070 PDECLARE_LIST(PStringList, PString);
02071   public:
02076     PStringList(
02077       PINDEX count,                 
02078       char const * const * strarr,  
02079       PBoolean caseless = PFalse         
02080     );
02083     PStringList(
02084       const PString & str  
02085     );
02088     PStringList(
02089       const PStringArray & array  
02090     );
02093     PStringList(
02094       const PSortedStringList & list  
02095     );
02097 
02105     virtual void ReadFrom(
02106       istream &strm   // Stream to read the objects contents from.
02107     );
02109 
02114     PINDEX AppendString(
02115       const PString & str 
02116     );
02117 
02120     PINDEX InsertString(
02121       const PString & before,   
02122       const PString & str       
02123     );
02124 
02128     PINDEX GetStringsIndex(
02129       const PString & str   
02130     ) const;
02131 
02137     PStringList & operator +=(const PStringList & list);
02138     PStringList & operator +=(const PString & str);
02139 
02140 
02147     PStringList operator + (const PStringList & array);
02148     PStringList operator + (const PString & str);
02149 
02153     template <typename stlContainer>
02154     static PStringList container(
02155       const stlContainer & vec
02156     )
02157     {
02158       PStringList list;
02159       for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
02160         list.AppendString(PString(*r));
02161       return list;
02162     }
02164 };
02165 
02166 
02179 #ifdef DOC_PLUS_PLUS
02180 class PSortedStringList : public PSortedList {
02181 #endif
02182 PDECLARE_SORTED_LIST(PSortedStringList, PString);
02183   public:
02188     PSortedStringList(
02189       PINDEX count,                 
02190       char const * const * strarr,  
02191       PBoolean caseless = PFalse         
02192     );
02195     PSortedStringList(
02196       const PString & str  
02197     );
02200     PSortedStringList(
02201       const PStringArray & array  
02202     );
02205     PSortedStringList(
02206       const PStringList & list  
02207     );
02209 
02217     virtual void ReadFrom(
02218       istream &strm   // Stream to read the objects contents from.
02219     );
02221 
02227     PINDEX AppendString(
02228       const PString & str 
02229     );
02230 
02234     PINDEX GetStringsIndex(
02235       const PString & str   
02236     ) const;
02237 
02242     PINDEX GetNextStringsIndex(
02243       const PString & str   
02244     ) const;
02246 
02247   protected:
02248     PINDEX InternalStringSelect(
02249       const char * str,
02250       PINDEX len,
02251       Element * thisElement,
02252       Element * & lastElement
02253     ) const;
02254 };
02255 
02256 
02273 #ifdef DOC_PLUS_PLUS
02274 class PStringSet : public PSet {
02275 #endif
02276 PDECLARE_SET(PStringSet, PString, PTrue);
02277   public:
02282     PStringSet(
02283       PINDEX count,                 
02284       char const * const * strarr,  
02285       PBoolean caseless = PFalse         
02286     );
02289     PStringSet(
02290       const PString & str  
02291     );
02293 
02301     virtual void ReadFrom(
02302       istream &strm   
02303     );
02305 
02309     void Include(
02310       const PString & key 
02311     );
02313     PStringSet & operator+=(
02314       const PString & key 
02315     );
02317     void Exclude(
02318       const PString & key 
02319     );
02321     PStringSet & operator-=(
02322       const PString & key 
02323     );
02325 };
02326 
02327 
02335 template <class K> class PStringDictionary : public PAbstractDictionary
02336 {
02337   PCLASSINFO(PStringDictionary, PAbstractDictionary);
02338 
02339   public:
02348     PStringDictionary()
02349       : PAbstractDictionary() { }
02351 
02358     virtual PObject * Clone() const
02359       { return PNEW PStringDictionary(0, this); }
02361 
02376     const PString & operator[](const K & key) const
02377       { return (const PString &)GetRefAt(key); }
02378 
02392     PString operator()(const K & key, const char * dflt = "") const
02393       { if (AbstractContains(key)) return (*this)[key]; return dflt; }
02394 
02403     PBoolean Contains(
02404       const K & key   // Key to look for in the dictionary.
02405       ) const { return AbstractContains(key); }
02406 
02418     virtual PString * RemoveAt(
02419       const K & key   // Key for position in dictionary to get object.
02420     ) {
02421         PString * s = GetAt(key); AbstractSetAt(key, NULL);
02422         return reference->deleteObjects ? (s ? (PString *)-1 : NULL) : s;
02423       }
02424 
02431     virtual PString * GetAt(
02432       const K & key   // Key for position in dictionary to get object.
02433     ) const { return (PString *)AbstractGetAt(key); }
02434 
02443     virtual PBoolean SetDataAt(
02444       PINDEX index,        // Ordinal index in the dictionary.
02445       const PString & str  // New string value to put into the dictionary.
02446     ) { return PAbstractDictionary::SetDataAt(index, PNEW PString(str)); }
02447 
02459     virtual PBoolean SetAt(
02460       const K & key,       // Key for position in dictionary to add object.
02461       const PString & str  // New string value to put into the dictionary.
02462     ) { return AbstractSetAt(key, PNEW PString(str)); }
02463 
02475     const K & GetKeyAt(PINDEX index) const
02476       { return (const K &)AbstractGetKeyAt(index); }
02477 
02489     PString & GetDataAt(PINDEX index) const
02490       { return (PString &)AbstractGetDataAt(index); }
02492 
02493   protected:
02494     PStringDictionary(int dummy, const PStringDictionary * c)
02495       : PAbstractDictionary(dummy, c) { }
02496 };
02497 
02498 
02513 #define PDECLARE_STRING_DICTIONARY(cls, K) \
02514   PDECLARE_CLASS(cls, PStringDictionary<K>) \
02515   protected: \
02516     cls(int dummy, const cls * c) \
02517       : PStringDictionary<K>(dummy, c) { } \
02518   public: \
02519     cls() \
02520       : PStringDictionary<K>() { } \
02521     virtual PObject * Clone() const \
02522       { return PNEW cls(0, this); } \
02523 
02524 
02537 #define PSTRING_DICTIONARY(cls, K) typedef PStringDictionary<K> cls
02538 
02539 
02554 #ifdef DOC_PLUS_PLUS
02555 class POrdinalToString : public PStringDictionary {
02556 #endif
02557 PDECLARE_STRING_DICTIONARY(POrdinalToString, POrdinalKey);
02558   public:
02561 
02562     struct Initialiser {
02564       PINDEX key;
02566       const char * value;
02567     };
02570     POrdinalToString(
02571       PINDEX count,                
02572       const Initialiser * init     
02573     );
02575 
02583     virtual void ReadFrom(
02584       istream &strm   // Stream to read the objects contents from.
02585     );
02587 };
02588 
02601 #ifdef DOC_PLUS_PLUS
02602 class PStringToOrdinal : public POrdinalDictionary {
02603 #endif
02604 PDECLARE_ORDINAL_DICTIONARY(PStringToOrdinal, PString);
02605   public:
02608 
02609     struct Initialiser {
02611       const char * key;
02613       PINDEX value;
02614     };
02617     PStringToOrdinal(
02618       PINDEX count,                
02619       const Initialiser * init,    
02620       PBoolean caseless = PFalse        
02621     );
02623 
02631     virtual void ReadFrom(
02632       istream &strm   // Stream to read the objects contents from.
02633     );
02635 };
02636 
02637 
02651 #ifdef DOC_PLUS_PLUS
02652 class PStringToString : public PStringDictionary {
02653 #endif
02654 PDECLARE_STRING_DICTIONARY(PStringToString, PString);
02655   public:
02658 
02659     struct Initialiser {
02661       const char * key;
02663       const char * value;
02664     };
02667     PStringToString(
02668       PINDEX count,                
02669       const Initialiser * init,    
02670       PBoolean caselessKeys = PFalse,   
02671       PBoolean caselessValues = PFalse  
02672     );
02674 
02682     virtual void ReadFrom(
02683       istream &strm   // Stream to read the objects contents from.
02684     );
02686 
02699     char ** ToCharArray(
02700       bool withEqualSign,
02701       PCharArray * storage = NULL
02702     ) const;
02703 };
02704 
02705 
02711 class PRegularExpression : public PObject
02712 {
02713   PCLASSINFO(PRegularExpression, PObject);
02714 
02715   public:
02718 
02719     enum {
02721       Extended = 1,
02723       IgnoreCase = 2,
02728       AnchorNewLine = 4
02729     };
02731     enum {
02738       NotBeginningOfLine = 1,
02740       NotEndofLine = 2
02741     };
02742 
02744     PRegularExpression();
02745 
02748     PRegularExpression(
02749       const PString & pattern,    
02750       int flags = IgnoreCase      
02751     );
02752 
02755     PRegularExpression(
02756       const char * cpattern,      
02757       int flags = IgnoreCase      
02758     );
02759 
02763     PRegularExpression(
02764       const PRegularExpression &
02765     );
02766 
02770     PRegularExpression & operator =(
02771       const PRegularExpression &
02772     );
02773 
02775     ~PRegularExpression();
02777 
02780 
02781     enum ErrorCodes {
02783       NoError = 0,    
02785       NoMatch,      
02786 
02787       // POSIX regcomp return error codes.  (In the order listed in the standard.)
02789       BadPattern,  
02791       CollateError,  
02793       BadClassType,  
02795       BadEscape,    
02797       BadSubReg,
02799       UnmatchedBracket, 
02801       UnmatchedParen,
02803       UnmatchedBrace,
02805       BadBR,        
02807       RangeError,  
02809       OutOfMemory,
02811       BadRepitition,
02812 
02813       /* Error codes we've added.  */
02815       PrematureEnd,
02817       TooBig,
02819       UnmatchedRParen,
02821       NotCompiled
02822     };
02823 
02829     ErrorCodes GetErrorCode() const;
02830 
02837     PString GetErrorText() const;
02839 
02843     PBoolean Compile(
02844       const PString & pattern,    
02845       int flags = IgnoreCase      
02846     );
02854     PBoolean Compile(
02855       const char * cpattern,      
02856       int flags = IgnoreCase      
02857     );
02858 
02859 
02861     PBoolean Execute(
02862       const PString & str,    
02863       PINDEX & start,         
02864       int flags = 0           
02865     ) const;
02867     PBoolean Execute(
02868       const PString & str,    
02869       PINDEX & start,         
02870       PINDEX & len,           
02871       int flags = 0           
02872     ) const;
02874     PBoolean Execute(
02875       const char * cstr,      
02876       PINDEX & start,         
02877       int flags = 0           
02878     ) const;
02880     PBoolean Execute(
02881       const char * cstr,      
02882       PINDEX & start,         
02883       PINDEX & len,           
02884       int flags = 0           
02885     ) const;
02887     PBoolean Execute(
02888       const PString & str,    
02889       PIntArray & starts,     
02890       int flags = 0           
02891     ) const;
02893     PBoolean Execute(
02894       const PString & str,    
02895       PIntArray & starts,     
02896       PIntArray & ends,       
02897       int flags = 0           
02898     ) const;
02900     PBoolean Execute(
02901       const char * cstr,      
02902       PIntArray & starts,     
02903       int flags = 0           
02904     ) const;
02920     PBoolean Execute(
02921       const char * cstr,      
02922       PIntArray & starts,     
02923       PIntArray & ends,       
02924       int flags = 0           
02925     ) const;
02927 
02936     static PString EscapeString(
02937       const PString & str     
02938     );
02940 
02941   protected:
02942     PString patternSaved;
02943     int flagsSaved;
02944 
02945     void * expression;
02946     mutable ErrorCodes lastError;
02947 };
02948 
02949 PString psprintf(const char * cfmt, ...);
02950 
02951 #endif // #ifndef __PSTRING__
02952 // End Of File ///////////////////////////////////////////////////////////////

Generated on Mon Feb 23 01:57:54 2009 for PTLib by  doxygen 1.5.1