PWLib
1.10.10
|
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 * $Log: pstring.h,v $ 00030 * Revision 1.85 2005/12/15 21:14:34 dsandras 00031 * Fixed from Alexander Larsson <alexl _AT__ redhat.com> for gcc 4.1 compilation. Thanks! 00032 * 00033 * Revision 1.84 2005/11/30 12:47:38 csoutheren 00034 * Removed tabs, reformatted some code, and changed tags for Doxygen 00035 * 00036 * Revision 1.83 2005/11/25 03:43:47 csoutheren 00037 * Fixed function argument comments to be compatible with Doxygen 00038 * 00039 * Revision 1.82 2005/09/18 11:05:36 dominance 00040 * include/ptlib/channel.h, include/ptlib/pstring.h, src/ptlib/common/contain.cxx, 00041 * src/ptlib/common/pchannel.cxx: 00042 * correct the STL defined checking to use proper syntax. 00043 * 00044 * include/ptlib/object.h: 00045 * re-add typedef to compile on mingw 00046 * 00047 * make/ptlib-config.in: 00048 * import a long-standing fix from the Debian packs which allows usage of 00049 * ptlib-config without manually adding -lpt for each of the subsequent 00050 * projects 00051 * 00052 * Revision 1.81 2004/12/22 04:04:36 dereksmithies 00053 * Modify description of parameters for PString::Replace() 00054 * 00055 * Revision 1.80 2004/11/23 11:33:08 csoutheren 00056 * Fixed problem with RemoveAt returning invalid pointer in some cases, 00057 * and added extra documentation on this case. 00058 * Thanks to Diego Tartara for pointing out this potential problem 00059 * 00060 * Revision 1.79 2004/10/21 13:04:20 rjongbloed 00061 * Fixed possibility of const operator[] on PStringArray returning a NULL reference. This 00062 * function should return a non-lvalue PString anyway as it is const! 00063 * 00064 * Revision 1.78 2004/08/16 08:49:59 csoutheren 00065 * Removed error when compiling with gcc 00066 * 00067 * Revision 1.77 2004/08/16 06:40:59 csoutheren 00068 * Added adapters template to make device plugins available via the abstract factory interface 00069 * 00070 * Revision 1.76 2004/06/01 05:54:18 csoutheren 00071 * Added <vector> and <string> 00072 * 00073 * Revision 1.75 2004/06/01 05:21:38 csoutheren 00074 * Added conversions between std::string and PString, and vector<PString> and PStringArray 00075 * 00076 * Revision 1.74 2004/05/04 11:10:36 rjongbloed 00077 * Fixed usage of MakeEmpty() with PStringStream. 00078 * 00079 * Revision 1.73 2004/04/18 04:33:36 rjongbloed 00080 * Changed all operators that return BOOL to return standard type bool. This is primarily 00081 * for improved compatibility with std STL usage removing many warnings. 00082 * 00083 * Revision 1.72 2004/04/11 13:26:25 csoutheren 00084 * Removed namespace problems and removed warnings for Windows <string> 00085 * 00086 * Revision 1.71 2004/04/09 06:38:10 rjongbloed 00087 * Fixed compatibility with STL based streams, eg as used by VC++2003 00088 * 00089 * Revision 1.70 2004/04/09 03:42:34 csoutheren 00090 * Removed all usages of "virtual inline" and "inline virtual" 00091 * 00092 * Revision 1.69 2004/04/03 06:54:22 rjongbloed 00093 * Many and various changes to support new Visual C++ 2003 00094 * 00095 * Revision 1.68 2004/02/23 00:44:38 csoutheren 00096 * A completely different, other regex include hack to avoid requiring 00097 * the sources when using a header-file only environment 00098 * 00099 * Revision 1.67 2004/02/23 00:26:05 csoutheren 00100 * Finally, a generic and elegant fix for the regex include hacks. Thanks to Roger Hardiman 00101 * 00102 * Revision 1.66 2004/02/11 05:09:14 csoutheren 00103 * Fixed problems with regex libraries on Solaris, and with host OS numbering 00104 * being a quoted string rather than a number. Thanks to Chad Attermann 00105 * Fixed problems SSL detection problems thanks to Michal Zygmuntowicz 00106 * 00107 * Revision 1.65 2004/02/08 11:13:11 rjongbloed 00108 * Fixed crash in heavily loaded multi-threaded systems using simultaneous sorted 00109 * lists, Thanks Federico Pinna, Fabrizio Ammollo and the gang at Reitek S.p.A. 00110 * 00111 * Revision 1.64 2004/01/18 13:43:48 rjongbloed 00112 * Fixed broken PString::MakeEmpty() function and moved implementations to .inl file. 00113 * 00114 * Revision 1.63 2004/01/17 18:15:24 csoutheren 00115 * Fixed multi-threading problem with PString::Empty 00116 * Created PString::MakeEmpty for efficient emptying of existing strings 00117 * 00118 * Revision 1.62 2004/01/16 13:24:37 csoutheren 00119 * Changed PString::Empty to be thread-safe 00120 * Fixed PContainer::SetMinSize and PAbstractArray::SetSize, thanks to 123@call2ua.com 00121 * Fixed PString::FindLast, thanks to Andreas Sikkema 00122 * 00123 * Revision 1.61 2003/12/13 23:08:46 csoutheren 00124 * Changed PRegularExpression to allow a copy constructor and operator = 00125 * 00126 * Revision 1.60 2003/12/10 03:28:50 csoutheren 00127 * Removed compile time warning under Linux 00128 * 00129 * Revision 1.59 2003/12/07 05:50:49 csoutheren 00130 * Blocked operator = for PRegularExpression 00131 * 00132 * Revision 1.58 2003/12/04 13:10:38 csoutheren 00133 * Made PRegularExpression copy constructor private to avoid accidental usage (and subsequent crash) 00134 * 00135 * Revision 1.57 2003/05/14 00:46:47 rjongbloed 00136 * Added constructor to string lists/arrays etc that takes a single PString. 00137 * 00138 * Revision 1.56 2003/03/31 01:23:56 robertj 00139 * Added ReadFrom functions for standard container classes such as 00140 * PIntArray and PStringList etc 00141 * 00142 * Revision 1.55 2003/03/05 08:48:32 robertj 00143 * Added PStringArray::ToCharAray() function at suggestion of Ravelli Rossano 00144 * 00145 * Revision 1.54 2002/11/12 09:17:44 robertj 00146 * Added PString::NumCompare() as functional equivalent of strncmp(). 00147 * Added PSortedStringList::GetNextStringsIndex() to do searches of binary 00148 * tree on partal strings. 00149 * 00150 * Revision 1.53 2002/10/31 05:53:44 robertj 00151 * Now comprehensively stated that a PString is ALWAYS an 8 bit string as 00152 * there are far too many inheerent assumptions every to make it 16 bit. 00153 * Added UTF-8/UCS-2 conversion functions to PString. 00154 * 00155 * Revision 1.52 2002/09/16 01:08:59 robertj 00156 * Added #define so can select if #pragma interface/implementation is used on 00157 * platform basis (eg MacOS) rather than compiler, thanks Robert Monaghan. 00158 * 00159 * Revision 1.51 2002/08/14 00:43:39 robertj 00160 * Added ability to have fixed maximum length PStringStream's so does not do 00161 * unwanted malloc()'s while outputing data. 00162 * 00163 * Revision 1.50 2002/04/09 02:30:18 robertj 00164 * Removed GCC3 variable as __GNUC__ can be used instead, thanks jason Spence 00165 * 00166 * Revision 1.49 2002/02/15 04:29:31 robertj 00167 * Added PString::Empty() to return the primordial empty string. Saves on a 00168 * couple of memory allocations for every empty string ever used. 00169 * 00170 * Revision 1.48 2002/01/26 23:55:55 craigs 00171 * Changed for GCC 3.0 compatibility, thanks to manty@manty.net 00172 * 00173 * Revision 1.47 2002/01/22 01:03:57 craigs 00174 * Added operator += and operator + functions to PStringArray and PStringList 00175 * Added AppendString operator to PStringArray 00176 * 00177 * Revision 1.46 2001/10/17 05:09:22 robertj 00178 * Added contructors and assigmnent operators so integer types can be 00179 * automatically converted to strings. 00180 * 00181 * Revision 1.45 2001/08/11 07:57:30 rogerh 00182 * Add Mac OS Carbon changes from John Woods <jfw@jfwhome.funhouse.com> 00183 * 00184 * Revision 1.44 2001/04/18 04:10:15 robertj 00185 * Removed hash function for caseless strings as confuses mixed dictionaries. 00186 * 00187 * Revision 1.43 2001/04/18 01:20:58 robertj 00188 * Fixed problem with hash function for short strings, thanks Patrick Koorevaar. 00189 * Also fixed hash function for caseless strings. 00190 * 00191 * Revision 1.42 2001/02/21 03:38:37 robertj 00192 * Added ability to copy between various string lists/arrays etc during construction. 00193 * 00194 * Revision 1.41 2001/02/13 04:39:08 robertj 00195 * Fixed problem with operator= in container classes. Some containers will 00196 * break unless the copy is virtual (eg PStringStream's buffer pointers) so 00197 * needed to add a new AssignContents() function to all containers. 00198 * 00199 * Revision 1.40 1999/08/22 12:13:43 robertj 00200 * Fixed warning when using inlines on older GNU compiler 00201 * 00202 * Revision 1.39 1999/05/28 14:01:22 robertj 00203 * Added initialisers to string containers (list, sorted list and set). 00204 * 00205 * Revision 1.38 1999/03/09 09:34:05 robertj 00206 * Fixed typo's. 00207 * 00208 * Revision 1.37 1999/03/09 02:59:50 robertj 00209 * Changed comments to doc++ compatible documentation. 00210 * 00211 * Revision 1.36 1999/02/16 08:11:09 robertj 00212 * MSVC 6.0 compatibility changes. 00213 * 00214 * Revision 1.35 1998/09/23 06:21:12 robertj 00215 * Added open source copyright license. 00216 * 00217 * Revision 1.34 1998/01/26 00:33:46 robertj 00218 * Added FindRegEx function to PString that returns position and length. 00219 * Added Execute() functions to PRegularExpression that take PINDEX references instead of PIntArrays. 00220 * Added static function to PRegularExpression to escape all special operator characters in a string. 00221 * 00222 * Revision 1.33 1998/01/05 10:39:35 robertj 00223 * Fixed "typesafe" templates/macros for dictionaries, especially on GNU. 00224 * 00225 * Revision 1.32 1997/12/11 13:32:47 robertj 00226 * Added AsUnsigned() function to convert string to DWORD. 00227 * 00228 * Revision 1.31 1997/12/11 10:29:49 robertj 00229 * Added type correct Contains() function to dictionaries. 00230 * 00231 * Revision 1.30 1997/07/08 13:13:47 robertj 00232 * DLL support. 00233 * 00234 * Revision 1.29 1997/06/10 11:36:32 craigs 00235 * Added inline statements to allow compilation uder Unix 00236 * 00237 * Revision 1.28 1997/06/08 04:48:58 robertj 00238 * Added regular expressions. 00239 * Fixed non-template class descendent order. 00240 * 00241 * Revision 1.27 1997/05/16 12:10:12 robertj 00242 * Fixed G++ compatibility bug. 00243 * 00244 * Revision 1.26 1996/10/08 13:13:38 robertj 00245 * Added operator += and &= for char so no implicit PString construction. 00246 * 00247 * Revision 1.25 1996/09/14 12:52:39 robertj 00248 * Added operator! for !IsEmpty(). 00249 * 00250 * Revision 1.24 1996/08/17 10:00:25 robertj 00251 * Changes for Windows DLL support. 00252 * 00253 * Revision 1.23 1996/05/26 03:27:02 robertj 00254 * Compatibility to GNU 2.7.x 00255 * 00256 * Revision 1.22 1996/03/31 08:51:22 robertj 00257 * Added RemoveAt() function to remove entries from dictionaries. 00258 * 00259 * Revision 1.21 1996/03/10 13:15:50 robertj 00260 * Added operator() to template version. 00261 * 00262 * Revision 1.20 1996/02/19 13:17:33 robertj 00263 * Removed PCaselessString hash function to fix dictionary match failure. 00264 * Added operator() to do string dictionary lookup with default value. 00265 * 00266 * Revision 1.19 1996/02/08 12:19:16 robertj 00267 * Added new operators to PString for case insensitive compare and spaced concatenate. 00268 * 00269 * Revision 1.18 1996/01/24 14:43:15 robertj 00270 * Added initialisers to string dictionaries. 00271 * 00272 * Revision 1.17 1996/01/23 13:15:17 robertj 00273 * Added Replace() function to strings. 00274 * Mac Metrowerks compiler support. 00275 * String searching algorithm rewrite. 00276 * 00277 * Revision 1.16 1996/01/02 12:04:31 robertj 00278 * Mac OS compatibility changes. 00279 * Removed requirement that PArray elements have parameterless constructor.. 00280 * 00281 * Revision 1.15 1995/12/23 03:46:23 robertj 00282 * Added operators for include and exclude from string set. 00283 * 00284 * Revision 1.14 1995/10/14 15:02:56 robertj 00285 * Changed arrays to not break references, but strings still need to. 00286 * 00287 * Revision 1.13 1995/06/17 11:13:08 robertj 00288 * Documentation update. 00289 * 00290 * Revision 1.12 1995/06/17 00:43:40 robertj 00291 * Added flag for PStringArray constructor to create caseless strings. 00292 * 00293 * Revision 1.11 1995/06/04 12:34:57 robertj 00294 * Better C++ compatibility (with BC++) 00295 * 00296 * Revision 1.10 1995/04/02 09:27:23 robertj 00297 * Added "balloon" help. 00298 * 00299 * Revision 1.9 1995/03/14 12:42:16 robertj 00300 * Updated documentation to use HTML codes. 00301 * 00302 * Revision 1.8 1995/03/12 04:44:39 robertj 00303 * Fixed use of PCaselessString as dictionary key. 00304 * 00305 * Revision 1.7 1995/02/05 00:48:09 robertj 00306 * Fixed template version. 00307 * 00308 * Revision 1.6 1995/01/15 04:50:20 robertj 00309 * Added inlines on friend functions, required by GNU compiler. 00310 * 00311 * Revision 1.5 1995/01/10 11:43:41 robertj 00312 * Removed PString parameter in stdarg function for GNU C++ compatibility. 00313 * 00314 * Revision 1.4 1995/01/09 12:33:44 robertj 00315 * Removed unnecesary return value from I/O functions. 00316 * Changed function names due to Mac port. 00317 * 00318 * Revision 1.3 1994/12/21 11:53:21 robertj 00319 * Documentation and variable normalisation. 00320 * 00321 * Revision 1.2 1994/12/12 13:13:13 robertj 00322 * Fixed bugs in PString mods just made. 00323 * 00324 * Revision 1.1 1994/12/12 09:59:37 robertj 00325 * Initial revision 00326 * 00327 */ 00328 00329 #ifdef P_USE_PRAGMA 00330 #pragma interface 00331 #endif 00332 00333 #include <string> 00334 #include <vector> 00335 00337 // PString class 00338 00339 class PStringArray; 00340 class PRegularExpression; 00341 00376 class PString : public PCharArray { 00377 PCLASSINFO(PString, PCharArray); 00378 00379 // using namespace std; 00380 00381 public: 00387 PINLINE PString(); 00388 00392 PINLINE PString( 00393 const PString & str 00394 ); 00395 00398 PINLINE PString( 00399 const std::string & str 00400 ); 00401 00410 PString( 00411 const char * cstr 00412 ); 00413 00418 PString( 00419 const WORD * ustr 00420 ); 00421 00435 PString( 00436 const char * cstr, 00437 PINDEX len 00438 ); 00439 00450 PString( 00451 const WORD * ustr, 00452 PINDEX len 00453 ); 00454 00465 PString( 00466 const PWORDArray & ustr 00467 ); 00468 00477 PString( 00478 char ch 00479 ); 00480 00485 PString( 00486 short n 00487 ); 00488 00493 PString( 00494 unsigned short n 00495 ); 00496 00501 PString( 00502 int n 00503 ); 00504 00509 PString( 00510 unsigned int n 00511 ); 00512 00517 PString( 00518 long n 00519 ); 00520 00525 PString( 00526 unsigned long n 00527 ); 00528 00533 PString( 00534 PInt64 n 00535 ); 00536 00541 PString( 00542 PUInt64 n 00543 ); 00544 00545 00546 enum ConversionType { 00547 Pascal, // Data is a length byte followed by characters. 00548 Basic, // Data is two length bytes followed by characters. 00549 Literal, // Data is C language style string with \ escape codes. 00550 Signed, // Convert a signed integer to a string. 00551 Unsigned, // Convert an unsigned integer to a string. 00552 Decimal, // Convert a real number to a string in decimal format. 00553 Exponent, // Convert a real number to a string in exponent format. 00554 Printf, // Formatted output, sprintf() style function. 00555 NumConversionTypes 00556 }; 00557 /* Type of conversion to make in the conversion constructors. 00558 */ 00559 00560 /* Contruct a new string converting from the spcified data source into 00561 a string array. 00562 */ 00563 PString( 00564 ConversionType type, 00565 const char * str, 00566 ... 00567 ); 00568 PString( 00569 ConversionType type, 00570 long value, 00571 unsigned base = 10 00572 ); 00573 PString( 00574 ConversionType type, 00575 double value, 00576 unsigned places 00577 ); 00578 00586 PString & operator=( 00587 const PString & str 00588 ); 00589 00599 PString & operator=( 00600 const char * cstr 00601 ); 00602 00611 PString & operator=( 00612 char ch 00613 ); 00614 00619 PString & operator=( 00620 short n 00621 ); 00622 00627 PString & operator=( 00628 unsigned short n 00629 ); 00630 00635 PString & operator=( 00636 int n 00637 ); 00638 00643 PString & operator=( 00644 unsigned int n 00645 ); 00646 00651 PString & operator=( 00652 long n 00653 ); 00654 00659 PString & operator=( 00660 unsigned long n 00661 ); 00662 00667 PString & operator=( 00668 PInt64 n 00669 ); 00670 00675 PString & operator=( 00676 PUInt64 n 00677 ); 00678 00681 virtual PString & MakeEmpty(); 00682 00685 static PString Empty(); 00687 00694 virtual PObject * Clone() const; 00695 00705 virtual Comparison Compare( 00706 const PObject & obj 00707 ) const; 00708 00711 virtual void PrintOn( 00712 ostream & strm 00713 ) const; 00714 00720 virtual void ReadFrom( 00721 istream & strm 00722 ); 00723 00737 virtual PINDEX HashFunction() const; 00739 00754 virtual BOOL SetSize( 00755 PINDEX newSize 00756 ); 00757 00766 virtual BOOL IsEmpty() const; 00767 00776 virtual BOOL MakeUnique(); 00778 00779 00792 BOOL MakeMinimumSize(); 00793 00802 PINLINE PINDEX GetLength() const; 00803 00810 bool operator!() const; 00812 00821 PString operator+( 00822 const PString & str 00823 ) const; 00824 00836 PString operator+( 00837 const char * cstr 00838 ) const; 00839 00851 PString operator+( 00852 char ch 00853 ) const; 00854 00866 friend PString operator+( 00867 const char * cstr, 00868 const PString & str 00869 ); 00870 00882 friend PString operator+( 00883 char c, 00884 const PString & str 00885 ); 00886 00892 PString & operator+=( 00893 const PString & str 00894 ); 00895 00905 PString & operator+=( 00906 const char * cstr 00907 ); 00908 00918 PString & operator+=( 00919 char ch 00920 ); 00921 00922 00929 PString operator&( 00930 const PString & str 00931 ) const; 00932 00949 PString operator&( 00950 const char * cstr 00951 ) const; 00952 00969 PString operator&( 00970 char ch 00971 ) const; 00972 00989 friend PString operator&( 00990 const char * cstr, 00991 const PString & str 00992 ); 00993 01010 friend PString operator&( 01011 char ch, 01012 const PString & str 01013 ); 01014 01020 PString & operator&=( 01021 const PString & str 01022 ); 01023 01038 PString & operator&=( 01039 const char * cstr 01040 ); 01041 01042 01057 PString & operator&=( 01058 char ch 01059 ); 01061 01062 01070 bool operator*=( 01071 const PString & str 01072 ) const; 01073 01081 bool operator==( 01082 const PObject & str 01083 ) const; 01084 01092 bool operator!=( 01093 const PObject & str 01094 ) const; 01095 01103 bool operator<( 01104 const PObject & str 01105 ) const; 01106 01114 bool operator>( 01115 const PObject & str 01116 ) const; 01117 01125 bool operator<=( 01126 const PObject & str 01127 ) const; 01128 01136 bool operator>=( 01137 const PObject & str 01138 ) const; 01139 01140 01151 bool operator*=( 01152 const char * cstr 01153 ) const; 01154 01165 bool operator==( 01166 const char * cstr 01167 ) const; 01168 01179 bool operator!=( 01180 const char * cstr 01181 ) const; 01182 01193 bool operator<( 01194 const char * cstr 01195 ) const; 01196 01207 bool operator>( 01208 const char * cstr 01209 ) const; 01210 01221 bool operator<=( 01222 const char * cstr 01223 ) const; 01224 01235 bool operator>=( 01236 const char * cstr 01237 ) const; 01238 01250 Comparison NumCompare( 01251 const PString & str, 01252 PINDEX count = P_MAX_INDEX, 01253 PINDEX offset = 0 01254 ) const; 01255 01267 Comparison NumCompare( 01268 const char * cstr, 01269 PINDEX count = P_MAX_INDEX, 01270 PINDEX offset = 0 01271 ) const; 01273 01274 01278 PINDEX Find( 01279 char ch, 01280 PINDEX offset = 0 01281 ) const; 01282 01284 PINDEX Find( 01285 const PString & str, 01286 PINDEX offset = 0 01287 ) const; 01288 01289 /* Locate the position within the string of the character or substring. The 01290 search will begin at the character offset provided. 01291 01292 If #offset# is beyond the length of the string, then the 01293 function will always return #P_MAX_INDEX#. 01294 01295 The matching will be for identical character or string. If a search 01296 ignoring case is required then the string should be converted to a 01297 #PCaselessString# before the search is made. 01298 01299 @return 01300 position of character or substring in the string, or P_MAX_INDEX if the 01301 character or substring is not in the string. 01302 */ 01303 PINDEX Find( 01304 const char * cstr, 01305 PINDEX offset = 0 01306 ) const; 01307 01309 PINDEX FindLast( 01310 char ch, 01311 PINDEX offset = P_MAX_INDEX 01312 ) const; 01313 01315 PINDEX FindLast( 01316 const PString & str, 01317 PINDEX offset = P_MAX_INDEX 01318 ) const; 01319 01337 PINDEX FindLast( 01338 const char * cstr, 01339 PINDEX offset = P_MAX_INDEX 01340 ) const; 01341 01343 PINDEX FindOneOf( 01344 const PString & set, 01345 PINDEX offset = 0 01346 ) const; 01347 01362 PINDEX FindOneOf( 01363 const char * cset, 01364 PINDEX offset = 0 01365 ) const; 01366 01377 PINDEX FindRegEx( 01378 const PRegularExpression & regex, 01379 PINDEX offset = 0 01380 ) const; 01381 01392 BOOL FindRegEx( 01393 const PRegularExpression & regex, 01394 PINDEX & pos, 01395 PINDEX & len, 01396 PINDEX offset = 0, 01397 PINDEX maxPos = P_MAX_INDEX 01398 ) const; 01399 01400 01411 void Replace( 01412 const PString & target, 01413 const PString & subs, 01414 BOOL all = FALSE, 01415 PINDEX offset = 0 01416 ); 01417 01425 void Splice( 01426 const PString & str, 01427 PINDEX pos, 01428 PINDEX len = 0 01429 ); 01430 01438 void Splice( 01439 const char * cstr, 01440 PINDEX pos, 01441 PINDEX len = 0 01442 ); 01443 01450 void Delete( 01451 PINDEX start, 01452 PINDEX len 01453 ); 01455 01456 01476 PString operator()( 01477 PINDEX start, 01478 PINDEX end 01479 ) const; 01480 01495 PString Left( 01496 PINDEX len 01497 ) const; 01498 01513 PString Right( 01514 PINDEX len 01515 ) const; 01516 01533 PString Mid( 01534 PINDEX start, 01535 PINDEX len = P_MAX_INDEX 01536 ) const; 01537 01538 01546 PString LeftTrim() const; 01547 01555 PString RightTrim() const; 01556 01565 PString Trim() const; 01566 01567 01576 PString ToLower() const; 01577 01586 PString ToUpper() const; 01587 01588 01590 PStringArray Tokenise( 01591 const PString & separators, 01593 BOOL onePerSeparator = TRUE 01595 ) const; 01618 PStringArray Tokenise( 01619 const char * cseparators, 01621 BOOL onePerSeparator = TRUE 01623 ) const; 01624 01638 PStringArray Lines() const; 01640 01657 PString & sprintf( 01658 const char * cfmt, 01659 ... 01660 ); 01661 01676 friend PString psprintf( 01677 const char * cfmt, 01678 ... 01679 ); 01680 01682 PString & vsprintf( 01683 const PString & fmt, 01684 va_list args 01685 ); 01700 PString & vsprintf( 01701 const char * cfmt, 01702 va_list args 01703 ); 01704 01706 friend PString pvsprintf( 01707 const char * cfmt, 01708 va_list args 01709 ); 01724 friend PString pvsprintf( 01725 const PString & fmt, 01726 va_list args 01727 ); 01728 01729 01742 long AsInteger( 01743 unsigned base = 10 01744 ) const; 01757 DWORD AsUnsigned( 01758 unsigned base = 10 01759 ) const; 01773 PInt64 AsInt64( 01774 unsigned base = 10 01775 ) const; 01789 PUInt64 AsUnsigned64( 01790 unsigned base = 10 01791 ) const; 01792 01803 double AsReal() const; 01804 01808 PWORDArray AsUCS2() const; 01809 01820 PBYTEArray ToPascal() const; 01821 01830 PString ToLiteral() const; 01831 01839 operator const unsigned char *() const; 01840 01842 01843 01844 protected: 01845 void InternalFromUCS2( 01846 const WORD * ptr, 01847 PINDEX len 01848 ); 01849 virtual Comparison InternalCompare( 01850 PINDEX offset, // Offset into string to compare. 01851 char c // Character to compare against. 01852 ) const; 01853 virtual Comparison InternalCompare( 01854 PINDEX offset, // Offset into string to compare. 01855 PINDEX length, // Number of characters to compare. 01856 const char * cstr // C string to compare against. 01857 ) const; 01858 /* Internal function to compare the current string value against the 01859 specified C string. 01860 01861 @return 01862 relative rank of the two strings. 01863 */ 01864 01865 PString(int dummy, const PString * str); 01866 }; 01867 01868 01870 01880 class PCaselessString : public PString 01881 { 01882 PCLASSINFO(PCaselessString, PString); 01883 01884 public: 01887 PCaselessString(); 01888 01892 PCaselessString( 01893 const char * cstr 01894 ); 01895 01900 PCaselessString( 01901 const PString & str 01902 ); 01903 01904 01912 PCaselessString & operator=( 01913 const PString & str 01914 ); 01915 01925 PCaselessString & operator=( 01926 const char * cstr 01927 ); 01928 01937 PCaselessString & operator=( 01938 char ch 01939 ); 01940 01941 01942 // Overrides from class PObject 01947 virtual PObject * Clone() const; 01948 01949 protected: 01950 // Overrides from class PString 01951 virtual Comparison InternalCompare( 01952 PINDEX offset, // Offset into string to compare. 01953 char c // Character to compare against. 01954 ) const; 01955 virtual Comparison InternalCompare( 01956 PINDEX offset, // Offset into string to compare. 01957 PINDEX length, // Number of characters to compare. 01958 const char * cstr // C string to compare against. 01959 ) const; 01960 /* Internal function to compare the current string value against the 01961 specified C string. 01962 01963 @return 01964 relative rank of the two strings or characters. 01965 */ 01966 01967 PCaselessString(int dummy, const PCaselessString * str); 01968 }; 01969 01971 01972 class PStringStream; 01973 01980 class PStringStream : public PString, public iostream 01981 { 01982 PCLASSINFO(PStringStream, PString); 01983 01984 public: 01990 PStringStream(); 01991 01996 PStringStream( 01997 PINDEX fixedBufferSize 01998 ); 01999 02006 PStringStream( 02007 const PString & str 02008 ); 02009 02014 PStringStream( 02015 const char * cstr 02016 ); 02017 02020 virtual PString & MakeEmpty(); 02021 02033 PStringStream & operator=( 02034 const PStringStream & strm 02035 ); 02036 02048 PStringStream & operator=( 02049 const PString & str 02050 ); 02051 02067 PStringStream & operator=( 02068 const char * cstr 02069 ); 02070 02079 PStringStream & operator=( 02080 char ch 02081 ); 02082 02083 02085 virtual ~PStringStream(); 02086 02087 02088 protected: 02089 virtual void AssignContents(const PContainer & cont); 02090 02091 private: 02092 PStringStream(int, const PStringStream &) : iostream(cout.rdbuf()) { } 02093 02094 class Buffer : public streambuf { 02095 public: 02096 Buffer(PStringStream & str, PINDEX size); 02097 Buffer(const Buffer & sbuf); 02098 Buffer & operator=(const Buffer & sbuf); 02099 virtual int overflow(int=EOF); 02100 virtual int underflow(); 02101 virtual int sync(); 02102 #ifdef __USE_STL__ 02103 virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out); 02104 virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out); 02105 #else 02106 virtual streampos seekoff(streamoff, ios::seek_dir, int); 02107 #endif 02108 PStringStream & string; 02109 BOOL fixedBufferSize; 02110 }; 02111 }; 02112 02113 02114 class PStringList; 02115 class PSortedStringList; 02116 02129 #ifdef DOC_PLUS_PLUS 02130 class PStringArray : public PArray { 02131 #endif 02132 PDECLARE_ARRAY(PStringArray, PString); 02133 public: 02140 PStringArray( 02141 PINDEX count, 02142 char const * const * strarr, 02143 BOOL caseless = FALSE 02144 ); 02147 PStringArray( 02148 const PString & str 02149 ); 02152 PStringArray( 02153 const PStringList & list 02154 ); 02157 PStringArray( 02158 const PSortedStringList & list 02159 ); 02160 02164 PStringArray( 02165 const std::vector<PString> & vec 02166 ) 02167 { 02168 for (std::vector<PString>::const_iterator r = vec.begin(); r != vec.end(); ++r) 02169 AppendString(*r); 02170 } 02171 02175 PStringArray( 02176 const std::vector<std::string> & vec 02177 ) 02178 { 02179 for (std::vector<std::string>::const_iterator r = vec.begin(); r != vec.end(); ++r) 02180 AppendString(PString(*r)); 02181 } 02182 02186 template <typename stlContainer> 02187 static PStringArray container( 02188 const stlContainer & vec 02189 ) 02190 { 02191 PStringArray list; 02192 for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r) 02193 list.AppendString(PString(*r)); 02194 return list; 02195 } 02196 02198 02206 virtual void ReadFrom( 02207 istream &strm // Stream to read the objects contents from. 02208 ); 02210 02219 PINDEX GetStringsIndex( 02220 const PString & str 02221 ) const; 02222 02223 PString operator[]( 02224 PINDEX index 02225 ) const; 02226 02234 PString & operator[]( 02235 PINDEX index 02236 ); 02237 02240 PINDEX AppendString( 02241 const PString & str 02242 ); 02243 02249 PStringArray & operator +=(const PStringArray & array); 02250 PStringArray & operator +=(const PString & str); 02251 02252 02259 PStringArray operator + (const PStringArray & array); 02260 PStringArray operator + (const PString & str); 02261 02269 char ** ToCharArray( 02270 PCharArray * storage = NULL 02271 ) const; 02273 }; 02274 02275 02288 #ifdef DOC_PLUS_PLUS 02289 class PStringList : public PList { 02290 #endif 02291 PDECLARE_LIST(PStringList, PString); 02292 public: 02297 PStringList( 02298 PINDEX count, 02299 char const * const * strarr, 02300 BOOL caseless = FALSE 02301 ); 02304 PStringList( 02305 const PString & str 02306 ); 02309 PStringList( 02310 const PStringArray & array 02311 ); 02314 PStringList( 02315 const PSortedStringList & list 02316 ); 02318 02326 virtual void ReadFrom( 02327 istream &strm // Stream to read the objects contents from. 02328 ); 02330 02335 PINDEX AppendString( 02336 const PString & str 02337 ); 02338 02341 PINDEX InsertString( 02342 const PString & before, 02343 const PString & str 02344 ); 02345 02349 PINDEX GetStringsIndex( 02350 const PString & str 02351 ) const; 02352 02358 PStringList & operator +=(const PStringList & list); 02359 PStringList & operator +=(const PString & str); 02360 02361 02368 PStringList operator + (const PStringList & array); 02369 PStringList operator + (const PString & str); 02370 02374 template <typename stlContainer> 02375 static PStringList container( 02376 const stlContainer & vec 02377 ) 02378 { 02379 PStringList list; 02380 for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r) 02381 list.AppendString(PString(*r)); 02382 return list; 02383 } 02385 }; 02386 02387 02400 #ifdef DOC_PLUS_PLUS 02401 class PSortedStringList : public PSortedList { 02402 #endif 02403 PDECLARE_SORTED_LIST(PSortedStringList, PString); 02404 public: 02409 PSortedStringList( 02410 PINDEX count, 02411 char const * const * strarr, 02412 BOOL caseless = FALSE 02413 ); 02416 PSortedStringList( 02417 const PString & str 02418 ); 02421 PSortedStringList( 02422 const PStringArray & array 02423 ); 02426 PSortedStringList( 02427 const PStringList & list 02428 ); 02430 02438 virtual void ReadFrom( 02439 istream &strm // Stream to read the objects contents from. 02440 ); 02442 02448 PINDEX AppendString( 02449 const PString & str 02450 ); 02451 02455 PINDEX GetStringsIndex( 02456 const PString & str 02457 ) const; 02458 02463 PINDEX GetNextStringsIndex( 02464 const PString & str 02465 ) const; 02467 02468 protected: 02469 PINDEX InternalStringSelect( 02470 const char * str, 02471 PINDEX len, 02472 Element * thisElement 02473 ) const; 02474 }; 02475 02476 02493 #ifdef DOC_PLUS_PLUS 02494 class PStringSet : public PSet { 02495 #endif 02496 PDECLARE_SET(PStringSet, PString, TRUE); 02497 public: 02502 PStringSet( 02503 PINDEX count, 02504 char const * const * strarr, 02505 BOOL caseless = FALSE 02506 ); 02509 PStringSet( 02510 const PString & str 02511 ); 02513 02521 virtual void ReadFrom( 02522 istream &strm 02523 ); 02525 02529 void Include( 02530 const PString & key 02531 ); 02533 PStringSet & operator+=( 02534 const PString & key 02535 ); 02537 void Exclude( 02538 const PString & key 02539 ); 02541 PStringSet & operator-=( 02542 const PString & key 02543 ); 02545 }; 02546 02547 02548 #ifdef PHAS_TEMPLATES 02549 02557 template <class K> class PStringDictionary : public PAbstractDictionary 02558 { 02559 PCLASSINFO(PStringDictionary, PAbstractDictionary); 02560 02561 public: 02570 PStringDictionary() 02571 : PAbstractDictionary() { } 02573 02580 virtual PObject * Clone() const 02581 { return PNEW PStringDictionary(0, this); } 02583 02598 const PString & operator[](const K & key) const 02599 { return (const PString &)GetRefAt(key); } 02600 02614 PString operator()(const K & key, const char * dflt = "") const 02615 { if (AbstractContains(key)) return (*this)[key]; return dflt; } 02616 02625 BOOL Contains( 02626 const K & key // Key to look for in the dictionary. 02627 ) const { return AbstractContains(key); } 02628 02640 virtual PString * RemoveAt( 02641 const K & key // Key for position in dictionary to get object. 02642 ) { 02643 PString * s = GetAt(key); AbstractSetAt(key, NULL); 02644 return reference->deleteObjects ? (s ? (PString *)-1 : NULL) : s; 02645 } 02646 02653 virtual PString * GetAt( 02654 const K & key // Key for position in dictionary to get object. 02655 ) const { return (PString *)AbstractGetAt(key); } 02656 02665 virtual BOOL SetDataAt( 02666 PINDEX index, // Ordinal index in the dictionary. 02667 const PString & str // New string value to put into the dictionary. 02668 ) { return PAbstractDictionary::SetDataAt(index, PNEW PString(str)); } 02669 02681 virtual BOOL SetAt( 02682 const K & key, // Key for position in dictionary to add object. 02683 const PString & str // New string value to put into the dictionary. 02684 ) { return AbstractSetAt(key, PNEW PString(str)); } 02685 02697 const K & GetKeyAt(PINDEX index) const 02698 { return (const K &)AbstractGetKeyAt(index); } 02699 02711 PString & GetDataAt(PINDEX index) const 02712 { return (PString &)AbstractGetDataAt(index); } 02714 02715 protected: 02716 PStringDictionary(int dummy, const PStringDictionary * c) 02717 : PAbstractDictionary(dummy, c) { } 02718 }; 02719 02720 02735 #define PDECLARE_STRING_DICTIONARY(cls, K) \ 02736 PDECLARE_CLASS(cls, PStringDictionary<K>) \ 02737 protected: \ 02738 cls(int dummy, const cls * c) \ 02739 : PStringDictionary<K>(dummy, c) { } \ 02740 public: \ 02741 cls() \ 02742 : PStringDictionary<K>() { } \ 02743 virtual PObject * Clone() const \ 02744 { return PNEW cls(0, this); } \ 02745 02746 02759 #define PSTRING_DICTIONARY(cls, K) typedef PStringDictionary<K> cls 02760 02761 02762 #else // PHAS_TEMPLATES 02763 02764 02765 #define PSTRING_DICTIONARY(cls, K) \ 02766 class cls : public PAbstractDictionary { \ 02767 PCLASSINFO(cls, PAbstractDictionary) \ 02768 protected: \ 02769 inline cls(int dummy, const cls * c) \ 02770 : PAbstractDictionary(dummy, c) { } \ 02771 public: \ 02772 inline cls() \ 02773 : PAbstractDictionary() { } \ 02774 inline PObject * Clone() const \ 02775 { return PNEW cls(0, this); } \ 02776 inline PString & operator[](const K & key) const \ 02777 { return (PString &)GetRefAt(key); } \ 02778 inline PString operator()(const K & key, const char * dflt = "") const \ 02779 { if (Contains(key)) return (PString &)GetRefAt(key); return dflt; } \ 02780 virtual BOOL Contains(const K & key) const \ 02781 { return AbstractContains(key); } \ 02782 virtual PString * RemoveAt(const K & key) \ 02783 { PString * s = GetAt(key); AbstractSetAt(key, NULL); \ 02784 return reference->deleteObjects ? (s ? (PString *)-1 : NULL) : s; } \ 02785 virtual PString * GetAt(const K & key) const \ 02786 { return (PString *)AbstractGetAt(key); } \ 02787 virtual BOOL SetDataAt(PINDEX index, const PString & str) \ 02788 { return PAbstractDictionary::SetDataAt(index,PNEW PString(str));} \ 02789 virtual BOOL SetAt(const K & key, const PString & str) \ 02790 { return AbstractSetAt(key, PNEW PString(str)); } \ 02791 inline const K & GetKeyAt(PINDEX index) const \ 02792 { return (const K &)AbstractGetKeyAt(index); } \ 02793 inline PString & GetDataAt(PINDEX index) const \ 02794 { return (PString &)AbstractGetDataAt(index); } \ 02795 } 02796 02797 #define PDECLARE_STRING_DICTIONARY(cls, K) \ 02798 PSTRING_DICTIONARY(cls##_PTemplate, K); \ 02799 PDECLARE_CLASS(cls, cls##_PTemplate) \ 02800 protected: \ 02801 cls(int dummy, const cls * c) \ 02802 : cls##_PTemplate(dummy, c) { } \ 02803 public: \ 02804 cls() \ 02805 : cls##_PTemplate() { } \ 02806 virtual PObject * Clone() const \ 02807 { return PNEW cls(0, this); } \ 02808 02809 #endif // PHAS_TEMPLATES 02810 02811 02826 #ifdef DOC_PLUS_PLUS 02827 class POrdinalToString : public PStringDictionary { 02828 #endif 02829 PDECLARE_STRING_DICTIONARY(POrdinalToString, POrdinalKey); 02830 public: 02833 02834 struct Initialiser { 02836 PINDEX key; 02838 const char * value; 02839 }; 02842 POrdinalToString( 02843 PINDEX count, 02844 const Initialiser * init 02845 ); 02847 02855 virtual void ReadFrom( 02856 istream &strm // Stream to read the objects contents from. 02857 ); 02859 }; 02860 02873 #ifdef DOC_PLUS_PLUS 02874 class PStringToOrdinal : public POrdinalDictionary { 02875 #endif 02876 PDECLARE_ORDINAL_DICTIONARY(PStringToOrdinal, PString); 02877 public: 02880 02881 struct Initialiser { 02883 const char * key; 02885 PINDEX value; 02886 }; 02889 PStringToOrdinal( 02890 PINDEX count, 02891 const Initialiser * init, 02892 BOOL caseless = FALSE 02893 ); 02895 02903 virtual void ReadFrom( 02904 istream &strm // Stream to read the objects contents from. 02905 ); 02907 }; 02908 02909 02923 #ifdef DOC_PLUS_PLUS 02924 class PStringToString : public PStringDictionary { 02925 #endif 02926 PDECLARE_STRING_DICTIONARY(PStringToString, PString); 02927 public: 02930 02931 struct Initialiser { 02933 const char * key; 02935 const char * value; 02936 }; 02939 PStringToString( 02940 PINDEX count, 02941 const Initialiser * init, 02942 BOOL caselessKeys = FALSE, 02943 BOOL caselessValues = FALSE 02944 ); 02946 02954 virtual void ReadFrom( 02955 istream &strm // Stream to read the objects contents from. 02956 ); 02958 }; 02959 02960 02966 class PRegularExpression : public PObject 02967 { 02968 PCLASSINFO(PRegularExpression, PObject); 02969 02970 public: 02973 02974 enum { 02976 Extended = 1, 02978 IgnoreCase = 2, 02983 AnchorNewLine = 4 02984 }; 02986 enum { 02993 NotBeginningOfLine = 1, 02995 NotEndofLine = 2 02996 }; 02997 02999 PRegularExpression(); 03000 03003 PRegularExpression( 03004 const PString & pattern, 03005 int flags = IgnoreCase 03006 ); 03007 03010 PRegularExpression( 03011 const char * cpattern, 03012 int flags = IgnoreCase 03013 ); 03014 03018 PRegularExpression( 03019 const PRegularExpression & 03020 ); 03021 03025 PRegularExpression & operator =( 03026 const PRegularExpression & 03027 ); 03028 03030 ~PRegularExpression(); 03032 03035 03036 enum ErrorCodes { 03038 NoError = 0, 03040 NoMatch, 03041 03042 // POSIX regcomp return error codes. (In the order listed in the standard.) 03044 BadPattern, 03046 CollateError, 03048 BadClassType, 03050 BadEscape, 03052 BadSubReg, 03054 UnmatchedBracket, 03056 UnmatchedParen, 03058 UnmatchedBrace, 03060 BadBR, 03062 RangeError, 03064 OutOfMemory, 03066 BadRepitition, 03067 03068 /* Error codes we've added. */ 03070 PrematureEnd, 03072 TooBig, 03074 UnmatchedRParen, 03076 NotCompiled 03077 }; 03078 03084 ErrorCodes GetErrorCode() const; 03085 03092 PString GetErrorText() const; 03094 03098 BOOL Compile( 03099 const PString & pattern, 03100 int flags = IgnoreCase 03101 ); 03109 BOOL Compile( 03110 const char * cpattern, 03111 int flags = IgnoreCase 03112 ); 03113 03114 03116 BOOL Execute( 03117 const PString & str, 03118 PINDEX & start, 03119 int flags = 0 03120 ) const; 03122 BOOL Execute( 03123 const PString & str, 03124 PINDEX & start, 03125 PINDEX & len, 03126 int flags = 0 03127 ) const; 03129 BOOL Execute( 03130 const char * cstr, 03131 PINDEX & start, 03132 int flags = 0 03133 ) const; 03135 BOOL Execute( 03136 const char * cstr, 03137 PINDEX & start, 03138 PINDEX & len, 03139 int flags = 0 03140 ) const; 03142 BOOL Execute( 03143 const PString & str, 03144 PIntArray & starts, 03145 int flags = 0 03146 ) const; 03148 BOOL Execute( 03149 const PString & str, 03150 PIntArray & starts, 03151 PIntArray & ends, 03152 int flags = 0 03153 ) const; 03155 BOOL Execute( 03156 const char * cstr, 03157 PIntArray & starts, 03158 int flags = 0 03159 ) const; 03175 BOOL Execute( 03176 const char * cstr, 03177 PIntArray & starts, 03178 PIntArray & ends, 03179 int flags = 0 03180 ) const; 03182 03191 static PString EscapeString( 03192 const PString & str 03193 ); 03195 03196 protected: 03197 PString patternSaved; 03198 int flagsSaved; 03199 03200 void * expression; 03201 int lastError; 03202 }; 03203 03204 PString psprintf(const char * cfmt, ...); 03205 03206 // End Of File ///////////////////////////////////////////////////////////////