home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
Chip 1997 October
/
Chip_1997-10_cd.bin
/
tema
/
sybase
/
powerj
/
hpp.z
/
wsstring.hpp
< prev
next >
Wrap
C/C++ Source or Header
|
1996-12-23
|
18KB
|
478 lines
/*************************************************************************
*
* WSimpleString
*
*************************************************************************/
#ifndef _WSSTRING_HPP_INCLUDED
#define _WSSTRING_HPP_INCLUDED
#ifndef _WNO_PRAGMA_PUSH
#pragma pack(push,8);
#pragma enum int;
#endif
#ifndef _WSTRDATA_HPP_INCLUDED
# include "wstrdata.hpp"
#endif
class WSimpleString;
extern template WArrayReference<WSimpleString>;
extern template WArray<WSimpleString>;
typedef WArray<WSimpleString> WSimpleStringArray;
//
// WSimpleString
//
class WCMCLASS WSimpleString {
public:
/**********************************************************
* Constructors and Destructors
*********************************************************/
WSimpleString() : _string( NULL ) {}
WSimpleString( const WChar *string, WULong numChars=USE_STR_LEN ) : _string( NULL ) { Create( string, numChars ); }
WSimpleString( const WResourceID & id, WModuleHandle module=_ApplicationModule ) : _string( NULL ) { Create( id, module ); }
WSimpleString( const WMessageID & id, WModuleHandle module=_ApplicationModule ) : _string( NULL ) { Create( id, module ); }
WSimpleString( const WString & s, WBool makeCopy=FALSE ) : _string( NULL ) { Create( s, makeCopy ); }
WSimpleString( const WSimpleString & s, WBool makeCopy=FALSE ) : _string( NULL ) { Create( s, makeCopy ); }
~WSimpleString() { Clear(); }
/**********************************************************
* Properties
*********************************************************/
// Character
//
// Set/get an individual character in the string. Forces
// the string to be the default type.
WWidestChar GetCharacter( WULong index ) const;
WBool SetCharacter( WULong , WWidestChar ) { return FALSE; }
// Dirty
WBool GetDirty() const { return TRUE; }
WBool SetDirty( WBool =TRUE ) { return FALSE; }
// Double
//
// Convert the string to a WDouble, optionally setting an error
// flag.
WDouble WCMRETURNSFLOAT GetDouble( WBool *ok=NULL ) const;
// Empty
//
// TRUE if the string is null or is empty.
WBool GetEmpty() const { return( !_string || *_string == 0 ); }
// Length
//
// Returns the length of the string in characters, not
// including the null terminator.
WULong GetAnsiLength() const { return _string ? GetData()->_stringLength : 0; }
WULong GetUnicodeLength() const { return 0; } // TODO
#ifdef _UNICODE
WULong GetLength() const { return GetUnicodeLength(); }
#else
WULong GetLength() const { return GetAnsiLength(); }
#endif
// Long
//
// Convert the string to a WLong, optionally setting an error
// flag.
WLong GetLong( WBool *ok=NULL ) const;
// Null
//
// TRUE if the string is a null (but not an empty) string.
WBool GetNull() const { return !_string; }
// Size
//
// Returns the size of the string in bytes, not including
// the null terminator.
WULong GetAnsiSize() const { return _string ? GetData()->_stringSize : 0; }
WULong GetUnicodeSize() const { return 0; } // TODO
#ifdef _UNICODE
WULong GetSize() const { return GetUnicodeSize(); }
#else
WULong GetSize() const { return GetAnsiSize(); }
#endif
// Text
//
// Set/get the actual text of the string. The optional parm
// on the set controls whether or not the string should store
// just a pointer to the text or make a copy of the text.
const WChar *const GetText() const { return GetAnsiText(); }
WBool SetText( const WChar *str, WBool makeCopy=TRUE )
{ return SetAnsiText( str, makeCopy ); }
const WAnsiChar *const GetAnsiText() const
{ return _string ? _string : ""; }
WBool SetAnsiText( const WAnsiChar *text,
WBool makeCopy=TRUE );
const WUnicodeChar *const GetUnicodeText() const;
WBool SetUnicodeText( const WUnicodeChar *text,
WBool makeCopy=TRUE );
// Type
//
// Sets/gets the primary type of a string.
WStringType GetType() const { return WAnsiString; }
WBool SetType( WStringType ) const { return FALSE; }
// ULong
//
// Convert the string to a WULong, optionally setting an error
// flag.
WULong GetULong( WBool *ok=NULL ) const;
/**********************************************************
* Methods
*********************************************************/
// Chop
//
// If given a non-negative value, removes all the characters
// up to but not including charPos. If given a negative value,
// removes the last charPos*-1 characters.
WBool Chop( WLong charPos );
// Clear
//
// Frees the string, leaving a null string.
void Clear();
// Compare
static int Compare( const WSimpleString & a, const WSimpleString & b,
WBool caseSensitive=TRUE, WULong numChars=0 );
static int Compare( const WSimpleString & a, const WAnsiChar *b );
static int Compare( const WSimpleString & a, const WUnicodeChar *b );
static int Compare( const WAnsiChar *a, const WSimpleString & b )
{ return -Compare( b, a ); }
static int Compare( const WUnicodeChar *a, const WSimpleString & b )
{ return -Compare( b, a ); }
// Create
//
// Various constructors for building a new string. First
// frees the old string.
WBool Create() { Clear(); return TRUE; }
WBool Create( const WResourceID & id, WModuleHandle module=_ApplicationModule );
WBool Create( const WMessageID & id, WModuleHandle module=_ApplicationModule );
WBool Create( const WString & s, WBool makeCopy=FALSE );
WBool Create( const WSimpleString & s, WBool makeCopy=FALSE );
WBool CreateAnsi( const WAnsiChar *string,
WULong numChars=USE_STR_LEN );
WBool CreateUnicode( const WUnicodeChar *string,
WULong numChars=USE_STR_LEN );
#ifdef _UNICODE
WBool Create( const WChar *string, WULong numChars=USE_STR_LEN )
{ return CreateUnicode( string, numChars ); }
#else
WBool Create( const WChar *string, WULong numChars=USE_STR_LEN )
{ return CreateAnsi( string, numChars ); }
#endif
// Concat
//
// Concatenate a string onto another.
WBool Concat( const WSimpleString & suffix );
WBool Concat( const WAnsiChar *string );
WBool Concat( const WUnicodeChar *string );
WBool Concat( WWidestChar singleChar );
// Concatf
WBool Concatf( const WAnsiChar *parms, ... );
WBool Concatf( const WUnicodeChar *parms, ... );
// ConvertToInteger
WInt ConvertToInteger() const;
// ConvertToLong
WLong ConvertToLong() const;
// Format
//
// Formats a message.
// Returns number of characters
// (not bytes) copied into the string.
WULong Format( const WChar *format, ... );
WULong Format( WULong messageID, WULong langID, ... );
WULong Format( const WModule *module, WULong messageID,
WULong langID, ... );
// FormatEx
//
// Formats a message, but with more options.
#define WSTRF_NOFORMAT 0x0200
#define WSTRF_FROMSTRING 0x0400
#define WSTRF_FROMMODULE 0x0800
#define WSTRF_FROMSYSTEM 0x1000
#define WSTRF_USEARRAY 0x2000
WULong FormatEx( WULong flags, const WChar *format,
WByte maxWidth, ... );
WULong FormatEx( WULong flags, const WModule *module,
WULong messageID, WULong langID,
WByte maxWidth, ... );
// FormatV
//
// The basic form of Format/FormatEx.
WULong FormatV( WULong flags, const WChar *format,
WByte maxWidth, va_list args );
WULong FormatV( WULong flags, const WModule *module,
WULong messageID, WULong langID,
WByte maxWidth, va_list args );
// Left
//
// Returns the n leftmost characters of a string. If string
// is smaller than n, just returns the string.
WSimpleString Left( WULong numChars ) const;
// Lock
//
// Call this if you want to work directly on the buffer
// inside the WString. It will ensure that no one else
// is referencing it, that it has a minimum size (in bytes),
// return its pointer. Call Unlock when you're done.
WChar *Lock( WULong minimumSize=0, WStringType type=WDefaultString );
// Parse
//
// Parse a string into an array of substrings. The original
// string is left unchanged. The delimeterList specifies
// the list of characters that delimit the substrings
// (default is whitespace). If quoteList is non-NULL,
// it consists of pairs of begin-end characters which are
// used to declare a substring with delimiter characters
// embedded in it. If ignoreMultiple is TRUE, multiple
// delimiter characters (such as multiple spaces) are
// ignored. You can specify an index to start and end at.
// Finally, if allowEscapes is TRUE, you can use a backslash
// within a quoted string as an escape character to escape
// either delimiter character or a backslash itself.
WSimpleStringArray Parse( const WChar *delimiterList=NULL,
WBool ignoreMultiples=TRUE,
const WChar *quoteList=WSTRING_DEFAULT_QUOTELIST,
WBool stripQuotes=TRUE,
WULong startAt=0,
WULong endAt=USE_STR_LEN,
WBool allowEscapes=TRUE ) const;
// Position
//
// Search for a given character or substring in the string
// and return its position. NOT_FOUND is returned if not
// found.
WULong Position( const WWidestChar character, WULong startAt=0,
WBool ignoreCase=FALSE ) const;
WULong Position( const WChar *substring, WULong startAt=0,
WBool ignoreCase=FALSE ) const;
WULong Position( const WSimpleString & substring, WULong startAt=0,
WBool ignoreCase=FALSE ) const;
// Right
//
// Return the n rightmost characters of a string. If string
// is smaller than n, just returns the string.
WSimpleString Right( WULong numChars ) const;
// Sprintf
//
// Calls with the C function sprintf functionality.
WULong Sprintf( const WAnsiChar *parms, ... );
WULong Sprintf( const WUnicodeChar *parms, ... );
// Strip
//
// Remove spaces from one or both ends.
WSimpleString Strip( WBool fromBeg=TRUE, WBool fromEnd=TRUE ) const;
// SubString
//
// Returns the substring starting at position start and
// of length n. Start is 0-based. If n is not specified,
// the rest of the string is returned.
WSimpleString Substring( WULong startAt, WULong numChars=USE_STR_LEN ) const;
// ToLowercase
//
// Convert the string (in-place) to lowercase.
WBool ToLowercase();
// ToUppercase
//
// Convert the string (in-place) to uppercase.
WBool ToUppercase();
// Trim
//
// Strip the string in place.
WBool Trim( WBool fromBeg=TRUE, WBool fromEnd=TRUE )
{ return Create( Strip( fromBeg, fromEnd ) ); }
// Truncate
//
// Truncate the string to the given length.
WBool Truncate( WULong length );
// Unlock
//
// Call this when done writing directly to the buffer.
// The string then recalculates its size and length.
WBool Unlock();
/*********************************************************
* Operators
*********************************************************/
//
// [] operator -- Note that a WWidestChar is always used,
// not a WChar, to ensure that the result
// can handle the largest type of char.
const WWidestChar operator[]( int index ) const {
return GetCharacter( index );
}
//
// casting operators
//
operator const WAnsiChar*() const { return GetAnsiText(); }
operator const WUnicodeChar*() const { return GetUnicodeText(); }
//
// = operator
//
WSimpleString & operator=( const WAnsiChar *s ) { CreateAnsi( s ); return *this; }
WSimpleString & operator=( const WUnicodeChar *s ) { CreateUnicode( s ); return *this; }
WSimpleString & operator=( const WSimpleString & s ) { Create( s ); return *this; }
WSimpleString & operator=( const WString & s ) { Create( s ); return *this; }
//
// == operator
//
friend int operator==( const WSimpleString & a, const WSimpleString & b )
{ return( WSimpleString::Compare( a, b ) == 0 ); }
friend int operator==( const WSimpleString & a, const WAnsiChar * b )
{ return( WSimpleString::Compare( a, b ) == 0 ); }
friend int operator==( const WSimpleString & a, WAnsiChar * b )
{ return( WSimpleString::Compare( a, b ) == 0 ); }
friend int operator==( const WSimpleString & a, const WUnicodeChar * b )
{ return( WSimpleString::Compare( a, b ) == 0 ); }
friend int operator==( const WSimpleString & a, WUnicodeChar * b )
{ return( WSimpleString::Compare( a, b ) == 0 ); }
friend int operator==( const WAnsiChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) == 0 ); }
friend int operator==( WAnsiChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) == 0 ); }
friend int operator==( const WUnicodeChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) == 0 ); }
friend int operator==( WUnicodeChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) == 0 ); }
//
// != operator
//
friend int operator!=( const WSimpleString & a, const WSimpleString & b )
{ return( WSimpleString::Compare( a, b ) != 0 ); }
friend int operator!=( const WSimpleString & a, const WAnsiChar * b )
{ return( WSimpleString::Compare( a, b ) != 0 ); }
friend int operator!=( const WSimpleString & a, WAnsiChar * b )
{ return( WSimpleString::Compare( a, b ) != 0 ); }
friend int operator!=( const WSimpleString & a, const WUnicodeChar * b )
{ return( WSimpleString::Compare( a, b ) != 0 ); }
friend int operator!=( const WSimpleString & a, WUnicodeChar * b )
{ return( WSimpleString::Compare( a, b ) != 0 ); }
friend int operator!=( const WAnsiChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) != 0 ); }
friend int operator!=( WAnsiChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) != 0 ); }
friend int operator!=( const WUnicodeChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) != 0 ); }
friend int operator!=( WUnicodeChar * a, const WSimpleString & b )
{ return( WSimpleString::Compare( b, a ) != 0 ); }
protected:
WStringHeader *GetHeader() const {
return ((WStringHeader *)_string) - 1;
}
WStringData *GetData() const {
return (WStringData *) GetHeader();
}
WBool CopyOnWrite();
WStringData *GrowTo( WULong size );
WULong SearchFor( const WChar *buf, WULong size, WULong at,
WBool ignoreCase ) const;
protected:
WChar *_string;
};
#ifndef _WNO_PRAGMA_PUSH
#pragma enum pop;
#pragma pack(pop);
#endif
#endif // _WSTRING_HPP_INCLUDED