1 /* 2 * pstring.h 3 * 4 * Character string class. 5 * 6 * Portable Windows Library 7 * 8 * Copyright (c) 1993-1998 Equivalence Pty. Ltd. 9 * 10 * The contents of this file are subject to the Mozilla Public License 11 * Version 1.0 (the "License"); you may not use this file except in 12 * compliance with the License. You may obtain a copy of the License at 13 * http://www.mozilla.org/MPL/ 14 * 15 * Software distributed under the License is distributed on an "AS IS" 16 * basis, WITHOUT WARRANTY OF ANY KIND, either express or implied. See 17 * the License for the specific language governing rights and limitations 18 * under the License. 19 * 20 * The Original Code is Portable Windows Library. 21 * 22 * The Initial Developer of the Original Code is Equivalence Pty. Ltd. 23 * 24 * Portions are Copyright (C) 1993 Free Software Foundation, Inc. 25 * All Rights Reserved. 26 * 27 * Contributor(s): ______________________________________. 28 * 29 * $Revision: 27359 $ 30 * $Author: rjongbloed $ 31 * $Date: 2012-03-28 21:17:54 -0500 (Wed, 28 Mar 2012) $ 32 */ 33 34 #ifndef PTLIB_STRING_H 35 #define PTLIB_STRING_H 36 37 #ifdef P_USE_PRAGMA 38 #pragma interface 39 #endif 40 41 #include <string> 42 #include <vector> 43 #include <ptlib/array.h> 44 45 /////////////////////////////////////////////////////////////////////////////// 46 // PString class 47 48 class PStringArray; 49 class PRegularExpression; 50 class PString; 51 52 /**The same as the standard C snprintf(fmt, 1000, ...), but returns a 53 PString instead of a const char *. 54 */ 55 PString psprintf( 56 const char * fmt, ///< printf style format string 57 ... 58 ); 59 60 /**The same as the standard C vsnprintf(fmt, 1000, va_list arg), but 61 returns a PString instead of a const char *. 62 */ 63 PString pvsprintf( 64 const char * fmt, ///< printf style format string 65 va_list arg ///< Arguments for formatting 66 ); 67 68 #if (defined(_WIN32) || defined(_WIN32_WCE)) && (!defined(_NATIVE_WCHAR_T_DEFINED)) && (!defined(__MINGW32__)) 69 PBASEARRAY(PWCharArray, unsigned short); 70 #else 71 PBASEARRAY(PWCharArray, wchar_t); 72 #endif 73 74 /**The character string class. It supports a wealth of additional functions 75 for string processing and conversion. Operators are provided so that 76 strings can virtually be treated as a basic type. 77 78 An important feature of the string class, which is not present in other 79 container classes, is that when the string contents is changed, that is 80 resized or elements set, the string is "dereferenced", and a duplicate 81 made of its contents. That is this instance of the array is disconnected 82 from all other references to the string data, if any, and a new string array 83 contents created. For example consider the following: 84 <pre><code> 85 PString s1 = "String"; // New array allocated and set to "String" 86 PString s2 = s1; // s2 has pointer to same array as s1 87 // and reference count is 2 for both 88 s1[0] = 's'; // Breaks references into different strings 89 </code></pre> 90 at the end s1 is "string" and s2 is "String" both with reference count of 1. 91 92 The functions that will "break" a reference are <code>SetSize()</code>, 93 <code>SetMinSize()</code>, <code>GetPointer()</code>, <code>SetAt()</code> and 94 <code>operator[]</code>. 95 96 Note that the array is a '\\0' terminated string as in C strings. Thus the 97 memory allocated, and the length of the string may be different values. 98 99 Also note that the PString is inherently an 8 bit string. The character set 100 is not defined for most operations and it may be any 8 bit character set. 101 However when conversions are being made to or from 2 byte formats then the 102 PString is assumed to be the UTF-8 format. The 2 byte format is nominally 103 UCS-2 (aka BMP string) and while it is not exactly the same as UNICODE 104 they are compatible enough for them to be treated the same for most real 105 world usage. 106 */ 107 108 class PString : public PCharArray 109 { 110 PCLASSINFO(PString, PCharArray); 111 112 // using namespace std; 113 114 public: 115 typedef const char * Initialiser; 116 117 /**@name Construction */ 118 //@{ 119 /**Construct an empty string. This will have one character in it which is 120 the '\\0' character. 121 */ 122 PINLINE PString(); 123 124 /**Create a new reference to the specified string. The string memory is not 125 copied, only the pointer to the data. 126 */ 127 PINLINE PString( 128 const PString & str ///< String to create new reference to. 129 ); 130 131 /**Create a new string from the specified std::string 132 */ 133 PINLINE PString( 134 const std::string & str 135 ); 136 137 /**Create a string from the C string array. This is most commonly used with 138 a literal string, eg "hello". A new memory block is allocated of a size 139 sufficient to take the length of the string and its terminating 140 '\\0' character. 141 142 If UCS-2 is used then each char from the char pointer is mapped to a 143 single UCS-2 character. 144 */ 145 PString( 146 const char * cstr ///< Standard '\\0' terminated C string. 147 ); 148 149 /**Create a string from the UCS-2 string array. 150 A new memory block is allocated of a size sufficient to take the length 151 of the string and its terminating '\\0' character. 152 */ 153 PString( 154 const wchar_t * ustr ///< UCS-2 null terminated string. 155 ); 156 157 /**Create a string from the array. A new memory block is allocated of 158 a size equal to <code>len</code> plus one which is sufficient to take 159 the string and a terminating '\\0' character. 160 161 If UCS-2 is used then each char from the char pointer is mapped to a 162 single UCS-2 character. 163 164 Note that this function will allow a string with embedded '\\0' 165 characters to be created, but most of the functions here will be unable 166 to access characters beyond the first '\\0'. Furthermore, if the 167 <code>MakeMinimumSize()</code> function is called, all data beyond that first 168 '\\0' character will be lost. 169 */ 170 PString( 171 const char * cstr, ///< Pointer to a string of characters. 172 PINDEX len ///< Length of the string in bytes. 173 ); 174 175 /**Create a string from the UCS-2 array. A new memory block is allocated 176 of a size equal to <code>len</code> plus one which is sufficient to take 177 the string and a terminating '\\0' character. 178 179 Note that this function will allow a string with embedded '\\0' 180 characters to be created, but most of the functions here will be unable 181 to access characters beyond the first '\\0'. Furthermore, if the 182 <code>MakeMinimumSize()</code> function is called, all data beyond that first 183 '\\0' character will be lost. 184 */ 185 PString( 186 const wchar_t * ustr, ///< Pointer to a string of UCS-2 characters. 187 PINDEX len ///< Length of the string in bytes. 188 ); 189 190 /**Create a string from the UCS-2 array. A new memory block is allocated 191 of a size equal to <code>len</code> plus one which is sufficient to take 192 the string and a terminating '\\0' character. 193 194 Note that this function will allow a string with embedded '\\0' 195 characters to be created, but most of the functions here will be unable 196 to access characters beyond the first '\\0'. Furthermore, if the 197 <code>MakeMinimumSize()</code> function is called, all data beyond that first 198 '\\0' character will be lost. 199 */ 200 PString( 201 const PWCharArray & ustr ///< UCS-2 null terminated string. 202 ); 203 204 /**Create a string from the single character. This is most commonly used 205 as a type conversion constructor when a literal character, eg 'A' is 206 used in a string expression. A new memory block is allocated of two 207 characters to take the char and its terminating '\\0' character. 208 209 If UCS-2 is used then the char is mapped to a single UCS-2 210 character. 211 */ 212 PString( 213 char ch ///< Single character to initialise string. 214 ); 215 216 /**Create a string from the integer type. 217 This will create a simple base 10, shortest length conversion of the 218 integer (with sign character if appropriate) into the string. 219 */ 220 PString( 221 short n ///< Integer to convert 222 ); 223 224 /**Create a string from the integer type. 225 This will create a simple base 10, shortest length conversion of the 226 integer (with sign character if appropriate) into the string. 227 */ 228 PString( 229 unsigned short n ///< Integer to convert 230 ); 231 232 /**Create a string from the integer type. 233 This will create a simple base 10, shortest length conversion of the 234 integer (with sign character if appropriate) into the string. 235 */ 236 PString( 237 int n ///< Integer to convert 238 ); 239 240 /**Create a string from the integer type. 241 This will create a simple base 10, shortest length conversion of the 242 integer (with sign character if appropriate) into the string. 243 */ 244 PString( 245 unsigned int n ///< Integer to convert 246 ); 247 248 /**Create a string from the integer type. 249 This will create a simple base 10, shortest length conversion of the 250 integer (with sign character if appropriate) into the string. 251 */ 252 PString( 253 long n ///< Integer to convert 254 ); 255 256 /**Create a string from the integer type. 257 This will create a simple base 10, shortest length conversion of the 258 integer (with sign character if appropriate) into the string. 259 */ 260 PString( 261 unsigned long n ///< Integer to convert 262 ); 263 264 /**Create a string from the integer type. 265 This will create a simple base 10, shortest length conversion of the 266 integer (with sign character if appropriate) into the string. 267 */ 268 PString( 269 PInt64 n ///< Integer to convert 270 ); 271 272 /**Create a string from the integer type. 273 This will create a simple base 10, shortest length conversion of the 274 integer (with sign character if appropriate) into the string. 275 */ 276 PString( 277 PUInt64 n ///< Integer to convert 278 ); 279 280 281 enum ConversionType { 282 Pascal, // Data is a length byte followed by characters. 283 Basic, // Data is two length bytes followed by characters. 284 Literal, // Data is C language style string with \\ escape codes. 285 Signed, // Convert a signed integer to a string. 286 Unsigned, // Convert an unsigned integer to a string. 287 Decimal, // Convert a real number to a string in decimal format. 288 Exponent, // Convert a real number to a string in exponent format. 289 Printf, // Formatted output, sprintf() style function. 290 NumConversionTypes 291 }; 292 /* Type of conversion to make in the conversion constructors. 293 */ 294 295 /* Contruct a new string converting from the spcified data source into 296 a string array. 297 */ 298 PString( 299 ConversionType type, ///< Type of data source for conversion. 300 const char * str, ///< String to convert. 301 ... ///< Extra parameters for <code>sprintf()</code> call. 302 ); 303 PString( 304 ConversionType type, ///< Type of data source for conversion. 305 long value, ///< Integer value to convert. 306 unsigned base = 10 ///< Number base to use for the integer conversion. 307 ); 308 PString( 309 ConversionType type, ///< Type of data source for conversion. 310 double value, ///< Floating point value to convert. 311 unsigned places ///< Number of decimals in real number output. 312 ); 313 314 /**Assign the string to the current object. The current instance then 315 becomes another reference to the same string in the <code>str</code> 316 parameter. 317 318 @return 319 reference to the current PString object. 320 */ 321 PString & operator=( 322 const PString & str ///< New string to assign. 323 ); 324 325 /**Assign the string to the current object. The current instance then 326 becomes another reference to the same string in the <code>str</code> 327 parameter. 328 329 @return 330 reference to the current PString object. 331 */ 332 PString & operator=( 333 const std::string & str ///< New string to assign. 334 ) { return operator=(str.c_str()); } 335 336 /**Assign the C string to the current object. The current instance then 337 becomes a unique reference to a copy of the <code>cstr</code> parameter. 338 The <code>cstr</code> parameter is typically a literal string, eg: 339 <pre><code> 340 myStr = "fred"; 341 </code></pre> 342 @return 343 reference to the current PString object. 344 */ 345 PString & operator=( 346 const char * cstr ///< C string to assign. 347 ); 348 349 /**Assign the character to the current object. The current instance then 350 becomes a unique reference to a copy of the character parameter. eg: 351 <pre><code> 352 myStr = 'A'; 353 </code></pre> 354 @return 355 reference to the current PString object. 356 */ 357 PString & operator=( 358 char ch ///< Character to assign. 359 ); 360 361 /**Assign a string from the integer type. 362 This will create a simple base 10, shortest length conversion of the 363 integer (with sign character if appropriate) into the string. 364 */ 365 PString & operator=( 366 short n ///< Integer to convert 367 ); 368 369 /**Assign a string from the integer type. 370 This will create a simple base 10, shortest length conversion of the 371 integer (with sign character if appropriate) into the string. 372 */ 373 PString & operator=( 374 unsigned short n ///< Integer to convert 375 ); 376 377 /**Assign a string from the integer type. 378 This will create a simple base 10, shortest length conversion of the 379 integer (with sign character if appropriate) into the string. 380 */ 381 PString & operator=( 382 int n ///< Integer to convert 383 ); 384 385 /**Assign a string from the integer type. 386 This will create a simple base 10, shortest length conversion of the 387 integer (with sign character if appropriate) into the string. 388 */ 389 PString & operator=( 390 unsigned int n ///< Integer to convert 391 ); 392 393 /**Assign a string from the integer type. 394 This will create a simple base 10, shortest length conversion of the 395 integer (with sign character if appropriate) into the string. 396 */ 397 PString & operator=( 398 long n ///< Integer to convert 399 ); 400 401 /**Assign a string from the integer type. 402 This will create a simple base 10, shortest length conversion of the 403 integer (with sign character if appropriate) into the string. 404 */ 405 PString & operator=( 406 unsigned long n ///< Integer to convert 407 ); 408 409 /**Assign a string from the integer type. 410 This will create a simple base 10, shortest length conversion of the 411 integer (with sign character if appropriate) into the string. 412 */ 413 PString & operator=( 414 PInt64 n ///< Integer to convert 415 ); 416 417 /**Assign a string from the integer type. 418 This will create a simple base 10, shortest length conversion of the 419 integer (with sign character if appropriate) into the string. 420 */ 421 PString & operator=( 422 PUInt64 n ///< Integer to convert 423 ); 424 425 /**Make the current string empty 426 */ 427 virtual PString & MakeEmpty(); 428 429 /**Return an empty string. 430 */ 431 static PString Empty(); 432 //@} 433 434 /**@name Overrides from class PObject */ 435 //@{ 436 /**Make a complete duplicate of the string. Note that the data in the 437 array of characters is duplicated as well and the new object is a 438 unique reference to that data. 439 */ 440 virtual PObject * Clone() const; 441 442 /**Get the relative rank of the two strings. The system standard function, 443 eg strcmp(), is used. 444 445 @return 446 comparison of the two objects, <code>EqualTo</code> for same, 447 <code>LessThan</code> for <code>obj</code> logically less than the 448 object and <code>GreaterThan</code> for <code>obj</code> logically 449 greater than the object. 450 */ 451 virtual Comparison Compare( 452 const PObject & obj ///< Other PString to compare against. 453 ) const; 454 455 /**Output the string to the specified stream. 456 */ 457 virtual void PrintOn( 458 ostream & strm ///< I/O stream to output to. 459 ) const; 460 461 /**Input the string from the specified stream. This will read all 462 characters until a end of line is reached. The end of line itself is 463 <b>not</b> placed in the string, however it <b>is</b> removed from 464 the stream. 465 */ 466 virtual void ReadFrom( 467 istream & strm ///< I/O stream to input from. 468 ); 469 470 /**Calculate a hash value for use in sets and dictionaries. 471 472 The hash function for strings will produce a value based on the sum of 473 the first three characters of the string. This is a fairly basic 474 function and make no assumptions about the string contents. A user may 475 descend from PString and override the hash function if they can take 476 advantage of the types of strings being used, eg if all strings start 477 with the letter 'A' followed by 'B or 'C' then the current hash function 478 will not perform very well. 479 480 @return 481 hash value for string. 482 */ 483 virtual PINDEX HashFunction() const; 484 //@} 485 486 /**@name Overrides from class PContainer */ 487 //@{ 488 /**Set the size of the string. A new string may be allocated to accomodate 489 the new number of characters. If the string increases in size then the 490 new characters are initialised to zero. If the string is made smaller 491 then the data beyond the new size is lost. 492 493 Note that this function will break the current instance from multiple 494 references to an array. A new array is allocated and the data from the 495 old array copied to it. 496 497 @return 498 true if the memory for the array was allocated successfully. 499 */ 500 virtual PBoolean SetSize( 501 PINDEX newSize ///< New size of the array in elements. 502 ); 503 504 /**Determine if the string is empty. This is semantically slightly 505 different from the usual <code>PContainer::IsEmpty()</code> function. It does 506 not test for <code>PContainer::GetSize()</code> equal to zero, it tests for 507 <code>GetLength()</code> equal to zero. 508 509 @return 510 true if no non-null characters in string. 511 */ 512 virtual PBoolean IsEmpty() const; 513 514 /**Make this instance to be the one and only reference to the container 515 contents. This implicitly does a clone of the contents of the container 516 to make a unique reference. If the instance was already unique then 517 the function does nothing. 518 519 @return 520 true if the instance was already unique. 521 */ 522 virtual PBoolean MakeUnique(); 523 //@} 524 525 526 /**@name Size/Length functions */ 527 //@{ 528 /**Set the actual memory block array size to the minimum required to hold 529 the current string contents. 530 531 Note that this function will break the current instance from multiple 532 references to the string. A new string buffer is allocated and the data 533 from the old string buffer copied to it. 534 535 @return 536 true if new memory block successfully allocated. 537 */ 538 PBoolean MakeMinimumSize(); 539 540 /**Determine the length of the null terminated string. This is different 541 from <code>PContainer::GetSize()</code> which returns the amount of memory 542 allocated to the string. This is often, though no necessarily, one 543 larger than the length of the string. 544 545 @return 546 length of the null terminated string. 547 */ 548 PINLINE PINDEX GetLength() const; 549 550 /**Determine if the string is NOT empty. This is semantically identical 551 to executing !IsEmpty() on the string. 552 553 @return 554 true if non-null characters in string. 555 */ 556 bool operator!() const; 557 //@} 558 559 /**@name Concatenation operators **/ 560 //@{ 561 /**Concatenate two strings to produce a third. The original strings are 562 not modified, an entirely new unique reference to a string is created. 563 564 @return 565 new string with concatenation of the object and parameter. 566 */ 567 PString operator+( 568 const PString & str ///< String to concatenate. 569 ) const; 570 571 /**Concatenate a C string to a PString to produce a third. The original 572 string is not modified, an entirely new unique reference to a string 573 is created. The <code>cstr</code> parameter is typically a literal 574 string, eg: 575 <pre><code> 576 myStr = aStr + "fred"; 577 </code></pre> 578 579 @return 580 new string with concatenation of the object and parameter. 581 */ 582 PString operator+( 583 const char * cstr ///< C string to concatenate. 584 ) const; 585 586 /**Concatenate a single character to a PString to produce a third. The 587 original string is not modified, an entirely new unique reference to a 588 string is created. The <code>ch</code> parameter is typically a 589 literal, eg: 590 <pre><code> 591 myStr = aStr + '!'; 592 </code></pre> 593 594 @return 595 new string with concatenation of the object and parameter. 596 */ 597 PString operator+( 598 char ch ///< Character to concatenate. 599 ) const; 600 601 /**Concatenate a PString to a C string to produce a third. The original 602 string is not modified, an entirely new unique reference to a string 603 is created. The <code>cstr</code> parameter is typically a literal 604 string, eg: 605 <pre><code> 606 myStr = "fred" + aStr; 607 </code></pre> 608 609 @return 610 new string with concatenation of the object and parameter. 611 */ 612 friend PString operator+( 613 const char * cstr, ///< C string to be concatenated to. 614 const PString & str ///< String to concatenate. 615 ); 616 617 /**Concatenate a PString to a single character to produce a third. The 618 original string is not modified, an entirely new unique reference to a 619 string is created. The <code>ch</code> parameter is typically a literal, 620 eg: 621 <pre><code> 622 myStr = '!' + aStr; 623 </code></pre> 624 625 @return 626 new string with concatenation of the object and parameter. 627 */ 628 friend PString operator+( 629 char ch, ///< Character to be concatenated to. 630 const PString & str ///< String to concatenate. 631 ); 632 633 /**Concatenate a string to another string, modifiying that string. 634 635 @return 636 reference to string that was concatenated to. 637 */ 638 PString & operator+=( 639 const PString & str ///< String to concatenate. 640 ); 641 642 /**Concatenate a C string to a PString, modifiying that string. The 643 <code>cstr</code> parameter is typically a literal string, eg: 644 <pre><code> 645 myStr += "fred"; 646 </code></pre> 647 648 @return 649 reference to string that was concatenated to. 650 */ 651 PString & operator+=( 652 const char * cstr ///< C string to concatenate. 653 ); 654 655 /**Concatenate a single character to a PString. The <code>ch</code> 656 parameter is typically a literal, eg: 657 <pre><code> 658 myStr += '!'; 659 </code></pre> 660 661 @return 662 new string with concatenation of the object and parameter. 663 */ 664 PString & operator+=( 665 char ch ///< Character to concatenate. 666 ); 667 668 669 /**Concatenate two strings to produce a third. The original strings are 670 not modified, an entirely new unique reference to a string is created. 671 672 @return 673 new string with concatenation of the object and parameter. 674 */ 675 PString operator&( 676 const PString & str ///< String to concatenate. 677 ) const; 678 679 /**Concatenate a C string to a PString to produce a third. The original 680 string is not modified, an entirely new unique reference to a string 681 is created. The <code>cstr</code> parameter is typically a literal 682 string, eg: 683 <pre><code> 684 myStr = aStr & "fred"; 685 </code></pre> 686 687 This function differes from operator+ in that it assures there is at 688 least one space between the strings. Exactly one space is added if 689 there is not a space at the end of the first or beggining of the last 690 string. 691 692 @return 693 new string with concatenation of the object and parameter. 694 */ 695 PString operator&( 696 const char * cstr ///< C string to concatenate. 697 ) const; 698 699 /**Concatenate a single character to a PString to produce a third. The 700 original string is not modified, an entirely new unique reference to a 701 string is created. The <code>ch</code> parameter is typically a 702 literal, eg: 703 <pre><code> 704 myStr = aStr & '!'; 705 </code></pre> 706 707 This function differes from operator+ in that it assures there is at 708 least one space between the strings. Exactly one space is added if 709 there is not a space at the end of the first or beggining of the last 710 string. 711 712 @return 713 new string with concatenation of the object and parameter. 714 */ 715 PString operator&( 716 char ch ///< Character to concatenate. 717 ) const; 718 719 /**Concatenate a PString to a C string to produce a third. The original 720 string is not modified, an entirely new unique reference to a string 721 is created. The <code>cstr</code> parameter is typically a literal 722 string, eg: 723 <pre><code> 724 myStr = "fred" & aStr; 725 </code></pre> 726 727 This function differes from operator+ in that it assures there is at 728 least one space between the strings. Exactly one space is added if 729 there is not a space at the end of the first or beggining of the last 730 string. 731 732 @return 733 new string with concatenation of the object and parameter. 734 */ 735 friend PString operator&( 736 const char * cstr, ///< C string to be concatenated to. 737 const PString & str ///< String to concatenate. 738 ); 739 740 /**Concatenate a PString to a single character to produce a third. The 741 original string is not modified, an entirely new unique reference to a 742 string is created. The <code>c</code> parameter is typically a literal, 743 eg: 744 <pre><code> 745 myStr = '!' & aStr; 746 </code></pre> 747 748 This function differs from <code>operator+</code> in that it assures there is at 749 least one space between the strings. Exactly one space is added if 750 there is not a space at the end of the first or beggining of the last 751 string. 752 753 @return 754 new string with concatenation of the object and parameter. 755 */ 756 friend PString operator&( 757 char ch, ///< Character to be concatenated to. 758 const PString & str ///< String to concatenate. 759 ); 760 761 /**Concatenate a string to another string, modifiying that string. 762 763 @return 764 reference to string that was concatenated to. 765 */ 766 PString & operator&=( 767 const PString & str ///< String to concatenate. 768 ); 769 770 /**Concatenate a C string to a PString, modifiying that string. The 771 <code>cstr</code> parameter is typically a literal string, eg: 772 <pre><code> 773 myStr &= "fred"; 774 </code></pre> 775 776 This function differes from operator+ in that it assures there is at 777 least one space between the strings. Exactly one space is added if 778 there is not a space at the end of the first or beggining of the last 779 string. 780 781 @return 782 reference to string that was concatenated to. 783 */ 784 PString & operator&=( 785 const char * cstr ///< C string to concatenate. 786 ); 787 788 789 /**Concatenate a character to a PString, modifiying that string. The 790 <code>ch</code> parameter is typically a literal string, eg: 791 <pre><code> 792 myStr &= '!'; 793 </code></pre> 794 795 This function differes from operator+ in that it assures there is at 796 least one space between the strings. Exactly one space is added if 797 there is not a space at the end of the first or beggining of the last 798 string. 799 800 @return 801 reference to string that was concatenated to. 802 */ 803 PString & operator&=( 804 char ch ///< Character to concatenate. 805 ); 806 //@} 807 808 809 /**@name Comparison operators */ 810 //@{ 811 /**Compare two strings using case insensitive comparison. 812 813 @return 814 true if equal. 815 */ 816 bool operator*=( 817 const PString & str ///< PString object to compare against. 818 ) const; 819 820 /**Compare two strings using the <code>PObject::Compare()</code> function. This 821 is identical to the <code>PObject</code> class function but is necessary due 822 to other overloaded versions. 823 824 @return 825 true if equal. 826 */ 827 bool operator==( 828 const PObject & str ///< PString object to compare against. 829 ) const; 830 831 /**Compare two strings using the <code>PObject::Compare()</code> function. This 832 is identical to the <code>PObject</code> class function but is necessary due 833 to other overloaded versions. 834 835 @return 836 true if not equal. 837 */ 838 bool operator!=( 839 const PObject & str ///< PString object to compare against. 840 ) const; 841 842 /**Compare two strings using the <code>PObject::Compare()</code> function. This 843 is identical to the <code>PObject</code> class function but is necessary due 844 to other overloaded versions. 845 846 @return 847 true if less than. 848 */ 849 bool operator<( 850 const PObject & str ///< PString object to compare against. 851 ) const; 852 853 /**Compare two strings using the <code>PObject::Compare()</code> function. This 854 is identical to the <code>PObject</code> class function but is necessary due 855 to other overloaded versions. 856 857 @return 858 true if greater than. 859 */ 860 bool operator>( 861 const PObject & str ///< PString object to compare against. 862 ) const; 863 864 /**Compare two strings using the <code>PObject::Compare()</code> function. This 865 is identical to the <code>PObject</code> class function but is necessary due 866 to other overloaded versions. 867 868 @return 869 true if less than or equal. 870 */ 871 bool operator<=( 872 const PObject & str ///< PString object to compare against. 873 ) const; 874 875 /**Compare two strings using the <code>PObject::Compare()</code> function. This 876 is identical to the <code>PObject</code> class function but is necessary due 877 to other overloaded versions. 878 879 @return 880 true if greater than or equal. 881 */ 882 bool operator>=( 883 const PObject & str ///< PString object to compare against. 884 ) const; 885 886 887 /**Compare a PString to a C string using a case insensitive compare 888 function. The <code>cstr</code> parameter is typically a literal string, 889 eg: 890 <pre><code> 891 if (myStr == "fred") 892 </code></pre> 893 894 @return 895 true if equal. 896 */ 897 bool operator*=( 898 const char * cstr ///< C string to compare against. 899 ) const; 900 901 /**Compare a PString to a C string using the <code>Compare()</code> 902 function. The <code>cstr</code> parameter is typically a literal string, 903 eg: 904 <pre><code> 905 if (myStr == "fred") 906 </code></pre> 907 908 @return 909 true if equal. 910 */ 911 bool operator==( 912 const char * cstr ///< C string to compare against. 913 ) const; 914 915 /**Compare a PString to a C string using the <code>PObject::Compare()</code> 916 function. The <code>cstr</code> parameter is typically a literal 917 string, eg: 918 <pre><code> 919 if (myStr != "fred") 920 </code></pre> 921 922 @return 923 true if not equal. 924 */ 925 bool operator!=( 926 const char * cstr ///< C string to compare against. 927 ) const; 928 929 /**Compare a PString to a C string using the <code>PObject::Compare()</code> 930 function. The <code>cstr</code> parameter is typically a literal 931 string, eg: 932 <pre><code> 933 if (myStr < "fred") 934 </code></pre> 935 936 @return 937 true if less than. 938 */ 939 bool operator<( 940 const char * cstr ///< C string to compare against. 941 ) const; 942 943 /**Compare a PString to a C string using the <code>PObject::Compare()</code> 944 function. The <code>cstr</code> parameter is typically a literal 945 string, eg: 946 <pre><code> 947 if (myStr > "fred") 948 </code></pre> 949 950 @return 951 true if greater than. 952 */ 953 bool operator>( 954 const char * cstr ///< C string to compare against. 955 ) const; 956 957 /**Compare a PString to a C string using the <code>PObject::Compare()</code> 958 function. The <code>cstr</code> parameter is typically a literal 959 string, eg: 960 <pre><code> 961 if (myStr <= "fred") 962 </code></pre> 963 964 @return 965 true if less than or equal. 966 */ 967 bool operator<=( 968 const char * cstr ///< C string to compare against. 969 ) const; 970 971 /**Compare a PString to a C string using the <code>PObject::Compare()</code> 972 function. The <code>cstr</code> parameter is typically a literal 973 string, eg: 974 <pre><code> 975 if (myStr >= "fred") 976 </code></pre> 977 978 @return 979 true if greater than or equal. 980 */ 981 bool operator>=( 982 const char * cstr ///< C string to compare against. 983 ) const; 984 985 /**Compare a string against a substring of the object. 986 This will compare at most <code>count</code> characters of the string, starting at 987 the specified <code>offset</code>, against that many characters of the <code>str</code> 988 parameter. If <code>count</code> greater than the length of the <code>str</code> parameter 989 then the actual length of <code>str</code> is used. If <code>count</code> and the length of 990 <code>str</code> are greater than the length of the string remaining from the 991 <code>offset</code> then false is returned. 992 993 @return 994 true if str is a substring of . 995 */ 996 Comparison NumCompare( 997 const PString & str, ///< PString object to compare against. 998 PINDEX count = P_MAX_INDEX, ///< Number of chacracters in str to compare 999 PINDEX offset = 0 ///< Offset into string to compare 1000 ) const; 1001 1002 /**Compare a string against a substring of the object. 1003 This will compare at most <code>count</code> characters of the string, starting at 1004 the specified <code>offset</code>, against that many characters of the <code>str</code> 1005 parameter. If <code>count</code> greater than the length of the <code>str</code> parameter 1006 then the actual length of <code>str</code> is used. If <code>count</code> and the length of 1007 <code>str</code> are greater than the length of the string remaining from the 1008 <code>offset</code> then false is returned. 1009 1010 @return 1011 true if str is a substring of . 1012 */ 1013 Comparison NumCompare( 1014 const char * cstr, ///< C string object to compare against. 1015 PINDEX count = P_MAX_INDEX, ///< Number of chacracters in str to compare 1016 PINDEX offset = 0 ///< Offset into string to compare 1017 ) const; 1018 //@} 1019 1020 1021 /**@name Search & replace functions */ 1022 //@{ 1023 /** Locate the position within the string of the character. */ 1024 PINDEX Find( 1025 char ch, ///< Character to search for in string. 1026 PINDEX offset = 0 ///< Offset into string to begin search. 1027 ) const; 1028 1029 /** Locate the position within the string of the substring. */ 1030 PINDEX Find( 1031 const PString & str, ///< String to search for in string. 1032 PINDEX offset = 0 ///< Offset into string to begin search. 1033 ) const; 1034 1035 /* Locate the position within the string of the character or substring. The 1036 search will begin at the character offset provided. 1037 1038 If <code>offset</code> is beyond the length of the string, then the 1039 function will always return P_MAX_INDEX. 1040 1041 The matching will be for identical character or string. If a search 1042 ignoring case is required then the string should be converted to a 1043 PCaselessString before the search is made. 1044 1045 @return 1046 position of character or substring in the string, or P_MAX_INDEX if the 1047 character or substring is not in the string. 1048 */ 1049 PINDEX Find( 1050 const char * cstr, ///< C string to search for in string. 1051 PINDEX offset = 0 ///< Offset into string to begin search. 1052 ) const; 1053 1054 /** Locate the position of the last matching character. */ 1055 PINDEX FindLast( 1056 char ch, ///< Character to search for in string. 1057 PINDEX offset = P_MAX_INDEX ///< Offset into string to begin search. 1058 ) const; 1059 1060 /** Locate the position of the last matching substring. */ 1061 PINDEX FindLast( 1062 const PString & str, ///< String to search for in string. 1063 PINDEX offset = P_MAX_INDEX ///< Offset into string to begin search. 1064 ) const; 1065 1066 /**Locate the position of the last matching substring. 1067 Locate the position within the string of the last matching character or 1068 substring. The search will begin at the character offset provided, 1069 moving backward through the string. 1070 1071 If <code>offset</code> is beyond the length of the string, then the 1072 search begins at the end of the string. If <code>offset</code> is zero 1073 then the function always returns P_MAX_INDEX. 1074 1075 The matching will be for identical character or string. If a search 1076 ignoring case is required then the string should be converted to a 1077 PCaselessString before the search is made. 1078 1079 @return 1080 position of character or substring in the string, or P_MAX_INDEX if the 1081 character or substring is not in the string. 1082 */ 1083 PINDEX FindLast( 1084 const char * cstr, ///< C string to search for in string. 1085 PINDEX offset = P_MAX_INDEX ///< Offset into string to begin search. 1086 ) const; 1087 1088 /** Locate the position of one of the characters in the set. */ 1089 PINDEX FindOneOf( 1090 const PString & set, ///< String of characters to search for in string. 1091 PINDEX offset = 0 ///< Offset into string to begin search. 1092 ) const; 1093 1094 /**Locate the position of one of the characters in the set. 1095 The search will begin at the character offset provided. 1096 1097 If <code>offset</code> is beyond the length of the string, then the 1098 function will always return P_MAX_INDEX. 1099 1100 The matching will be for identical character or string. If a search 1101 ignoring case is required then the string should be converted to a 1102 PCaselessString before the search is made. 1103 1104 @return 1105 position of character in the string, or P_MAX_INDEX if no characters 1106 from the set are in the string. 1107 */ 1108 PINDEX FindOneOf( 1109 const char * cset, ///< C string of characters to search for in string. 1110 PINDEX offset = 0 ///< Offset into string to begin search. 1111 ) const; 1112 1113 /** Locate the position of character not in the set. */ 1114 PINDEX FindSpan( 1115 const PString & set, ///< String of characters to search for in string. 1116 PINDEX offset = 0 ///< Offset into string to begin search. 1117 ) const; 1118 1119 /**Locate the position of character not in the set. 1120 The search will begin at the character offset provided. 1121 1122 If <code>offset</code> is beyond the length of the string, or every character 1123 in the string is a member of the set, then the function will always 1124 return P_MAX_INDEX. 1125 1126 The matching will be for identical character or string. If a search 1127 ignoring case is required then the string should be converted to a 1128 PCaselessString before the search is made. 1129 1130 @return 1131 position of character not in the set, or P_MAX_INDEX if all characters 1132 from the string are in the set. 1133 */ 1134 PINDEX FindSpan( 1135 const char * cset, ///< C string of characters to search for in string. 1136 PINDEX offset = 0 ///< Offset into string to begin search. 1137 ) const; 1138 1139 /**Locate the position within the string of one of the regular expression. 1140 The search will begin at the character offset provided. 1141 1142 If <code>offset</code> is beyond the length of the string, then the 1143 function will always return P_MAX_INDEX. 1144 1145 @return 1146 position of regular expression in the string, or P_MAX_INDEX if no 1147 characters from the set are in the string. 1148 */ 1149 PINDEX FindRegEx( 1150 const PRegularExpression & regex, ///< regular expression to find 1151 PINDEX offset = 0 ///< Offset into string to begin search. 1152 ) const; 1153 1154 /**Locate the position within the string of one of the regular expression. 1155 The search will begin at the character offset provided. 1156 1157 If <code>offset</code> is beyond the length of the string, then the 1158 function will always return P_MAX_INDEX. 1159 1160 @return 1161 position of regular expression in the string, or P_MAX_INDEX if no 1162 characters from the set are in the string. 1163 */ 1164 PBoolean FindRegEx( 1165 const PRegularExpression & regex, ///< regular expression to find 1166 PINDEX & pos, ///< Position of matched expression 1167 PINDEX & len, ///< Length of matched expression 1168 PINDEX offset = 0, ///< Offset into string to begin search. 1169 PINDEX maxPos = P_MAX_INDEX ///< Maximum offset into string 1170 ) const; 1171 1172 1173 /**Return true if the entire string matches the regular expression 1174 */ 1175 PBoolean MatchesRegEx( 1176 const PRegularExpression & regex ///< regular expression to match 1177 ) const; 1178 1179 /**Locate the substring within the string and replace it with the specifed 1180 substring. The search will begin at the character offset provided. 1181 1182 If <code>offset</code> is beyond the length of the string, then the 1183 function will do nothing. 1184 1185 The matching will be for identical character or string. If a search 1186 ignoring case is required then the string should be converted to a 1187 PCaselessString before the search is made. 1188 */ 1189 void Replace( 1190 const PString & target, ///< Text to be removed. 1191 const PString & subs, ///< String to be inserted into the gaps created 1192 PBoolean all = false, ///< Replace all occurrences of target text. 1193 PINDEX offset = 0 ///< Offset into string to begin search. 1194 ); 1195 1196 /**Splice the string into the current string at the specified position. The 1197 specified number of bytes are removed from the string. 1198 1199 Note that this function will break the current instance from multiple 1200 references to the string. A new string buffer is allocated and the data 1201 from the old string buffer copied to it. 1202 */ 1203 void Splice( 1204 const PString & str, ///< Substring to insert. 1205 PINDEX pos, ///< Position in string to insert the substring. 1206 PINDEX len = 0 ///< Length of section to remove. 1207 ); 1208 1209 /**Splice the string into the current string at the specified position. The 1210 specified number of bytes are removed from the string. 1211 1212 Note that this function will break the current instance from multiple 1213 references to the string. A new string buffer is allocated and the data 1214 from the old string buffer copied to it. 1215 */ 1216 void Splice( 1217 const char * cstr, ///< Substring to insert. 1218 PINDEX pos, ///< Position in string to insert the substring. 1219 PINDEX len = 0 ///< Length of section to remove. 1220 ); 1221 1222 /**Remove the substring from the string. 1223 1224 Note that this function will break the current instance from multiple 1225 references to the string. A new string buffer is allocated and the data 1226 from the old string buffer copied to it. 1227 */ 1228 void Delete( 1229 PINDEX start, ///< Position in string to remove. 1230 PINDEX len ///< Number of characters to delete. 1231 ); 1232 //@} 1233 1234 1235 /**@name Sub-string functions */ 1236 //@{ 1237 /**Extract a portion of the string into a new string. The original string 1238 is not changed and a new unique reference to a string is returned. 1239 1240 The substring is returned inclusive of the characters at the 1241 <code>start</code> and <code>end</code> positions. 1242 1243 If the <code>end</code> position is greater than the length of the 1244 string then all characters from the <code>start</code> up to the end of 1245 the string are returned. 1246 1247 If <code>start</code> is greater than the length of the string or 1248 <code>end</code> is before <code>start</code> then an empty string is 1249 returned. 1250 1251 @return 1252 substring of the source string. 1253 */ 1254 PString operator()( 1255 PINDEX start, ///< Starting position of the substring. 1256 PINDEX end ///< Ending position of the substring. 1257 ) const; 1258 1259 /**Extract a portion of the string into a new string. The original string 1260 is not changed and a new unique reference to a string is returned. 1261 1262 A substring from the beginning of the string for the number of 1263 characters specified is extracted. 1264 1265 If <code>len</code> is greater than the length of the string then all 1266 characters to the end of the string are returned. 1267 1268 If <code>len</code> is zero then an empty string is returned. 1269 1270 @return 1271 substring of the source string. 1272 */ 1273 PString Left( 1274 PINDEX len ///< Number of characters to extract. 1275 ) const; 1276 1277 /**Extract a portion of the string into a new string. The original string 1278 is not changed and a new unique reference to a string is returned. 1279 1280 A substring from the end of the string for the number of characters 1281 specified is extracted. 1282 1283 If <code>len</code> is greater than the length of the string then all 1284 characters to the beginning of the string are returned. 1285 1286 If <code>len</code> is zero then an empty string is returned. 1287 1288 @return 1289 substring of the source string. 1290 */ 1291 PString Right( 1292 PINDEX len ///< Number of characters to extract. 1293 ) const; 1294 1295 /**Extract a portion of the string into a new string. The original string 1296 is not changed and a new unique reference to a string is returned. 1297 1298 A substring from the <code>start</code> position for the number of 1299 characters specified is extracted. 1300 1301 If <code>len</code> is greater than the length of the string from the 1302 <code>start</code> position then all characters to the end of the 1303 string are returned. 1304 1305 If <code>start</code> is greater than the length of the string or 1306 <code>len</code> is zero then an empty string is returned. 1307 1308 @return 1309 substring of the source string. 1310 */ 1311 PString Mid( 1312 PINDEX start, ///< Starting position of the substring. 1313 PINDEX len = P_MAX_INDEX ///< Number of characters to extract. 1314 ) const; 1315 1316 1317 /**Create a string consisting of all characters from the source string 1318 except all spaces at the beginning of the string. The original string 1319 is not changed and a new unique reference to a string is returned. 1320 1321 @return 1322 string with leading spaces removed. 1323 */ 1324 PString LeftTrim() const; 1325 1326 /**Create a string consisting of all characters from the source string 1327 except all spaces at the end of the string. The original string is not 1328 changed and a new unique reference to a string is returned. 1329 1330 @return 1331 string with trailing spaces removed. 1332 */ 1333 PString RightTrim() const; 1334 1335 /**Create a string consisting of all characters from the source string 1336 except all spaces at the beginning and end of the string. The original 1337 string is not changed and a new unique reference to a string is 1338 returned. 1339 1340 @return 1341 string with leading and trailing spaces removed. 1342 */ 1343 PString Trim() const; 1344 1345 1346 /**Create a string consisting of all characters from the source string 1347 with all upper case letters converted to lower case. The original 1348 string is not changed and a new unique reference to a string is 1349 returned. 1350 1351 @return 1352 string with upper case converted to lower case. 1353 */ 1354 PString ToLower() const; 1355 1356 /**Create a string consisting of all characters from the source string 1357 with all lower case letters converted to upper case. The original 1358 string is not changed and a new unique reference to a string is 1359 returned. 1360 1361 @return 1362 string with lower case converted to upper case. 1363 */ 1364 PString ToUpper() const; 1365 1366 1367 /** Split the string into an array of substrings. */ 1368 PStringArray Tokenise( 1369 const PString & separators, 1370 ///< A string for the set of separator characters that delimit tokens. 1371 PBoolean onePerSeparator = true 1372 ///< Flag for if there are empty tokens between consecutive separators. 1373 ) const; 1374 /**Split the string into an array of substrings. 1375 Divide the string into an array of substrings delimited by characters 1376 from the specified set. 1377 1378 There are two options for the tokenisation, the first is where the 1379 <code>onePerSeparator</code> is true. This form will produce a token 1380 for each delimiter found in the set. Thus the string ",two,three,,five" 1381 would be split into 5 substrings; "", "two", "three", "" and "five". 1382 1383 The second form where <code>onePerSeparator</code> is false is used 1384 where consecutive delimiters do not constitute a empty token. In this 1385 case the string " a list of words " would be split into 5 substrings; 1386 "a", "list", "of", "words" and "". 1387 1388 There is an important distinction when there are delimiters at the 1389 beginning or end of the source string. In the first case there will be 1390 empty strings at the end of the array. In the second case delimeters 1391 at the beginning of the source string are ignored and if there are one 1392 or more trailing delimeters, they will yeild a single empty string at 1393 the end of the array. 1394 1395 @return 1396 an array of substring for each token in the string. 1397 */ 1398 PStringArray Tokenise( 1399 const char * cseparators, 1400 ///< A C string for the set of separator characters that delimit tokens. 1401 PBoolean onePerSeparator = true 1402 ///< Flag for if there are empty tokens between consecutive separators. 1403 ) const; 1404 1405 /**Split the string into individual lines. The line delimiters may be a 1406 carriage return ('\\r'), a line feed ('\\n') or a carriage return and 1407 line feed pair ("\\r\\n"). A line feed and carriage return pair ("\\n\\r") 1408 would yield a blank line. between the characters. 1409 1410 The <code>Tokenise()</code> function should not be used to split a string 1411 into lines as a #"\\r\\n"# pair consitutes a single line 1412 ending. The <code>Tokenise()</code> function would produce a blank line in 1413 between them. 1414 1415 @return 1416 string array with a substring for each line in the string. 1417 */ 1418 PStringArray Lines() const; 1419 //@} 1420 1421 /**@name Conversion functions */ 1422 //@{ 1423 /**Concatenate a formatted output to the string. This is identical to the 1424 standard C library <code>sprintf()</code> function, but appends its 1425 output to the string. 1426 1427 This function makes the assumption that there is less the 1000 1428 characters of formatted output. The function will assert if this occurs. 1429 1430 Note that this function will break the current instance from multiple 1431 references to the string. A new string buffer is allocated and the data 1432 from the old string buffer copied to it. 1433 1434 @return 1435 reference to the current string object. 1436 */ 1437 PString & sprintf( 1438 const char * cfmt, ///< C string for output format. 1439 ... ///< Extra parameters for <code>sprintf()</code> call. 1440 ); 1441 1442 /**Produce formatted output as a string. This is identical to the standard 1443 C library <code>sprintf()</code> function, but sends its output to a 1444 <code>PString</code>. 1445 1446 This function makes the assumption that there is less the 1000 1447 characters of formatted output. The function will assert if this occurs. 1448 1449 Note that this function will break the current instance from multiple 1450 references to the string. A new string buffer is allocated and the data 1451 from the old string buffer copied to it. 1452 1453 @return 1454 reference to the current string object. 1455 */ 1456 friend PString psprintf( 1457 const char * cfmt, ///< C string for output format. 1458 ... ///< Extra parameters for <code>sprintf()</code> call. 1459 ); 1460 1461 /** Concatenate a formatted output to the string. */ 1462 PString & vsprintf( 1463 const PString & fmt, ///< String for output format. 1464 va_list args ///< Extra parameters for <code>sprintf()</code> call. 1465 ); 1466 /**Concatenate a formatted output to the string. This is identical to the 1467 standard C library <code>vsprintf()</code> function, but appends its 1468 output to the string. 1469 1470 This function makes the assumption that there is less the 1000 1471 characters of formatted output. The function will assert if this occurs. 1472 1473 Note that this function will break the current instance from multiple 1474 references to the string. A new string buffer is allocated and the data 1475 from the old string buffer copied to it. 1476 1477 @return 1478 reference to the current string object. 1479 */ 1480 PString & vsprintf( 1481 const char * cfmt, ///< C string for output format. 1482 va_list args ///< Extra parameters for <code>sprintf()</code> call. 1483 ); 1484 1485 /** Produce formatted output as a string. */ 1486 friend PString pvsprintf( 1487 const char * cfmt, ///< C string for output format. 1488 va_list args ///< Extra parameters for <code>sprintf()</code> call. 1489 ); 1490 /**Produce formatted output as a string. This is identical to the standard 1491 C library <code>vsprintf()</code> function, but sends its output to a 1492 <code>PString</code>. 1493 1494 This function makes the assumption that there is less the 1000 1495 characters of formatted output. The function will assert if this occurs. 1496 1497 Note that this function will break the current instance from multiple 1498 references to the string. A new string buffer is allocated and the data 1499 from the old string buffer copied to it. 1500 1501 @return 1502 reference to the current string object. 1503 */ 1504 friend PString pvsprintf( 1505 const PString & fmt, ///< String for output format. 1506 va_list args ///< Extra parameters for <code>sprintf()</code> call. 1507 ); 1508 1509 1510 /**Convert the string to an integer value using the specified number base. 1511 All characters up to the first illegal character for the number base are 1512 converted. Case is not significant for bases greater than 10. 1513 1514 The number base may only be from 2 to 36 and the function will assert 1515 if it is not in this range. 1516 1517 This function uses the standard C library <code>strtol()</code> function. 1518 1519 @return 1520 integer value for the string. 1521 */ 1522 long AsInteger( 1523 unsigned base = 10 ///< Number base to convert the string in. 1524 ) const; 1525 /**Convert the string to an integer value using the specified number base. 1526 All characters up to the first illegal character for the number base are 1527 converted. Case is not significant for bases greater than 10. 1528 1529 The number base may only be from 2 to 36 and the function will assert 1530 if it is not in this range. 1531 1532 This function uses the standard C library <code>strtoul()</code> function. 1533 1534 @return 1535 integer value for the string. 1536 */ 1537 DWORD AsUnsigned( 1538 unsigned base = 10 ///< Number base to convert the string in. 1539 ) const; 1540 /**Convert the string to an integer value using the specified number base. 1541 All characters up to the first illegal character for the number base are 1542 converted. Case is not significant for bases greater than 10. 1543 1544 The number base may only be from 2 to 36 and the function will assert 1545 if it is not in this range. 1546 1547 This function uses the standard C library <code>strtoq()</code> 1548 or <code>strtoul()</code> function. 1549 1550 @return 1551 integer value for the string. 1552 */ 1553 PInt64 AsInt64( 1554 unsigned base = 10 ///< Number base to convert the string in. 1555 ) const; 1556 /**Convert the string to an integer value using the specified number base. 1557 All characters up to the first illegal character for the number base are 1558 converted. Case is not significant for bases greater than 10. 1559 1560 The number base may only be from 2 to 36 and the function will assert 1561 if it is not in this range. 1562 1563 This function uses the standard C library <code>strtouq()</code> 1564 or <code>strtoul()</code> function. 1565 1566 @return 1567 integer value for the string. 1568 */ 1569 PUInt64 AsUnsigned64( 1570 unsigned base = 10 ///< Number base to convert the string in. 1571 ) const; 1572 1573 /**Convert the string to a floating point number. This number may be in 1574 decimal or exponential form. All characters up to the first illegal 1575 character for a floting point number are converted. 1576 1577 This function uses the standard C library <code>strtod()</code> 1578 function. 1579 1580 @return 1581 floating point value for the string. 1582 */ 1583 double AsReal() const; 1584 1585 /**Convert UTF-8 string to UCS-2. 1586 Note the resultant PWCharArray will have the trailing null included. 1587 */ 1588 PWCharArray AsUCS2() const; 1589 1590 /**Convert a standard null terminated string to a "pascal" style string. 1591 This consists of a songle byte for the length of the string and then 1592 the string characters following it. 1593 1594 This function will assert if the string is greater than 255 characters 1595 in length. 1596 1597 @return 1598 byte array containing the "pascal" style string. 1599 */ 1600 PBYTEArray ToPascal() const; 1601 1602 /**Convert the string to C literal string format. This will convert non 1603 printable characters to the \\nnn form or for standard control characters 1604 such as line feed, to \\n form. Any '"' characters are also escaped with 1605 a \\ character and the entire string is enclosed in '"' characters. 1606 1607 @return 1608 string converted to a C language literal form. 1609 */ 1610 PString ToLiteral() const; 1611 1612 /**Get the internal buffer as a pointer to unsigned characters. The 1613 standard "operator const char *" function is provided by the 1614 <code>PCharArray</code> ancestor class. 1615 1616 @return 1617 pointer to character buffer. 1618 */ 1619 operator const unsigned char *() const; 1620 1621 /** Cast the PString to a std::string 1622 */ string()1623 operator std::string () const 1624 { return std::string(theArray); } 1625 //@} 1626 1627 1628 protected: 1629 void InternalFromUCS2( 1630 const wchar_t * ptr, 1631 PINDEX len 1632 ); 1633 virtual Comparison InternalCompare( 1634 PINDEX offset, // Offset into string to compare. 1635 char c // Character to compare against. 1636 ) const; 1637 virtual Comparison InternalCompare( 1638 PINDEX offset, // Offset into string to compare. 1639 PINDEX length, // Number of characters to compare. 1640 const char * cstr // C string to compare against. 1641 ) const; 1642 1643 /* Internal function to compare the current string value against the 1644 specified C string. 1645 1646 @return 1647 relative rank of the two strings. 1648 */ 1649 PString(int dummy, const PString * str); 1650 PString(PContainerReference & reference)1651 PString(PContainerReference & reference) : PCharArray(reference) { } 1652 }; 1653 1654 1655 inline ostream & operator<<(ostream & stream, const PString & string) 1656 { 1657 string.PrintOn(stream); 1658 return stream; 1659 } 1660 1661 1662 inline wostream & operator<<(wostream & stream, const PString & string) 1663 { 1664 return stream << (const char *)string; 1665 } 1666 1667 1668 #ifdef _WIN32 1669 class PWideString : public PWCharArray { 1670 PCLASSINFO(PWideString, PWCharArray); 1671 1672 public: 1673 typedef const wchar_t * Initialiser; 1674 PWideString()1675 PWideString() { } PWideString(const PWCharArray & arr)1676 PWideString(const PWCharArray & arr) : PWCharArray(arr) { } PWideString(const PString & str)1677 PWideString(const PString & str) : PWCharArray(str.AsUCS2()) { } PWideString(const char * str)1678 PWideString(const char * str) : PWCharArray(PString(str).AsUCS2()) { } 1679 PWideString & operator=(const PWideString & str) { PWCharArray::operator=(str); return *this; } 1680 PWideString & operator=(const PString & str) { PWCharArray::operator=(str.AsUCS2()); return *this; } 1681 PWideString & operator=(const std::string & str) { PWCharArray::operator=(PString(str.c_str()).AsUCS2()); return *this; } 1682 PWideString & operator=(const char * str) { PWCharArray::operator=(PString(str).AsUCS2()); return *this; } 1683 friend inline ostream & operator<<(ostream & stream, const PWideString & string) { return stream << PString(string); } 1684 1685 protected: PWideString(PContainerReference & reference)1686 PWideString(PContainerReference & reference) : PWCharArray(reference) { } 1687 }; 1688 1689 #ifdef UNICODE 1690 typedef PWideString PVarString; 1691 #else 1692 typedef PString PVarString; 1693 #endif 1694 #endif 1695 1696 1697 ////////////////////////////////////////////////////////////////////////////// 1698 1699 /**This class is a variation of a string that ignores case. Thus in all 1700 standard comparison (==, < etc) and search 1701 (<code>Find()</code> etc) functions the case of the characters and strings is 1702 ignored. 1703 1704 The characters in the string still maintain their case. Only the comparison 1705 operations are affected. So printing etc will still display the string as 1706 entered. 1707 */ 1708 class PCaselessString : public PString 1709 { 1710 PCLASSINFO(PCaselessString, PString); 1711 1712 public: 1713 /**Create a new, empty, caseless string. 1714 */ 1715 PCaselessString(); 1716 1717 /**Create a new caseless string, initialising it to the characters in the 1718 C string provided. 1719 */ 1720 PCaselessString( 1721 const char * cstr ///< C string to initialise the caseless string from. 1722 ); 1723 1724 /**Create a caseless string, with a reference to the characters in the 1725 normal <code>PString</code> provided. A PCaselessString may also be provided 1726 to this constructor. 1727 */ 1728 PCaselessString( 1729 const PString & str ///< String to initialise the caseless string from. 1730 ); 1731 1732 1733 /**Create a caseless string from a std::string 1734 */ PCaselessString(const std::string & str)1735 PCaselessString( 1736 const std::string & str ///< String to initialise the caseless string from. 1737 ) : PString(str) 1738 { } 1739 1740 /**Assign the string to the current object. The current instance then 1741 becomes another reference to the same string in the <code>str</code> 1742 parameter. 1743 1744 @return 1745 reference to the current PString object. 1746 */ 1747 PCaselessString & operator=( 1748 const PString & str ///< New string to assign. 1749 ); 1750 1751 /**Assign the string to the current object. The current instance then 1752 becomes another reference to the same string in the <code>str</code> 1753 parameter. 1754 1755 @return 1756 reference to the current PString object. 1757 */ 1758 PCaselessString & operator=( 1759 const std::string & str ///< New string to assign. 1760 ) { return operator=(str.c_str()); } 1761 1762 /**Assign the C string to the current object. The current instance then 1763 becomes a unique reference to a copy of the <code>cstr</code> parameter. 1764 The <code>cstr</code> parameter is typically a literal string, eg: 1765 <pre><code> 1766 myStr = "fred"; 1767 </code></pre> 1768 @return 1769 reference to the current PString object. 1770 */ 1771 PCaselessString & operator=( 1772 const char * cstr ///< C string to assign. 1773 ); 1774 1775 /**Assign the character to the current object. The current instance then 1776 becomes a unique reference to a copy of the character parameter. eg: 1777 <pre><code> 1778 myStr = 'A'; 1779 </code></pre> 1780 @return 1781 reference to the current PString object. 1782 */ 1783 PCaselessString & operator=( 1784 char ch ///< Character to assign. 1785 ); 1786 1787 1788 // Overrides from class PObject 1789 /**Make a complete duplicate of the string. Note that the data in the 1790 array of characters is duplicated as well and the new object is a 1791 unique reference to that data. 1792 */ 1793 virtual PObject * Clone() const; 1794 1795 protected: 1796 // Overrides from class PString 1797 virtual Comparison InternalCompare( 1798 PINDEX offset, // Offset into string to compare. 1799 char c // Character to compare against. 1800 ) const; 1801 virtual Comparison InternalCompare( 1802 PINDEX offset, // Offset into string to compare. 1803 PINDEX length, // Number of characters to compare. 1804 const char * cstr // C string to compare against. 1805 ) const; 1806 /* Internal function to compare the current string value against the 1807 specified C string. 1808 1809 @return 1810 relative rank of the two strings or characters. 1811 */ 1812 1813 PCaselessString(int dummy, const PCaselessString * str); PCaselessString(PContainerReference & reference)1814 PCaselessString(PContainerReference & reference) : PString(reference) { } 1815 }; 1816 1817 1818 ////////////////////////////////////////////////////////////////////////////// 1819 1820 /**Create a constant string. 1821 This is used to create a PString wrapper around a constant char string. Thus 1822 internal memory allocations are avoided as it does not change. The resultant 1823 object can be used in almost every way that a PString does, except being 1824 able modify it. However, copying to another PString instance and then 1825 making modifications is OK. 1826 1827 It is particularly useful in static string declarations, e.g. 1828 <CODE> 1829 static const PConstantString<PString> str("A test string"); 1830 </CODE> 1831 and is completely thread safe in it's construction. 1832 */ 1833 template <class ParentString> 1834 class PConstantString : public ParentString 1835 { 1836 private: 1837 PContainerReference m_staticReference; 1838 public: PConstantString(typename ParentString::Initialiser init)1839 PConstantString(typename ParentString::Initialiser init) 1840 : ParentString(m_staticReference) 1841 , m_staticReference((PINDEX)strlen(init)+1, true) 1842 { 1843 this->theArray = (char *)init; 1844 } ~PConstantString()1845 ~PConstantString() { this->Destruct(); } 1846 SetSize(PINDEX)1847 virtual PBoolean SetSize(PINDEX) { return false; } AssignContents(const PContainer &)1848 virtual void AssignContents(const PContainer &) { PAssertAlways(PInvalidParameter); } DestroyReference()1849 virtual void DestroyReference() { } 1850 1851 private: PConstantString(const PConstantString &)1852 PConstantString(const PConstantString &) 1853 : ParentString(m_staticReference) 1854 , m_staticReference(0, true) 1855 { } 1856 void operator=(const PConstantString &) { } 1857 }; 1858 1859 1860 /// Constant PString type. See PConstantString. 1861 typedef PConstantString<PString> PConstString; 1862 1863 /// Constant PCaselessString type. See PConstantString. 1864 typedef PConstantString<PCaselessString> PConstCaselessString; 1865 1866 1867 1868 ////////////////////////////////////////////////////////////////////////////// 1869 1870 class PStringStream; 1871 1872 /**This class is a standard C++ stream class descendent for reading or writing 1873 streamed data to or from a <code>PString</code> class. 1874 1875 All of the standard stream I/O operators, manipulators etc will operate on 1876 the PStringStream class. 1877 */ 1878 class PStringStream : public PString, public iostream 1879 { 1880 PCLASSINFO(PStringStream, PString); 1881 1882 public: 1883 /**Create a new, empty, string stream. Data may be output to this stream, 1884 but attempts to input from it will return end of file. 1885 1886 The internal string is continually grown as required during output. 1887 */ 1888 PStringStream(); 1889 1890 /**Create a new, empty, string stream of a fixed size. Data may be output 1891 to this stream, but attempts to input from it will return end of file. 1892 When the fixed size is reached then no more data may be output to it. 1893 */ 1894 PStringStream( 1895 PINDEX fixedBufferSize 1896 ); 1897 1898 /**Create a new string stream and initialise it to the provided value. The 1899 string stream references the same string buffer as the <code>str</code> 1900 parameter until any output to the string stream is attempted. The 1901 reference is then broken and the instance of the string stream becomes 1902 a unique reference to a string buffer. 1903 */ 1904 PStringStream( 1905 const PString & str ///< Initial value for string stream. 1906 ); 1907 1908 /**Create a new string stream and initialise it with the provided value. 1909 The stream may be read or written from. Writes will append to the end of 1910 the string. 1911 */ 1912 PStringStream( 1913 const char * cstr ///< Initial value for the string stream. 1914 ); 1915 1916 /**Make the current string empty 1917 */ 1918 virtual PString & MakeEmpty(); 1919 1920 /**Assign the string part of the stream to the current object. The current 1921 instance then becomes another reference to the same string in the <code>strm</code> 1922 parameter. 1923 1924 This will reset the read pointer for input to the beginning of the 1925 string. Also, any data output to the string up until the asasignement 1926 will be lost. 1927 1928 @return 1929 reference to the current PStringStream object. 1930 */ 1931 PStringStream & operator=( 1932 const PStringStream & strm 1933 ); 1934 1935 /**Assign the string to the current object. The current instance then 1936 becomes another reference to the same string in the <code>str</code> 1937 parameter. 1938 1939 This will reset the read pointer for input to the beginning of the 1940 string. Also, any data output to the string up until the asasignement 1941 will be lost. 1942 1943 @return 1944 reference to the current PStringStream object. 1945 */ 1946 PStringStream & operator=( 1947 const PString & str ///< New string to assign. 1948 ); 1949 1950 /**Assign the C string to the string stream. The current instance then 1951 becomes a unique reference to a copy of the <code>cstr</code> 1952 parameter. The <code>cstr</code> parameter is typically a literal 1953 string, eg: 1954 <pre><code> 1955 myStr = "fred"; 1956 </code></pre> 1957 1958 This will reset the read pointer for input to the beginning of the 1959 string. Also, any data output to the string up until the asasignement 1960 will be lost. 1961 1962 @return 1963 reference to the current PStringStream object. 1964 */ 1965 PStringStream & operator=( 1966 const char * cstr ///< C string to assign. 1967 ); 1968 1969 /**Assign the character to the current object. The current instance then 1970 becomes a unique reference to a copy of the character parameter. eg: 1971 <pre><code> 1972 myStr = 'A'; 1973 </code></pre> 1974 @return 1975 reference to the current PString object. 1976 */ 1977 PStringStream & operator=( 1978 char ch ///< Character to assign. 1979 ); 1980 1981 1982 /// Destroy the string stream, deleting the stream buffer 1983 virtual ~PStringStream(); 1984 1985 1986 protected: 1987 virtual void AssignContents(const PContainer & cont); 1988 1989 private: PStringStream(int,const PStringStream &)1990 PStringStream(int, const PStringStream &) : iostream(cout.rdbuf()) { } 1991 1992 class Buffer : public streambuf { 1993 public: 1994 Buffer(PStringStream & str, PINDEX size); 1995 Buffer(const Buffer & sbuf); 1996 Buffer & operator=(const Buffer & sbuf); 1997 virtual int_type overflow(int_type = EOF); 1998 virtual int_type underflow(); 1999 virtual int sync(); 2000 virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out); 2001 virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out); 2002 PStringStream & string; 2003 PBoolean fixedBufferSize; 2004 }; 2005 }; 2006 2007 2008 class PStringList; 2009 class PSortedStringList; 2010 2011 /**This is an array collection class of <code>PString</code> objects. It has all the 2012 usual functions for a collection, with the object types set to 2013 <code>PString</code> pointers. 2014 2015 In addition some addition functions are added that take a const 2016 <code>PString</code> reference instead of a pointer as most standard collection 2017 functions do. This is more convenient for when string expressions are used 2018 as parameters to function in the collection. 2019 2020 See the <code>PAbstractArray</code> and <code>PArray</code> classes and 2021 <code>PDECLARE_ARRAY</code> macro for more information. 2022 */ 2023 #ifdef DOC_PLUS_PLUS 2024 class PStringArray : public PArray { 2025 #endif 2026 PDECLARE_ARRAY(PStringArray, PString); 2027 public: 2028 /**@name Construction */ 2029 //@{ 2030 /**Create a PStringArray from the array of C strings. If count is 2031 P_MAX_INDEX then strarr is assumed to point to an array of strings 2032 where the last pointer is NULL. 2033 */ 2034 PStringArray( 2035 PINDEX count, ///< Count of strings in array 2036 char const * const * strarr, ///< Array of C strings 2037 PBoolean caseless = false ///< New strings are to be PCaselessStrings 2038 ); 2039 /**Create a PStringArray of length one from the single string. 2040 */ 2041 PStringArray( 2042 const PString & str ///< Single string to convert to an array of one. 2043 ); 2044 /**Create a PStringArray from the list of strings. 2045 */ 2046 PStringArray( 2047 const PStringList & list ///< List of strings to convert to array. 2048 ); 2049 /**Create a PStringArray from the sorted list strings. 2050 */ 2051 PStringArray( 2052 const PSortedStringList & list ///< List of strings to convert to array. 2053 ); 2054 2055 /** 2056 * Create a PStringArray from a vector of PStrings 2057 */ PStringArray(const std::vector<PString> & vec)2058 PStringArray( 2059 const std::vector<PString> & vec 2060 ) 2061 { 2062 for (std::vector<PString>::const_iterator r = vec.begin(); r != vec.end(); ++r) 2063 AppendString(*r); 2064 } 2065 2066 /** 2067 * Create a PStringArray from a vector of std::string 2068 */ PStringArray(const std::vector<std::string> & vec)2069 PStringArray( 2070 const std::vector<std::string> & vec 2071 ) 2072 { 2073 for (std::vector<std::string>::const_iterator r = vec.begin(); r != vec.end(); ++r) 2074 AppendString(PString(*r)); 2075 } 2076 2077 /** 2078 * Create a PStringArray from an STL container 2079 */ 2080 template <typename stlContainer> container(const stlContainer & vec)2081 static PStringArray container( 2082 const stlContainer & vec 2083 ) 2084 { 2085 PStringArray list; 2086 for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r) 2087 list.AppendString(PString(*r)); 2088 return list; 2089 } 2090 2091 //@} 2092 2093 /**@name Overrides from class PObject */ 2094 //@{ 2095 /** Input the contents of the object from the stream. This is 2096 primarily used by the standard <code>operator>></code> function. 2097 2098 The default behaviour reads '\\n' separated strings until 2099 <code>!strm.good()</code>. 2100 */ 2101 virtual void ReadFrom( 2102 istream &strm // Stream to read the objects contents from. 2103 ); 2104 //@} 2105 2106 /**@name New functions for class */ 2107 //@{ 2108 /**As for <code>GetValuesIndex()</code> but takes a PString argument so that 2109 literals will be automatically converted. 2110 2111 @return 2112 Index of string in array or P_MAX_INDEX if not found. 2113 */ 2114 PINDEX GetStringsIndex( 2115 const PString & str ///< String to search for index of 2116 ) const; 2117 2118 PString operator[]( 2119 PINDEX index ///< Index position in the collection of the object. 2120 ) const; 2121 2122 /**Retrieve a reference to the object in the array. If there was not an 2123 object at that ordinal position or the index was beyond the size of the 2124 array then the function will create a new one. 2125 2126 @return 2127 reference to the object at <code>index</code> position. 2128 */ 2129 PString & operator[]( 2130 PINDEX index ///< Index position in the collection of the object. 2131 ); 2132 2133 /** Append a string to the array 2134 */ 2135 PINDEX AppendString( 2136 const PString & str ///< String to append. 2137 ); 2138 2139 /**Concatenate a PString or PStringArray to the array 2140 2141 @return 2142 The PStringArray with the new items appended 2143 */ 2144 PStringArray & operator +=(const PStringArray & array); 2145 PStringArray & operator +=(const PString & str); 2146 2147 2148 /**Create a new PStringArray, and add PString or PStringArray to it 2149 a new PStringArray 2150 2151 @return 2152 A new PStringArray with the additional elements(s) 2153 */ 2154 PStringArray operator + (const PStringArray & array); 2155 PStringArray operator + (const PString & str); 2156 2157 /**Create an array of C strings. 2158 If storage is NULL then this returns a single pointer that must be 2159 disposed of using free(). Note that each of the strings are part of the 2160 same memory allocation so only one free() is required. 2161 2162 If storage is not null then that is used to allocate the memory. 2163 */ 2164 char ** ToCharArray( 2165 PCharArray * storage = NULL 2166 ) const; 2167 //@} 2168 }; 2169 2170 2171 /**This is a list collection class of <code>PString</code> objects. It has all the 2172 usual functions for a collection, with the object types set to 2173 <code>PString</code> pointers. 2174 2175 In addition some addition functions are added that take a const 2176 <code>PString</code> reference instead of a pointer as most standard collection 2177 functions do. This is more convenient for when string expressions are used 2178 as parameters to function in the collection. 2179 2180 See the <code>PAbstractList</code> and <code>PList</code> classes and 2181 <code>PDECLARE_LIST</code> macro for more information. 2182 */ 2183 #ifdef DOC_PLUS_PLUS 2184 class PStringList : public PList { 2185 #endif 2186 PDECLARE_LIST(PStringList, PString); 2187 public: 2188 /**@name Construction */ 2189 //@{ 2190 /**Create a PStringList from the array of C strings. 2191 */ 2192 PStringList( 2193 PINDEX count, ///< Count of strings in array 2194 char const * const * strarr, ///< Array of C strings 2195 PBoolean caseless = false ///< New strings are to be PCaselessStrings 2196 ); 2197 /**Create a PStringList of length one from the single string. 2198 */ 2199 PStringList( 2200 const PString & str ///< Single string to convert to a list of one. 2201 ); 2202 /**Create a PStringList from the array of strings. 2203 */ 2204 PStringList( 2205 const PStringArray & array ///< Array of strings to convert to list 2206 ); 2207 /**Create a PStringList from the sorted list of strings. 2208 */ 2209 PStringList( 2210 const PSortedStringList & list ///< List of strings to convert to list. 2211 ); 2212 //@} 2213 2214 /**@name Overrides from class PObject */ 2215 //@{ 2216 /** Input the contents of the object from the stream. This is 2217 primarily used by the standard <code>operator>></code> function. 2218 2219 The default behaviour reads '\\n' separated strings until 2220 <code>!strm.good()</code>. 2221 */ 2222 virtual void ReadFrom( 2223 istream &strm // Stream to read the objects contents from. 2224 ); 2225 //@} 2226 2227 /**@name Operations */ 2228 //@{ 2229 /** Append a string to the list. 2230 */ 2231 PINDEX AppendString( 2232 const PString & str ///< String to append. 2233 ); 2234 2235 /** Insert a string into the list. 2236 */ 2237 PINDEX InsertString( 2238 const PString & before, ///< String to insert before. 2239 const PString & str ///< String to insert. 2240 ); 2241 2242 /** Get the index of the string with the specified value. 2243 A linear search of list is performed to find the string value. 2244 */ 2245 PINDEX GetStringsIndex( 2246 const PString & str ///< String value to search for. 2247 ) const; 2248 2249 /**Concatenate a PString or PStringArray to the list 2250 2251 @return 2252 The PStringArray with the new items appended 2253 */ 2254 PStringList & operator +=(const PStringList & list); 2255 PStringList & operator +=(const PString & str); 2256 2257 2258 /**Create a new PStringList, and add PString or PStringList to it 2259 a new PStringList 2260 2261 @return 2262 A new PStringList with the additional elements(s) 2263 */ 2264 PStringList operator + (const PStringList & array); 2265 PStringList operator + (const PString & str); 2266 2267 /** 2268 * Create a PStringArray from an STL container 2269 */ 2270 template <typename stlContainer> container(const stlContainer & vec)2271 static PStringList container( 2272 const stlContainer & vec 2273 ) 2274 { 2275 PStringList list; 2276 for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r) 2277 list.AppendString(PString(*r)); 2278 return list; 2279 } 2280 //@} 2281 }; 2282 2283 2284 /**This is a sorted list collection class of <code>PString</code> objects. It has all 2285 the usual functions for a collection, with the object types set to 2286 <code>PString</code> pointers. 2287 2288 In addition some addition functions are added that take a const 2289 <code>PString</code> reference instead of a pointer as most standard collection 2290 functions do. This is more convenient for when string expressions are used 2291 as parameters to function in the collection. 2292 2293 See the PAbstractSortedList and PSortedList classes for more information. 2294 */ 2295 #ifdef DOC_PLUS_PLUS 2296 class PSortedStringList : public PSortedList { 2297 #endif 2298 PDECLARE_SORTED_LIST(PSortedStringList, PString); 2299 public: 2300 /**@name Construction */ 2301 //@{ 2302 /**Create a PStringArray from the array of C strings. 2303 */ 2304 PSortedStringList( 2305 PINDEX count, ///< Count of strings in array 2306 char const * const * strarr, ///< Array of C strings 2307 PBoolean caseless = false ///< New strings are to be PCaselessStrings 2308 ); 2309 /**Create a PSortedStringList of length one from the single string. 2310 */ 2311 PSortedStringList( 2312 const PString & str ///< Single string to convert to a list of one. 2313 ); 2314 /**Create a PSortedStringList from the array of strings. 2315 */ 2316 PSortedStringList( 2317 const PStringArray & array ///< Array of strings to convert to list 2318 ); 2319 /**Create a PSortedStringList from the list of strings. 2320 */ 2321 PSortedStringList( 2322 const PStringList & list ///< List of strings to convert to list. 2323 ); 2324 //@} 2325 2326 /**@name Overrides from class PObject */ 2327 //@{ 2328 /** Input the contents of the object from the stream. This is 2329 primarily used by the standard <code>operator>></code> function. 2330 2331 The default behaviour reads '\\n' separated strings until 2332 <code>!strm.good()</code>. 2333 */ 2334 virtual void ReadFrom( 2335 istream &strm // Stream to read the objects contents from. 2336 ); 2337 //@} 2338 2339 /**@name Operations */ 2340 //@{ 2341 /** Add a string to the list. 2342 This will place the string in the correct position in the sorted list. 2343 */ 2344 PINDEX AppendString( 2345 const PString & str ///< String to append. 2346 ); 2347 2348 /** Get the index of the string with the specified value. 2349 A binary search of tree is performed to find the string value. 2350 */ 2351 PINDEX GetStringsIndex( 2352 const PString & str ///< String value to search for. 2353 ) const; 2354 2355 /** Get the index of the next string after specified value. 2356 A binary search of tree is performed to find the string greater than 2357 or equal to the specified string value. 2358 */ 2359 PINDEX GetNextStringsIndex( 2360 const PString & str ///< String value to search for. 2361 ) const; 2362 //@} 2363 2364 protected: 2365 PINDEX InternalStringSelect( 2366 const char * str, 2367 PINDEX len, 2368 Element * thisElement, 2369 Element * & lastElement 2370 ) const; 2371 }; 2372 2373 2374 /**This is a set collection class of <code>PString</code> objects. It has all the 2375 usual functions for a collection, with the object types set to 2376 <code>PString</code> pointers. 2377 2378 In addition some addition functions are added that take a const 2379 <code>PString</code> reference instead of a pointer as most standard collection 2380 functions do. This is more convenient for when string expressions are used 2381 as parameters to function in the collection. 2382 2383 Unlike the normal sets, this will delete the PStrings removed from it. This 2384 complements the automatic creation of new PString objects when literals or 2385 expressions are used. 2386 2387 See the <code>PAbstractSet</code> and <code>PSet</code> classes and <code>PDECLARE_SET</code> 2388 macro for more information. 2389 */ 2390 #ifdef DOC_PLUS_PLUS 2391 class PStringSet : public PSet { 2392 #endif 2393 PDECLARE_SET(PStringSet, PString, true); 2394 public: 2395 /**@name Construction */ 2396 //@{ 2397 /**Create a PStringArray from the array of C strings. 2398 */ 2399 PStringSet( 2400 PINDEX count, ///< Count of strings in array 2401 char const * const * strarr, ///< Array of C strings 2402 PBoolean caseless = false ///< New strings are to be PCaselessStrings 2403 ); 2404 /**Create a PStringSet containing the single string. 2405 */ 2406 PStringSet( 2407 const PString & str ///< Single string to convert to a set of one. 2408 ); 2409 /**Create a PStringSet containing the strings. 2410 */ 2411 PStringSet( 2412 const PStringArray & strArray ///< String to convert to a set. 2413 ); 2414 /**Create a PStringSet containing the strings. 2415 */ 2416 PStringSet( 2417 const PStringList & strList ///< String to convert to a set. 2418 ); 2419 //@} 2420 2421 /**@name Overrides from class PObject */ 2422 //@{ 2423 /** Input the contents of the object from the stream. This is 2424 primarily used by the standard <code>operator>></code> function. 2425 2426 The default behaviour reads '\\n' separated strings until 2427 <code>!strm.good()</code>. 2428 */ 2429 virtual void ReadFrom( 2430 istream &strm ///< Stream to read the objects contents from. 2431 ); 2432 //@} 2433 2434 /**@name Operations */ 2435 //@{ 2436 /** Include the spcified string value into the set. */ 2437 void Include( 2438 const PString & key ///< String value to add to set. 2439 ); 2440 /** Include the spcified string value into the set. */ 2441 PStringSet & operator+=( 2442 const PString & key ///< String value to add to set. 2443 ); 2444 /** Exclude the spcified string value from the set. */ 2445 void Exclude( 2446 const PString & key ///< String value to remove from set. 2447 ); 2448 /** Exclude the spcified string value from the set. */ 2449 PStringSet & operator-=( 2450 const PString & key ///< String value to remove from set. 2451 ); 2452 //@} 2453 }; 2454 2455 2456 /**This template class maps the PAbstractDictionary to a specific key type and 2457 a <code>PString</code> data type. The functions in this class primarily do all the 2458 appropriate casting of types. 2459 2460 Note that if templates are not used the <code>PDECLARE_STRING_DICTIONARY</code> 2461 macro will simulate the template instantiation. 2462 */ 2463 template <class K> class PStringDictionary : public PAbstractDictionary 2464 { 2465 PCLASSINFO(PStringDictionary, PAbstractDictionary); 2466 2467 public: 2468 /**@name Construction */ 2469 //@{ 2470 /**Create a new, empty, dictionary. 2471 2472 Note that by default, objects placed into the dictionary will be 2473 deleted when removed or when all references to the dictionary are 2474 destroyed. 2475 */ PStringDictionary()2476 PStringDictionary() 2477 : PAbstractDictionary() { } 2478 //@} 2479 2480 /**@name Overrides from class PObject */ 2481 //@{ 2482 /**Make a complete duplicate of the dictionary. Note that all objects in 2483 the array are also cloned, so this will make a complete copy of the 2484 dictionary. 2485 */ Clone()2486 virtual PObject * Clone() const 2487 { return PNEW PStringDictionary(0, this); } 2488 //@} 2489 2490 /**@name New functions for class */ 2491 //@{ 2492 /**Get the string contained in the dictionary at the <code>key</code> 2493 position. The hash table is used to locate the data quickly via the 2494 hash function provided by the key. 2495 2496 The last key/data pair is remembered by the class so that subseqent 2497 access is very fast. 2498 2499 This function asserts if there is no data at the key position. 2500 2501 @return 2502 reference to the object indexed by the key. 2503 */ 2504 const PString & operator[](const K & key) const 2505 { return (const PString &)GetRefAt(key); } 2506 2507 /**Get the string contained in the dictionary at the <code>key</code> 2508 position. The hash table is used to locate the data quickly via the 2509 hash function provided by the key. 2510 2511 The last key/data pair is remembered by the class so that subseqent 2512 access is very fast. 2513 2514 This function returns the <code>dflt</code> value if there is no data 2515 at the key position. 2516 2517 @return 2518 reference to the object indexed by the key. 2519 */ operator()2520 PString operator()(const K & key, const char * dflt = NULL) const 2521 { 2522 PString * str = this->GetAt(key); 2523 return str != NULL ? *str : PString(dflt); 2524 } 2525 2526 /**Determine if the value of the object is contained in the hash table. The 2527 object values are compared, not the pointers. So the objects in the 2528 collection must correctly implement the <code>PObject::Compare()</code> 2529 function. The hash table is used to locate the entry. 2530 2531 @return 2532 true if the object value is in the dictionary. 2533 */ Contains(const K & key)2534 PBoolean Contains( 2535 const K & key // Key to look for in the dictionary. 2536 ) const { return AbstractContains(key); } 2537 2538 /**Remove an object at the specified key. The returned pointer is then 2539 removed using the <code>SetAt()</code> function to set that key value to 2540 NULL. If the <code>AllowDeleteObjects</code> option is set then the 2541 object is also deleted. 2542 2543 @return 2544 pointer to the object being removed, or NULL if the key was not 2545 present in the dictionary. If the dictionary is set to delete objects 2546 upon removal, the value -1 is returned if the key existed prior to removal 2547 rather than returning an illegal pointer 2548 */ RemoveAt(const K & key)2549 virtual PString * RemoveAt( 2550 const K & key // Key for position in dictionary to get object. 2551 ) { 2552 PString * s = GetAt(key); AbstractSetAt(key, NULL); 2553 return reference->deleteObjects ? (s ? (PString *)-1 : NULL) : s; 2554 } 2555 2556 /**Get the object at the specified key position. If the key was not in the 2557 collection then NULL is returned. 2558 2559 @return 2560 pointer to object at the specified key. 2561 */ GetAt(const K & key)2562 virtual PString * GetAt( 2563 const K & key // Key for position in dictionary to get object. 2564 ) const { return (PString *)AbstractGetAt(key); } 2565 2566 /**Set the data at the specified ordinal index position in the dictionary. 2567 2568 The ordinal position in the dictionary is determined by the hash values 2569 of the keys and the order of insertion. 2570 2571 @return 2572 true if the new object could be placed into the dictionary. 2573 */ SetDataAt(PINDEX index,const PString & str)2574 virtual PBoolean SetDataAt( 2575 PINDEX index, // Ordinal index in the dictionary. 2576 const PString & str // New string value to put into the dictionary. 2577 ) { return PAbstractDictionary::SetDataAt(index, PNEW PString(str)); } 2578 2579 /**Add a new object to the collection. If the objects value is already in 2580 the dictionary then the object is overrides the previous value. If the 2581 AllowDeleteObjects option is set then the old object is also deleted. 2582 2583 The object is placed in the an ordinal position dependent on the keys 2584 hash function. Subsequent searches use the has function to speed access 2585 to the data item. 2586 2587 @return 2588 true if the object was successfully added. 2589 */ SetAt(const K & key,const PString & str)2590 virtual PBoolean SetAt( 2591 const K & key, // Key for position in dictionary to add object. 2592 const PString & str // New string value to put into the dictionary. 2593 ) { return AbstractSetAt(key, PNEW PString(str)); } 2594 2595 /**Get the key in the dictionary at the ordinal index position. 2596 2597 The ordinal position in the dictionary is determined by the hash values 2598 of the keys and the order of insertion. 2599 2600 The last key/data pair is remembered by the class so that subseqent 2601 access is very fast. 2602 2603 @return 2604 reference to key at the index position. 2605 */ GetKeyAt(PINDEX index)2606 const K & GetKeyAt(PINDEX index) const 2607 { return (const K &)AbstractGetKeyAt(index); } 2608 2609 /**Get the data in the dictionary at the ordinal index position. 2610 2611 The ordinal position in the dictionary is determined by the hash values 2612 of the keys and the order of insertion. 2613 2614 The last key/data pair is remembered by the class so that subseqent 2615 access is very fast. 2616 2617 @return 2618 reference to data at the index position. 2619 */ GetDataAt(PINDEX index)2620 PString & GetDataAt(PINDEX index) const 2621 { return (PString &)AbstractGetDataAt(index); } 2622 //@} 2623 2624 protected: PStringDictionary(int dummy,const PStringDictionary * c)2625 PStringDictionary(int dummy, const PStringDictionary * c) 2626 : PAbstractDictionary(dummy, c) { } 2627 }; 2628 2629 2630 /**Begin declaration of a dictionary of strings class. 2631 This macro is used to declare a descendent of PAbstractList class, 2632 customised for a particular key type <b>K</b> and data object type 2633 <code>PString</code>. 2634 2635 If the compilation is using templates then this macro produces a descendent 2636 of the <code>PStringDictionary</code> template class. If templates are not being 2637 used then the macro defines a set of inline functions to do all casting of 2638 types. The resultant classes have an identical set of functions in either 2639 case. 2640 2641 See the <code>PStringDictionary</code> and <code>PAbstractDictionary</code> classes for 2642 more information. 2643 */ 2644 #define PDECLARE_STRING_DICTIONARY(cls, K) \ 2645 PDECLARE_CLASS(cls, PStringDictionary<K>) \ 2646 protected: \ 2647 cls(int dummy, const cls * c) \ 2648 : PStringDictionary<K>(dummy, c) { } \ 2649 public: \ 2650 cls() \ 2651 : PStringDictionary<K>() { } \ 2652 virtual PObject * Clone() const \ 2653 { return PNEW cls(0, this); } \ 2654 2655 2656 /**Declare a dictionary of strings class. 2657 This macro is used to declare a descendent of PAbstractDictionary class, 2658 customised for a particular key type <b>K</b> and data object type 2659 <code>PString</code>. This macro closes the class declaration off so no additional 2660 members can be added. 2661 2662 If the compilation is using templates then this macro produces a typedef 2663 of the <code>PStringDictionary</code> template class. 2664 2665 See the <code>PStringDictionary</code> class and <code>PDECLARE_STRING_DICTIONARY</code> 2666 macro for more information. 2667 */ 2668 #define PSTRING_DICTIONARY(cls, K) typedef PStringDictionary<K> cls 2669 2670 2671 /**This is a dictionary collection class of <code>PString</code> objects, keyed by an 2672 ordinal value. It has all the usual functions for a collection, with the 2673 object types set to <code>PString</code> pointers. The class could be considered 2674 like a sparse array of strings. 2675 2676 In addition some addition functions are added that take a const 2677 <code>PString</code> reference instead of a pointer as most standard collection 2678 functions do. This is more convenient for when string expressions are used 2679 as parameters to function in the collection. 2680 2681 See the <code>PAbstractDictionary</code> and <code>PStringDictionary</code> classes and 2682 <code>PDECLARE_DICTIONARY</code> and <code>PDECLARE_STRING_DICTIONARY</code> macros for 2683 more information. 2684 */ 2685 #ifdef DOC_PLUS_PLUS 2686 class POrdinalToString : public PStringDictionary { 2687 #endif 2688 PDECLARE_STRING_DICTIONARY(POrdinalToString, POrdinalKey); 2689 public: 2690 /**@name Construction */ 2691 //@{ 2692 /// Structure for static array initialiser for class. 2693 struct Initialiser { 2694 /// Ordinal key for string. 2695 PINDEX key; 2696 /// String value for ordinal. 2697 const char * value; 2698 }; 2699 /** Initialise the ordinal dictionary of strings from the static array. 2700 */ 2701 POrdinalToString( 2702 PINDEX count, ///< Count of strings in initialiser array 2703 const Initialiser * init ///< Array of Initialiser structures 2704 ); 2705 //@} 2706 2707 /**@name Overrides from class PObject */ 2708 //@{ 2709 /** Input the contents of the object from the stream. This is 2710 primarily used by the standard <code>operator>></code> function. 2711 2712 The default behaviour reads '\\n' separated strings until 2713 <code>!strm.good()</code>. 2714 */ 2715 virtual void ReadFrom( 2716 istream &strm // Stream to read the objects contents from. 2717 ); 2718 //@} 2719 }; 2720 2721 /**This is a dictionary collection class of ordinals keyed by 2722 <code>PString</code> objects. It has all the usual functions for a collection, 2723 with the object types set to <code>POrdinalKey</code> pointers. 2724 2725 In addition some addition functions are added that take a const 2726 <code>POrdinalKey</code> reference or a simple <code>PINDEX</code> instead of a pointer 2727 as most standard collection functions do. This is more convenient for when 2728 integer expressions are used as parameters to function in the collection. 2729 2730 See the PAbstractDicionary and POrdinalDictionary classes for more information. 2731 */ 2732 #ifdef DOC_PLUS_PLUS 2733 class PStringToOrdinal : public POrdinalDictionary { 2734 #endif 2735 PDECLARE_ORDINAL_DICTIONARY(PStringToOrdinal, PString); 2736 public: 2737 /**@name Construction */ 2738 //@{ 2739 /// Structure for static array initialiser for class. 2740 struct Initialiser { 2741 /// String key for ordinal. 2742 const char * key; 2743 /// Ordinal value for string. 2744 PINDEX value; 2745 }; 2746 /** Initialise the string dictionary of ordinals from the static array. 2747 */ 2748 PStringToOrdinal( 2749 PINDEX count, ///< Count of strings in initialiser array 2750 const Initialiser * init, ///< Array of Initialiser structures 2751 PBoolean caseless = false ///< New keys are to be PCaselessStrings 2752 ); 2753 //@} 2754 2755 /**@name Overrides from class PObject */ 2756 //@{ 2757 /** Input the contents of the object from the stream. This is 2758 primarily used by the standard <code>operator>></code> function. 2759 2760 The default behaviour reads '\\n' separated strings until 2761 <code>!strm.good()</code>. 2762 */ 2763 virtual void ReadFrom( 2764 istream &strm // Stream to read the objects contents from. 2765 ); 2766 //@} 2767 }; 2768 2769 2770 /**This is a dictionary collection class of <code>PString</code> objects, keyed by 2771 another string. It has all the usual functions for a collection, with the 2772 object types set to <code>PString</code> pointers. 2773 2774 In addition some addition functions are added that take a const 2775 <code>PString</code> reference instead of a pointer as most standard collection 2776 functions do. This is more convenient for when string expressions are used 2777 as parameters to function in the collection. 2778 2779 See the <code>PAbstractDictionary</code> and <code>PStringDictionary</code> classes and 2780 <code>PDECLARE_DICTIONARY</code> and <code>PDECLARE_STRING_DICTIONARY</code> macros for 2781 more information. 2782 */ 2783 #ifdef DOC_PLUS_PLUS 2784 class PStringToString : public PStringDictionary { 2785 #endif 2786 PDECLARE_STRING_DICTIONARY(PStringToString, PString); 2787 public: 2788 /**@name Construction */ 2789 //@{ 2790 /// Structure for static array initialiser for class. 2791 struct Initialiser { 2792 /// String key for string. 2793 const char * key; 2794 /// String value for string. 2795 const char * value; 2796 }; 2797 /** Initialise the string dictionary of strings from the static array. 2798 */ 2799 PStringToString( 2800 PINDEX count, ///< Count of strings in initialiser array 2801 const Initialiser * init, ///< Array of Initialiser structures 2802 PBoolean caselessKeys = false, ///< New keys are to be PCaselessStrings 2803 PBoolean caselessValues = false ///< New values are to be PCaselessStrings 2804 ); 2805 //@} 2806 2807 /**@name Overrides from class PObject */ 2808 //@{ 2809 /** Input the contents of the object from the stream. This is 2810 primarily used by the standard <code>operator>></code> function. 2811 2812 The default behaviour reads '\\n' separated strings until 2813 <code>!strm.good()</code>. 2814 */ 2815 virtual void ReadFrom( 2816 istream &strm // Stream to read the objects contents from. 2817 ); 2818 //@} 2819 2820 /**Create an array of C strings. 2821 If withEqualSign is true then array is GetSize()+1 strings of the form 2822 key=value. If false then the array is GetSize()*2+1 strings where 2823 consecutive pointers are the key and option respecitively of each 2824 entry in the dictionary. 2825 2826 If storage is NULL then this returns a single pointer that must be 2827 disposed of using free(). Note that each of the strings are part of the 2828 same memory allocation so only one free() is required. 2829 2830 If storage is not null then that is used to allocate the memory. 2831 */ 2832 char ** ToCharArray( 2833 bool withEqualSign, 2834 PCharArray * storage = NULL 2835 ) const; 2836 }; 2837 2838 2839 /** Specialised version of PStringToString to contain a dictionary of 2840 options/attributes. 2841 2842 This assures that the keys are caseless and has some access functions for 2843 bool/int types for ease of access with default values. 2844 */ 2845 class PStringOptions : public PStringToString 2846 { 2847 public: PStringOptions()2848 PStringOptions() { } PStringOptions(const PStringToString & other)2849 PStringOptions(const PStringToString & other) : PStringToString(other) { } 2850 PStringOptions & operator=(const PStringToString & other) { PStringToString::operator=(other); return *this; } 2851 2852 /// Determine if the specified key is present. Contains(const char * key)2853 bool Contains(const char * key ) const { PConstCaselessString k(key); return PStringToString::Contains(k); } Contains(const PString & key)2854 bool Contains(const PString & key ) const { return PStringToString::Contains(PCaselessString(key)); } Contains(const PCaselessString & key)2855 bool Contains(const PCaselessString & key ) const { return PStringToString::Contains(key); } Contains(const PCaselessString & (* key)())2856 bool Contains(const PCaselessString & (*key)()) const { return PStringToString::Contains(key()); } 2857 2858 // Overide default PStringToString::SetAt() to make sure the key is caseless GetAt(const char * key)2859 PString * GetAt(const char * key ) const { PConstCaselessString k(key); return PStringToString::GetAt(k); } GetAt(const PString & key)2860 PString * GetAt(const PString & key ) const { return PStringToString::GetAt(PCaselessString(key)); } GetAt(const PCaselessString & key)2861 PString * GetAt(const PCaselessString & key ) const { return PStringToString::GetAt(key); } GetAt(const PCaselessString & (* key)())2862 PString * GetAt(const PCaselessString & (*key)()) const { return PStringToString::GetAt(key()); } 2863 2864 // Overide default PStringToString::SetAt() to make sure the key is caseless SetAt(const char * key,const PString & data)2865 PBoolean SetAt(const char * key, const PString & data) { PConstCaselessString k(key); return SetAt(k, data); } SetAt(const PString & key,const PString & data)2866 PBoolean SetAt(const PString & key, const PString & data) { return SetAt(PCaselessString(key), data); } SetAt(const PCaselessString & key,const PString & data)2867 PBoolean SetAt(const PCaselessString & key, const PString & data) { MakeUnique(); return PStringToString::SetAt(key, data); } SetAt(const PCaselessString & (* key)(),const PString & data)2868 PBoolean SetAt(const PCaselessString & (*key)(), const PString & data) { return SetAt(key(), data); } 2869 2870 // Overide default PStringToString::RemoveAt() to make sure the key is caseless RemoveAt(const char * key)2871 PString * RemoveAt(const char * key) { PConstCaselessString k(key); return RemoveAt(k); } RemoveAt(const PString & key)2872 PString * RemoveAt(const PString & key) { return RemoveAt(PCaselessString(key)); } RemoveAt(const PCaselessString & key)2873 PString * RemoveAt(const PCaselessString & key) { MakeUnique(); return PStringToString::RemoveAt(key); } RemoveAt(const PCaselessString & (* key)())2874 PString * RemoveAt(const PCaselessString & (*key)()) { return RemoveAt(key()); } 2875 2876 /// Get an option value. 2877 PString GetString(const char * key, const char * dflt = NULL) const { PConstCaselessString k(key); return GetString(k, dflt); } 2878 PString GetString(const PString & key, const char * dflt = NULL) const { return GetString(PCaselessString(key), dflt); } 2879 PString GetString(const PCaselessString & key, const char * dflt = NULL) const; 2880 PString GetString(const PCaselessString & (*key)(), const char * dflt = NULL) const { return GetString(key(), dflt); } 2881 2882 /// Set the option value. SetString(const char * key,const PString & value)2883 bool SetString(const char * key, const PString & value) { return SetAt(key, value); } SetString(const PString & key,const PString & value)2884 bool SetString(const PString & key, const PString & value) { return SetAt(key, value); } SetString(const PCaselessString & key,const PString & value)2885 bool SetString(const PCaselessString & key, const PString & value) { return SetAt(key, value); } SetString(const PCaselessString & (* key)(),const PString & value)2886 bool SetString(const PCaselessString & (*key)(), const PString & value) { return SetAt(key, value); } 2887 2888 /// Get the option value as a boolean. 2889 bool GetBoolean(const char * key, bool dflt = false) const { PConstCaselessString k(key); return GetBoolean(k, dflt); } 2890 bool GetBoolean(const PString & key, bool dflt = false) const { return GetBoolean(PCaselessString(key), dflt); } 2891 bool GetBoolean(const PCaselessString & key, bool dflt = false) const; 2892 bool GetBoolean(const PCaselessString & (*key)(), bool dflt = false) const { return GetBoolean(key(), dflt); } 2893 2894 /// Set the option value as a boolean. SetBoolean(const char * key,bool value)2895 void SetBoolean(const char * key, bool value) { PConstCaselessString k(key); SetBoolean(k, value); } SetBoolean(const PString & key,bool value)2896 void SetBoolean(const PString & key, bool value) { SetBoolean(PCaselessString(key), value); } SetBoolean(const PCaselessString & key,bool value)2897 void SetBoolean(const PCaselessString & key, bool value) { SetAt(key, value ? "true" : "false"); } SetBoolean(const PCaselessString & (* key)(),bool value)2898 void SetBoolean(const PCaselessString & (*key)(), bool value) { SetBoolean(key(), value); } 2899 2900 /// Get the option value as an integer. 2901 long GetInteger(const char * key, long dflt = 0) const { PConstCaselessString k(key); return GetInteger(k, dflt); } 2902 long GetInteger(const PString & key, long dflt = 0) const { return GetInteger(PCaselessString(key), dflt); } 2903 long GetInteger(const PCaselessString & key, long dflt = 0) const; 2904 long GetInteger(const PCaselessString & (*key)(), long dflt = 0) const { return GetInteger(key(), dflt); } 2905 2906 /// Set an integer value for the particular MIME info field. SetInteger(const char * key,long value)2907 void SetInteger(const char * key, long value) { PConstCaselessString k(key); SetInteger(k, value); } SetInteger(const PString & key,long value)2908 void SetInteger(const PString & key, long value) { SetInteger(PCaselessString(key), value); } 2909 void SetInteger(const PCaselessString & key, long value); SetInteger(const PCaselessString & (* key)(),long value)2910 void SetInteger(const PCaselessString & (*key)(), long value) { SetInteger(key(), value); } 2911 2912 /// Get the option value as a floating point real. 2913 double GetReal(const char * key, double dflt = 0) const { PConstCaselessString k(key); return GetReal(k, dflt); } 2914 double GetReal(const PString & key, double dflt = 0) const { return GetReal(PCaselessString(key), dflt); } 2915 double GetReal(const PCaselessString & key, double dflt = 0) const; 2916 double GetReal(const PCaselessString & (*key)(), double dflt = 0) const { return GetReal(key(), dflt); } 2917 2918 /// Set a floating point real value for the particular MIME info field. SetReal(const char * key,double value,int decimals)2919 void SetReal(const char * key, double value, int decimals) { PConstCaselessString k(key); SetReal(k, value, decimals); } SetReal(const PString & key,double value,int decimals)2920 void SetReal(const PString & key, double value, int decimals) { SetReal(PCaselessString(key), value, decimals); } 2921 void SetReal(const PCaselessString & key, double value, int decimals); SetReal(const PCaselessString & (* key)(),double value,int decimals)2922 void SetReal(const PCaselessString & (*key)(), double value, int decimals) { SetReal(key(), value, decimals); } 2923 2924 /// Determine of the option exists. Has(const char * key)2925 __inline bool Has(const char * key) const { return Contains(key); } Has(const PString & key)2926 __inline bool Has(const PString & key) const { return Contains(key); } Has(const PCaselessString & key)2927 __inline bool Has(const PCaselessString & key) const { return Contains(key); } Has(const PCaselessString & (* key)())2928 __inline bool Has(const PCaselessString & (*key)()) const { return Contains(key); } 2929 2930 /// Get the option value. 2931 __inline PString Get(const char * key, const char * dflt = NULL) const { return GetString(key, dflt); } 2932 __inline PString Get(const PString & key, const char * dflt = NULL) const { return GetString(key, dflt); } 2933 __inline PString Get(const PCaselessString & key, const char * dflt = NULL) const { return GetString(key, dflt); } 2934 __inline PString Get(const PCaselessString & (*key)(), const char * dflt = NULL) const { return GetString(key, dflt); } Get(const char * key,const PString & dflt)2935 __inline PString Get(const char * key, const PString & dflt) const { return GetString(key, dflt); } Get(const PString & key,const PString & dflt)2936 __inline PString Get(const PString & key, const PString & dflt) const { return GetString(key, dflt); } Get(const PCaselessString & key,const PString & dflt)2937 __inline PString Get(const PCaselessString & key, const PString & dflt) const { return GetString(key, dflt); } Get(const PCaselessString & (* key)(),const PString & dflt)2938 __inline PString Get(const PCaselessString & (*key)(), const PString & dflt) const { return GetString(key, dflt); } 2939 2940 /// Set the option value. Set(const char * key,const PString & value)2941 __inline bool Set(const char * key, const PString & value) { return SetAt(key, value); } Set(const PString & key,const PString & value)2942 __inline bool Set(const PString & key, const PString & value) { return SetAt(key, value); } Set(const PCaselessString & key,const PString & value)2943 __inline bool Set(const PCaselessString & key, const PString & value) { return SetAt(key, value); } Set(const PCaselessString & (* key)(),const PString & value)2944 __inline bool Set(const PCaselessString & (*key)(), const PString & value) { return SetAt(key, value); } 2945 2946 /// Remove option value Remove(const char * key)2947 __inline void Remove(const char * key) { RemoveAt(key); } Remove(const PString & key)2948 __inline void Remove(const PString & key) { RemoveAt(key); } Remove(const PCaselessString & key)2949 __inline void Remove(const PCaselessString & key) { RemoveAt(key); } Remove(const PCaselessString & (* key)())2950 __inline void Remove(const PCaselessString & (*key)()) { RemoveAt(key); } 2951 }; 2952 2953 2954 /**A class representing a regular expression that may be used for locating 2955 patterns in strings. The regular expression string is "compiled" into a 2956 form that is more efficient during the matching. This compiled form 2957 exists for the lifetime of the PRegularExpression instance. 2958 */ 2959 class PRegularExpression : public PObject 2960 { 2961 PCLASSINFO(PRegularExpression, PObject); 2962 2963 public: 2964 /**@name Constructors & destructors */ 2965 //@{ 2966 /// Flags for compiler options. 2967 enum { 2968 /// Use extended regular expressions 2969 Extended = 1, 2970 /// Ignore case in search. 2971 IgnoreCase = 2, 2972 /**If this bit is set, then anchors do not match at newline 2973 characters in the string. If not set, then anchors do match 2974 at newlines. 2975 */ 2976 AnchorNewLine = 4 2977 }; 2978 /// Flags for execution options. 2979 enum { 2980 /**If this bit is set, then the beginning-of-line operator doesn't match 2981 the beginning of the string (presumably because it's not the 2982 beginning of a line). 2983 If not set, then the beginning-of-line operator does match the 2984 beginning of the string. 2985 */ 2986 NotBeginningOfLine = 1, 2987 /**Like <code>NotBeginningOfLine</code>, except for the end-of-line. */ 2988 NotEndofLine = 2 2989 }; 2990 2991 /// Create a new, empty, regular expression 2992 PRegularExpression(); 2993 2994 /** Create and compile a new regular expression pattern. 2995 */ 2996 PRegularExpression( 2997 const PString & pattern, ///< Pattern to compile 2998 int flags = IgnoreCase ///< Pattern match options 2999 ); 3000 3001 /** Create and compile a new regular expression pattern. 3002 */ 3003 PRegularExpression( 3004 const char * cpattern, ///< Pattern to compile 3005 int flags = IgnoreCase ///< Pattern match options 3006 ); 3007 3008 /** 3009 * Copy a regular expression 3010 */ 3011 PRegularExpression( 3012 const PRegularExpression & 3013 ); 3014 3015 /** 3016 * Assign a regular expression 3017 */ 3018 PRegularExpression & operator =( 3019 const PRegularExpression & 3020 ); 3021 3022 /// Release storage for the compiled regular expression. 3023 ~PRegularExpression(); 3024 //@} 3025 3026 3027 /**@name Overrides from class PObject */ 3028 //@{ 3029 /**Output the regular expression to the specified stream. 3030 */ 3031 virtual void PrintOn( 3032 ostream & strm ///< I/O stream to output to. 3033 ) const; 3034 //@} 3035 3036 /**@name Status functions */ 3037 //@{ 3038 /// Error codes. 3039 enum ErrorCodes { 3040 /// Success. 3041 NoError = 0, 3042 /// Didn't find a match (for regexec). 3043 NoMatch, 3044 3045 // POSIX regcomp return error codes. (In the order listed in the standard.) 3046 /// Invalid pattern. 3047 BadPattern, 3048 /// Not implemented. 3049 CollateError, 3050 /// Invalid character class name. 3051 BadClassType, 3052 /// Trailing backslash. 3053 BadEscape, 3054 /// Invalid back reference. 3055 BadSubReg, 3056 /// Unmatched left bracket. 3057 UnmatchedBracket, 3058 /// Parenthesis imbalance. 3059 UnmatchedParen, 3060 /// Unmatched <b>\\</b>. 3061 UnmatchedBrace, 3062 /// Invalid contents of <b>\\</b>. 3063 BadBR, 3064 /// Invalid range end. 3065 RangeError, 3066 /// Ran out of memory. 3067 OutOfMemory, 3068 /// No preceding re for repetition op. 3069 BadRepitition, 3070 3071 /* Error codes we've added. */ 3072 /// Premature end. 3073 PrematureEnd, 3074 /// Compiled pattern bigger than 2^16 bytes. 3075 TooBig, 3076 /// Unmatched ) or \\); not returned from regcomp. 3077 UnmatchedRParen, 3078 /// Miscellaneous error 3079 NotCompiled 3080 }; 3081 3082 /**Get the error code for the last Compile() or Execute() operation. 3083 3084 @return 3085 Error code. 3086 */ 3087 ErrorCodes GetErrorCode() const; 3088 3089 /**Get the text description for the error of the last Compile() or 3090 Execute() operation. 3091 3092 @return 3093 Error text string. 3094 */ 3095 PString GetErrorText() const; 3096 3097 /** Return the string which represents the pattern matched by the regular expression. */ GetPattern()3098 const PString & GetPattern() const { return patternSaved; } 3099 //@} 3100 3101 /**@name Compile & Execute functions */ 3102 //@{ 3103 /** Compiler pattern. */ 3104 PBoolean Compile( 3105 const PString & pattern, ///< Pattern to compile 3106 int flags = IgnoreCase ///< Pattern match options 3107 ); 3108 /**Compiler pattern. 3109 The pattern is compiled into an internal format to speed subsequent 3110 execution of the pattern match algorithm. 3111 3112 @return 3113 true if successfully compiled. 3114 */ 3115 PBoolean Compile( 3116 const char * cpattern, ///< Pattern to compile 3117 int flags = IgnoreCase ///< Pattern match options 3118 ); 3119 3120 3121 /** Execute regular expression */ 3122 PBoolean Execute( 3123 const PString & str, ///< Source string to search 3124 PINDEX & start, ///< First match location 3125 int flags = 0 ///< Pattern match options 3126 ) const; 3127 /** Execute regular expression */ 3128 PBoolean Execute( 3129 const PString & str, ///< Source string to search 3130 PINDEX & start, ///< First match location 3131 PINDEX & len, ///< Length of match 3132 int flags = 0 ///< Pattern match options 3133 ) const; 3134 /** Execute regular expression */ 3135 PBoolean Execute( 3136 const char * cstr, ///< Source string to search 3137 PINDEX & start, ///< First match location 3138 int flags = 0 ///< Pattern match options 3139 ) const; 3140 /** Execute regular expression */ 3141 PBoolean Execute( 3142 const char * cstr, ///< Source string to search 3143 PINDEX & start, ///< First match location 3144 PINDEX & len, ///< Length of match 3145 int flags = 0 ///< Pattern match options 3146 ) const; 3147 /** Execute regular expression */ 3148 PBoolean Execute( 3149 const PString & str, ///< Source string to search 3150 PIntArray & starts, ///< Array of match locations 3151 int flags = 0 ///< Pattern match options 3152 ) const; 3153 /** Execute regular expression */ 3154 PBoolean Execute( 3155 const PString & str, ///< Source string to search 3156 PIntArray & starts, ///< Array of match locations 3157 PIntArray & ends, ///< Array of match ends 3158 int flags = 0 ///< Pattern match options 3159 ) const; 3160 /** Execute regular expression */ 3161 PBoolean Execute( 3162 const char * cstr, ///< Source string to search 3163 PIntArray & starts, ///< Array of match locations 3164 int flags = 0 ///< Pattern match options 3165 ) const; 3166 /**Execute regular expression. 3167 Execute the pattern match algorithm using the previously compiled 3168 pattern. 3169 3170 The \p starts and \p ends arrays indicate the start and end positions 3171 of the matched substrings in \p cstr. The 0th member is filled in to 3172 indicate what substring was matched by the entire regular expression. 3173 The remaining members report what substring was matched by parenthesized 3174 subexpressions within the regular expression. 3175 3176 The caller should set the size of the \p starts array before calling to 3177 indicated how many subexpressions are expected. If the \p starts array 3178 is empty, it will be set to one entry. 3179 3180 @return true if successfully compiled. 3181 */ 3182 PBoolean Execute( 3183 const char * cstr, ///< Source string to search 3184 PIntArray & starts, ///< Array of match locations 3185 PIntArray & ends, ///< Array of match ends 3186 int flags = 0 ///< Pattern match options 3187 ) const; 3188 /**Execute regular expression. 3189 Execute the pattern match algorithm using the previously compiled 3190 pattern. 3191 3192 The \p substring arrays is filled with the matched substrings out of 3193 \p cstr. The 0th member is filled in to indicate what substring was 3194 matched by the entire regular expression. The remaining members report 3195 what substring was matched by parenthesized subexpressions within the 3196 regular expression. 3197 3198 The caller should set the size of the \p substring array before calling 3199 to indicated how many subexpressions are expected. If the \p substring 3200 array is empty, it will be set to one entry. 3201 3202 @return true if successfully compiled. 3203 */ 3204 PBoolean Execute( 3205 const char * cstr, ///< Source string to search 3206 PStringArray & substring, ///< Array of matched substrings 3207 int flags = 0 ///< Pattern match options 3208 ) const; 3209 //@} 3210 3211 /**@name Miscellaneous functions */ 3212 //@{ 3213 /**Escape all characters in the <code>str</code> parameter that have a 3214 special meaning within a regular expression. 3215 3216 @return 3217 String with additional escape ('\\') characters. 3218 */ 3219 static PString EscapeString( 3220 const PString & str ///< String to add esacpes to. 3221 ); 3222 //@} 3223 3224 protected: 3225 PString patternSaved; 3226 int flagsSaved; 3227 3228 void * expression; 3229 mutable ErrorCodes lastError; 3230 }; 3231 3232 3233 #endif // PTLIB_STRING_H 3234 3235 3236 // End Of File /////////////////////////////////////////////////////////////// 3237