1 mike 1.38 //%/////////////////////////////////////////////////////////////////////////////
2 //
3 // Copyright (c) 2000, 2001 The Open group, BMC Software, Tivoli Systems, IBM
4 //
5 // Permission is hereby granted, free of charge, to any person obtaining a copy
6 // of this software and associated documentation files (the "Software"), to
7 // deal in the Software without restriction, including without limitation the
8 // rights to use, copy, modify, merge, publish, distribute, sublicense, and/or
9 // sell copies of the Software, and to permit persons to whom the Software is
10 // furnished to do so, subject to the following conditions:
11 //
12 // THE ABOVE COPYRIGHT NOTICE AND THIS PERMISSION NOTICE SHALL BE INCLUDED IN
13 // ALL COPIES OR SUBSTANTIAL PORTIONS OF THE SOFTWARE. THE SOFTWARE IS PROVIDED
14 // "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT
15 // LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR
16 // PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
17 // HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
18 // ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
19 // WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
20 //
21 //==============================================================================
22 mike 1.38 //
23 // Author: Mike Brasher (mbrasher@bmc.com)
24 //
|
25 karl 1.43 // Modified By: Karl Schopmeyer(k.schopmeyer@opengroup.org)
|
26 mike 1.38 //
27 //%/////////////////////////////////////////////////////////////////////////////
28
29 #ifndef Pegasus_String_h
30 #define Pegasus_String_h
31
32 #include <iostream>
33 #include <fstream>
34 #include <cstring>
35 #include <Pegasus/Common/Config.h>
36 #include <Pegasus/Common/Char16.h>
37 #include <Pegasus/Common/Array.h>
38
39 PEGASUS_NAMESPACE_BEGIN
40
41 /**
42 The Pegasus String C++ Class implements the CIM string type.
43 This class is based on the general handle/representation pattern
44 defined for all the Pegasus objects. However, it differes from
45 most in that it implements "copy on assign" all of the others implement
46 "no copy on assign" semantics. The string class uses the Array class and
47 mike 1.38 implements an array of characters.
48 */
49 class PEGASUS_COMMON_LINKAGE String
50 {
51 public:
52
53 /** Default constructor without parameters. This constructor creates a
54 null string
55 <pre>
56 String test;
57 </pre>
58 */
59 String();
60
61 /// Copy constructor.
62 String(const String& x);
63
64 /// Initialize with first n characters from x.
65 String(const String& x, Uint32 n);
66
67 /// Initialize with x.
68 mike 1.38 String(const Char16* x);
69
70 /// Initialize with first n characters of x.
71 String(const Char16* x, Uint32 n);
72
73 /// Initialize from a plain old C-String:
74 String(const char* x);
75
76 /// Initialize from the first n characters of a plain old C-String:
77 String(const char* x, Uint32 n);
78
79 /// String destructor. Used by the representation of the String object
80 ~String();
81
82 /** Assign this string with x.
83 <pre>
84 String t1 = "abc";
85 String t2 = t1;
86 </pre>
87 */
88 String& operator=(const String& x) { return assign(x); }
89 mike 1.38
90 /// Assign this string with Char16 x.
91 String& operator=(const Char16* x) { assign(x); return *this; }
92
93 /** Assign this string with String x
94 @param x String to assign
95 @return Returns the String
96 */
97 String& assign(const String& x);
98
99 /// Assign this string with x.
100 String& assign(const Char16* x);
101
102 /// Assign this string with first n characters of x.
103 String& assign(const Char16* x, Uint32 n);
104
105 /// Assign this string with the plain old C-String x.
106 String& assign(const char* x);
107
108 /// Assign this string with first n characters of the plain old C-String x.
109 String& assign(const char* x, Uint32 n);
110 mike 1.38
111 /** clear - Clear this string. After calling clear(), size() will return 0.
112 <pre>
113 String test = "abc";
114 test.clear(); // String test is now NULL (length == 0)
115 </pre>
116 */
117 void clear();
118
119
120 /** reserve - Reserves memory for capacity characters. Notice that this does
121 not
122 change the size of the string (size() returns what it did before).
123 If the capacity of the string is already greater or equal to the
124 capacity argument, this method has no effect. After calling reserve(),
125 getCapicty() returns a value which is greater or equal to the
126 capacity argument.
127 @param capacity defines the capacity in characters to reserve.
128 */
129 void reserve(Uint32 capacity);
130
131 mike 1.38 /** Returns the length of the String object.
132 @return Length of the string in characters.
133 <pre>
134 String s = "abcd";
135 assert(s.size() == 4);
136 </pre>
137 */
138 Uint32 size() const { return _rep.size() - 1; }
139
|
140 mike 1.41 /** getData Returns a pointer to the first character in the
141 null-terminated string string of the String object.
|
142 mike 1.38 @return Pointer to the first character of the String object
143 <pre>
144 String t1 = "abc";
145 const Char16* q = t1.getData();
146 </pre>
147 */
148 const Char16* getData() const { return _rep.getData(); }
149
|
150 mday 1.44 /** allocateCString - llocates an 8 bit representation of this String
|
151 mike 1.41 object. The user is responsible for freeing the result. If any
152 characters are truncated, a TruncatedCharacter exception is thrown.
153 This exception may be suppressed by passing true as the noThrow
154 argument. Extra characters may be allocated at the end of the
155 new string by passing a non-zero value to the extraBytes argument.
156
|
157 mike 1.38 @param extraBytes Defines the number of extra characters to be
158 allocated at the end of the new string. Default is zero.
|
159 mike 1.41
|
160 mike 1.38 @param noThrow If true, no exception will be thrown if characters
161 are truncated
|
162 mike 1.41
|
163 mike 1.38 @return pointer to the new representation of the string
|
164 mike 1.41
|
165 mike 1.38 @exception Throws TruncatedCharacter exception if any characters are
166 truncated
167 <pre>
168 String test = "abc";
169 char* p = test.allocateCString();
170 ...
171 delete [] p;
172 </pre>
173 */
174 char* allocateCString(Uint32 extraBytes = 0, Boolean noThrow = false) const;
175
|
176 mike 1.41 /** appendToCString - Append the given String object to a C-string. If the
177 length is not PEG_NOT_FOUND, then the lesser of the the length argument
178 and he length of this string is truncated. Otherwise, the entire string
179 is trunctated. The TruncatedCharacter exception is thrown if any
180 characters are truncated.
|
181 mike 1.38 @param str Char pointer to the string to append
182 @param length Length to append or PEG_NOT_FOUND (Uint32(-1)
183 @param noThrow - If false, throw the "TruncatedCharacter" exception of
184 any characters are truncated
185 @return void
186 @exception Throws TruncatedCharacter exception of characters are
187 truncated and noThrow parameter is false.
188 <pre>
189 const char STR0[] = "one two three four";
190 String s = STR0;
191 const char STR1[] = "zero ";
192 char* tmp = new char[strlen(STR1) + s.size() + 1];
193 strcpy(tmp, STR1);
194 s.appendToCString(tmp, 7);
195 assert(strcmp(tmp, "zero one two") == 0);
196 </pre>
197 */
198 void appendToCString(
199 char* str,
200 Uint32 length = PEG_NOT_FOUND,
201 Boolean noThrow = false) const;
202 mike 1.38
203 /** Returns the Ith character of the String object.
204 @exception - Throws exception "OutofBounds" if the index
205 is outside the length of the string.
206 <pre>
207 String t1 = "abc;
208 Char16 c = t1[1]; // character b
209 </pre>
210 */
211 Char16& operator[](Uint32 i);
212
213 /** Returns the Ith character of the String (const version).
214 @exception - Throws exception "OutofBounds" if the index
215 is outside the length of the string.
216
217 */
218 const Char16 operator[](Uint32 i) const;
219
220 /** Append the given character to the string.
221 <pre>
222 String s4 = "Hello";
223 mike 1.38 s4.append(Char16(0x0000))
224 </pre>
225 */
226 String& append(const Char16& c);
227
228 /// Append n characters from str to this String object.
229 String& append(const Char16* str, Uint32 n);
230
231 /// Append the characters of str to this String object.
232 String& append(const String& str)
233 {
234 return append(str.getData(), str.size());
235 }
236
237 /** Overload operator += appends the parameter String to this String.
238 @param String to append.
239 @return This String
240 <pre>
241 String test = "abc";
242 test += "def";
243 assert(test == "abcdef");
244 mike 1.38 </pre>
245 */
246 String& operator+=(const String& x)
247 {
248 return append(x);
249 }
250
251 /** Append the character given by c to this String object.
252 @param c Single character to be appended
253 @return String with appended character
254 */
255 String& operator+=(Char16 c)
256 {
257 return append(c);
258 }
259
260 /** Append the character given by c to this string.
261 <pre>
262 String t1 = "abc";
263 t1 += 'd'
264 assert(t1 == "abcd");
265 mike 1.38 </pre>
266 */
267 String& operator+=(char c)
268 {
269 return append(Char16(c));
270 }
271
272 /** Remove size characters from the string starting at the given
273 position. If size is PEG_NOT_FOUND, then all characters after pos are
274 removed.
275 @param pos Position in string to start remove
276 @param size Number of characters to remove. Default is PEG_NOT_FOUND
277 (Uint32(-1) which causes all characters after pos to be removed
278 <pre>
279 String s;
280 s = "abc";
281 s.remove(0, 1);
282 assert(String::equal(s, "bc"));
283 assert(s.size() == 2);
284 s.remove(0);
285 assert(String::equal(s, ""));
286 mike 1.38 assert(s.size() == 0);
287 </pre>
288 @exception throws "OutOfBounds" exception if size is greater than
289 length of String plus starting position for remove.
290 */
291 void remove(Uint32 pos, Uint32 size = PEG_NOT_FOUND);
292
293 /** Return a new String which is initialzed with <TT>length</TT>
294 characters from this string starting at <TT>pos</TT>.
295 @param <TT>pos</TT> is the positon in string to start getting the
296 substring.
297 @param <TT>length</TT> is the number of characters to get. If length
298 is PEG_NOT_FOUND, then all characters after pos are added to the new
299 string.
300 @return String with the defined substring.
301 <pre>
302 s = "abcdefg";
303 s.remove(3);
304 assert(String::equal(s, "abc"));
305 </pre>
306 */
307 mike 1.38 String subString(Uint32 pos, Uint32 length = PEG_NOT_FOUND) const;
308
309 /** Find the position of the first occurence of the character c.
310 If the character is not found, PEG_NOT_FOUND is returned.
311 @param c Char to be found in the String
312 @return Position of the character in the string or PEG_NOT_FOUND if not
313 found.
314 */
315 Uint32 find(Char16 c) const;
316
|
317 mike 1.41 /** Same as above but starts searching from the given position. */
318 Uint32 find(Uint32 pos, Char16 c) const;
|
319 mike 1.38
320 /** Find the position of the first occurence of the string object.
321 This function finds one string inside another
322 If the matching substring is not found, PEG_NOT_FOUND is returned.
323 @param s String object to be found in the String
324 @return Position of the substring in the String or PEG_NOT_FOUND if not
325 found.
326 */
327 Uint32 find(const String& s) const;
328
329 /** Find substring
330 @ param 16 bit character pointer
331 @seealso find
332 @return Position of the substring in the String or PEG_NOT_FOUND if not
333 found.
334 */
335 Uint32 find(const Char16* s) const;
336
337 /** find substring
338 @param s char* to substring
339 @return Position of the substring in the String or PEG_NOT_FOUND if not
340 mike 1.38 found.
341 */
342 Uint32 find(const char* s) const;
343
344 /** reverseFind - Same as find() but start looking in reverse (last
345 character first).
346 @param c Char16 character to find in String.
347 @Seealso find
348 @return Position of the character in the string or PEG_NOT_FOUND if not
349 found.
350
351 NOTE: This function is defined only for char* input, not for
352 String.
353 */
354 Uint32 reverseFind(Char16 c) const;
355
356 /** Converts all characters in this string to lower case.
357 */
358 void toLower();
359
360 /** Translate any occurences of fromChar to toChar.
361 mike 1.38 */
362 void translate(Char16 fromChar, Char16 toChar);
363
|
364 mike 1.39 /** Method for printing a string.
365 */
366 void print() const;
367
|
368 mike 1.38 /** Compare the first n characters of the two strings..
369 @param s1 First null-terminated string for the comparison.
370 @param s2 Second null-terminated string for the comparison.
371 @param n Number of characters to compare.
372 @return Return -1 if s1 is lexographically less than s2. If they are
373 equavalent return 0. Otherwise return 1.
374 */
375 static int compare(const Char16* s1, const Char16* s2, Uint32 n);
376
377 /** Just like one above except ignores case differences.
378 */
379 static int compareNoCase(const char* s1, const char* s2, Uint32 n);
380
|
381 mike 1.41 static int compareNoCase(const char* s1, const char* s2);
382
|
383 mike 1.38 /** Compare two null-terminated strings.
384 @param s1 First null-terminated string for the comparison.
385 @param s2 Second null-terminated string for the comparison.
386 @return If s1 is less than s2, return -1; if equal return 0;
387 otherwise, return 1.
388
389 NOTE: Use the comparison operators <,<= > >= to compare
390 String objects.
391 */
392 static int compare(const Char16* s1, const Char16* s2);
393
394 /** Compare two String objects for equality.
395 @param s1 First <TT>String</TT> for comparison.
396 @param s2 Second <TT>String</TT> for comparison.
397
398 @return Boolean true if the two strings are equal.
399 <pre>
400 String s1 = "Hello World";
401 String s2 = s1;
402 String s3(s2);
403 assert(String::equal(s1, s3));
404 mike 1.38 </pre>
405 */
406 static Boolean equal(const String& x, const String& y);
407
408 /// Return true if the two strings are equal.
409 static Boolean equal(const String& x, const Char16* y);
410
411 /// Return true if the two strings are equal.
412 static Boolean equal(const Char16* x, const String& y);
413
414 /// Return true if the two strings are equal.
415 static Boolean equal(const String& x, const char* y);
416
417 /// Return true if the two strings are equal.
418 static Boolean equal(const char* x, const String& y);
419
420 static Boolean equalNoCase(const String& x, const String& y);
421
422 /// Convert the plain old C-string to lower case:
423 static void toLower(char* str);
424
425 mike 1.38 /** EMPTY - Represent an empty string.
426 This member is used to represent empty strings. Using this member
427 avoids an expensive construction of an empty string (e.g., String()).
428 */
429 static const String EMPTY;
430
|
431 mike 1.41 /** Return true if the str parameter matches the pattern. C-Shell style
432 glob matching is used.
|
433 karl 1.43 @param str String to be matched against the pattern
434 @param pattern Pattern to use in the match
435 @return Boolean true if str matches pattern
436 The pattern definition is as follows:
437 <pre>
438 * Matches any number of any characters
439 ? Match exactly one character
440 [chars] Match any character in chars
441 [chara-charb] Match any character in the range between chara and charb
442 </pre>
443 The literal characters *, ?, [, ] can be included in a string by
444 escaping them with backslash "\". Ranges of characters can be concatenated.
445 <pre>
446 Boolean result = String::match("This is a test", "*is*");
447 Boolean works = String::match("abcdef123", "*[0-9]");
448 </pre>
|
449 mike 1.41 */
450 static Boolean match(const String& str, const String& pattern);
451
452 /** Return true if the str parameter matches the pattern. C-Shell style
453 glob matching is used. Ignore case in all comparisons.
454 */
455 static Boolean matchNoCase(const String& str, const String& pattern);
|
456 mike 1.40
|
457 mike 1.38 private:
458
|
459 mike 1.42 static Uint32 _pegasusMin(Uint32 x, Uint32 y) { return x < y ? x : y; }
|
460 mike 1.41
|
461 mike 1.38 Array<Char16> _rep;
462 };
|
463 mike 1.40
|
464 mike 1.38 /** String operator ==. Test for equality between two strings of any of the
465 types String or char*.
466 @return Boolean - True if the strings are equal
467
468 */
469 inline Boolean operator==(const String& x, const String& y)
470 {
471 return String::equal(x, y);
472 }
473
474 /** String operator ==. Test for equality between two strings
475
476 */
477 inline Boolean operator==(const String& x, const char* y)
478 {
479 return String::equal(x, y);
480 }
481
482 /** String operator ==. Test for equality between two strings
483
484 */
485 mike 1.38 inline Boolean operator==(const char* x, const String& y)
486 {
487 return String::equal(x, y);
488 }
489
490 /** String operator ==. Test for equality between two strings
491
492 */
493 inline Boolean operator!=(const String& x, const String& y)
494 {
495 return !String::equal(x, y);
496 }
497
498 PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
499 PEGASUS_STD(ostream)& os,
500 const String& x);
501
502 /** overload operator + - Concatenates String objects.
503
504 <pre>
505 String t1 = "abc";
506 mike 1.38 String t2;
507 t2 = t1 + "def"
508 assert(t2 == "abcdef");
509 </pre>
510 */
511 inline String operator+(const String& x, const String& y)
512 {
513 return String(x).append(y);
514 }
515
516 /** overload operator < - Compares String obects.
517 <pre>
518 String t1 = "def";
519 String t2 = "a";
520 assert (t2 < t1);
521 </pre>
522 */
523 inline Boolean operator<(const String& x, const String& y)
524 {
525 return String::compare(x.getData(), y.getData()) < 0;
526 }
527 mike 1.38
528 /** overload operator <= compares String objects.
529
530 */
531 inline Boolean operator<=(const String& x, const String& y)
532 {
533 return String::compare(x.getData(), y.getData()) <= 0;
534 }
535
536 /** Overload operator > compares String objects
537 */
538 inline Boolean operator>(const String& x, const String& y)
539 {
540 return String::compare(x.getData(), y.getData()) > 0;
541 }
542
543 /** overload operator >= - Compares String objects
544 */
545 inline Boolean operator>=(const String& x, const String& y)
546 {
547 return String::compare(x.getData(), y.getData()) >= 0;
548 mike 1.38 }
549
550 /** Return a version of this string whose characters have been shifted
551 to lower case.
552 */
553 PEGASUS_COMMON_LINKAGE String ToLower(const String& str);
554
555 /** Compare two strings but ignore any case differences.
556 */
557 PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2);
|
558 mike 1.41
559 inline int EqualNoCase(const char* s1, const char* s2)
560 {
561 return CompareNoCase(s1, s2) == 0;
562 }
|
563 mike 1.38
564 /** Get the next line from the input file.
565 */
566 PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line);
567
568 /* This is an internal class to be used by the internal Pegasus
569 components only. It provides an easy way to create an 8-bit string
570 representation on the fly without calling allocateCString() and
571 then worrying about deleting the string. The underscore before the
572 class name denotes that this class is internal, unsupported, undocumented,
573 and may be removed in future releases.
574 */
575 class _CString
576 {
577 public:
578
579 _CString(const String& x)
580 {
581 _rep = x.allocateCString();
582 }
583
584 mike 1.38 _CString(const _CString& x)
585 {
586 _rep = strcpy(new char[strlen(x._rep) + 1], x._rep);
587 }
588
589 ~_CString()
590 {
591 delete [] _rep;
592 }
593
594 _CString& operator=(const _CString& x)
595 {
596 if (this != &x)
597 _rep = strcpy(new char[strlen(x._rep) + 1], x._rep);
598
599 return *this;
600 }
601
602 operator const char*() const
603 {
604 return _rep;
605 mike 1.38 }
606
607 const char* data() const
608 {
609 return _rep;
610 }
611
612 private:
613
614 char* _rep;
615 };
616
617 inline Uint32 _Length(const String& s1) { return s1.size(); }
618
619 inline Uint32 _Length(const char* s1) { return strlen(s1); }
620
621 inline Uint32 _Length(const char) { return 1; }
622
623 template<class S1, class S2>
624 inline String Cat(const S1& s1, const S2& s2)
625 {
626 mike 1.38 String tmp;
627 tmp.reserve(_Length(s1) + _Length(s2));
628 tmp.append(s1);
629 tmp.append(s2);
630 return tmp;
631 }
632
633 template<class S1, class S2, class S3>
634 inline String Cat(const S1& s1, const S2& s2, const S3& s3)
635 {
636 String tmp;
637 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3));
638 tmp.append(s1);
639 tmp.append(s2);
640 tmp.append(s3);
641 return tmp;
642 }
643
644 template<class S1, class S2, class S3, class S4>
645 inline String Cat(const S1& s1, const S2& s2, const S3& s3, const S4& s4)
646 {
647 mike 1.38 String tmp;
648 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4));
649 tmp.append(s1);
650 tmp.append(s2);
651 tmp.append(s3);
652 tmp.append(s4);
653 return tmp;
654 }
655
656 template<class S1, class S2, class S3, class S4, class S5>
657 inline String Cat(
658 const S1& s1,
659 const S2& s2,
660 const S3& s3,
661 const S4& s4,
662 const S5& s5)
663 {
664 String tmp;
665
666 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) +
667 _Length(s5));
668 mike 1.38
669 tmp.append(s1);
670 tmp.append(s2);
671 tmp.append(s3);
672 tmp.append(s4);
673 tmp.append(s5);
674
675 return tmp;
676 }
677
678 template<class S1, class S2, class S3, class S4, class S5, class S6>
679 inline String Cat(
680 const S1& s1,
681 const S2& s2,
682 const S3& s3,
683 const S4& s4,
684 const S5& s5,
685 const S6& s6)
686 {
687 String tmp;
688
689 mike 1.38 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) +
690 _Length(s5) + _Length(s6));
691
692 tmp.append(s1);
693 tmp.append(s2);
694 tmp.append(s3);
695 tmp.append(s4);
696 tmp.append(s5);
697 tmp.append(s6);
698
699 return tmp;
700 }
701
702 PEGASUS_COMMON_LINKAGE const Array<String>& EmptyStringArray();
703
704 PEGASUS_COMMON_LINKAGE Boolean Equal(const String& x, const String& y);
705
706 inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path)
707 {
708 char* tmpPath = path.allocateCString();
709 is.open(tmpPath);
710 mike 1.38 delete [] tmpPath;
711 return !!is;
712 }
713
714 inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path)
715 {
716 char* tmpPath = path.allocateCString();
717 os.open(tmpPath);
718 delete [] tmpPath;
719 return !!os;
720 }
721
722 inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path)
723 {
724 char* tmpPath = path.allocateCString();
725 os.open(tmpPath, PEGASUS_STD(ios::app));
726 delete [] tmpPath;
727 return !!os;
728 }
729
730 #define PEGASUS_ARRAY_T String
731 mike 1.38 #include <Pegasus/Common/ArrayInter.h>
732 #undef PEGASUS_ARRAY_T
733
734 PEGASUS_NAMESPACE_END
735
736 #endif /* Pegasus_String_h */
|