PTLib Version 2.12.9

- Main Page
- Related Pages
- Namespaces
- Classes
- Files

- File List
- File Members

All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
pstring.h
Go to the documentation of this file.
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: 31109 $
30 * $Author: ededu $
31 * $Date: 2013-12-18 22:26:43 +1100 (Wed, 18 Dec 2013) $
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
42 #include <string>
43 #include <vector>
44 #include <ptlib/array.h>
45
46
48 // PString class
49
50 class PStringArray;
51 class PRegularExpression;
52 class PString;
53
57 PString psprintf(
58 const char * fmt,
59 ...
60 );
61
65 PString pvsprintf(
66 const char * fmt,
67 va_list arg
68 );
69
70
71 #ifdef P_HAS_WCHAR
72
73 typedef PBaseArray<wchar_t> PWCharArray;
74 #endif
75
76
111 class PString : public PCharArray
112 {
113 PCLASSINFO(PString, PCharArray);
114 public:
115 typedef const char * Initialiser;
116
122 PString();
123
127 PString(
128 const PString & str
129 );
130
134 PString(
135 const PCharArray & buf
136 );
137
142 PString(
143 const PBYTEArray & buf
144 );
145
148 PString(
149 const std::string & str
150 );
151
160 PString(
161 const char * cstr
162 );
163
168 #ifdef P_HAS_WCHAR
169 PString(
170 const wchar_t * ustr
171 );
172 #endif
173
187 PString(
188 const char * cstr,
189 PINDEX len
190 );
191
202 #ifdef P_HAS_WCHAR
203 PString(
204 const wchar_t * ustr,
205 PINDEX len
206 );
207
218 PString(
219 const PWCharArray & ustr
220 );
221 #endif
222
231 PString(
232 char ch
233 );
234
239 PString(
240 short n
241 );
242
247 PString(
248 unsigned short n
249 );
250
255 PString(
256 int n
257 );
258
263 PString(
264 unsigned int n
265 );
266
271 PString(
272 long n
273 );
274
279 PString(
280 unsigned long n
281 );
282
287 PString(
288 PInt64 n
289 );
290
295 PString(
296 PUInt64 n
297 );
298
299
300 /* Type of conversion to make in the conversion constructors.
301 */
302 enum ConversionType {
303 Pascal,
304 Basic,
305 Literal,
306 Signed,
307 Unsigned,
308 Decimal,
309 Exponent,
310 ScaleSI,
311 Printf,
312 NumConversionTypes
313 };
314
315 /* Contruct a new string converting from the spcified data source into
316 a string array.
317 */
318 PString(
319 ConversionType type,
320 const char * str,
321 ...
322 );
323 PString(
324 ConversionType type,
325 PInt64 value,
326 unsigned param = 10
330 );
331 PString(ConversionType type, PUInt64 value, unsigned param = 10);
332 PString(ConversionType type, unsigned long value, unsigned param = 10);
333 PString(ConversionType type, signed long value, unsigned param = 10);
334 PString(ConversionType type, unsigned int value, unsigned param = 10);
335 PString(ConversionType type, signed int value, unsigned param = 10);
336 PString(ConversionType type, unsigned short value, unsigned param = 10);
337 PString(ConversionType type, signed short value, unsigned param = 10);
338 PString(ConversionType type, unsigned char value, unsigned param = 10);
339 PString(ConversionType type, signed char value, unsigned param = 10);
340
341 /* Contruct a new string converting from the spcified data source into
342 a string array.
343 */
344 PString(
345 ConversionType type,
346 double value,
347 unsigned places
348 );
349
357 PString & operator=(
358 const PString & str
359 );
360
368 PString & operator=(
369 const std::string & str
370 ) { return operator=(str.c_str()); }
371
381 PString & operator=(
382 const char * cstr
383 );
384
393 PString & operator=(
394 char ch
395 );
396
401 PString & operator=(
402 short n
403 );
404
409 PString & operator=(
410 unsigned short n
411 );
412
417 PString & operator=(
418 int n
419 );
420
425 PString & operator=(
426 unsigned int n
427 );
428
433 PString & operator=(
434 long n
435 );
436
441 PString & operator=(
442 unsigned long n
443 );
444
449 PString & operator=(
450 PInt64 n
451 );
452
457 PString & operator=(
458 PUInt64 n
459 );
460
463 virtual PString & MakeEmpty();
464
467 static const PString & Empty();
469
476 virtual PObject * Clone() const;
477
487 virtual Comparison Compare(
488 const PObject & obj
489 ) const;
490
493 virtual void PrintOn(
494 ostream & strm
495 ) const;
496
502 virtual void ReadFrom(
503 istream & strm
504 );
505
519 virtual PINDEX HashFunction() const;
521
536 virtual PBoolean SetSize(
537 PINDEX newSize
538 );
539
548 virtual PBoolean IsEmpty() const;
549
558 virtual PBoolean MakeUnique();
560
561
577 bool MakeMinimumSize(
578 PINDEX newLength = 0
579 );
580
589 virtual PINDEX GetLength() const { return m_length; }
590
597 bool operator!() const;
599
608 PString operator+(
609 const PString & str
610 ) const;
611
623 PString operator+(
624 const char * cstr
625 ) const;
626
638 PString operator+(
639 char ch
640 ) const;
641
653 friend PString operator+(
654 const char * cstr,
655 const PString & str
656 );
657
669 friend PString operator+(
670 char ch,
671 const PString & str
672 );
673
679 PString & operator+=(
680 const PString & str
681 );
682
692 PString & operator+=(
693 const char * cstr
694 );
695
705 PString & operator+=(
706 char ch
707 );
708
709
716 PString operator&(
717 const PString & str
718 ) const;
719
736 PString operator&(
737 const char * cstr
738 ) const;
739
756 PString operator&(
757 char ch
758 ) const;
759
776 friend PString operator&(
777 const char * cstr,
778 const PString & str
779 );
780
797 friend PString operator&(
798 char ch,
799 const PString & str
800 );
801
807 PString & operator&=(
808 const PString & str
809 );
810
825 PString & operator&=(
826 const char * cstr
827 );
828
829
844 PString & operator&=(
845 char ch
846 );
848
849
857 bool operator*=(
858 const PString & str
859 ) const;
860
868 bool operator==(
869 const PString & str
870 ) const;
871
879 bool operator!=(
880 const PString & str
881 ) const;
882
890 bool operator<(
891 const PString & str
892 ) const;
893
901 bool operator>(
902 const PString & str
903 ) const;
904
912 bool operator<=(
913 const PString & str
914 ) const;
915
923 bool operator>=(
924 const PString & str
925 ) const;
926
927
938 bool operator*=(
939 const char * cstr
940 ) const;
941
952 bool operator==(
953 const char * cstr
954 ) const;
955
966 bool operator!=(
967 const char * cstr
968 ) const;
969
980 bool operator<(
981 const char * cstr
982 ) const;
983
994 bool operator>(
995 const char * cstr
996 ) const;
997
1008 bool operator<=(
1009 const char * cstr
1010 ) const;
1011
1022 bool operator>=(
1023 const char * cstr
1024 ) const;
1025
1036 friend bool operator*=(
1037 const char * cstr,
1038 const PString & str
1039 );
1040
1051 friend bool operator==(
1052 const char * cstr,
1053 const PString & str
1054 );
1055
1066 friend bool operator!=(
1067 const char * cstr,
1068 const PString & str
1069 );
1070
1081 friend bool operator<(
1082 const char * cstr,
1083 const PString & str
1084 );
1085
1096 friend bool operator>(
1097 const char * cstr,
1098 const PString & str
1099 );
1100
1111 friend bool operator<=(
1112 const char * cstr,
1113 const PString & str
1114 );
1115
1126 friend bool operator>=(
1127 const char * cstr,
1128 const PString & str
1129 );
1130
1142 Comparison NumCompare(
1143 const PString & str,
1144 PINDEX count = P_MAX_INDEX,
1145 PINDEX offset = 0
1146 ) const;
1147
1159 Comparison NumCompare(
1160 const char * cstr,
1161 PINDEX count = P_MAX_INDEX,
1162 PINDEX offset = 0
1163 ) const;
1165
1166
1170 PINDEX Find(
1171 char ch,
1172 PINDEX offset = 0
1173 ) const;
1174
1176 PINDEX Find(
1177 const PString & str,
1178 PINDEX offset = 0
1179 ) const;
1180
1181 /* Locate the position within the string of the character or substring. The
1182 search will begin at the character offset provided.
1183
1184 If <code>offset</code> is beyond the length of the string, then the
1185 function will always return P_MAX_INDEX.
1186
1187 The matching will be for identical character or string. If a search
1188 ignoring case is required then the string should be converted to a
1189 PCaselessString before the search is made.
1190
1191 @return
1192 position of character or substring in the string, or P_MAX_INDEX if the
1193 character or substring is not in the string.
1194 */
1195 PINDEX Find(
1196 const char * cstr,
1197 PINDEX offset = 0
1198 ) const;
1199
1201 PINDEX FindLast(
1202 char ch,
1203 PINDEX offset = P_MAX_INDEX
1204 ) const;
1205
1207 PINDEX FindLast(
1208 const PString & str,
1209 PINDEX offset = P_MAX_INDEX
1210 ) const;
1211
1229 PINDEX FindLast(
1230 const char * cstr,
1231 PINDEX offset = P_MAX_INDEX
1232 ) const;
1233
1235 PINDEX FindOneOf(
1236 const PString & set,
1237 PINDEX offset = 0
1238 ) const;
1239
1254 PINDEX FindOneOf(
1255 const char * cset,
1256 PINDEX offset = 0
1257 ) const;
1258
1260 PINDEX FindSpan(
1261 const PString & set,
1262 PINDEX offset = 0
1263 ) const;
1264
1280 PINDEX FindSpan(
1281 const char * cset,
1282 PINDEX offset = 0
1283 ) const;
1284
1295 PINDEX FindRegEx(
1296 const PRegularExpression & regex,
1297 PINDEX offset = 0
1298 ) const;
1299
1310 PBoolean FindRegEx(
1311 const PRegularExpression & regex,
1312 PINDEX & pos,
1313 PINDEX & len,
1314 PINDEX offset = 0,
1315 PINDEX maxPos = P_MAX_INDEX
1316 ) const;
1317
1318
1321 PBoolean MatchesRegEx(
1322 const PRegularExpression & regex
1323 ) const;
1324
1335 void Replace(
1336 const PString & target,
1337 const PString & subs,
1338 PBoolean all = false,
1339 PINDEX offset = 0
1340 );
1341
1349 void Splice(
1350 const PString & str,
1351 PINDEX pos,
1352 PINDEX len = 0
1353 );
1354
1362 void Splice(
1363 const char * cstr,
1364 PINDEX pos,
1365 PINDEX len = 0
1366 );
1367
1374 void Delete(
1375 PINDEX start,
1376 PINDEX len
1377 );
1379
1380
1400 PString operator()(
1401 PINDEX start,
1402 PINDEX end
1403 ) const;
1404
1419 PString Left(
1420 PINDEX len
1421 ) const;
1422
1437 PString Right(
1438 PINDEX len
1439 ) const;
1440
1457 PString Mid(
1458 PINDEX start,
1459 PINDEX len = P_MAX_INDEX
1460 ) const;
1461
1462
1470 PString LeftTrim() const;
1471
1479 PString RightTrim() const;
1480
1489 PString Trim() const;
1490
1491
1500 PString ToLower() const;
1501
1510 PString ToUpper() const;
1511
1512
1516 bool Split(
1517 char delimiter,
1518 PString & before,
1519 PString & after,
1520 bool trim = true
1521 ) const;
1522
1523
1525 PStringArray Tokenise(
1526 const PString & separators,
1528 PBoolean onePerSeparator = true
1530 ) const;
1555 PStringArray Tokenise(
1556 const char * cseparators,
1558 PBoolean onePerSeparator = true
1560 ) const;
1561
1575 PStringArray Lines() const;
1577
1594 PString & sprintf(
1595 const char * cfmt,
1596 ...
1597 );
1598
1613 friend PString psprintf(
1614 const char * cfmt,
1615 ...
1616 );
1617
1619 PString & vsprintf(
1620 const PString & fmt,
1621 va_list args
1622 );
1637 PString & vsprintf(
1638 const char * cfmt,
1639 va_list args
1640 );
1641
1643 friend PString pvsprintf(
1644 const char * cfmt,
1645 va_list args
1646 );
1661 friend PString pvsprintf(
1662 const PString & fmt,
1663 va_list args
1664 );
1665
1666
1679 long AsInteger(
1680 unsigned base = 10
1681 ) const;
1694 DWORD AsUnsigned(
1695 unsigned base = 10
1696 ) const;
1710 PInt64 AsInt64(
1711 unsigned base = 10
1712 ) const;
1726 PUInt64 AsUnsigned64(
1727 unsigned base = 10
1728 ) const;
1729
1740 double AsReal() const;
1741
1742 #ifdef P_HAS_WCHAR
1743
1746 PWCharArray AsUCS2() const;
1747 #endif
1748
1759 PBYTEArray ToPascal() const;
1760
1769 PString ToLiteral() const;
1770
1778 PString FromLiteral(
1779 PINDEX & offset
1780 ) const;
1781
1789 operator const unsigned char *() const;
1790
1793 operator std::string () const
1794 { return std::string(theArray); }
1795
1817 char * GetPointerAndSetLength(
1818 PINDEX len
1819 );
1820
1825 virtual const char * GetPointer(PINDEX = 0) const { return (const char *)(*this); }
1826
1828
1829
1830 protected:
1831 #ifdef P_HAS_WCHAR
1832 void InternalFromUCS2(
1833 const wchar_t * ptr,
1834 PINDEX len
1835 );
1836 #endif
1837 virtual Comparison InternalCompare(
1838 PINDEX offset, // Offset into string to compare.
1839 char c // Character to compare against.
1840 ) const;
1841 virtual Comparison InternalCompare(
1842 PINDEX offset, // Offset into string to compare.
1843 PINDEX length, // Number of characters to compare.
1844 const char * cstr // C string to compare against.
1845 ) const;
1846
1847 /* Internal function to compare the current string value against the
1848 specified C string.
1849
1850 @return
1851 relative rank of the two strings.
1852 */
1853 PString(int dummy, const PString * str);
1854
1855 virtual void AssignContents(const PContainer &);
1856 PString(PContainerReference & reference_, PINDEX len)
1857 : PCharArray(reference_)
1858 , m_length(len)
1859 { }
1860
1861 protected:
1862 mutable PINDEX m_length; // Length of the string, always at least one less than GetSize()
1863 };
1864
1865
1866 inline ostream & operator<<(ostream & stream, const PString & string)
1867 {
1868 string.PrintOn(stream);
1869 return stream;
1870 }
1871
1872 #ifdef P_HAS_WOSTREAM
1873 inline wostream & operator<<(wostream & stream, const PString & string)
1874 {
1875 return stream << (const char *)string;
1876 }
1877 #endif
1878
1879
1880 #ifdef _WIN32
1881 class PWideString : public PWCharArray {
1882 PCLASSINFO(PWideString, PWCharArray);
1883
1884 public:
1885 typedef const wchar_t * Initialiser;
1886
1887 PWideString() { }
1888 PWideString(const PWCharArray & arr) : PWCharArray(arr) { }
1889 PWideString(const PString & str) : PWCharArray(str.AsUCS2()) { }
1890 PWideString(const char * str) : PWCharArray(PString(str).AsUCS2()) { }
1891 PWideString & operator=(const PWideString & str) { PWCharArray::operator=(str); return *this; }
1892 PWideString & operator=(const PString & str) { PWCharArray::operator=(str.AsUCS2()); return *this; }
1893 PWideString & operator=(const std::string & str) { PWCharArray::operator=(PString(str.c_str()).AsUCS2()); return *this; }
1894 PWideString & operator=(const char * str) { PWCharArray::operator=(PString(str).AsUCS2()); return *this; }
1895 friend inline ostream & operator<<(ostream & stream, const PWideString & string) { return stream << PString(string); }
1896
1897 protected:
1898 PWideString(PContainerReference & reference_) : PWCharArray(reference_) { }
1899 };
1900
1901 #ifdef UNICODE
1902 typedef PWideString PVarString;
1903 #else
1904 typedef PString PVarString;
1905 #endif
1906 #endif
1907
1908
1910
1920 class PCaselessString : public PString
1921 {
1922 PCLASSINFO(PCaselessString, PString);
1923
1924 public:
1927 PCaselessString();
1928
1932 PCaselessString(
1933 const char * cstr
1934 );
1935
1940 PCaselessString(
1941 const PString & str
1942 );
1943
1944
1947 PCaselessString(
1948 const std::string & str
1949 ) : PString(str)
1950 { }
1951
1959 PCaselessString & operator=(
1960 const PString & str
1961 );
1962
1970 PCaselessString & operator=(
1971 const std::string & str
1972 ) { return operator=(str.c_str()); }
1973
1983 PCaselessString & operator=(
1984 const char * cstr
1985 );
1986
1995 PCaselessString & operator=(
1996 char ch
1997 );
1998
1999
2000 // Overrides from class PObject
2005 virtual PObject * Clone() const;
2006
2007 protected:
2008 // Overrides from class PString
2009 virtual Comparison InternalCompare(
2010 PINDEX offset, // Offset into string to compare.
2011 char c // Character to compare against.
2012 ) const;
2013 virtual Comparison InternalCompare(
2014 PINDEX offset, // Offset into string to compare.
2015 PINDEX length, // Number of characters to compare.
2016 const char * cstr // C string to compare against.
2017 ) const;
2018 /* Internal function to compare the current string value against the
2019 specified C string.
2020
2021 @return
2022 relative rank of the two strings or characters.
2023 */
2024
2025 PCaselessString(int dummy, const PCaselessString * str);
2026 PCaselessString(PContainerReference & reference_, PINDEX len) : PString(reference_, len) { }
2027 };
2028
2029
2031
2045 template <class ParentString>
2046 class PConstantString : public ParentString
2047 {
2048 private:
2049 PContainerReference m_staticReference;
2050 public:
2051 PConstantString(typename ParentString::Initialiser init)
2052 : ParentString(m_staticReference, init != NULL ? strlen(init) : 0)
2053 , P_DISABLE_MSVC_WARNINGS(4267 4355, m_staticReference(this->m_length+1, true))
2054 {
2055 this->theArray = (char *)(init != NULL ? init : "");
2056 }
2057 ~PConstantString() { this->Destruct(); }
2058
2059 virtual PBoolean SetSize(PINDEX s) { return s <= this->m_length+1; }
2060 virtual void AssignContents(const PContainer &) { PAssertAlways(PInvalidParameter); }
2061 virtual void DestroyReference() { }
2062
2063 private:
2064 PConstantString(const PConstantString &)
2065 : ParentString(m_staticReference)
2066 , m_staticReference(0, true)
2067 { }
2068 void operator=(const PConstantString &) { }
2069 };
2070
2071
2073 typedef PConstantString<PString> PConstString;
2074
2076 typedef PConstantString<PCaselessString> PConstCaselessString;
2077
2078
2079
2081
2088 class PStringStream : public PString, public P_IOSTREAM
2089 {
2090 PCLASSINFO(PStringStream, PString);
2091
2092 public:
2098 PStringStream();
2099
2104 PStringStream(
2105 PINDEX fixedBufferSize
2106 );
2107
2114 PStringStream(
2115 const PString & str
2116 );
2117
2122 PStringStream(
2123 const char * cstr
2124 );
2125
2127 virtual ~PStringStream();
2128
2131 virtual PString & MakeEmpty();
2132
2144 PStringStream & operator=(
2145 const PStringStream & strm
2146 );
2147
2159 PStringStream & operator=(
2160 const PString & str
2161 );
2162
2178 PStringStream & operator=(
2179 const char * cstr
2180 );
2181
2190 PStringStream & operator=(
2191 char ch
2192 );
2193
2194 virtual PINDEX GetLength() const;
2195
2196 protected:
2197 virtual void AssignContents(const PContainer & cont);
2198
2199 private:
2200 PStringStream(int, const PStringStream &) : P_IOSTREAM(cout.rdbuf()) { }
2201
2202 class Buffer : public streambuf {
2203 public:
2204 Buffer(PStringStream & str, PINDEX size);
2205 Buffer(const Buffer & sbuf);
2206 Buffer & operator=(const Buffer & sbuf);
2207 virtual int_type overflow(int_type = EOF);
2208 virtual int_type underflow();
2209 virtual int sync();
2210 virtual pos_type seekoff(off_type, ios_base::seekdir, ios_base::openmode = ios_base::in | ios_base::out);
2211 virtual pos_type seekpos(pos_type, ios_base::openmode = ios_base::in | ios_base::out);
2212 PCharArray & string;
2213 PBoolean fixedBufferSize;
2214 };
2215 };
2216
2217
2229 #define PSTRSTRM(arg) dynamic_cast<const PString &>(PStringStream() << std::flush << arg)
2230
2231
2232 class PStringList;
2233 class PSortedStringList;
2234
2247 class PStringArray : public PArray<PString>
2248 {
2249 typedef PArray<PString> ParentClass;
2250 PCLASSINFO(PStringArray, ParentClass);
2251
2252 protected:
2253 inline PStringArray(int dummy, const PStringArray * c)
2254 : ParentClass(dummy, c) { }
2255
2256 public:
2259
2260 inline PStringArray(PINDEX initialSize = 0)
2261 : ParentClass(initialSize) { }
2262
2267 PStringArray(
2268 PINDEX count,
2269 char const * const * strarr,
2270 PBoolean caseless = false
2271 );
2274 PStringArray(
2275 const PString & str
2276 );
2279 PStringArray(
2280 const PStringList & list
2281 );
2284 PStringArray(
2285 const PSortedStringList & list
2286 );
2287
2291 PStringArray(
2292 const std::vector<PString> & vec
2293 )
2294 {
2295 for (std::vector<PString>::const_iterator r = vec.begin(); r != vec.end(); ++r)
2296 AppendString(*r);
2297 }
2298
2302 PStringArray(
2303 const std::vector<std::string> & vec
2304 )
2305 {
2306 for (std::vector<std::string>::const_iterator r = vec.begin(); r != vec.end(); ++r)
2307 AppendString(PString(*r));
2308 }
2309
2313 template <typename stlContainer>
2314 static PStringArray container(
2315 const stlContainer & vec
2316 )
2317 {
2318 PStringArray list;
2319 for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
2320 list.AppendString(PString(*r));
2321 return list;
2322 }
2323
2325
2334 virtual void ReadFrom(
2335 istream &strm // Stream to read the objects contents from.
2336 );
2337
2338 virtual PObject * Clone() const
2339 {
2340 return new PStringArray(0, this);
2341 }
2343
2352 PINDEX GetStringsIndex(
2353 const PString & str
2354 ) const;
2355
2356 PString operator[](
2357 PINDEX index
2358 ) const;
2359
2367 PString & operator[](
2368 PINDEX index
2369 );
2370
2373 PINDEX AppendString(
2374 const PString & str
2375 );
2376
2382 PStringArray & operator +=(const PStringArray & array);
2383 PStringArray & operator +=(const PString & str);
2384
2385
2392 PStringArray operator + (const PStringArray & array);
2393 PStringArray operator + (const PString & str);
2394
2402 char ** ToCharArray(
2403 PCharArray * storage = NULL
2404 ) const;
2406 };
2407
2408
2421 class PStringList : public PList<PString>
2422 {
2423 typedef PList<PString> ParentClass;
2424 PCLASSINFO(PStringList, ParentClass);
2425
2426 protected:
2427 PStringList(int dummy, const PStringList * c)
2428 : ParentClass(dummy, c) { }
2429
2430 public:
2435 PStringList()
2436 : ParentClass() { }
2437
2440 PStringList(
2441 PINDEX count,
2442 char const * const * strarr,
2443 PBoolean caseless = false
2444 );
2447 PStringList(
2448 const PString & str
2449 );
2452 PStringList(
2453 const PStringArray & array
2454 );
2457 PStringList(
2458 const PSortedStringList & list
2459 );
2461
2470 virtual void ReadFrom(
2471 istream &strm // Stream to read the objects contents from.
2472 );
2473
2474 virtual PObject * Clone() const
2475 {
2476 return new PStringList(0, this);
2477 }
2479
2484 PINDEX AppendString(
2485 const PString & str
2486 );
2487
2490 PINDEX InsertString(
2491 const PString & before,
2492 const PString & str
2493 );
2494
2498 PINDEX GetStringsIndex(
2499 const PString & str
2500 ) const;
2501
2507 PStringList & operator +=(const PStringList & list);
2508 PStringList & operator +=(const PString & str);
2509
2510
2517 PStringList operator + (const PStringList & array);
2518 PStringList operator + (const PString & str);
2519
2523 template <typename stlContainer>
2524 static PStringList container(
2525 const stlContainer & vec
2526 )
2527 {
2528 PStringList list;
2529 for (typename stlContainer::const_iterator r = vec.begin(); r != vec.end(); ++r)
2530 list.AppendString(PString(*r));
2531 return list;
2532 }
2534 };
2535
2536
2548 #ifdef DOC_PLUS_PLUS
2549 class PSortedStringList : public PSortedList {
2550 #endif
2551 PDECLARE_SORTED_LIST(PSortedStringList, PString);
2552 public:
2557 PSortedStringList(
2558 PINDEX count,
2559 char const * const * strarr,
2560 PBoolean caseless = false
2561 );
2564 PSortedStringList(
2565 const PString & str
2566 );
2569 PSortedStringList(
2570 const PStringArray & array
2571 );
2574 PSortedStringList(
2575 const PStringList & list
2576 );
2578
2587 virtual void ReadFrom(
2588 istream &strm // Stream to read the objects contents from.
2589 );
2591
2597 PINDEX AppendString(
2598 const PString & str
2599 );
2600
2604 PINDEX GetStringsIndex(
2605 const PString & str
2606 ) const;
2607
2612 PINDEX GetNextStringsIndex(
2613 const PString & str
2614 ) const;
2616
2617 protected:
2618 PINDEX InternalStringSelect(
2619 const char * str,
2620 PINDEX len,
2621 PSortedListElement * thisElement,
2622 PSortedListElement * & lastElement
2623 ) const;
2624 };
2625
2626
2643 class PStringSet : public PSet<PString>
2644 {
2645 typedef PSet<PString> BaseClass;
2646 PCLASSINFO(PStringSet, BaseClass);
2647
2648 protected:
2649 PStringSet(int dummy, const PStringSet * c)
2650 : BaseClass(dummy, c) { }
2651
2652 public:
2657 PStringSet(PBoolean initialDeleteObjects = true)
2658 : BaseClass(initialDeleteObjects) { }
2659
2662 PStringSet(
2663 PINDEX count,
2664 char const * const * strarr,
2665 PBoolean caseless = false
2666 );
2667
2670 PStringSet(
2671 const PString & str
2672 );
2675 PStringSet(
2676 const PStringArray & strArray
2677 );
2680 PStringSet(
2681 const PStringList & strList
2682 );
2684
2693 virtual void ReadFrom(
2694 istream &strm
2695 );
2696
2697 virtual PObject * Clone() const
2698 {
2699 return PNEW PStringSet(0, this);
2700 }
2702
2706 void Include(
2707 const PString & key
2708 );
2710 PStringSet & operator+=(
2711 const PString & key
2712 );
2714 void Exclude(
2715 const PString & key
2716 );
2718 PStringSet & operator-=(
2719 const PString & key
2720 );
2722 };
2723
2724
2732 template <class K> class PStringDictionary : public PDictionary<K, PString>
2733 {
2734 typedef PDictionary<K, PString> ParentClass;
2735 PCLASSINFO(PStringDictionary, ParentClass);
2736
2737 public:
2746 PStringDictionary()
2747 : ParentClass() { }
2749
2756 virtual PObject * Clone() const
2757 { return PNEW PStringDictionary(0, this); }
2759
2775 PString operator()(const K & key, const char * dflt = NULL) const
2776 {
2777 PString * str = this->GetAt(key);
2778 return str != NULL ? *str : PString(dflt);
2779 }
2780
2792 virtual PBoolean SetAt(
2793 const K & key, // Key for position in dictionary to add object.
2794 const PString & str // New string value to put into the dictionary.
2795 ) { return this->AbstractSetAt(key, PNEW PString(str)) != NULL; }
2797
2798 protected:
2799 PStringDictionary(int dummy, const PStringDictionary * c)
2800 : ParentClass(dummy, c) { }
2801 };
2802
2803
2818 #define PDECLARE_STRING_DICTIONARY(cls, K) \
2819 PDECLARE_CLASS(cls, PStringDictionary<K>) \
2820 protected: \
2821 cls(int dummy, const cls * c) \
2822 : PStringDictionary<K>(dummy, c) { } \
2823 public: \
2824 cls() \
2825 : PStringDictionary<K>() { } \
2826 virtual PObject * Clone() const \
2827 { return PNEW cls(0, this); } \
2828
2829
2842 #define PSTRING_DICTIONARY(cls, K) typedef PStringDictionary<K> cls
2843
2844
2859 #ifdef DOC_PLUS_PLUS
2860 class POrdinalToString : public PStringDictionary {
2861 #endif
2862 PDECLARE_STRING_DICTIONARY(POrdinalToString, POrdinalKey);
2863 public:
2866
2867 struct Initialiser {
2869 PINDEX key;
2871 const char * value;
2872 };
2875 POrdinalToString(
2876 PINDEX count,
2877 const Initialiser * init
2878 );
2880
2889 virtual void ReadFrom(
2890 istream &strm // Stream to read the objects contents from.
2891 );
2893 };
2894
2906 #ifdef DOC_PLUS_PLUS
2907 class PStringToOrdinal : public POrdinalDictionary {
2908 #endif
2909 PDECLARE_ORDINAL_DICTIONARY(PStringToOrdinal, PString);
2910 public:
2913
2914 struct Initialiser {
2916 const char * key;
2918 PINDEX value;
2919 };
2922 PStringToOrdinal(
2923 PINDEX count,
2924 const Initialiser * init,
2925 PBoolean caseless = false
2926 );
2928
2937 virtual void ReadFrom(
2938 istream &strm // Stream to read the objects contents from.
2939 );
2941 };
2942
2943
2957 #ifdef DOC_PLUS_PLUS
2958 class PStringToString : public PStringDictionary {
2959 #endif
2960 PDECLARE_STRING_DICTIONARY(PStringToString, PString);
2961 public:
2964
2965 struct Initialiser {
2967 const char * key;
2969 const char * value;
2970 };
2973 PStringToString(
2974 PINDEX count,
2975 const Initialiser * init,
2976 PBoolean caselessKeys = false,
2977 PBoolean caselessValues = false
2978 );
2979
2983 PStringToString(
2984 const PString & str
2985 ) { FromString(str); }
2987
2996 virtual void ReadFrom(
2997 istream &strm // Stream to read the objects contents from.
2998 );
3000
3013 char ** ToCharArray(
3014 bool withEqualSign,
3015 PCharArray * storage = NULL
3016 ) const;
3017
3021 void FromString(
3022 const PString & str
3023 );
3024 };
3025
3026
3033 class PStringOptions : public PStringToString
3034 {
3035 public:
3036 PStringOptions() { }
3037 PStringOptions(const PString & str) : PStringToString(str) { }
3038 PStringOptions(const PStringToString & other) : PStringToString(other) { }
3039 PStringOptions & operator=(const PStringToString & other) { PStringToString::operator=(other); return *this; }
3040
3042 bool Contains(const char * key ) const { PConstCaselessString k(key); return PStringToString::Contains(k); }
3043 bool Contains(const PString & key ) const { return PStringToString::Contains(PCaselessString(key)); }
3044 bool Contains(const PCaselessString & key ) const { return PStringToString::Contains(key); }
3045 bool Contains(const PCaselessString & (*key)()) const { return PStringToString::Contains(key()); }
3046
3047 // Overide default PStringToString::SetAt() to make sure the key is caseless
3048 PString * GetAt(const char * key ) const { PConstCaselessString k(key); return PStringToString::GetAt(k); }
3049 PString * GetAt(const PString & key ) const { return PStringToString::GetAt(PCaselessString(key)); }
3050 PString * GetAt(const PCaselessString & key ) const { return PStringToString::GetAt(key); }
3051 PString * GetAt(const PCaselessString & (*key)()) const { return PStringToString::GetAt(key()); }
3052
3053 // Overide default PStringToString::SetAt() to make sure the key is caseless
3054 PBoolean SetAt(const char * key, const PString & data) { PConstCaselessString k(key); return SetAt(k, data); }
3055 PBoolean SetAt(const PString & key, const PString & data) { return SetAt(PCaselessString(key), data); }
3056 PBoolean SetAt(const PCaselessString & key, const PString & data) { MakeUnique(); return PStringToString::SetAt(key, data); }
3057 PBoolean SetAt(const PCaselessString & (*key)(), const PString & data) { return SetAt(key(), data); }
3058
3059 // Overide default PStringToString::RemoveAt() to make sure the key is caseless
3060 PString * RemoveAt(const char * key) { PConstCaselessString k(key); return RemoveAt(k); }
3061 PString * RemoveAt(const PString & key) { return RemoveAt(PCaselessString(key)); }
3062 PString * RemoveAt(const PCaselessString & key) { MakeUnique(); return PStringToString::RemoveAt(key); }
3063 PString * RemoveAt(const PCaselessString & (*key)()) { return RemoveAt(key()); }
3064
3066 PString GetString(const char * key, const char * dflt = NULL) const { PConstCaselessString k(key); return GetString(k, dflt); }
3067 PString GetString(const PString & key, const char * dflt = NULL) const { return GetString(PCaselessString(key), dflt); }
3068 PString GetString(const PCaselessString & key, const char * dflt = NULL) const;
3069 PString GetString(const PCaselessString & (*key)(), const char * dflt = NULL) const { return GetString(key(), dflt); }
3070
3072 bool SetString(const char * key, const PString & value) { return SetAt(key, value); }
3073 bool SetString(const PString & key, const PString & value) { return SetAt(key, value); }
3074 bool SetString(const PCaselessString & key, const PString & value) { return SetAt(key, value); }
3075 bool SetString(const PCaselessString & (*key)(), const PString & value) { return SetAt(key, value); }
3076
3078 bool GetBoolean(const char * key, bool dflt = false) const { PConstCaselessString k(key); return GetBoolean(k, dflt); }
3079 bool GetBoolean(const PString & key, bool dflt = false) const { return GetBoolean(PCaselessString(key), dflt); }
3080 bool GetBoolean(const PCaselessString & key, bool dflt = false) const;
3081 bool GetBoolean(const PCaselessString & (*key)(), bool dflt = false) const { return GetBoolean(key(), dflt); }
3082
3084 void SetBoolean(const char * key, bool value) { PConstCaselessString k(key); SetBoolean(k, value); }
3085 void SetBoolean(const PString & key, bool value) { SetBoolean(PCaselessString(key), value); }
3086 void SetBoolean(const PCaselessString & key, bool value) { SetAt(key, value ? "true" : "false"); }
3087 void SetBoolean(const PCaselessString & (*key)(), bool value) { SetBoolean(key(), value); }
3088
3090 long GetInteger(const char * key, long dflt = 0) const { PConstCaselessString k(key); return GetInteger(k, dflt); }
3091 long GetInteger(const PString & key, long dflt = 0) const { return GetInteger(PCaselessString(key), dflt); }
3092 long GetInteger(const PCaselessString & key, long dflt = 0) const;
3093 long GetInteger(const PCaselessString & (*key)(), long dflt = 0) const { return GetInteger(key(), dflt); }
3094
3096 void SetInteger(const char * key, long value) { PConstCaselessString k(key); SetInteger(k, value); }
3097 void SetInteger(const PString & key, long value) { SetInteger(PCaselessString(key), value); }
3098 void SetInteger(const PCaselessString & key, long value);
3099 void SetInteger(const PCaselessString & (*key)(), long value) { SetInteger(key(), value); }
3100
3102 template<typename E> E GetEnum(const char * key, E dflt) const { PConstCaselessString k(key); return GetEnum(k, dflt); }
3103 template<typename E> E GetEnum(const PString & key, E dflt) const { return GetEnum(PCaselessString(key), dflt); }
3104 template<typename E> E GetEnum(const PCaselessString & key, E dflt) const { return (E)GetInteger(key, dflt); }
3105 template<typename E> E GetEnum(const PCaselessString & (*key)(), E dflt) const { return GetEnum(key(), dflt); }
3106
3108 template<typename E> void SetEnum(const char * key, E value) { PConstCaselessString k(key); SetInteger(k, value); }
3109 template<typename E> void SetEnum(const PString & key, E value) { SetInteger(PCaselessString(key), value); }
3110 template<typename E> void SetEnum(const PCaselessString & key, E value) { SetInteger(key, value); }
3111 template<typename E> void SetEnum(const PCaselessString & (*key)(), E value) { SetInteger(key(), value); }
3112
3114 double GetReal(const char * key, double dflt = 0) const { PConstCaselessString k(key); return GetReal(k, dflt); }
3115 double GetReal(const PString & key, double dflt = 0) const { return GetReal(PCaselessString(key), dflt); }
3116 double GetReal(const PCaselessString & key, double dflt = 0) const;
3117 double GetReal(const PCaselessString & (*key)(), double dflt = 0) const { return GetReal(key(), dflt); }
3118
3120 void SetReal(const char * key, double value, int decimals) { PConstCaselessString k(key); SetReal(k, value, decimals); }
3121 void SetReal(const PString & key, double value, int decimals) { SetReal(PCaselessString(key), value, decimals); }
3122 void SetReal(const PCaselessString & key, double value, int decimals);
3123 void SetReal(const PCaselessString & (*key)(), double value, int decimals) { SetReal(key(), value, decimals); }
3124
3126 __inline bool Has(const char * key) const { return Contains(key); }
3127 __inline bool Has(const PString & key) const { return Contains(key); }
3128 __inline bool Has(const PCaselessString & key) const { return Contains(key); }
3129 __inline bool Has(const PCaselessString & (*key)()) const { return Contains(key); }
3130
3132 __inline PString Get(const char * key, const char * dflt = NULL) const { return GetString(key, dflt); }
3133 __inline PString Get(const PString & key, const char * dflt = NULL) const { return GetString(key, dflt); }
3134 __inline PString Get(const PCaselessString & key, const char * dflt = NULL) const { return GetString(key, dflt); }
3135 __inline PString Get(const PCaselessString & (*key)(), const char * dflt = NULL) const { return GetString(key, dflt); }
3136 __inline PString Get(const char * key, const PString & dflt) const { return GetString(key, dflt); }
3137 __inline PString Get(const PString & key, const PString & dflt) const { return GetString(key, dflt); }
3138 __inline PString Get(const PCaselessString & key, const PString & dflt) const { return GetString(key, dflt); }
3139 __inline PString Get(const PCaselessString & (*key)(), const PString & dflt) const { return GetString(key, dflt); }
3140
3142 __inline bool Set(const char * key, const PString & value) { return SetAt(key, value); }
3143 __inline bool Set(const PString & key, const PString & value) { return SetAt(key, value); }
3144 __inline bool Set(const PCaselessString & key, const PString & value) { return SetAt(key, value); }
3145 __inline bool Set(const PCaselessString & (*key)(), const PString & value) { return SetAt(key, value); }
3146
3148 __inline void Remove(const char * key) { RemoveAt(key); }
3149 __inline void Remove(const PString & key) { RemoveAt(key); }
3150 __inline void Remove(const PCaselessString & key) { RemoveAt(key); }
3151 __inline void Remove(const PCaselessString & (*key)()) { RemoveAt(key); }
3152 };
3153
3154
3155 // Put include here as needs PString above to be defined.
3156 #include <ptlib/bitwise_enum.h>
3157
3158
3164 class PRegularExpression : public PObject
3165 {
3166 PCLASSINFO(PRegularExpression, PObject);
3167 public:
3170
3171 P_DECLARE_BITWISE_ENUM(
3172 CompileOptions, 3,
3173 (
3174 Simple,
3175 Extended,
3176 IgnoreCase,
3177 AnchorNewLine
3180 )
3181 );
3182
3184 P_DECLARE_BITWISE_ENUM(
3185 ExecOptions, 2,
3186 (
3187 Normal,
3188 NotBeginningOfLine,
3193 NotEndofLine
3195 )
3196 );
3197
3199 PRegularExpression();
3200
3203 PRegularExpression(
3204 const PString & pattern,
3205 CompileOptions options = IgnoreCase
3206 );
3207
3210 PRegularExpression(
3211 const char * cpattern,
3212 CompileOptions options = IgnoreCase
3213 );
3214
3218 PRegularExpression(
3219 const PRegularExpression &
3220 );
3221
3225 PRegularExpression & operator =(
3226 const PRegularExpression &
3227 );
3228
3230 ~PRegularExpression();
3232
3233
3238 virtual void PrintOn(
3239 ostream & strm
3240 ) const;
3242
3245
3246 enum ErrorCodes {
3248 NoError = 0,
3250 NoMatch,
3251
3252 // POSIX regcomp return error codes. (In the order listed in the standard.)
3254 BadPattern,
3256 CollateError,
3258 BadClassType,
3260 BadEscape,
3262 BadSubReg,
3264 UnmatchedBracket,
3266 UnmatchedParen,
3268 UnmatchedBrace,
3270 BadBR,
3272 RangeError,
3274 OutOfMemory,
3276 BadRepitition,
3277
3278 /* Error codes we've added. */
3280 PrematureEnd,
3282 TooBig,
3284 UnmatchedRParen,
3286 NotCompiled
3287 };
3288
3294 ErrorCodes GetErrorCode() const { return m_lastError; }
3295
3302 PString GetErrorText() const;
3303
3305 const PString & GetPattern() const { return m_pattern; }
3307
3311 bool Compile(
3312 const PString & pattern,
3313 CompileOptions options = IgnoreCase
3314 );
3322 bool Compile(
3323 const char * cpattern,
3324 CompileOptions options = IgnoreCase
3325 );
3326
3327
3329 bool Execute(
3330 const PString & str,
3331 PINDEX & start,
3332 ExecOptions options = Normal
3333 ) const;
3335 bool Execute(
3336 const PString & str,
3337 PINDEX & start,
3338 PINDEX & len,
3339 ExecOptions options = Normal
3340 ) const;
3342 bool Execute(
3343 const char * cstr,
3344 PINDEX & start,
3345 ExecOptions options = Normal
3346 ) const;
3348 bool Execute(
3349 const char * cstr,
3350 PINDEX & start,
3351 PINDEX & len,
3352 ExecOptions options = Normal
3353 ) const;
3355 bool Execute(
3356 const PString & str,
3357 PIntArray & starts,
3358 ExecOptions options = Normal
3359 ) const;
3361 bool Execute(
3362 const PString & str,
3363 PIntArray & starts,
3364 PIntArray & ends,
3365 ExecOptions options = Normal
3366 ) const;
3368 bool Execute(
3369 const char * cstr,
3370 PIntArray & starts,
3371 ExecOptions options = Normal
3372 ) const;
3389 bool Execute(
3390 const char * cstr,
3391 PIntArray & starts,
3392 PIntArray & ends,
3393 ExecOptions options = Normal
3394 ) const;
3411 bool Execute(
3412 const char * cstr,
3413 PStringArray & substring,
3414 ExecOptions options = Normal
3415 ) const;
3417
3426 static PString EscapeString(
3427 const PString & str
3428 );
3430
3431 protected:
3432 bool InternalCompile();
3433 void InternalClean();
3434
3435 PString m_pattern;
3436 CompileOptions m_compileOptions;
3437 void * m_compiledRegex;
3438 mutable ErrorCodes m_lastError;
3439 };
3440
3441
3442 #endif // PTLIB_STRING_H
3443
3444
3445 // End Of File ///////////////////////////////////////////////////////////////