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: 21788 $
00030  * $Author: rjongbloed $
00031  * $Date: 2008-12-11 23:42:13 -0600 (Thu, 11 Dec 2008) $
00032  */
00033 
00034 #ifndef PTLIB_STRING_H
00035 #define PTLIB_STRING_H
00036 
00037 #ifdef P_USE_PRAGMA
00038 #pragma interface
00039 #endif
00040 
00041 #include <string>
00042 #include <vector>
00043 #include <ptlib/array.h>
00044 
00046 // PString class
00047 
00048 class PStringArray;
00049 class PRegularExpression;
00050 class PString;
00051 
00055 PString psprintf(const char * fmt, ...);
00056 
00060 PString pvsprintf(const char * fmt, va_list arg);
00061 
00062 #if (defined(_WIN32) || defined(_WIN32_WCE)) && (!defined(_NATIVE_WCHAR_T_DEFINED)) && (!defined(__MINGW32__))
00063 PBASEARRAY(PWCharArray, unsigned short);
00064 #else
00065 PBASEARRAY(PWCharArray, wchar_t);
00066 #endif
00067 
00102 class PString : public PCharArray {
00103   PCLASSINFO(PString, PCharArray);
00104 
00105 //  using namespace std;
00106 
00107   public:
00113     PINLINE PString();
00114 
00118     PINLINE PString(
00119       const PString & str  
00120     );
00121 
00124     PINLINE PString(
00125       const std::string & str
00126     );
00127 
00136     PString(
00137       const char * cstr 
00138     );
00139 
00144     PString(
00145       const wchar_t * ustr 
00146     );
00147 
00161     PString(
00162       const char * cstr,  
00163       PINDEX len          
00164     );
00165 
00176     PString(
00177       const wchar_t * ustr,  
00178       PINDEX len          
00179     );
00180 
00191     PString(
00192       const PWCharArray & ustr 
00193     );
00194 
00203     PString(
00204       char ch    
00205     );
00206 
00211     PString(
00212       short n   
00213     );
00214 
00219     PString(
00220       unsigned short n   
00221     );
00222 
00227     PString(
00228       int n   
00229     );
00230 
00235     PString(
00236       unsigned int n   
00237     );
00238 
00243     PString(
00244       long n   
00245     );
00246 
00251     PString(
00252       unsigned long n   
00253     );
00254 
00259     PString(
00260       PInt64 n   
00261     );
00262 
00267     PString(
00268       PUInt64 n   
00269     );
00270 
00271  
00272     enum ConversionType {
00273       Pascal,   // Data is a length byte followed by characters.
00274       Basic,    // Data is two length bytes followed by characters.
00275       Literal,  // Data is C language style string with \ escape codes.
00276       Signed,   // Convert a signed integer to a string.
00277       Unsigned, // Convert an unsigned integer to a string.
00278       Decimal,  // Convert a real number to a string in decimal format.
00279       Exponent, // Convert a real number to a string in exponent format.
00280       Printf,   // Formatted output, sprintf() style function.
00281       NumConversionTypes
00282     };
00283     /* Type of conversion to make in the conversion constructors.
00284      */
00285 
00286     /* Contruct a new string converting from the spcified data source into
00287        a string array.
00288      */
00289     PString(
00290       ConversionType type,  
00291       const char * str,    
00292       ...                 
00293     );
00294     PString(
00295       ConversionType type,  
00296       long value,           
00297       unsigned base = 10    
00298     );
00299     PString(
00300       ConversionType type,  
00301       double value,         
00302       unsigned places       
00303     );
00304 
00312     PString & operator=(
00313       const PString & str  
00314     );
00315 
00325     PString & operator=(
00326       const char * cstr  
00327     );
00328 
00337     PString & operator=(
00338       char ch            
00339     );
00340 
00345     PString & operator=(
00346       short n   
00347     );
00348 
00353     PString & operator=(
00354       unsigned short n   
00355     );
00356 
00361     PString & operator=(
00362       int n   
00363     );
00364 
00369     PString & operator=(
00370       unsigned int n   
00371     );
00372 
00377     PString & operator=(
00378       long n   
00379     );
00380 
00385     PString & operator=(
00386       unsigned long n   
00387     );
00388 
00393     PString & operator=(
00394       PInt64 n   
00395     );
00396 
00401     PString & operator=(
00402       PUInt64 n   
00403     );
00404 
00407     virtual PString & MakeEmpty();
00408 
00411     static PString Empty();
00413 
00420     virtual PObject * Clone() const;
00421 
00431     virtual Comparison Compare(
00432       const PObject & obj   
00433     ) const;
00434 
00437     virtual void PrintOn(
00438       ostream & strm  
00439     ) const;
00440 
00446     virtual void ReadFrom(
00447       istream & strm  
00448     );
00449 
00463     virtual PINDEX HashFunction() const;
00465 
00480     virtual PBoolean SetSize(
00481       PINDEX newSize  
00482     );
00483 
00492     virtual PBoolean IsEmpty() const;
00493 
00502     virtual PBoolean MakeUnique();
00504 
00505 
00518     PBoolean MakeMinimumSize();
00519 
00528     PINLINE PINDEX GetLength() const;
00529 
00536     bool operator!() const;
00538 
00547     PString operator+(
00548       const PString & str   
00549     ) const;
00550 
00562     PString operator+(
00563       const char * cstr  
00564     ) const;
00565 
00577     PString operator+(
00578       char ch   
00579     ) const;
00580 
00592     friend PString operator+(
00593       const char * cstr,    
00594       const PString & str   
00595     );
00596 
00608     friend PString operator+(
00609       char  c,              
00610       const PString & str   
00611     );
00612 
00618     PString & operator+=(
00619       const PString & str   
00620     );
00621 
00631     PString & operator+=(
00632       const char * cstr  
00633     );
00634 
00644     PString & operator+=(
00645       char ch   
00646     );
00647 
00648 
00655     PString operator&(
00656       const PString & str   
00657     ) const;
00658 
00675     PString operator&(
00676       const char * cstr  
00677     ) const;
00678 
00695     PString operator&(
00696       char ch   
00697     ) const;
00698 
00715     friend PString operator&(
00716       const char * cstr,    
00717       const PString & str   
00718     );
00719 
00736     friend PString operator&(
00737       char  ch,              
00738       const PString & str   
00739     );
00740 
00746     PString & operator&=(
00747       const PString & str   
00748     );
00749 
00764     PString & operator&=(
00765       const char * cstr  
00766     );
00767 
00768 
00783     PString & operator&=(
00784       char ch  
00785     );
00787 
00788 
00796     bool operator*=(
00797       const PString & str  
00798     ) const;
00799 
00807     bool operator==(
00808       const PObject & str  
00809     ) const;
00810 
00818     bool operator!=(
00819       const PObject & str  
00820     ) const;
00821 
00829     bool operator<(
00830       const PObject & str  
00831     ) const;
00832 
00840     bool operator>(
00841       const PObject & str  
00842     ) const;
00843 
00851     bool operator<=(
00852       const PObject & str  
00853     ) const;
00854 
00862     bool operator>=(
00863       const PObject & str  
00864     ) const;
00865 
00866 
00877     bool operator*=(
00878       const char * cstr  
00879     ) const;
00880 
00891     bool operator==(
00892       const char * cstr  
00893     ) const;
00894 
00905     bool operator!=(
00906       const char * cstr  
00907     ) const;
00908 
00919     bool operator<(
00920       const char * cstr  
00921     ) const;
00922 
00933     bool operator>(
00934       const char * cstr  
00935     ) const;
00936 
00947     bool operator<=(
00948       const char * cstr  
00949     ) const;
00950 
00961     bool operator>=(
00962       const char * cstr  
00963     ) const;
00964 
00976     Comparison NumCompare(
00977       const PString & str,        
00978       PINDEX count = P_MAX_INDEX, 
00979       PINDEX offset = 0           
00980     ) const;
00981 
00993     Comparison NumCompare(
00994       const char * cstr,          
00995       PINDEX count = P_MAX_INDEX, 
00996       PINDEX offset = 0           
00997     ) const;
00999 
01000 
01004     PINDEX Find(
01005       char ch,              
01006       PINDEX offset = 0     
01007     ) const;
01008 
01010     PINDEX Find(
01011       const PString & str,  
01012       PINDEX offset = 0     
01013     ) const;
01014 
01015     /* Locate the position within the string of the character or substring. The
01016        search will begin at the character offset provided.
01017        
01018        If #offset# is beyond the length of the string, then the
01019        function will always return #P_MAX_INDEX#.
01020        
01021        The matching will be for identical character or string. If a search
01022        ignoring case is required then the string should be converted to a
01023        #PCaselessString# before the search is made.
01024 
01025        @return
01026        position of character or substring in the string, or P_MAX_INDEX if the
01027        character or substring is not in the string.
01028      */
01029     PINDEX Find(
01030       const char * cstr,    
01031       PINDEX offset = 0     
01032     ) const;
01033 
01035     PINDEX FindLast(
01036       char ch,                     
01037       PINDEX offset = P_MAX_INDEX  
01038     ) const;
01039 
01041     PINDEX FindLast(
01042       const PString & str,         
01043       PINDEX offset = P_MAX_INDEX  
01044     ) const;
01045 
01063     PINDEX FindLast(
01064       const char * cstr,           
01065       PINDEX offset = P_MAX_INDEX  
01066     ) const;
01067 
01069     PINDEX FindOneOf(
01070       const PString & set,  
01071       PINDEX offset = 0     
01072     ) const;
01073 
01088     PINDEX FindOneOf(
01089       const char * cset,    
01090       PINDEX offset = 0     
01091     ) const;
01092 
01094     PINDEX FindSpan(
01095       const PString & set,  
01096       PINDEX offset = 0     
01097     ) const;
01098 
01114     PINDEX FindSpan(
01115       const char * cset,    
01116       PINDEX offset = 0     
01117     ) const;
01118 
01129     PINDEX FindRegEx(
01130       const PRegularExpression & regex, 
01131       PINDEX offset = 0                 
01132     ) const;
01133 
01144     PBoolean FindRegEx(
01145       const PRegularExpression & regex, 
01146       PINDEX & pos,                     
01147       PINDEX & len,                     
01148       PINDEX offset = 0,                
01149       PINDEX maxPos = P_MAX_INDEX       
01150     ) const;
01151 
01152 
01163     void Replace(
01164       const PString & target,   
01165       const PString & subs,     
01166       PBoolean all = PFalse,         
01167       PINDEX offset = 0         
01168     );
01169 
01177     void Splice(
01178       const PString & str,  
01179       PINDEX pos,           
01180       PINDEX len = 0        
01181     );
01182 
01190     void Splice(
01191       const char * cstr,    
01192       PINDEX pos,           
01193       PINDEX len = 0        
01194     );
01195 
01202     void Delete(
01203       PINDEX start,   
01204       PINDEX len      
01205     );
01207 
01208 
01228     PString operator()(
01229       PINDEX start,  
01230       PINDEX end     
01231     ) const;
01232 
01247     PString Left(
01248       PINDEX len   
01249     ) const;
01250 
01265     PString Right(
01266       PINDEX len   
01267     ) const;
01268 
01285     PString Mid(
01286       PINDEX start,             
01287       PINDEX len = P_MAX_INDEX  
01288     ) const;
01289 
01290 
01298     PString LeftTrim() const;
01299 
01307     PString RightTrim() const;
01308 
01317     PString Trim() const;
01318 
01319 
01328     PString ToLower() const;
01329 
01338     PString ToUpper() const;
01339 
01340 
01342     PStringArray Tokenise(
01343       const PString & separators,
01345       PBoolean onePerSeparator = PTrue
01347     ) const;
01372     PStringArray Tokenise(
01373       const char * cseparators,
01375       PBoolean onePerSeparator = PTrue
01377     ) const;
01378 
01392     PStringArray Lines() const;
01394 
01411     PString & sprintf(
01412       const char * cfmt,   
01413       ...                  
01414     );
01415 
01430     friend PString psprintf(
01431       const char * cfmt,   
01432       ...                  
01433     );
01434 
01436     PString & vsprintf(
01437       const PString & fmt, 
01438       va_list args         
01439     );
01454     PString & vsprintf(
01455       const char * cfmt,   
01456       va_list args         
01457     );
01458 
01460     friend PString pvsprintf(
01461       const char * cfmt,   
01462       va_list args         
01463     );
01478     friend PString pvsprintf(
01479       const PString & fmt, 
01480       va_list args         
01481     );
01482 
01483 
01496     long AsInteger(
01497       unsigned base = 10    
01498     ) const;
01511     DWORD AsUnsigned(
01512       unsigned base = 10    
01513     ) const;
01527     PInt64 AsInt64(
01528       unsigned base = 10    
01529     ) const;
01543     PUInt64 AsUnsigned64(
01544       unsigned base = 10    
01545     ) const;
01546 
01557     double AsReal() const;
01558      
01562     PWCharArray AsUCS2() const;
01563 
01574     PBYTEArray ToPascal() const;
01575 
01584     PString ToLiteral() const;
01585 
01593     operator const unsigned char *() const;
01594 
01597     operator std::string () const
01598     { return std::string(theArray); }
01600 
01601 
01602   protected:
01603     void InternalFromUCS2(
01604       const wchar_t * ptr,
01605       PINDEX len
01606     );
01607     virtual Comparison InternalCompare(
01608       PINDEX offset,      // Offset into string to compare.
01609       char c              // Character to compare against.
01610     ) const;
01611     virtual Comparison InternalCompare(
01612       PINDEX offset,      // Offset into string to compare.
01613       PINDEX length,      // Number of characters to compare.
01614       const char * cstr   // C string to compare against.
01615     ) const;
01616     /* Internal function to compare the current string value against the
01617        specified C string.
01618 
01619        @return
01620        relative rank of the two strings.
01621      */
01622 
01623     PString(int dummy, const PString * str);
01624 };
01625 
01626 
01627 inline ostream & operator<<(ostream & stream, const PString & string)
01628 {
01629   string.PrintOn(stream);
01630   return stream;
01631 }
01632 
01633 
01634 inline wostream & operator<<(wostream & stream, const PString & string)
01635 {
01636   return stream << (const char *)string;
01637 }
01638 
01639 
01640 #ifdef _WIN32
01641   class PWideString : public PWCharArray {
01642     PCLASSINFO(PWideString, PWCharArray);
01643 
01644     public:
01645       PWideString() { }
01646       PWideString(const PWCharArray & arr) : PWCharArray(arr) { }
01647       PWideString(const PString     & str) : PWCharArray(str.AsUCS2()) { }
01648       PWideString(const char        * str) : PWCharArray(PString(str).AsUCS2()) { }
01649       PWideString & operator=(const PWideString & str) { PWCharArray::operator=(str); return *this; }
01650       PWideString & operator=(const PString     & str) { PWCharArray::operator=(str.AsUCS2()); return *this; }
01651       PWideString & operator=(const char        * str) { PWCharArray::operator=(PString(str).AsUCS2()); return *this; }
01652       friend inline ostream & operator<<(ostream & stream, const PWideString & string) { return stream << PString(string); }
01653   };
01654 
01655   #ifdef UNICODE
01656     typedef PWideString PVarString;
01657   #else
01658     typedef PString PVarString;
01659   #endif
01660 #endif
01661 
01662 
01664 
01674 class PCaselessString : public PString
01675 {
01676   PCLASSINFO(PCaselessString, PString);
01677 
01678   public:
01681     PCaselessString();
01682 
01686     PCaselessString(
01687       const char * cstr   
01688     );
01689 
01694     PCaselessString(
01695       const PString & str  
01696     );
01697 
01698 
01701     PCaselessString(
01702       const std::string & str  
01703       ) : PString(str)
01704     { }
01705 
01713     PCaselessString & operator=(
01714       const PString & str  
01715     );
01716 
01726     PCaselessString & operator=(
01727       const char * cstr  
01728     );
01729 
01738     PCaselessString & operator=(
01739       char ch            
01740     );
01741 
01742 
01743   // Overrides from class PObject
01748     virtual PObject * Clone() const;
01749 
01750   protected:
01751   // Overrides from class PString
01752     virtual Comparison InternalCompare(
01753       PINDEX offset,      // Offset into string to compare.
01754       char c              // Character to compare against.
01755     ) const;
01756     virtual Comparison InternalCompare(
01757       PINDEX offset,      // Offset into string to compare.
01758       PINDEX length,      // Number of characters to compare.
01759       const char * cstr   // C string to compare against.
01760     ) const;
01761     /* Internal function to compare the current string value against the
01762        specified C string.
01763 
01764        @return
01765        relative rank of the two strings or characters.
01766      */
01767 
01768     PCaselessString(int dummy, const PCaselessString * str);
01769 };
01770 
01772 
01773 class PStringStream;
01774 
01781 class PStringStream : public PString, public iostream
01782 {
01783   PCLASSINFO(PStringStream, PString);
01784 
01785   public:
01791     PStringStream();
01792 
01797     PStringStream(
01798       PINDEX fixedBufferSize
01799     );
01800 
01807     PStringStream(
01808       const PString & str   
01809     );
01810 
01815     PStringStream(
01816       const char * cstr   
01817     );
01818 
01821     virtual PString & MakeEmpty();
01822 
01834     PStringStream & operator=(
01835       const PStringStream & strm
01836     );
01837 
01849     PStringStream & operator=(
01850       const PString & str  
01851     );
01852 
01868     PStringStream & operator=(
01869       const char * cstr  
01870     );
01871 
01880     PStringStream & operator=(
01881       char ch            
01882     );
01883 
01884 
01886     virtual ~PStringStream();
01887 
01888 
01889   protected:
01890     virtual void AssignContents(const PContainer & cont);
01891 
01892   private:
01893     PStringStream(int, const PStringStream &) : iostream(cout.rdbuf()) { }
01894 
01895     class Buffer : public streambuf {
01896       public:
01897         Buffer(PStringStream & str, PINDEX size);
01898         Buffer(const Buffer & sbuf);
01899         Buffer & operator=(const Buffer & sbuf);
01900         virtual int_type overflow(int_type = EOF);
01901         virtual int_type underflow();
01902         virtual int sync();
01903         virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
01904         virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
01905         PStringStream & string;
01906         PBoolean            fixedBufferSize;
01907     };
01908 };
01909 
01910 
01911 class PStringList;
01912 class PSortedStringList;
01913 
01926 #ifdef DOC_PLUS_PLUS
01927 class PStringArray : public PArray {
01928 #endif
01929   PDECLARE_ARRAY(PStringArray, PString);
01930   public:
01937     PStringArray(
01938       PINDEX count,                 
01939       char const * const * strarr,  
01940       PBoolean caseless = PFalse         
01941     );
01944     PStringArray(
01945       const PString & str  
01946     );
01949     PStringArray(
01950       const PStringList & list  
01951     );
01954     PStringArray(
01955       const PSortedStringList & list  
01956     );
01957 
01961     PStringArray(
01962       const std::vector<PString> & vec
01963     )
01964     {
01965       for (std::vector<PString>::const_iterator r = vec.begin(); r != vec.end(); ++r)
01966         AppendString(*r);
01967     }
01968 
01972     PStringArray(
01973       const std::vector<std::string> & vec
01974     )
01975     {
01976       for (std::vector<std::string>::const_iterator r = vec.begin(); r != vec.end(); ++r)
01977         AppendString(PString(*r));
01978     }
01979 
01983     template <typename stlContainer>
01984     static PStringArray container(
01985       const stlContainer & vec
01986     )
01987     {
01988       PStringArray list;
01989       for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
01990         list.AppendString(PString(*r));
01991       return list;
01992     }
01993 
01995 
02003     virtual void ReadFrom(
02004       istream &strm   // Stream to read the objects contents from.
02005     );
02007 
02016     PINDEX GetStringsIndex(
02017       const PString & str 
02018     ) const;
02019 
02020     PString operator[](
02021       PINDEX index  
02022     ) const;
02023 
02031     PString & operator[](
02032       PINDEX index  
02033     );
02034 
02037     PINDEX AppendString(
02038       const PString & str 
02039     );
02040 
02046     PStringArray & operator +=(const PStringArray & array);
02047     PStringArray & operator +=(const PString & str);
02048 
02049 
02056     PStringArray operator + (const PStringArray & array);
02057     PStringArray operator + (const PString & str);
02058 
02066     char ** ToCharArray(
02067       PCharArray * storage = NULL
02068     ) const;
02070 };
02071 
02072 
02085 #ifdef DOC_PLUS_PLUS
02086 class PStringList : public PList {
02087 #endif
02088 PDECLARE_LIST(PStringList, PString);
02089   public:
02094     PStringList(
02095       PINDEX count,                 
02096       char const * const * strarr,  
02097       PBoolean caseless = PFalse         
02098     );
02101     PStringList(
02102       const PString & str  
02103     );
02106     PStringList(
02107       const PStringArray & array  
02108     );
02111     PStringList(
02112       const PSortedStringList & list  
02113     );
02115 
02123     virtual void ReadFrom(
02124       istream &strm   // Stream to read the objects contents from.
02125     );
02127 
02132     PINDEX AppendString(
02133       const PString & str 
02134     );
02135 
02138     PINDEX InsertString(
02139       const PString & before,   
02140       const PString & str       
02141     );
02142 
02146     PINDEX GetStringsIndex(
02147       const PString & str   
02148     ) const;
02149 
02155     PStringList & operator +=(const PStringList & list);
02156     PStringList & operator +=(const PString & str);
02157 
02158 
02165     PStringList operator + (const PStringList & array);
02166     PStringList operator + (const PString & str);
02167 
02171     template <typename stlContainer>
02172     static PStringList container(
02173       const stlContainer & vec
02174     )
02175     {
02176       PStringList list;
02177       for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
02178         list.AppendString(PString(*r));
02179       return list;
02180     }
02182 };
02183 
02184 
02197 #ifdef DOC_PLUS_PLUS
02198 class PSortedStringList : public PSortedList {
02199 #endif
02200 PDECLARE_SORTED_LIST(PSortedStringList, PString);
02201   public:
02206     PSortedStringList(
02207       PINDEX count,                 
02208       char const * const * strarr,  
02209       PBoolean caseless = PFalse         
02210     );
02213     PSortedStringList(
02214       const PString & str  
02215     );
02218     PSortedStringList(
02219       const PStringArray & array  
02220     );
02223     PSortedStringList(
02224       const PStringList & list  
02225     );
02227 
02235     virtual void ReadFrom(
02236       istream &strm   // Stream to read the objects contents from.
02237     );
02239 
02245     PINDEX AppendString(
02246       const PString & str 
02247     );
02248 
02252     PINDEX GetStringsIndex(
02253       const PString & str   
02254     ) const;
02255 
02260     PINDEX GetNextStringsIndex(
02261       const PString & str   
02262     ) const;
02264 
02265   protected:
02266     PINDEX InternalStringSelect(
02267       const char * str,
02268       PINDEX len,
02269       Element * thisElement,
02270       Element * & lastElement
02271     ) const;
02272 };
02273 
02274 
02291 #ifdef DOC_PLUS_PLUS
02292 class PStringSet : public PSet {
02293 #endif
02294 PDECLARE_SET(PStringSet, PString, PTrue);
02295   public:
02300     PStringSet(
02301       PINDEX count,                 
02302       char const * const * strarr,  
02303       PBoolean caseless = PFalse         
02304     );
02307     PStringSet(
02308       const PString & str  
02309     );
02311 
02319     virtual void ReadFrom(
02320       istream &strm   
02321     );
02323 
02327     void Include(
02328       const PString & key 
02329     );
02331     PStringSet & operator+=(
02332       const PString & key 
02333     );
02335     void Exclude(
02336       const PString & key 
02337     );
02339     PStringSet & operator-=(
02340       const PString & key 
02341     );
02343 };
02344 
02345 
02353 template <class K> class PStringDictionary : public PAbstractDictionary
02354 {
02355   PCLASSINFO(PStringDictionary, PAbstractDictionary);
02356 
02357   public:
02366     PStringDictionary()
02367       : PAbstractDictionary() { }
02369 
02376     virtual PObject * Clone() const
02377       { return PNEW PStringDictionary(0, this); }
02379 
02394     const PString & operator[](const K & key) const
02395       { return (const PString &)GetRefAt(key); }
02396 
02410     PString operator()(const K & key, const char * dflt = "") const
02411       { if (AbstractContains(key)) return (*this)[key]; return dflt; }
02412 
02421     PBoolean Contains(
02422       const K & key   // Key to look for in the dictionary.
02423       ) const { return AbstractContains(key); }
02424 
02436     virtual PString * RemoveAt(
02437       const K & key   // Key for position in dictionary to get object.
02438     ) {
02439         PString * s = GetAt(key); AbstractSetAt(key, NULL);
02440         return reference->deleteObjects ? (s ? (PString *)-1 : NULL) : s;
02441       }
02442 
02449     virtual PString * GetAt(
02450       const K & key   // Key for position in dictionary to get object.
02451     ) const { return (PString *)AbstractGetAt(key); }
02452 
02461     virtual PBoolean SetDataAt(
02462       PINDEX index,        // Ordinal index in the dictionary.
02463       const PString & str  // New string value to put into the dictionary.
02464     ) { return PAbstractDictionary::SetDataAt(index, PNEW PString(str)); }
02465 
02477     virtual PBoolean SetAt(
02478       const K & key,       // Key for position in dictionary to add object.
02479       const PString & str  // New string value to put into the dictionary.
02480     ) { return AbstractSetAt(key, PNEW PString(str)); }
02481 
02493     const K & GetKeyAt(PINDEX index) const
02494       { return (const K &)AbstractGetKeyAt(index); }
02495 
02507     PString & GetDataAt(PINDEX index) const
02508       { return (PString &)AbstractGetDataAt(index); }
02510 
02511   protected:
02512     PStringDictionary(int dummy, const PStringDictionary * c)
02513       : PAbstractDictionary(dummy, c) { }
02514 };
02515 
02516 
02531 #define PDECLARE_STRING_DICTIONARY(cls, K) \
02532   PDECLARE_CLASS(cls, PStringDictionary<K>) \
02533   protected: \
02534     cls(int dummy, const cls * c) \
02535       : PStringDictionary<K>(dummy, c) { } \
02536   public: \
02537     cls() \
02538       : PStringDictionary<K>() { } \
02539     virtual PObject * Clone() const \
02540       { return PNEW cls(0, this); } \
02541 
02542 
02555 #define PSTRING_DICTIONARY(cls, K) typedef PStringDictionary<K> cls
02556 
02557 
02572 #ifdef DOC_PLUS_PLUS
02573 class POrdinalToString : public PStringDictionary {
02574 #endif
02575 PDECLARE_STRING_DICTIONARY(POrdinalToString, POrdinalKey);
02576   public:
02579 
02580     struct Initialiser {
02582       PINDEX key;
02584       const char * value;
02585     };
02588     POrdinalToString(
02589       PINDEX count,                
02590       const Initialiser * init     
02591     );
02593 
02601     virtual void ReadFrom(
02602       istream &strm   // Stream to read the objects contents from.
02603     );
02605 };
02606 
02619 #ifdef DOC_PLUS_PLUS
02620 class PStringToOrdinal : public POrdinalDictionary {
02621 #endif
02622 PDECLARE_ORDINAL_DICTIONARY(PStringToOrdinal, PString);
02623   public:
02626 
02627     struct Initialiser {
02629       const char * key;
02631       PINDEX value;
02632     };
02635     PStringToOrdinal(
02636       PINDEX count,                
02637       const Initialiser * init,    
02638       PBoolean caseless = PFalse        
02639     );
02641 
02649     virtual void ReadFrom(
02650       istream &strm   // Stream to read the objects contents from.
02651     );
02653 };
02654 
02655 
02669 #ifdef DOC_PLUS_PLUS
02670 class PStringToString : public PStringDictionary {
02671 #endif
02672 PDECLARE_STRING_DICTIONARY(PStringToString, PString);
02673   public:
02676 
02677     struct Initialiser {
02679       const char * key;
02681       const char * value;
02682     };
02685     PStringToString(
02686       PINDEX count,                
02687       const Initialiser * init,    
02688       PBoolean caselessKeys = PFalse,   
02689       PBoolean caselessValues = PFalse  
02690     );
02692 
02700     virtual void ReadFrom(
02701       istream &strm   // Stream to read the objects contents from.
02702     );
02704 
02717     char ** ToCharArray(
02718       bool withEqualSign,
02719       PCharArray * storage = NULL
02720     ) const;
02721 };
02722 
02723 
02729 class PRegularExpression : public PObject
02730 {
02731   PCLASSINFO(PRegularExpression, PObject);
02732 
02733   public:
02736 
02737     enum {
02739       Extended = 1,
02741       IgnoreCase = 2,
02746       AnchorNewLine = 4
02747     };
02749     enum {
02756       NotBeginningOfLine = 1,
02758       NotEndofLine = 2
02759     };
02760 
02762     PRegularExpression();
02763 
02766     PRegularExpression(
02767       const PString & pattern,    
02768       int flags = IgnoreCase      
02769     );
02770 
02773     PRegularExpression(
02774       const char * cpattern,      
02775       int flags = IgnoreCase      
02776     );
02777 
02781     PRegularExpression(
02782       const PRegularExpression &
02783     );
02784 
02788     PRegularExpression & operator =(
02789       const PRegularExpression &
02790     );
02791 
02793     ~PRegularExpression();
02795 
02798 
02799     enum ErrorCodes {
02801       NoError = 0,    
02803       NoMatch,      
02804 
02805       // POSIX regcomp return error codes.  (In the order listed in the standard.)
02807       BadPattern,  
02809       CollateError,  
02811       BadClassType,  
02813       BadEscape,    
02815       BadSubReg,
02817       UnmatchedBracket, 
02819       UnmatchedParen,
02821       UnmatchedBrace,
02823       BadBR,        
02825       RangeError,  
02827       OutOfMemory,
02829       BadRepitition,
02830 
02831       /* Error codes we've added.  */
02833       PrematureEnd,
02835       TooBig,
02837       UnmatchedRParen,
02839       NotCompiled
02840     };
02841 
02847     ErrorCodes GetErrorCode() const;
02848 
02855     PString GetErrorText() const;
02857 
02861     PBoolean Compile(
02862       const PString & pattern,    
02863       int flags = IgnoreCase      
02864     );
02872     PBoolean Compile(
02873       const char * cpattern,      
02874       int flags = IgnoreCase      
02875     );
02876 
02877 
02879     PBoolean Execute(
02880       const PString & str,    
02881       PINDEX & start,         
02882       int flags = 0           
02883     ) const;
02885     PBoolean Execute(
02886       const PString & str,    
02887       PINDEX & start,         
02888       PINDEX & len,           
02889       int flags = 0           
02890     ) const;
02892     PBoolean Execute(
02893       const char * cstr,      
02894       PINDEX & start,         
02895       int flags = 0           
02896     ) const;
02898     PBoolean Execute(
02899       const char * cstr,      
02900       PINDEX & start,         
02901       PINDEX & len,           
02902       int flags = 0           
02903     ) const;
02905     PBoolean Execute(
02906       const PString & str,    
02907       PIntArray & starts,     
02908       int flags = 0           
02909     ) const;
02911     PBoolean Execute(
02912       const PString & str,    
02913       PIntArray & starts,     
02914       PIntArray & ends,       
02915       int flags = 0           
02916     ) const;
02918     PBoolean Execute(
02919       const char * cstr,      
02920       PIntArray & starts,     
02921       int flags = 0           
02922     ) const;
02938     PBoolean Execute(
02939       const char * cstr,      
02940       PIntArray & starts,     
02941       PIntArray & ends,       
02942       int flags = 0           
02943     ) const;
02945 
02954     static PString EscapeString(
02955       const PString & str     
02956     );
02958 
02959   protected:
02960     PString patternSaved;
02961     int flagsSaved;
02962 
02963     void * expression;
02964     mutable ErrorCodes lastError;
02965 };
02966 
02967 
02968 #endif // #ifndef PTLIB_STRING_H
02969 
02970 
02971 // End Of File ///////////////////////////////////////////////////////////////

Generated on Thu May 27 01:36:48 2010 for PTLib by  doxygen 1.4.7