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 karl 1.46 /** match matches a string against a GLOB style pattern.
432 Return trues if the String parameter matches the pattern. C-Shell style
433 glob matching is used.
434 @param str String to be matched against the pattern
435 @param pattern Pattern to use in the match
436 @return Boolean true if str matches pattern
437 The pattern definition is as follows:
438 <pre>
439 * Matches any number of any characters
440 ? Match exactly one character
441 [chars] Match any character in chars
442 [chara-charb] Match any character in the range between chara and charb
443 </pre>
444 The literal characters *, ?, [, ] can be included in a string by
445 escaping them with backslash "\". Ranges of characters can be concatenated.
446 <pre>
447 examples:
448 Boolean result = String::match("This is a test", "*is*");
449 Boolean works = String::match("abcdef123", "*[0-9]");
450 </pre>
451 */
452 karl 1.46 static Boolean match(const String& str, const String& pattern);
453
454 /** matcHoCase Matchses a String against a GLOB style pattern independent
455 of case.
456 Returns true if the str parameter matches the pattern. C-Shell style
457 glob matching is used. Ignore case in all comparisons. Case is
458 ignored in the match.
459 @parm str String containing the string to be matched\
460 @parm pattern GLOB style patterh to use in the match.
461 @return Boolean true if str matches patterh
462 @SeeAlso match
463 */
464 static Boolean matchNoCase(const String& str, const String& pattern);
465
|
466 mike 1.38 private:
467
|
468 mike 1.42 static Uint32 _pegasusMin(Uint32 x, Uint32 y) { return x < y ? x : y; }
|
469 mike 1.41
|
470 mike 1.38 Array<Char16> _rep;
471 };
|
472 mike 1.40
|
473 mike 1.38 /** String operator ==. Test for equality between two strings of any of the
474 types String or char*.
475 @return Boolean - True if the strings are equal
476
477 */
478 inline Boolean operator==(const String& x, const String& y)
479 {
480 return String::equal(x, y);
481 }
482
483 /** String operator ==. Test for equality between two strings
484
485 */
486 inline Boolean operator==(const String& x, const char* y)
487 {
488 return String::equal(x, y);
489 }
490
491 /** String operator ==. Test for equality between two strings
492
493 */
494 mike 1.38 inline Boolean operator==(const char* x, const String& y)
495 {
496 return String::equal(x, y);
497 }
498
499 /** String operator ==. Test for equality between two strings
500
501 */
502 inline Boolean operator!=(const String& x, const String& y)
503 {
504 return !String::equal(x, y);
505 }
506
507 PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
508 PEGASUS_STD(ostream)& os,
509 const String& x);
510
511 /** overload operator + - Concatenates String objects.
512
513 <pre>
514 String t1 = "abc";
515 mike 1.38 String t2;
516 t2 = t1 + "def"
517 assert(t2 == "abcdef");
518 </pre>
519 */
520 inline String operator+(const String& x, const String& y)
521 {
522 return String(x).append(y);
523 }
524
525 /** overload operator < - Compares String obects.
526 <pre>
527 String t1 = "def";
528 String t2 = "a";
529 assert (t2 < t1);
530 </pre>
531 */
532 inline Boolean operator<(const String& x, const String& y)
533 {
534 return String::compare(x.getData(), y.getData()) < 0;
535 }
536 mike 1.38
537 /** overload operator <= compares String objects.
538
539 */
540 inline Boolean operator<=(const String& x, const String& y)
541 {
542 return String::compare(x.getData(), y.getData()) <= 0;
543 }
544
545 /** Overload operator > compares String objects
546 */
547 inline Boolean operator>(const String& x, const String& y)
548 {
549 return String::compare(x.getData(), y.getData()) > 0;
550 }
551
552 /** overload operator >= - Compares String objects
553 */
554 inline Boolean operator>=(const String& x, const String& y)
555 {
556 return String::compare(x.getData(), y.getData()) >= 0;
557 mike 1.38 }
558
559 /** Return a version of this string whose characters have been shifted
560 to lower case.
561 */
562 PEGASUS_COMMON_LINKAGE String ToLower(const String& str);
563
564 /** Compare two strings but ignore any case differences.
565 */
566 PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2);
|
567 mike 1.41
568 inline int EqualNoCase(const char* s1, const char* s2)
569 {
570 return CompareNoCase(s1, s2) == 0;
571 }
|
572 mike 1.38
573 /** Get the next line from the input file.
574 */
575 PEGASUS_COMMON_LINKAGE Boolean GetLine(PEGASUS_STD(istream)& is, String& line);
576
577 /* This is an internal class to be used by the internal Pegasus
578 components only. It provides an easy way to create an 8-bit string
579 representation on the fly without calling allocateCString() and
580 then worrying about deleting the string. The underscore before the
581 class name denotes that this class is internal, unsupported, undocumented,
582 and may be removed in future releases.
583 */
584 class _CString
585 {
586 public:
587
588 _CString(const String& x)
589 {
590 _rep = x.allocateCString();
591 }
592
593 mike 1.38 _CString(const _CString& x)
594 {
595 _rep = strcpy(new char[strlen(x._rep) + 1], x._rep);
596 }
597
598 ~_CString()
599 {
600 delete [] _rep;
601 }
602
603 _CString& operator=(const _CString& x)
604 {
605 if (this != &x)
606 _rep = strcpy(new char[strlen(x._rep) + 1], x._rep);
607
608 return *this;
609 }
610
611 operator const char*() const
612 {
613 return _rep;
614 mike 1.38 }
615
616 const char* data() const
617 {
618 return _rep;
619 }
620
621 private:
622
623 char* _rep;
624 };
625
626 inline Uint32 _Length(const String& s1) { return s1.size(); }
627
628 inline Uint32 _Length(const char* s1) { return strlen(s1); }
629
630 inline Uint32 _Length(const char) { return 1; }
631
632 template<class S1, class S2>
633 inline String Cat(const S1& s1, const S2& s2)
634 {
635 mike 1.38 String tmp;
636 tmp.reserve(_Length(s1) + _Length(s2));
637 tmp.append(s1);
638 tmp.append(s2);
639 return tmp;
640 }
641
642 template<class S1, class S2, class S3>
643 inline String Cat(const S1& s1, const S2& s2, const S3& s3)
644 {
645 String tmp;
646 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3));
647 tmp.append(s1);
648 tmp.append(s2);
649 tmp.append(s3);
650 return tmp;
651 }
652
653 template<class S1, class S2, class S3, class S4>
654 inline String Cat(const S1& s1, const S2& s2, const S3& s3, const S4& s4)
655 {
656 mike 1.38 String tmp;
657 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4));
658 tmp.append(s1);
659 tmp.append(s2);
660 tmp.append(s3);
661 tmp.append(s4);
662 return tmp;
663 }
664
665 template<class S1, class S2, class S3, class S4, class S5>
666 inline String Cat(
667 const S1& s1,
668 const S2& s2,
669 const S3& s3,
670 const S4& s4,
671 const S5& s5)
672 {
673 String tmp;
674
675 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) +
676 _Length(s5));
677 mike 1.38
678 tmp.append(s1);
679 tmp.append(s2);
680 tmp.append(s3);
681 tmp.append(s4);
682 tmp.append(s5);
683
684 return tmp;
685 }
686
687 template<class S1, class S2, class S3, class S4, class S5, class S6>
688 inline String Cat(
689 const S1& s1,
690 const S2& s2,
691 const S3& s3,
692 const S4& s4,
693 const S5& s5,
694 const S6& s6)
695 {
696 String tmp;
697
698 mike 1.38 tmp.reserve(_Length(s1) + _Length(s2) + _Length(s3) + _Length(s4) +
699 _Length(s5) + _Length(s6));
700
701 tmp.append(s1);
702 tmp.append(s2);
703 tmp.append(s3);
704 tmp.append(s4);
705 tmp.append(s5);
706 tmp.append(s6);
707
708 return tmp;
709 }
710
711 PEGASUS_COMMON_LINKAGE const Array<String>& EmptyStringArray();
712
713 PEGASUS_COMMON_LINKAGE Boolean Equal(const String& x, const String& y);
714
715 inline Boolean Open(PEGASUS_STD(ifstream)& is, const String& path)
716 {
717 char* tmpPath = path.allocateCString();
718 is.open(tmpPath);
719 mike 1.38 delete [] tmpPath;
720 return !!is;
721 }
722
723 inline Boolean Open(PEGASUS_STD(ofstream)& os, const String& path)
724 {
725 char* tmpPath = path.allocateCString();
726 os.open(tmpPath);
727 delete [] tmpPath;
728 return !!os;
729 }
730
731 inline Boolean OpenAppend(PEGASUS_STD(ofstream)& os, const String& path)
732 {
733 char* tmpPath = path.allocateCString();
734 os.open(tmpPath, PEGASUS_STD(ios::app));
735 delete [] tmpPath;
736 return !!os;
737 }
738
739 #define PEGASUS_ARRAY_T String
740 mike 1.38 #include <Pegasus/Common/ArrayInter.h>
741 #undef PEGASUS_ARRAY_T
742
743 PEGASUS_NAMESPACE_END
744
745 #endif /* Pegasus_String_h */
|