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 // Modified By:
26 //
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 mike 1.38 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 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 mike 1.38 /// Initialize with first n characters from x.
65 String(const String& x, Uint32 n);
66
67 /// Initialize with x.
68 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 mike 1.38 String t2 = t1;
86 </pre>
87 */
88 String& operator=(const String& x) { return assign(x); }
89
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 mike 1.38 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
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 mike 1.38 @param capacity defines the capacity in characters to reserve.
128 */
129 void reserve(Uint32 capacity);
130
131 /** 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
|
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
318 /** Find the position of the first occurence of the string object.
319 This function finds one string inside another
320 If the matching substring is not found, PEG_NOT_FOUND is returned.
321 @param s String object to be found in the String
322 @return Position of the substring in the String or PEG_NOT_FOUND if not
323 found.
324 */
325 Uint32 find(const String& s) const;
326
327 /** Find substring
328 mike 1.38 @ param 16 bit character pointer
329 @seealso find
330 @return Position of the substring in the String or PEG_NOT_FOUND if not
331 found.
332 */
333 Uint32 find(const Char16* s) const;
334
335 /** find substring
336 @param s char* to substring
337 @return Position of the substring in the String or PEG_NOT_FOUND if not
338 found.
339 */
340 Uint32 find(const char* s) const;
341
342 /** reverseFind - Same as find() but start looking in reverse (last
343 character first).
344 @param c Char16 character to find in String.
345 @Seealso find
346 @return Position of the character in the string or PEG_NOT_FOUND if not
347 found.
348
349 mike 1.38 NOTE: This function is defined only for char* input, not for
350 String.
351 */
352 Uint32 reverseFind(Char16 c) const;
353
354 /** Converts all characters in this string to lower case.
355 */
356 void toLower();
357
358 /** Translate any occurences of fromChar to toChar.
359 */
360 void translate(Char16 fromChar, Char16 toChar);
361
|
381 mike 1.38 /** Compare two null-terminated strings.
382 @param s1 First null-terminated string for the comparison.
383 @param s2 Second null-terminated string for the comparison.
384 @return If s1 is less than s2, return -1; if equal return 0;
385 otherwise, return 1.
386
387 NOTE: Use the comparison operators <,<= > >= to compare
388 String objects.
389 */
390 static int compare(const Char16* s1, const Char16* s2);
391
392 /** Compare two String objects for equality.
393 @param s1 First <TT>String</TT> for comparison.
394 @param s2 Second <TT>String</TT> for comparison.
395
396 @return Boolean true if the two strings are equal.
397 <pre>
398 String s1 = "Hello World";
399 String s2 = s1;
400 String s3(s2);
401 assert(String::equal(s1, s3));
402 mike 1.38 </pre>
403 */
404 static Boolean equal(const String& x, const String& y);
405
406 /// Return true if the two strings are equal.
407 static Boolean equal(const String& x, const Char16* y);
408
409 /// Return true if the two strings are equal.
410 static Boolean equal(const Char16* x, const String& y);
411
412 /// Return true if the two strings are equal.
413 static Boolean equal(const String& x, const char* y);
414
415 /// Return true if the two strings are equal.
416 static Boolean equal(const char* x, const String& y);
417
418 static Boolean equalNoCase(const String& x, const String& y);
419
420 /// Convert the plain old C-string to lower case:
421 static void toLower(char* str);
422
423 mike 1.38 /** EMPTY - Represent an empty string.
424 This member is used to represent empty strings. Using this member
425 avoids an expensive construction of an empty string (e.g., String()).
426 */
427 static const String EMPTY;
428
429 private:
430
431 static Uint32 _min(Uint32 x, Uint32 y) { return x < y ? x : y; }
432
433 Array<Char16> _rep;
434 };
435
436 /** String operator ==. Test for equality between two strings of any of the
437 types String or char*.
438 @return Boolean - True if the strings are equal
439
440 */
441 inline Boolean operator==(const String& x, const String& y)
442 {
443 return String::equal(x, y);
444 mike 1.38 }
445
446 /** String operator ==. Test for equality between two strings
447
448 */
449 inline Boolean operator==(const String& x, const char* y)
450 {
451 return String::equal(x, y);
452 }
453
454 /** String operator ==. Test for equality between two strings
455
456 */
457 inline Boolean operator==(const char* x, const String& y)
458 {
459 return String::equal(x, y);
460 }
461
462 /** String operator ==. Test for equality between two strings
463
464 */
465 mike 1.38 inline Boolean operator!=(const String& x, const String& y)
466 {
467 return !String::equal(x, y);
468 }
469
470 PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
471 PEGASUS_STD(ostream)& os,
472 const String& x);
473
474 /** overload operator + - Concatenates String objects.
475
476 <pre>
477 String t1 = "abc";
478 String t2;
479 t2 = t1 + "def"
480 assert(t2 == "abcdef");
481 </pre>
482 */
483 inline String operator+(const String& x, const String& y)
484 {
485 return String(x).append(y);
486 mike 1.38 }
487
488 /** overload operator < - Compares String obects.
489 <pre>
490 String t1 = "def";
491 String t2 = "a";
492 assert (t2 < t1);
493 </pre>
494 */
495 inline Boolean operator<(const String& x, const String& y)
496 {
497 return String::compare(x.getData(), y.getData()) < 0;
498 }
499
500 /** overload operator <= compares String objects.
501
502 */
503 inline Boolean operator<=(const String& x, const String& y)
504 {
505 return String::compare(x.getData(), y.getData()) <= 0;
506 }
507 mike 1.38
508 /** Overload operator > compares String objects
509 */
510 inline Boolean operator>(const String& x, const String& y)
511 {
512 return String::compare(x.getData(), y.getData()) > 0;
513 }
514
515 /** overload operator >= - Compares String objects
516 */
517 inline Boolean operator>=(const String& x, const String& y)
518 {
519 return String::compare(x.getData(), y.getData()) >= 0;
520 }
521
522 /** Return a version of this string whose characters have been shifted
523 to lower case.
524 */
525 PEGASUS_COMMON_LINKAGE String ToLower(const String& str);
526
527 /** Compare two strings but ignore any case differences.
528 mike 1.38 */
529 PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2);
530
531 /** Get the next line from the input file.
532 */
533 PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line);
534
535 /* This is an internal class to be used by the internal Pegasus
536 components only. It provides an easy way to create an 8-bit string
537 representation on the fly without calling allocateCString() and
538 then worrying about deleting the string. The underscore before the
539 class name denotes that this class is internal, unsupported, undocumented,
540 and may be removed in future releases.
541 */
542 class _CString
543 {
544 public:
545
546 _CString(const String& x)
547 {
548 _rep = x.allocateCString();
549 mike 1.38 }
550
551 _CString(const _CString& x)
552 {
553 _rep = strcpy(new char[strlen(x._rep) + 1], x._rep);
554 }
555
556 ~_CString()
557 {
558 delete [] _rep;
559 }
560
561 _CString& operator=(const _CString& x)
562 {
563 if (this != &x)
564 _rep = strcpy(new char[strlen(x._rep) + 1], x._rep);
565
566 return *this;
567 }
568
569 operator const char*() const
570 mike 1.38 {
571 return _rep;
572 }
573
574 const char* data() const
575 {
576 return _rep;
577 }
578
579 private:
580
581 char* _rep;
582 };
583
584 inline Uint32 _Length(const String& s1) { return s1.size(); }
585
586 inline Uint32 _Length(const char* s1) { return strlen(s1); }
587
588 inline Uint32 _Length(const char) { return 1; }
589
590 template<class S1, class S2>
591 mike 1.38 inline String Cat(const S1& s1, const S2& s2)
592 {
593 String tmp;
594 tmp.reserve(_Length(s1) + _Length(s2));
595 tmp.append(s1);
596 tmp.append(s2);
597 return tmp;
598 }
599
600 template<class S1, class S2, class S3>
601 inline String Cat(const S1& s1, const S2& s2, const S3& s3)
602 {
603 String tmp;
604 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3));
605 tmp.append(s1);
606 tmp.append(s2);
607 tmp.append(s3);
608 return tmp;
609 }
610
611 template<class S1, class S2, class S3, class S4>
612 mike 1.38 inline String Cat(const S1& s1, const S2& s2, const S3& s3, const S4& s4)
613 {
614 String tmp;
615 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4));
616 tmp.append(s1);
617 tmp.append(s2);
618 tmp.append(s3);
619 tmp.append(s4);
620 return tmp;
621 }
622
623 template<class S1, class S2, class S3, class S4, class S5>
624 inline String Cat(
625 const S1& s1,
626 const S2& s2,
627 const S3& s3,
628 const S4& s4,
629 const S5& s5)
630 {
631 String tmp;
632
633 mike 1.38 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) +
634 _Length(s5));
635
636 tmp.append(s1);
637 tmp.append(s2);
638 tmp.append(s3);
639 tmp.append(s4);
640 tmp.append(s5);
641
642 return tmp;
643 }
644
645 template<class S1, class S2, class S3, class S4, class S5, class S6>
646 inline String Cat(
647 const S1& s1,
648 const S2& s2,
649 const S3& s3,
650 const S4& s4,
651 const S5& s5,
652 const S6& s6)
653 {
654 mike 1.38 String tmp;
655
656 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) +
657 _Length(s5) + _Length(s6));
658
659 tmp.append(s1);
660 tmp.append(s2);
661 tmp.append(s3);
662 tmp.append(s4);
663 tmp.append(s5);
664 tmp.append(s6);
665
666 return tmp;
667 }
668
669 PEGASUS_COMMON_LINKAGE const Array<String>& EmptyStringArray();
670
671 PEGASUS_COMMON_LINKAGE Boolean Equal(const String& x, const String& y);
672
673 inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path)
674 {
675 mike 1.38 char* tmpPath = path.allocateCString();
676 is.open(tmpPath);
677 delete [] tmpPath;
678 return !!is;
679 }
680
681 inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path)
682 {
683 char* tmpPath = path.allocateCString();
684 os.open(tmpPath);
685 delete [] tmpPath;
686 return !!os;
687 }
688
689 inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path)
690 {
691 char* tmpPath = path.allocateCString();
692 os.open(tmpPath, PEGASUS_STD(ios::app));
693 delete [] tmpPath;
694 return !!os;
695 }
696 mike 1.38
697 #define PEGASUS_ARRAY_T String
698 #include <Pegasus/Common/ArrayInter.h>
699 #undef PEGASUS_ARRAY_T
700
701 PEGASUS_NAMESPACE_END
702
703 #endif /* Pegasus_String_h */
|