(file) Return to String.h CVS log (file) (dir) Up to [Pegasus] / pegasus / src / Pegasus / Common

  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 kumpf 1.48 //              Roger Kumpf, Hewlett-Packard Company (roger_kumpf@hp.com)
 27 mike  1.38 //
 28            //%/////////////////////////////////////////////////////////////////////////////
 29            
 30            #ifndef Pegasus_String_h
 31            #define Pegasus_String_h
 32            
 33            #include <iostream>
 34            #include <fstream>
 35            #include <cstring>
 36            #include <Pegasus/Common/Config.h>
 37            #include <Pegasus/Common/Char16.h>
 38            #include <Pegasus/Common/Array.h>
 39            
 40            PEGASUS_NAMESPACE_BEGIN
 41            
 42            /**
 43                The Pegasus String C++ Class implements the CIM string type.
 44                This class is based on the general handle/representation pattern
 45                defined for all the Pegasus objects.  However, it differes from
 46                most in that it implements "copy on assign" all of the others implement
 47                "no copy on assign" semantics. The string class uses the Array class and
 48 mike  1.38     implements an array of characters.
 49            */
 50            class PEGASUS_COMMON_LINKAGE String
 51            {
 52            public:
 53            
 54 kumpf 1.48     /**	EMPTY - Represent an empty string.
 55            	This member is used to represent empty strings. Using this member
 56            	avoids an expensive construction of an empty string (e.g., String()).
 57                */
 58                static const String EMPTY;
 59            
 60 mike  1.38     /** Default constructor without parameters. This constructor creates a
 61            	null string
 62            	<pre>
 63            	    String test;
 64            	</pre>
 65                */
 66                String();
 67            
 68                /// Copy constructor.
 69 kumpf 1.48     String(const String& str);
 70 mike  1.38 
 71 kumpf 1.48     /// Initialize with first n characters from str.
 72                String(const String& str, Uint32 n);
 73 mike  1.38 
 74 kumpf 1.48     /// Initialize with str.
 75                String(const Char16* str);
 76 mike  1.38 
 77 kumpf 1.48     /// Initialize with first n characters of str.
 78                String(const Char16* str, Uint32 n);
 79 mike  1.38 
 80                /// Initialize from a plain old C-String:
 81 kumpf 1.48     String(const char* str);
 82 mike  1.38 
 83                /// Initialize from the first n characters of a plain old C-String:
 84 kumpf 1.48     String(const char* str, Uint32 n);
 85 mike  1.38 
 86                /// String destructor. Used by the representation of the String object
 87                ~String();
 88            
 89 kumpf 1.48     /** Assign this string with str.
 90 mike  1.38 	<pre>
 91            	    String t1 = "abc";
 92            	    String t2 = t1;
 93            	</pre>
 94                */
 95 kumpf 1.48     String& operator=(const String& str);
 96 mike  1.38 
 97 kumpf 1.48     /// Assign this string with Char16 str.
 98                String& operator=(const Char16* str);
 99 mike  1.38 
100 kumpf 1.48     /** Assign this string with String str
101                @param str String to assign
102 mike  1.38     @return Returns the String
103                */
104 kumpf 1.48     String& assign(const String& str);
105 mike  1.38 
106 kumpf 1.48     /// Assign this string with str.
107                String& assign(const Char16* str);
108 mike  1.38 
109 kumpf 1.48     /// Assign this string with first n characters of str.
110                String& assign(const Char16* str, Uint32 n);
111 mike  1.38 
112 kumpf 1.48     /// Assign this string with the plain old C-String str.
113                String& assign(const char* str);
114 mike  1.38 
115 kumpf 1.48     /// Assign this string with first n characters of the plain old C-String str.
116                String& assign(const char* str, Uint32 n);
117 mike  1.38 
118                /** clear - Clear this string. After calling clear(), size() will return 0.
119            	<pre>
120            	    String test = "abc";
121            	    test.clear();	// String test is now NULL (length == 0)
122            	</pre>
123                */
124                void clear();
125            
126            
127                /** reserve - Reserves memory for capacity characters. Notice that this does
128                not
129            	change the size of the string (size() returns what it did before).
130            	If the capacity of the string is already greater or equal to the
131            	capacity argument, this method has no effect. After calling reserve(),
132            	getCapicty() returns a value which is greater or equal to the
133            	capacity argument.
134            	@param capacity defines the capacity in characters to reserve.
135                */
136                void reserve(Uint32 capacity);
137            
138 mike  1.38     /** Returns the length of the String object.
139            	@return Length of the string in characters.
140            	<pre>
141            	    String s = "abcd";
142            	    assert(s.size() == 4);
143            	</pre>
144                */
145 kumpf 1.48     Uint32 size() const;
146 mike  1.38 
147 mike  1.41     /** getData Returns a pointer to the first character in the 
148            	null-terminated string string of the String object.
149 mike  1.38 	@return	Pointer to the first character of the String object
150                	<pre>
151            	    String t1 = "abc";
152            	    const Char16* q = t1.getData();
153            	</pre>
154                */
155 kumpf 1.48     const Char16* getData() const;
156 mike  1.38 
157 kumpf 1.48     /** allocateCString - allocates an 8 bit representation of this String 
158 mike  1.41 	object. The user is responsible for freeing the result. If any 
159            	characters are truncated, a TruncatedCharacter exception is thrown.
160            	This exception may be suppressed by passing true as the noThrow 
161            	argument. Extra characters may be allocated at the end of the
162            	new string by passing a non-zero value to the extraBytes argument.
163            	
164 mike  1.38 	@param extraBytes Defines the number of extra characters to be
165            	allocated at the end of the new string. Default is zero.
166 mike  1.41 	
167 mike  1.38 	@param	noThrow If true, no exception will be thrown if characters
168            	are truncated
169 mike  1.41 	
170 mike  1.38 	@return pointer to the new representation of the string
171 mike  1.41 	
172 mike  1.38 	@exception Throws TruncatedCharacter exception if any characters are
173            	truncated
174            	<pre>
175            	    String test = "abc";
176            	    char* p = test.allocateCString();
177            	    ...
178            	    delete [] p;
179            	</pre>
180                */
181                char* allocateCString(Uint32 extraBytes = 0, Boolean noThrow = false) const;
182            
183 mike  1.41     /** appendToCString - Append the given String object to a C-string. If the 
184            	length is not PEG_NOT_FOUND, then the lesser of the the length argument
185            	and he length of this string is truncated.  Otherwise, the entire string
186            	is trunctated.  The TruncatedCharacter exception is thrown if any 
187            	characters are truncated.  
188 mike  1.38     	@param str Char pointer to the string to append
189                	@param length Length to append or PEG_NOT_FOUND (Uint32(-1)
190                	@param noThrow - If false, throw the "TruncatedCharacter" exception of
191                	any characters are truncated
192                	@return void
193                	@exception Throws TruncatedCharacter exception of characters are
194                	truncated and noThrow parameter is false.
195            	<pre>
196            	    const char STR0[] = "one two three four";
197            	    String s = STR0;
198            	    const char STR1[] = "zero ";
199            	    char* tmp = new char[strlen(STR1) + s.size() + 1];
200            	    strcpy(tmp, STR1);
201            	    s.appendToCString(tmp, 7);
202            	    assert(strcmp(tmp, "zero one two") == 0);
203            	</pre>
204                */
205                void appendToCString(
206            	char* str,
207            	Uint32 length = PEG_NOT_FOUND,
208            	Boolean noThrow = false) const;
209 mike  1.38 
210                /** Returns the Ith character of the String object.
211            	@exception - Throws exception "OutofBounds" if the index
212            	is outside the length of the string.
213            	<pre>
214            	    String t1 = "abc;
215            	    Char16 c = t1[1];	// character b
216            	</pre>
217                */
218                Char16& operator[](Uint32 i);
219            
220                /** Returns the Ith character of the String (const version).
221            	@exception - Throws exception "OutofBounds" if the index
222            	is outside the length of the string.
223            
224                */
225                const Char16 operator[](Uint32 i) const;
226            
227                /** Append the given character to the string.
228                    <pre>
229            	     String s4 = "Hello";
230 mike  1.38 	    s4.append(Char16(0x0000))
231            	</pre>
232                */
233                String& append(const Char16& c);
234            
235                /// Append n characters from str to this String object.
236                String& append(const Char16* str, Uint32 n);
237            
238                /// Append the characters of str to this String object.
239 kumpf 1.48     String& append(const String& str);
240 mike  1.38 
241                /** Overload operator += appends the parameter String to this String.
242            	@param String to append.
243            	@return This String
244            	<pre>
245            	String test = "abc";
246            	test += "def";
247            	assert(test == "abcdef");
248            	</pre>
249                */
250 kumpf 1.48     String& operator+=(const String& str);
251 mike  1.38 
252                /** Append the character given by c to this String object.
253            	@param c Single character to be appended
254            	@return String with appended character
255                */
256 kumpf 1.48     String& operator+=(Char16 c);
257 mike  1.38 
258                /** Append the character given by c to this string.
259            	<pre>
260            	    String t1 = "abc";
261            	    t1 += 'd'
262            	    assert(t1 == "abcd");
263            	</pre>
264                */
265 kumpf 1.48     String& operator+=(char c);
266 mike  1.38 
267                /** Remove size characters from the string starting at the given
268            	position. If size is PEG_NOT_FOUND, then all characters after pos are
269            	removed.
270            	@param pos Position in string to start remove
271            	@param size Number of characters to remove. Default is PEG_NOT_FOUND
272            	(Uint32(-1) which causes all characters after pos to be removed
273            	<pre>
274            	    String s;
275            	    s = "abc";
276            	    s.remove(0, 1);
277            	    assert(String::equal(s, "bc"));
278            	    assert(s.size() == 2);
279            	    s.remove(0);
280            	    assert(String::equal(s, ""));
281            	    assert(s.size() == 0);
282            	</pre>
283            	@exception throws "OutOfBounds" exception if size is greater than
284            	length of String plus starting position for remove.
285                */
286                void remove(Uint32 pos, Uint32 size = PEG_NOT_FOUND);
287 mike  1.38 
288                /** Return a new String which is initialzed with <TT>length</TT>
289            	characters from this string starting at <TT>pos</TT>.
290            	@param <TT>pos</TT> is the positon in string to start getting the
291            	substring.
292            	@param <TT>length</TT> is the number of characters to get. If length
293            	is PEG_NOT_FOUND, then all characters after pos are added to the new
294            	string.
295            	@return String with the defined substring.
296            	<pre>
297            	    s = "abcdefg";
298            	    s.remove(3);
299            	    assert(String::equal(s, "abc"));
300            	</pre>
301                */
302                String subString(Uint32 pos, Uint32 length = PEG_NOT_FOUND) const;
303            
304                /** Find the position of the first occurence of the character c.
305            	If the character is not found, PEG_NOT_FOUND is returned.
306            	@param c Char to be found in the String
307            	@return Position of the character in the string or PEG_NOT_FOUND if not
308 mike  1.38 	found.
309                */
310                Uint32 find(Char16 c) const;
311            
312 mike  1.41     /** Same as above but starts searching from the given position. */
313                Uint32 find(Uint32 pos, Char16 c) const;
314 mike  1.38 
315                /** Find the position of the first occurence of the string object.
316            	This function finds one string inside another
317            	If the matching substring is not found, PEG_NOT_FOUND is returned.
318            	@param s String object to be found in the String
319            	@return Position of the substring in the String or PEG_NOT_FOUND if not
320            	found.
321                */
322                Uint32 find(const String& s) const;
323            
324                /** Find substring
325            	@ param 16 bit character pointer
326            	@seealso find
327            	@return Position of the substring in the String or PEG_NOT_FOUND if not
328            	found.
329                */
330                Uint32 find(const Char16* s) const;
331            
332                /** find substring
333            	@param s char* to substring
334            	@return Position of the substring in the String or PEG_NOT_FOUND if not
335 mike  1.38 	found.
336                */
337                Uint32 find(const char* s) const;
338            
339                /** reverseFind - Same as find() but start looking in reverse (last
340                character first).
341                	@param c Char16 character to find in String.
342            	@Seealso find
343            	@return Position of the character in the string or PEG_NOT_FOUND if not
344            	found.
345            
346            	NOTE: This function is defined only for char* input, not for
347            	String.
348                */
349                Uint32 reverseFind(Char16 c) const;
350            
351 kumpf 1.49 #ifdef PEGASUS_INTERNALONLY
352                // ATTN-RK-P3-20020509: Define case-sensitivity for non-English characters
353 mike  1.38     /** Converts all characters in this string to lower case.
354                */
355                void toLower();
356            
357 kumpf 1.48     /// Convert the plain old C-string to lower case:
358                static void toLower(char* str);
359 kumpf 1.49 #endif
360 kumpf 1.48 
361 mike  1.38     /** Translate any occurences of fromChar to toChar.
362                */
363                void translate(Char16 fromChar, Char16 toChar);
364            
365 mike  1.39     /** Method for printing a string.
366                */
367                void print() const;
368            
369 mike  1.38     /** Compare the first n characters of the two strings..
370                	@param s1 First null-terminated string for the comparison.
371            	@param s2 Second null-terminated string for the comparison.
372            	@param n Number of characters to compare.
373            	@return Return -1 if s1 is lexographically less than s2. If they are
374            	equavalent return 0. Otherwise return 1.
375                */
376                static int compare(const Char16* s1, const Char16* s2, Uint32 n);
377            
378                /** Compare two null-terminated strings.
379                	@param s1 First null-terminated string for the comparison.
380            	@param s2 Second null-terminated string for the comparison.
381            	@return If s1 is less than s2, return -1; if equal return 0;
382            	otherwise, return 1.
383            
384            	NOTE: Use the comparison operators <,<= > >= to compare
385            	String objects.
386                */
387                static int compare(const Char16* s1, const Char16* s2);
388            
389 kumpf 1.48     /** Just like one above except ignores case differences.
390                */
391                static int compareNoCase(const char* s1, const char* s2, Uint32 n);
392            
393                static int compareNoCase(const char* s1, const char* s2);
394 kumpf 1.49 
395            #ifdef PEGASUS_INTERNALONLY
396                static int compareNoCase(const String& s1, const String& s2);
397            #endif
398 kumpf 1.48 
399 mike  1.38     /** Compare two String objects for equality.
400            	@param s1 First <TT>String</TT> for comparison.
401            	@param s2 Second <TT>String</TT> for comparison.
402            
403            	@return Boolean true if the two strings are equal.
404            	<pre>
405            	    String s1 = "Hello World";
406            	    String s2 = s1;
407            	    String s3(s2);
408            	    assert(String::equal(s1, s3));
409            	</pre>
410                */
411 kumpf 1.48     static Boolean equal(const String& str1, const String& str2);
412 mike  1.38 
413                /// Return true if the two strings are equal.
414 kumpf 1.48     static Boolean equal(const String& str1, const Char16* str2);
415 mike  1.38 
416                /// Return true if the two strings are equal.
417 kumpf 1.48     static Boolean equal(const Char16* str1, const String& str2);
418 mike  1.38 
419                /// Return true if the two strings are equal.
420 kumpf 1.48     static Boolean equal(const String& str1, const char* str2);
421 mike  1.38 
422                /// Return true if the two strings are equal.
423 kumpf 1.48     static Boolean equal(const char* str1, const String& str2);
424 mike  1.38 
425 karl  1.47     /** equalNoCase - Compares two strings and returuns true if they
426            	are equal indpedent of case of the characters.
427 kumpf 1.48 	@param str1 First String parameter
428            	@param str2 Second String parameter
429 karl  1.47 	@return true if strings are equal independent of case.
430                */
431 kumpf 1.48     static Boolean equalNoCase(const String& str1, const String& str2);
432 mike  1.38 
433 karl  1.46     /** match matches a string against a GLOB style pattern.
434                    Return trues if the String parameter matches the pattern. C-Shell style
435            	glob matching is used.
436                    @param str String to be matched against the pattern
437                    @param pattern Pattern to use in the match
438                    @return Boolean true if str matches pattern
439                    The pattern definition is as follows:
440                    <pre>
441                    *             Matches any number of any characters
442                    ?             Match exactly one character
443                    [chars]       Match any character in chars
444                    [chara-charb] Match any character in the range between chara and charb
445                    </pre>
446                    The literal characters *, ?, [, ] can be included in a string by
447                    escaping them with backslash "\".  Ranges of characters can be concatenated.
448                    <pre>
449                    examples:
450                    Boolean result = String::match("This is a test", "*is*");
451                    Boolean works =  String::match("abcdef123", "*[0-9]");
452                    </pre>
453                */
454 karl  1.46     static Boolean match(const String& str, const String& pattern);
455            
456 kumpf 1.48     /** matchNoCase Matches a String against a GLOB style pattern independent
457 karl  1.46         of case. 
458                    Returns true if the str parameter matches the pattern. C-Shell style
459            	glob matching is used. Ignore case in all comparisons. Case is
460                    ignored in the match.
461                    @parm str String containing the string to be matched\
462                    @parm pattern GLOB style patterh to use in the match.
463                    @return Boolean true if str matches patterh
464                    @SeeAlso match
465                */
466                static Boolean matchNoCase(const String& str, const String& pattern);
467            
468 mike  1.38 private:
469            
470                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 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(
478                const String& str1,
479                const String& str2);
480 mike  1.38 
481            /** String operator ==. Test for equality between two strings
482            
483            */
484 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(const String& str1, const char* str2);
485 mike  1.38 
486            /** String operator ==. Test for equality between two strings
487            
488            */
489 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator==(const char* str1, const String& str2);
490 mike  1.38 
491            /** String operator ==. Test for equality between two strings
492            
493            */
494 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator!=(
495                const String& str1,
496                const String& str2);
497 mike  1.38 
498            PEGASUS_COMMON_LINKAGE PEGASUS_STD(ostream)& operator<<(
499                PEGASUS_STD(ostream)& os,
500 kumpf 1.48     const String& str);
501 mike  1.38 
502            /** overload operator +	 - Concatenates String objects.
503            
504                <pre>
505            	String t1 = "abc";
506            	String t2;
507            	t2 = t1 + "def"
508            	assert(t2 == "abcdef");
509                </pre>
510            */
511 kumpf 1.48 PEGASUS_COMMON_LINKAGE String operator+(const String& str1, const String& str2);
512 mike  1.38 
513            /** overload operator < - Compares String obects.
514                <pre>
515            	String t1 = "def";
516            	String t2 = "a";
517            	assert (t2 < t1);
518                </pre>
519            */
520 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator<(
521                const String& str1,
522                const String& str2);
523 mike  1.38 
524            /** overload operator <= compares String objects.
525            
526            */
527 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator<=(
528                const String& str1,
529                const String& str2);
530 mike  1.38 
531            /** Overload operator > compares String objects
532            */
533 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator>(
534                const String& str1,
535                const String& str2);
536 mike  1.38 
537            /** overload operator >= - Compares String objects
538            */
539 kumpf 1.48 PEGASUS_COMMON_LINKAGE Boolean operator>=(
540                const String& str1,
541                const String& str2);
542 mike  1.38 
543            /** Compare two strings but ignore any case differences.
544            */
545            PEGASUS_COMMON_LINKAGE int CompareNoCase(const char* s1, const char* s2);
546 mike  1.41 
547 kumpf 1.48 PEGASUS_COMMON_LINKAGE int EqualNoCase(const char* s1, const char* s2);
548 mike  1.38 
549 kumpf 1.48 #ifdef PEGASUS_INTERNALONLY
550 mike  1.38 /*  This is an internal class to be used by the internal Pegasus
551                components only. It provides an easy way to create an 8-bit string
552                representation on the fly without calling allocateCString() and
553                then worrying about deleting the string. The underscore before the
554                class name denotes that this class is internal, unsupported, undocumented,
555                and may be removed in future releases.
556            */
557            class _CString
558            {
559            public:
560            
561 kumpf 1.48     _CString(const String& str)
562 mike  1.38     {
563 kumpf 1.48 	_rep = str.allocateCString();
564 mike  1.38     }
565            
566 kumpf 1.48     _CString(const _CString& str)
567 mike  1.38     {
568 kumpf 1.48 	_rep = strcpy(new char[strlen(str._rep) + 1], str._rep);
569 mike  1.38     }
570            
571                ~_CString()
572                {
573            	delete [] _rep;
574                }
575            
576 kumpf 1.48     _CString& operator=(const _CString& str)
577 mike  1.38     {
578 kumpf 1.48 	if (this != &str)
579            	    _rep = strcpy(new char[strlen(str._rep) + 1], str._rep);
580 mike  1.38 
581            	return *this;
582                }
583            
584                operator const char*() const
585                {
586            	return _rep;
587                }
588            
589                const char* data() const
590                {
591            	return _rep;
592                }
593            
594            private:
595            
596                char* _rep;
597            };
598 kumpf 1.48 #endif
599 mike  1.38 
600            #define PEGASUS_ARRAY_T String
601            #include <Pegasus/Common/ArrayInter.h>
602            #undef PEGASUS_ARRAY_T
603            
604            PEGASUS_NAMESPACE_END
605            
606            #endif /* Pegasus_String_h */

No CVS admin address has been configured
Powered by
ViewCVS 0.9.2