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: 24177 $
00030  * $Author: rjongbloed $
00031  * $Date: 2010-04-05 06:52:04 -0500 (Mon, 05 Apr 2010) $
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(
00056   const char * fmt, 
00057   ...
00058 );
00059 
00063 PString pvsprintf(
00064   const char * fmt, 
00065   va_list arg       
00066 );
00067 
00068 #if (defined(_WIN32) || defined(_WIN32_WCE)) && (!defined(_NATIVE_WCHAR_T_DEFINED)) && (!defined(__MINGW32__))
00069 PBASEARRAY(PWCharArray, unsigned short);
00070 #else
00071 PBASEARRAY(PWCharArray, wchar_t);
00072 #endif
00073 
00108 class PString : public PCharArray {
00109   PCLASSINFO(PString, PCharArray);
00110 
00111 //  using namespace std;
00112 
00113   public:
00119     PINLINE PString();
00120 
00124     PINLINE PString(
00125       const PString & str  
00126     );
00127 
00130     PINLINE PString(
00131       const std::string & str
00132     );
00133 
00142     PString(
00143       const char * cstr 
00144     );
00145 
00150     PString(
00151       const wchar_t * ustr 
00152     );
00153 
00167     PString(
00168       const char * cstr,  
00169       PINDEX len          
00170     );
00171 
00182     PString(
00183       const wchar_t * ustr,  
00184       PINDEX len          
00185     );
00186 
00197     PString(
00198       const PWCharArray & ustr 
00199     );
00200 
00209     PString(
00210       char ch    
00211     );
00212 
00217     PString(
00218       short n   
00219     );
00220 
00225     PString(
00226       unsigned short n   
00227     );
00228 
00233     PString(
00234       int n   
00235     );
00236 
00241     PString(
00242       unsigned int n   
00243     );
00244 
00249     PString(
00250       long n   
00251     );
00252 
00257     PString(
00258       unsigned long n   
00259     );
00260 
00265     PString(
00266       PInt64 n   
00267     );
00268 
00273     PString(
00274       PUInt64 n   
00275     );
00276 
00277  
00278     enum ConversionType {
00279       Pascal,   // Data is a length byte followed by characters.
00280       Basic,    // Data is two length bytes followed by characters.
00281       Literal,  // Data is C language style string with \\ escape codes.
00282       Signed,   // Convert a signed integer to a string.
00283       Unsigned, // Convert an unsigned integer to a string.
00284       Decimal,  // Convert a real number to a string in decimal format.
00285       Exponent, // Convert a real number to a string in exponent format.
00286       Printf,   // Formatted output, sprintf() style function.
00287       NumConversionTypes
00288     };
00289     /* Type of conversion to make in the conversion constructors.
00290      */
00291 
00292     /* Contruct a new string converting from the spcified data source into
00293        a string array.
00294      */
00295     PString(
00296       ConversionType type,  
00297       const char * str,    
00298       ...                 
00299     );
00300     PString(
00301       ConversionType type,  
00302       long value,           
00303       unsigned base = 10    
00304     );
00305     PString(
00306       ConversionType type,  
00307       double value,         
00308       unsigned places       
00309     );
00310 
00318     PString & operator=(
00319       const PString & str  
00320     );
00321 
00331     PString & operator=(
00332       const char * cstr  
00333     );
00334 
00343     PString & operator=(
00344       char ch            
00345     );
00346 
00351     PString & operator=(
00352       short n   
00353     );
00354 
00359     PString & operator=(
00360       unsigned short n   
00361     );
00362 
00367     PString & operator=(
00368       int n   
00369     );
00370 
00375     PString & operator=(
00376       unsigned int n   
00377     );
00378 
00383     PString & operator=(
00384       long n   
00385     );
00386 
00391     PString & operator=(
00392       unsigned long n   
00393     );
00394 
00399     PString & operator=(
00400       PInt64 n   
00401     );
00402 
00407     PString & operator=(
00408       PUInt64 n   
00409     );
00410 
00413     virtual PString & MakeEmpty();
00414 
00417     static PString Empty();
00419 
00426     virtual PObject * Clone() const;
00427 
00437     virtual Comparison Compare(
00438       const PObject & obj   
00439     ) const;
00440 
00443     virtual void PrintOn(
00444       ostream & strm  
00445     ) const;
00446 
00452     virtual void ReadFrom(
00453       istream & strm  
00454     );
00455 
00469     virtual PINDEX HashFunction() const;
00471 
00486     virtual PBoolean SetSize(
00487       PINDEX newSize  
00488     );
00489 
00498     virtual PBoolean IsEmpty() const;
00499 
00508     virtual PBoolean MakeUnique();
00510 
00511 
00524     PBoolean MakeMinimumSize();
00525 
00534     PINLINE PINDEX GetLength() const;
00535 
00542     bool operator!() const;
00544 
00553     PString operator+(
00554       const PString & str   
00555     ) const;
00556 
00568     PString operator+(
00569       const char * cstr  
00570     ) const;
00571 
00583     PString operator+(
00584       char ch   
00585     ) const;
00586 
00598     friend PString operator+(
00599       const char * cstr,    
00600       const PString & str   
00601     );
00602 
00614     friend PString operator+(
00615       char  ch,             
00616       const PString & str   
00617     );
00618 
00624     PString & operator+=(
00625       const PString & str   
00626     );
00627 
00637     PString & operator+=(
00638       const char * cstr  
00639     );
00640 
00650     PString & operator+=(
00651       char ch   
00652     );
00653 
00654 
00661     PString operator&(
00662       const PString & str   
00663     ) const;
00664 
00681     PString operator&(
00682       const char * cstr  
00683     ) const;
00684 
00701     PString operator&(
00702       char ch   
00703     ) const;
00704 
00721     friend PString operator&(
00722       const char * cstr,    
00723       const PString & str   
00724     );
00725 
00742     friend PString operator&(
00743       char  ch,              
00744       const PString & str   
00745     );
00746 
00752     PString & operator&=(
00753       const PString & str   
00754     );
00755 
00770     PString & operator&=(
00771       const char * cstr  
00772     );
00773 
00774 
00789     PString & operator&=(
00790       char ch  
00791     );
00793 
00794 
00802     bool operator*=(
00803       const PString & str  
00804     ) const;
00805 
00813     bool operator==(
00814       const PObject & str  
00815     ) const;
00816 
00824     bool operator!=(
00825       const PObject & str  
00826     ) const;
00827 
00835     bool operator<(
00836       const PObject & str  
00837     ) const;
00838 
00846     bool operator>(
00847       const PObject & str  
00848     ) const;
00849 
00857     bool operator<=(
00858       const PObject & str  
00859     ) const;
00860 
00868     bool operator>=(
00869       const PObject & str  
00870     ) const;
00871 
00872 
00883     bool operator*=(
00884       const char * cstr  
00885     ) const;
00886 
00897     bool operator==(
00898       const char * cstr  
00899     ) const;
00900 
00911     bool operator!=(
00912       const char * cstr  
00913     ) const;
00914 
00925     bool operator<(
00926       const char * cstr  
00927     ) const;
00928 
00939     bool operator>(
00940       const char * cstr  
00941     ) const;
00942 
00953     bool operator<=(
00954       const char * cstr  
00955     ) const;
00956 
00967     bool operator>=(
00968       const char * cstr  
00969     ) const;
00970 
00982     Comparison NumCompare(
00983       const PString & str,        
00984       PINDEX count = P_MAX_INDEX, 
00985       PINDEX offset = 0           
00986     ) const;
00987 
00999     Comparison NumCompare(
01000       const char * cstr,          
01001       PINDEX count = P_MAX_INDEX, 
01002       PINDEX offset = 0           
01003     ) const;
01005 
01006 
01010     PINDEX Find(
01011       char ch,              
01012       PINDEX offset = 0     
01013     ) const;
01014 
01016     PINDEX Find(
01017       const PString & str,  
01018       PINDEX offset = 0     
01019     ) const;
01020 
01021     /* Locate the position within the string of the character or substring. The
01022        search will begin at the character offset provided.
01023        
01024        If <code>offset</code> is beyond the length of the string, then the
01025        function will always return P_MAX_INDEX.
01026        
01027        The matching will be for identical character or string. If a search
01028        ignoring case is required then the string should be converted to a
01029        PCaselessString before the search is made.
01030 
01031        @return
01032        position of character or substring in the string, or P_MAX_INDEX if the
01033        character or substring is not in the string.
01034      */
01035     PINDEX Find(
01036       const char * cstr,    
01037       PINDEX offset = 0     
01038     ) const;
01039 
01041     PINDEX FindLast(
01042       char ch,                     
01043       PINDEX offset = P_MAX_INDEX  
01044     ) const;
01045 
01047     PINDEX FindLast(
01048       const PString & str,         
01049       PINDEX offset = P_MAX_INDEX  
01050     ) const;
01051 
01069     PINDEX FindLast(
01070       const char * cstr,           
01071       PINDEX offset = P_MAX_INDEX  
01072     ) const;
01073 
01075     PINDEX FindOneOf(
01076       const PString & set,  
01077       PINDEX offset = 0     
01078     ) const;
01079 
01094     PINDEX FindOneOf(
01095       const char * cset,    
01096       PINDEX offset = 0     
01097     ) const;
01098 
01100     PINDEX FindSpan(
01101       const PString & set,  
01102       PINDEX offset = 0     
01103     ) const;
01104 
01120     PINDEX FindSpan(
01121       const char * cset,    
01122       PINDEX offset = 0     
01123     ) const;
01124 
01135     PINDEX FindRegEx(
01136       const PRegularExpression & regex, 
01137       PINDEX offset = 0                 
01138     ) const;
01139 
01150     PBoolean FindRegEx(
01151       const PRegularExpression & regex, 
01152       PINDEX & pos,                     
01153       PINDEX & len,                     
01154       PINDEX offset = 0,                
01155       PINDEX maxPos = P_MAX_INDEX       
01156     ) const;
01157 
01158 
01161     PBoolean MatchesRegEx(
01162       const PRegularExpression & regex 
01163     ) const;
01164 
01175     void Replace(
01176       const PString & target,   
01177       const PString & subs,     
01178       PBoolean all = false,         
01179       PINDEX offset = 0         
01180     );
01181 
01189     void Splice(
01190       const PString & str,  
01191       PINDEX pos,           
01192       PINDEX len = 0        
01193     );
01194 
01202     void Splice(
01203       const char * cstr,    
01204       PINDEX pos,           
01205       PINDEX len = 0        
01206     );
01207 
01214     void Delete(
01215       PINDEX start,   
01216       PINDEX len      
01217     );
01219 
01220 
01240     PString operator()(
01241       PINDEX start,  
01242       PINDEX end     
01243     ) const;
01244 
01259     PString Left(
01260       PINDEX len   
01261     ) const;
01262 
01277     PString Right(
01278       PINDEX len   
01279     ) const;
01280 
01297     PString Mid(
01298       PINDEX start,             
01299       PINDEX len = P_MAX_INDEX  
01300     ) const;
01301 
01302 
01310     PString LeftTrim() const;
01311 
01319     PString RightTrim() const;
01320 
01329     PString Trim() const;
01330 
01331 
01340     PString ToLower() const;
01341 
01350     PString ToUpper() const;
01351 
01352 
01354     PStringArray Tokenise(
01355       const PString & separators,
01357       PBoolean onePerSeparator = true
01359     ) const;
01384     PStringArray Tokenise(
01385       const char * cseparators,
01387       PBoolean onePerSeparator = true
01389     ) const;
01390 
01404     PStringArray Lines() const;
01406 
01423     PString & sprintf(
01424       const char * cfmt,   
01425       ...                  
01426     );
01427 
01442     friend PString psprintf(
01443       const char * cfmt,   
01444       ...                  
01445     );
01446 
01448     PString & vsprintf(
01449       const PString & fmt, 
01450       va_list args         
01451     );
01466     PString & vsprintf(
01467       const char * cfmt,   
01468       va_list args         
01469     );
01470 
01472     friend PString pvsprintf(
01473       const char * cfmt,   
01474       va_list args         
01475     );
01490     friend PString pvsprintf(
01491       const PString & fmt, 
01492       va_list args         
01493     );
01494 
01495 
01508     long AsInteger(
01509       unsigned base = 10    
01510     ) const;
01523     DWORD AsUnsigned(
01524       unsigned base = 10    
01525     ) const;
01539     PInt64 AsInt64(
01540       unsigned base = 10    
01541     ) const;
01555     PUInt64 AsUnsigned64(
01556       unsigned base = 10    
01557     ) const;
01558 
01569     double AsReal() const;
01570      
01574     PWCharArray AsUCS2() const;
01575 
01586     PBYTEArray ToPascal() const;
01587 
01596     PString ToLiteral() const;
01597 
01605     operator const unsigned char *() const;
01606 
01609     operator std::string () const
01610     { return std::string(theArray); }
01612 
01613 
01614   protected:
01615     void InternalFromUCS2(
01616       const wchar_t * ptr,
01617       PINDEX len
01618     );
01619     virtual Comparison InternalCompare(
01620       PINDEX offset,      // Offset into string to compare.
01621       char c              // Character to compare against.
01622     ) const;
01623     virtual Comparison InternalCompare(
01624       PINDEX offset,      // Offset into string to compare.
01625       PINDEX length,      // Number of characters to compare.
01626       const char * cstr   // C string to compare against.
01627     ) const;
01628     /* Internal function to compare the current string value against the
01629        specified C string.
01630 
01631        @return
01632        relative rank of the two strings.
01633      */
01634 
01635     PString(int dummy, const PString * str);
01636 };
01637 
01638 
01639 inline ostream & operator<<(ostream & stream, const PString & string)
01640 {
01641   string.PrintOn(stream);
01642   return stream;
01643 }
01644 
01645 
01646 inline wostream & operator<<(wostream & stream, const PString & string)
01647 {
01648   return stream << (const char *)string;
01649 }
01650 
01651 
01652 #ifdef _WIN32
01653   class PWideString : public PWCharArray {
01654     PCLASSINFO(PWideString, PWCharArray);
01655 
01656     public:
01657       PWideString() { }
01658       PWideString(const PWCharArray & arr) : PWCharArray(arr) { }
01659       PWideString(const PString     & str) : PWCharArray(str.AsUCS2()) { }
01660       PWideString(const char        * str) : PWCharArray(PString(str).AsUCS2()) { }
01661       PWideString & operator=(const PWideString & str) { PWCharArray::operator=(str); return *this; }
01662       PWideString & operator=(const PString     & str) { PWCharArray::operator=(str.AsUCS2()); return *this; }
01663       PWideString & operator=(const char        * str) { PWCharArray::operator=(PString(str).AsUCS2()); return *this; }
01664       friend inline ostream & operator<<(ostream & stream, const PWideString & string) { return stream << PString(string); }
01665   };
01666 
01667   #ifdef UNICODE
01668     typedef PWideString PVarString;
01669   #else
01670     typedef PString PVarString;
01671   #endif
01672 #endif
01673 
01674 
01676 
01686 class PCaselessString : public PString
01687 {
01688   PCLASSINFO(PCaselessString, PString);
01689 
01690   public:
01693     PCaselessString();
01694 
01698     PCaselessString(
01699       const char * cstr   
01700     );
01701 
01706     PCaselessString(
01707       const PString & str  
01708     );
01709 
01710 
01713     PCaselessString(
01714       const std::string & str  
01715       ) : PString(str)
01716     { }
01717 
01725     PCaselessString & operator=(
01726       const PString & str  
01727     );
01728 
01738     PCaselessString & operator=(
01739       const char * cstr  
01740     );
01741 
01750     PCaselessString & operator=(
01751       char ch            
01752     );
01753 
01754 
01755   // Overrides from class PObject
01760     virtual PObject * Clone() const;
01761 
01762   protected:
01763   // Overrides from class PString
01764     virtual Comparison InternalCompare(
01765       PINDEX offset,      // Offset into string to compare.
01766       char c              // Character to compare against.
01767     ) const;
01768     virtual Comparison InternalCompare(
01769       PINDEX offset,      // Offset into string to compare.
01770       PINDEX length,      // Number of characters to compare.
01771       const char * cstr   // C string to compare against.
01772     ) const;
01773     /* Internal function to compare the current string value against the
01774        specified C string.
01775 
01776        @return
01777        relative rank of the two strings or characters.
01778      */
01779 
01780     PCaselessString(int dummy, const PCaselessString * str);
01781 };
01782 
01784 
01785 class PStringStream;
01786 
01793 class PStringStream : public PString, public iostream
01794 {
01795   PCLASSINFO(PStringStream, PString);
01796 
01797   public:
01803     PStringStream();
01804 
01809     PStringStream(
01810       PINDEX fixedBufferSize
01811     );
01812 
01819     PStringStream(
01820       const PString & str   
01821     );
01822 
01827     PStringStream(
01828       const char * cstr   
01829     );
01830 
01833     virtual PString & MakeEmpty();
01834 
01846     PStringStream & operator=(
01847       const PStringStream & strm
01848     );
01849 
01861     PStringStream & operator=(
01862       const PString & str  
01863     );
01864 
01880     PStringStream & operator=(
01881       const char * cstr  
01882     );
01883 
01892     PStringStream & operator=(
01893       char ch            
01894     );
01895 
01896 
01898     virtual ~PStringStream();
01899 
01900 
01901   protected:
01902     virtual void AssignContents(const PContainer & cont);
01903 
01904   private:
01905     PStringStream(int, const PStringStream &) : iostream(cout.rdbuf()) { }
01906 
01907     class Buffer : public streambuf {
01908       public:
01909         Buffer(PStringStream & str, PINDEX size);
01910         Buffer(const Buffer & sbuf);
01911         Buffer & operator=(const Buffer & sbuf);
01912         virtual int_type overflow(int_type = EOF);
01913         virtual int_type underflow();
01914         virtual int sync();
01915         virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
01916         virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
01917         PStringStream & string;
01918         PBoolean            fixedBufferSize;
01919     };
01920 };
01921 
01922 
01923 class PStringList;
01924 class PSortedStringList;
01925 
01938 #ifdef DOC_PLUS_PLUS
01939 class PStringArray : public PArray {
01940 #endif
01941   PDECLARE_ARRAY(PStringArray, PString);
01942   public:
01949     PStringArray(
01950       PINDEX count,                 
01951       char const * const * strarr,  
01952       PBoolean caseless = false         
01953     );
01956     PStringArray(
01957       const PString & str  
01958     );
01961     PStringArray(
01962       const PStringList & list  
01963     );
01966     PStringArray(
01967       const PSortedStringList & list  
01968     );
01969 
01973     PStringArray(
01974       const std::vector<PString> & vec
01975     )
01976     {
01977       for (std::vector<PString>::const_iterator r = vec.begin(); r != vec.end(); ++r)
01978         AppendString(*r);
01979     }
01980 
01984     PStringArray(
01985       const std::vector<std::string> & vec
01986     )
01987     {
01988       for (std::vector<std::string>::const_iterator r = vec.begin(); r != vec.end(); ++r)
01989         AppendString(PString(*r));
01990     }
01991 
01995     template <typename stlContainer>
01996     static PStringArray container(
01997       const stlContainer & vec
01998     )
01999     {
02000       PStringArray list;
02001       for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
02002         list.AppendString(PString(*r));
02003       return list;
02004     }
02005 
02007 
02016     virtual void ReadFrom(
02017       istream &strm   // Stream to read the objects contents from.
02018     );
02020 
02029     PINDEX GetStringsIndex(
02030       const PString & str 
02031     ) const;
02032 
02033     PString operator[](
02034       PINDEX index  
02035     ) const;
02036 
02044     PString & operator[](
02045       PINDEX index  
02046     );
02047 
02050     PINDEX AppendString(
02051       const PString & str 
02052     );
02053 
02059     PStringArray & operator +=(const PStringArray & array);
02060     PStringArray & operator +=(const PString & str);
02061 
02062 
02069     PStringArray operator + (const PStringArray & array);
02070     PStringArray operator + (const PString & str);
02071 
02079     char ** ToCharArray(
02080       PCharArray * storage = NULL
02081     ) const;
02083 };
02084 
02085 
02098 #ifdef DOC_PLUS_PLUS
02099 class PStringList : public PList {
02100 #endif
02101 PDECLARE_LIST(PStringList, PString);
02102   public:
02107     PStringList(
02108       PINDEX count,                 
02109       char const * const * strarr,  
02110       PBoolean caseless = false         
02111     );
02114     PStringList(
02115       const PString & str  
02116     );
02119     PStringList(
02120       const PStringArray & array  
02121     );
02124     PStringList(
02125       const PSortedStringList & list  
02126     );
02128 
02137     virtual void ReadFrom(
02138       istream &strm   // Stream to read the objects contents from.
02139     );
02141 
02146     PINDEX AppendString(
02147       const PString & str 
02148     );
02149 
02152     PINDEX InsertString(
02153       const PString & before,   
02154       const PString & str       
02155     );
02156 
02160     PINDEX GetStringsIndex(
02161       const PString & str   
02162     ) const;
02163 
02169     PStringList & operator +=(const PStringList & list);
02170     PStringList & operator +=(const PString & str);
02171 
02172 
02179     PStringList operator + (const PStringList & array);
02180     PStringList operator + (const PString & str);
02181 
02185     template <typename stlContainer>
02186     static PStringList container(
02187       const stlContainer & vec
02188     )
02189     {
02190       PStringList list;
02191       for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
02192         list.AppendString(PString(*r));
02193       return list;
02194     }
02196 };
02197 
02198 
02210 #ifdef DOC_PLUS_PLUS
02211 class PSortedStringList : public PSortedList {
02212 #endif
02213 PDECLARE_SORTED_LIST(PSortedStringList, PString);
02214   public:
02219     PSortedStringList(
02220       PINDEX count,                 
02221       char const * const * strarr,  
02222       PBoolean caseless = false         
02223     );
02226     PSortedStringList(
02227       const PString & str  
02228     );
02231     PSortedStringList(
02232       const PStringArray & array  
02233     );
02236     PSortedStringList(
02237       const PStringList & list  
02238     );
02240 
02249     virtual void ReadFrom(
02250       istream &strm   // Stream to read the objects contents from.
02251     );
02253 
02259     PINDEX AppendString(
02260       const PString & str 
02261     );
02262 
02266     PINDEX GetStringsIndex(
02267       const PString & str   
02268     ) const;
02269 
02274     PINDEX GetNextStringsIndex(
02275       const PString & str   
02276     ) const;
02278 
02279   protected:
02280     PINDEX InternalStringSelect(
02281       const char * str,
02282       PINDEX len,
02283       Element * thisElement,
02284       Element * & lastElement
02285     ) const;
02286 };
02287 
02288 
02305 #ifdef DOC_PLUS_PLUS
02306 class PStringSet : public PSet {
02307 #endif
02308 PDECLARE_SET(PStringSet, PString, true);
02309   public:
02314     PStringSet(
02315       PINDEX count,                 
02316       char const * const * strarr,  
02317       PBoolean caseless = false         
02318     );
02321     PStringSet(
02322       const PString & str  
02323     );
02325 
02334     virtual void ReadFrom(
02335       istream &strm   
02336     );
02338 
02342     void Include(
02343       const PString & key 
02344     );
02346     PStringSet & operator+=(
02347       const PString & key 
02348     );
02350     void Exclude(
02351       const PString & key 
02352     );
02354     PStringSet & operator-=(
02355       const PString & key 
02356     );
02358 };
02359 
02360 
02368 template <class K> class PStringDictionary : public PAbstractDictionary
02369 {
02370   PCLASSINFO(PStringDictionary, PAbstractDictionary);
02371 
02372   public:
02381     PStringDictionary()
02382       : PAbstractDictionary() { }
02384 
02391     virtual PObject * Clone() const
02392       { return PNEW PStringDictionary(0, this); }
02394 
02409     const PString & operator[](const K & key) const
02410       { return (const PString &)GetRefAt(key); }
02411 
02425     PString operator()(const K & key, const char * dflt = "") const
02426       { if (AbstractContains(key)) return (*this)[key]; return dflt; }
02427 
02436     PBoolean Contains(
02437       const K & key   // Key to look for in the dictionary.
02438       ) const { return AbstractContains(key); }
02439 
02451     virtual PString * RemoveAt(
02452       const K & key   // Key for position in dictionary to get object.
02453     ) {
02454         PString * s = GetAt(key); AbstractSetAt(key, NULL);
02455         return reference->deleteObjects ? (s ? (PString *)-1 : NULL) : s;
02456       }
02457 
02464     virtual PString * GetAt(
02465       const K & key   // Key for position in dictionary to get object.
02466     ) const { return (PString *)AbstractGetAt(key); }
02467 
02476     virtual PBoolean SetDataAt(
02477       PINDEX index,        // Ordinal index in the dictionary.
02478       const PString & str  // New string value to put into the dictionary.
02479     ) { return PAbstractDictionary::SetDataAt(index, PNEW PString(str)); }
02480 
02492     virtual PBoolean SetAt(
02493       const K & key,       // Key for position in dictionary to add object.
02494       const PString & str  // New string value to put into the dictionary.
02495     ) { return AbstractSetAt(key, PNEW PString(str)); }
02496 
02508     const K & GetKeyAt(PINDEX index) const
02509       { return (const K &)AbstractGetKeyAt(index); }
02510 
02522     PString & GetDataAt(PINDEX index) const
02523       { return (PString &)AbstractGetDataAt(index); }
02525 
02526   protected:
02527     PStringDictionary(int dummy, const PStringDictionary * c)
02528       : PAbstractDictionary(dummy, c) { }
02529 };
02530 
02531 
02546 #define PDECLARE_STRING_DICTIONARY(cls, K) \
02547   PDECLARE_CLASS(cls, PStringDictionary<K>) \
02548   protected: \
02549     cls(int dummy, const cls * c) \
02550       : PStringDictionary<K>(dummy, c) { } \
02551   public: \
02552     cls() \
02553       : PStringDictionary<K>() { } \
02554     virtual PObject * Clone() const \
02555       { return PNEW cls(0, this); } \
02556 
02557 
02570 #define PSTRING_DICTIONARY(cls, K) typedef PStringDictionary<K> cls
02571 
02572 
02587 #ifdef DOC_PLUS_PLUS
02588 class POrdinalToString : public PStringDictionary {
02589 #endif
02590 PDECLARE_STRING_DICTIONARY(POrdinalToString, POrdinalKey);
02591   public:
02594 
02595     struct Initialiser {
02597       PINDEX key;
02599       const char * value;
02600     };
02603     POrdinalToString(
02604       PINDEX count,                
02605       const Initialiser * init     
02606     );
02608 
02617     virtual void ReadFrom(
02618       istream &strm   // Stream to read the objects contents from.
02619     );
02621 };
02622 
02634 #ifdef DOC_PLUS_PLUS
02635 class PStringToOrdinal : public POrdinalDictionary {
02636 #endif
02637 PDECLARE_ORDINAL_DICTIONARY(PStringToOrdinal, PString);
02638   public:
02641 
02642     struct Initialiser {
02644       const char * key;
02646       PINDEX value;
02647     };
02650     PStringToOrdinal(
02651       PINDEX count,                
02652       const Initialiser * init,    
02653       PBoolean caseless = false        
02654     );
02656 
02665     virtual void ReadFrom(
02666       istream &strm   // Stream to read the objects contents from.
02667     );
02669 };
02670 
02671 
02685 #ifdef DOC_PLUS_PLUS
02686 class PStringToString : public PStringDictionary {
02687 #endif
02688 PDECLARE_STRING_DICTIONARY(PStringToString, PString);
02689   public:
02692 
02693     struct Initialiser {
02695       const char * key;
02697       const char * value;
02698     };
02701     PStringToString(
02702       PINDEX count,                
02703       const Initialiser * init,    
02704       PBoolean caselessKeys = false,   
02705       PBoolean caselessValues = false  
02706     );
02708 
02717     virtual void ReadFrom(
02718       istream &strm   // Stream to read the objects contents from.
02719     );
02721 
02734     char ** ToCharArray(
02735       bool withEqualSign,
02736       PCharArray * storage = NULL
02737     ) const;
02738 };
02739 
02740 
02746 class PRegularExpression : public PObject
02747 {
02748   PCLASSINFO(PRegularExpression, PObject);
02749 
02750   public:
02753 
02754     enum {
02756       Extended = 1,
02758       IgnoreCase = 2,
02763       AnchorNewLine = 4
02764     };
02766     enum {
02773       NotBeginningOfLine = 1,
02775       NotEndofLine = 2
02776     };
02777 
02779     PRegularExpression();
02780 
02783     PRegularExpression(
02784       const PString & pattern,    
02785       int flags = IgnoreCase      
02786     );
02787 
02790     PRegularExpression(
02791       const char * cpattern,      
02792       int flags = IgnoreCase      
02793     );
02794 
02798     PRegularExpression(
02799       const PRegularExpression &
02800     );
02801 
02805     PRegularExpression & operator =(
02806       const PRegularExpression &
02807     );
02808 
02810     ~PRegularExpression();
02812 
02813 
02818     virtual void PrintOn(
02819       ostream & strm  
02820     ) const;
02822 
02825 
02826     enum ErrorCodes {
02828       NoError = 0,    
02830       NoMatch,      
02831 
02832       // POSIX regcomp return error codes.  (In the order listed in the standard.)
02834       BadPattern,  
02836       CollateError,  
02838       BadClassType,  
02840       BadEscape,    
02842       BadSubReg,
02844       UnmatchedBracket, 
02846       UnmatchedParen,
02848       UnmatchedBrace,
02850       BadBR,        
02852       RangeError,  
02854       OutOfMemory,
02856       BadRepitition,
02857 
02858       /* Error codes we've added.  */
02860       PrematureEnd,
02862       TooBig,
02864       UnmatchedRParen,
02866       NotCompiled
02867     };
02868 
02874     ErrorCodes GetErrorCode() const;
02875 
02882     PString GetErrorText() const;
02883 
02885     const PString & GetPattern() const { return patternSaved; }
02887 
02891     PBoolean Compile(
02892       const PString & pattern,    
02893       int flags = IgnoreCase      
02894     );
02902     PBoolean Compile(
02903       const char * cpattern,      
02904       int flags = IgnoreCase      
02905     );
02906 
02907 
02909     PBoolean Execute(
02910       const PString & str,    
02911       PINDEX & start,         
02912       int flags = 0           
02913     ) const;
02915     PBoolean Execute(
02916       const PString & str,    
02917       PINDEX & start,         
02918       PINDEX & len,           
02919       int flags = 0           
02920     ) const;
02922     PBoolean Execute(
02923       const char * cstr,      
02924       PINDEX & start,         
02925       int flags = 0           
02926     ) const;
02928     PBoolean Execute(
02929       const char * cstr,      
02930       PINDEX & start,         
02931       PINDEX & len,           
02932       int flags = 0           
02933     ) const;
02935     PBoolean Execute(
02936       const PString & str,    
02937       PIntArray & starts,     
02938       int flags = 0           
02939     ) const;
02941     PBoolean Execute(
02942       const PString & str,    
02943       PIntArray & starts,     
02944       PIntArray & ends,       
02945       int flags = 0           
02946     ) const;
02948     PBoolean Execute(
02949       const char * cstr,      
02950       PIntArray & starts,     
02951       int flags = 0           
02952     ) const;
02968     PBoolean Execute(
02969       const char * cstr,      
02970       PIntArray & starts,     
02971       PIntArray & ends,       
02972       int flags = 0           
02973     ) const;
02975 
02984     static PString EscapeString(
02985       const PString & str     
02986     );
02988 
02989   protected:
02990     PString patternSaved;
02991     int flagsSaved;
02992 
02993     void * expression;
02994     mutable ErrorCodes lastError;
02995 };
02996 
02997 
02998 #endif // PTLIB_STRING_H
02999 
03000 
03001 // End Of File ///////////////////////////////////////////////////////////////

Generated on Fri Oct 14 01:44:10 2011 for PTLib by  doxygen 1.4.7