CStr Class Reference

#include <strclass.h>

Class Description

See also
Class WStr, UTF8Str and MaxString, Guidelines for Handling Character Strings.

Description:
A simple character string class. This is the standard character string class used in 3ds Max. Methods and operators are provided for calculating lengths, concatenation, substring operations, character searching, case conversion, comparison, and formatted writing.

This class automatically allocates the proper amount of space for the string. This is very handy in the case of internationalization / localization. For example, if you code something like:

MSTR myString = GetString(IDS_STRING_ID);

then myString's constructor will allocate enough space to store the resource string no matter how long it is. This is much better than doing the following:

MCHAR myString[64];

_tcscpy(myString, GetString(IDS_STRING_ID));

because the resource string may turn out to be much longer than 64 bytes once it's translated to German or French (or whatever).

As another example, if you have the following code:

MSTR str1 = _M("This is string1.");

MSTR str2 = _M("This is string2.");

Then

MSTR concatStr = str1 + str2;

will again yield a (concatenated) string will enough space to hold the concatenated contents of str1 and str2, automatically.

All methods are implemented by the system.
Note
The memory occupied by a CStr object is cannot be larger than 2Gb.
By design, this class assume that all the character data that you use to instanciate them or manipulate them are using the code page defined by the current active language of the 3dsMax. It should not be used to contains data in UTF-8 or in other code page than the current one.
+ Inheritance diagram for CStr:

Public Member Functions

 CStr ()
 
 CStr (const char *cs)
 
 CStr (const CStr &ws)
 
 CStr (const MaxSDK::Util::MaxString &ws)
 
 CStr (const MaxSDK::Util::MaxStringCast< char > &ws)
 
 CStr (const QString &qstring)
 Conversion constructor, from QString. More...
 
 ~CStr ()
 
char * dataForWrite (size_t nchars=(size_t)-1)
 Returns a pointer to this string which can be written to with up to 'nchars' characters. More...
 
const char * data () const
 
 operator const char * () const
 
 operator QString () const
 Conversion operator to QString. More...
 
void Resize (int nchars)
 
int Length () const
 
int length () const
 
int ByteCount () const
 
int LanguageCharacterCount () const
 
size_t AllocatedChars () const
 
bool isNull () const
 
CStroperator= (const CStr &cs)
 
CStroperator= (const MaxSDK::Util::MaxString &mstr)
 
CStroperator= (const MaxSDK::Util::MaxStringCast< char > &mstr)
 
CStroperator= (const char *cs)
 
CStr operator+ (const CStr &cs) const
 
CStroperator+= (const CStr &cs)
 
CStrAppend (const CStr &cs)
 
CStrappend (const CStr &cs)
 
CStrremove (int pos)
 
CStrremove (int pos, int N)
 
CStr Substr (int start, int nchars) const
 
CStr MultiByteCharacterSubString (int firstCharacterIndex, int numberOfMBCharacters) const
 
int FindMultiByteCharacterFirstByteIndex (int characterIndex) const
 
int FindMultiByteCharacterLastByteIndex (int characterIndex) const
 
char operator[] (int i) const
 Returns the nth character of this string. More...
 
int first (char c) const
 
int last (char c) const
 
bool operator== (const CStr &cs) const
 
bool operator== (char c) const
 
bool operator!= (const CStr &cs) const
 
bool operator!= (char c) const
 
bool operator< (const CStr &cs) const
 
bool operator<= (const CStr &ws) const
 
bool operator> (const CStr &ws) const
 
bool operator>= (const CStr &ws) const
 
void toUpper ()
 
void toLower ()
 
int printf (const char *format,...)
 Write a formatted string into this CStr. More...
 
int vprintf (const char *format, va_list args)
 Write a formatted string into this CStr. More...
 
wchar_t * ToBSTR () const
 
MaxSDK::Util::MaxStringCastCP ToCP (UINT cp, size_t *length=NULL) const
 
MaxSDK::Util::MaxStringCast< char > ToACP (size_t *length=NULL) const
 
MaxSDK::Util::MaxStringCastUTF8 ToUTF8 (size_t *length=NULL) const
 
MaxSDK::Util::MaxStringCast< WCHARToOLESTR (size_t *length=NULL) const
 
MaxSDK::Util::MaxStringCast< WCHARToUTF16 (size_t *length=NULL) const
 
MaxSDK::Util::MaxStringCast< unsigned intToUTF32 (size_t *length=NULL) const
 
MaxSDK::Util::MaxString ToMaxString () const
 
void ToMaxString (MaxSDK::Util::MaxString &) const
 
CStr ToCStr () const
 
WStr ToWStr () const
 
MaxSDK::Util::MaxStringCast< wchar_t > ToMCHAR (size_t *length=NULL) const
 
WStr ToMSTR () const
 
bool EndsWith (const CStr &s, bool caseSensitive=true)
 Returns true if the string ends with s; otherwise returns false. More...
 
bool EndsWith (char c, bool caseSensitive=true)
 Returns true if the string ends with c; otherwise returns false. More...
 
bool StartsWith (const CStr &s, bool caseSensitive=true)
 Returns true if the string starts with s; otherwise returns false. More...
 
bool StartsWith (char c, bool caseSensitive=true)
 Returns true if the string starts with c; otherwise returns false. More...
 
size_t NumberOfLines () const
 Returns the number of line feeds inside a string. More...
 
size_t Replace (const char *pFind, const char *pReplaceBy, bool firstOnly=false, size_t startPosition=0)
 Replaces one substring with another in this string. More...
 

Static Public Member Functions

static CStr FromBSTR (const wchar_t *string, size_t length=(size_t)-1)
 
static CStr FromCP (UINT cp, const char *string, size_t length=(size_t)-1)
 
static CStr FromACP (const char *string, size_t length=(size_t)-1)
 
static CStr FromUTF8 (const char *string, size_t length=(size_t)-1)
 
static CStr FromOLESTR (const wchar_t *string, size_t length=(size_t)-1)
 
static CStr FromUTF16 (const wchar_t *string, size_t length=(size_t)-1)
 
static CStr FromUTF32 (const unsigned int *string, size_t length=(size_t)-1)
 
static CStr FromMaxString (MaxSDK::Util::MaxString &string)
 
static CStr FromCStr (const CStr &string)
 
static CStr FromWStr (const WStr &string)
 
static CStr FromMCHAR (const wchar_t *string, size_t length=(size_t)-1)
 
static CStr FromMSTR (const WStr &string)
 
- Static Public Member Functions inherited from MaxHeapOperators
static UtilExport voidoperator new (size_t size)
 Standard new operator used to allocate objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e)
 Standard new operator used to allocate objects if there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate objects that takes the type of memory, filename and line number where the new was called If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate objects that takes the filename and line number where the new was called If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new (size_t size, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate objects that takes extra flags to specify special operations If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, int block_type, const char *filename, int line)
 New operator used to allocate arrays of objects. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, const char *filename, int line)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport voidoperator new[] (size_t size, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, an exception will be thrown. More...
 
static UtilExport voidoperator new[] (size_t size, const std::nothrow_t &e, unsigned long flags)
 New operator used to allocate arrays of objects If there is insufficient memory, NULL will be returned. More...
 
static UtilExport void operator delete (void *ptr)
 Standard delete operator used to deallocate an object If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an object If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an object that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an object that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete (void *ptr, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an object that takes extra flags to specify special operations If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e)
 Standard delete operator used to deallocate an array of objects If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, int block_type, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the type of memory, filename and line number where the delete was called If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, const char *filename, int line)
 Delete operator used to deallocate an array of objects that takes the filename and line number where the delete was called If the pointer is invalid, nothing will happen. More...
 
static UtilExport void operator delete[] (void *ptr, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport void operator delete[] (void *ptr, const std::nothrow_t &e, unsigned long flags)
 Delete operator used to deallocate an array of objects that takes extra flags to specify special operations If the pointer is invalid, an exception will be thrown. More...
 
static UtilExport voidoperator new (size_t size, void *placement_ptr)
 Placement new operator. More...
 
static UtilExport void operator delete (void *ptr, void *placement_ptr)
 Placement delete operator. More...
 
static UtilExport voidaligned_malloc (size_t size, size_t alignment)
 Allocates memory on a specified alignment boundary. More...
 
static UtilExport voidaligned_realloc (void *ptr, size_t size, size_t alignment)
 Reallocates memory on a specified alignment boundary. More...
 
static UtilExport void aligned_free (void *ptr)
 Frees a block of memory that was allocated with aligned_malloc/aligned_realloc. More...
 

Constructor & Destructor Documentation

CStr ( )
Remarks
Constructor. The string is set to NULL.
CStr ( const char *  cs)
Remarks
Constructor. The string is initialized to the string passed.
CStr ( const CStr ws)
Remarks
Constructor. The string is initialized to the string passed.
CStr ( const MaxSDK::Util::MaxString ws)
Remarks
Constructor. The string is initialized to the string passed.
CStr ( const MaxSDK::Util::MaxStringCast< char > &  ws)
Remarks
Constructor. The string is initialized to the string passed.
CStr ( const QString &  qstring)

Conversion constructor, from QString.

~CStr ( )
Remarks
Destructor. The string is deleted.

Member Function Documentation

char* dataForWrite ( size_t  nchars = (size_t)-1)

Returns a pointer to this string which can be written to with up to 'nchars' characters.

Remarks
The terminating null character is not included in nchars, so technically up to nchars+1 characters may be written.
Parameters
ncharsThe minimum number of characters to allocate in the string buffer (excluding the terminating null character). A value of -1 specifies that the existing buffer should be re-used.
const char* data ( ) const
Remarks
Returns a const pointer to the string. If the string is NULL, a pointer to 0 is returned.
operator const char * ( ) const
Remarks
Returns a const pointer to the string. If the string is NULL, a pointer to 0 is returned.
operator QString ( ) const

Conversion operator to QString.

void Resize ( int  nchars)
Remarks
Reallocates the string to contain nchars characters. If the string is enlarged it is padded with blanks.
Parameters:
int nchars

Specifies the new number of characters for the string.
int Length ( ) const
inline
Remarks
Returns the number of chars used to store the string in memory, including spaces, but excluding the terminating NULL character.
114 { return ByteCount(); }
int ByteCount() const
int length ( ) const
inline
Remarks
Returns the number of chars used to store the string in memory, including spaces, but excluding the terminating NULL character.
116 { return Length(); }
int Length() const
Definition: strclass.h:114
int ByteCount ( ) const
Remarks
Returns the number of bytes used to store the string in memory, including spaces, but excluding the terminating NULL character.
int LanguageCharacterCount ( ) const
Remarks
Returns the number of natural language characters the string is represented on, including spaces, but excluding the terminal NULL character. For multi-byte languages, this may be equal or less than the amount of bytes used to store the string in memory. For example, a Chinese character might take two bytes. Length() or ByteCount() will count the character as two distinct char. LanguageCharacterCount() will count it as a single char.
size_t AllocatedChars ( ) const
Remarks
Returns the total number of char allocated to store the string. Including the NULL character and any unused char.
bool isNull ( ) const
inline
Remarks
Returns TRUE if the string length is 0; otherwise FALSE.
130 { return data()[0]==0; }
const char * data() const
CStr& operator= ( const CStr cs)
Remarks
Assignment operator.
CStr& operator= ( const MaxSDK::Util::MaxString mstr)
Remarks
Assignment operator.
CStr& operator= ( const MaxSDK::Util::MaxStringCast< char > &  mstr)
Remarks
Assignment operator.
CStr& operator= ( const char *  cs)
Remarks
Assignment operator. In release 3.0 and later this method check for self-assignment.
CStr operator+ ( const CStr cs) const
Remarks
Concatenation operator. Returns a new string that is this string with string cs appended.
CStr& operator+= ( const CStr cs)
Remarks
Concatenation. Returns this string with cs appended.
CStr& Append ( const CStr cs)
inline
Remarks
Concatenation. Returns this string with cs appended.
149 { return ((*this) += cs); }
CStr& append ( const CStr cs)
inline
Remarks
Concatenation. Returns this string with cs appended to the end.
151 { return ((*this) += cs); }
CStr& remove ( int  pos)
Remarks
Returns this string with all characters from pos to the end removed.
Parameters:
int pos

Specifies the last position in the string.
CStr& remove ( int  pos,
int  N 
)
Remarks
Returns this string with N characters removed from pos to the end.
Parameters:
int pos

Specifies the position to begin removing characters.

int N

Specifies the number of characters to remove.
CStr Substr ( int  start,
int  nchars 
) const
Remarks
Returns a substring of this string, beginning at position start, of length nchars.
CStr MultiByteCharacterSubString ( int  firstCharacterIndex,
int  numberOfMBCharacters 
) const
Remarks
Returns a substring of this string, beginning at multi-byte character position firstCharacterIndex, of length numberOfMBCharacters.
int FindMultiByteCharacterFirstByteIndex ( int  characterIndex) const
Remarks
Returns the first byte index value of the multi-byte character position characterIndex.
int FindMultiByteCharacterLastByteIndex ( int  characterIndex) const
Remarks
Returns the last byte index value of the multi-byte character position characterIndex.
char operator[] ( int  i) const

Returns the nth character of this string.

Warning: In some environment, this function may return a partial character, especially for asiatic languages. If you intend to do any advance transformation or interpretation of the returned data, you should consider calling CStr::MultiByteCharacterSubString instead.

Parameters
iIndex of that character.
int first ( char  c) const
Remarks
Returns the index of the first occurrence of character c in this string. Returns -1 if not found.
int last ( char  c) const
Remarks
Returns the index of the last occurrence of character c in this string. Returns -1 if not found.
bool operator== ( const CStr cs) const
Remarks
Equality operator.
Returns
true if the strings are equal; otherwise false.
bool operator== ( char  c) const
Remarks
Returns true if this string contain only the character c.
bool operator!= ( const CStr cs) const
Remarks
Inequality operator.
Returns
false if the strings are equal; otherwise true.
bool operator!= ( char  c) const
Remarks
Returns true if this string does not contain only the character c.
bool operator< ( const CStr cs) const
Remarks
Returns true if this string is less than cs; otherwise false.
bool operator<= ( const CStr ws) const
Remarks
Returns true if this string is less than or equal to ws; otherwise false.
bool operator> ( const CStr ws) const
Remarks
Returns true if this string is greater than ws; otherwise false.
bool operator>= ( const CStr ws) const
Remarks
Returns true if this string is greater than or equal to ws; otherwise false.
void toUpper ( )
Remarks
Converts all character of this string to uppercase.
void toLower ( )
Remarks
Converts all character of this string to lowercase.
int printf ( const char *  format,
  ... 
)

Write a formatted string into this CStr.

Writes the format string, filled in by the optional arguments into this CStr. See the ISO C++ documentation for more information on printf and format strings.

Parameters
formatSpecifies how to format the destination string.
...optional arguments to format into the destination string.
Precondition
format is not null.
There are the correct number of elliptical arguments to fill the format string.
Postcondition
This string is replaced with the formatted string.
Returns
The number of characters written to this string, not including the null terminating character, or a negative value if an error occurs.
int vprintf ( const char *  format,
va_list  args 
)

Write a formatted string into this CStr.

This method is similar to CStr::printf. Instead of taking a variable list of arguments as parameter, it takes a structure representing a variable list of argument. This allows CStr objects to be used to build strings based on a format string and a variable number of arguments:

void LogMessage(const char* format, ...) {
va_list args;
va_start(args, format);
CStr buf;
buf.vprintf(format, args);
va_end(args);
// log the message contained by buf
}
wchar_t* ToBSTR ( ) const
Remarks
Returns the string in memory allocated by SysAllocString. Caller is responsible for freeing memory using SysFreeString.
static CStr FromBSTR ( const wchar_t *  string,
size_t  length = (size_t)-1 
)
static
MaxSDK::Util::MaxStringCastCP ToCP ( UINT  cp,
size_t length = NULL 
) const
static CStr FromCP ( UINT  cp,
const char *  string,
size_t  length = (size_t)-1 
)
static
MaxSDK::Util::MaxStringCast<char> ToACP ( size_t length = NULL) const
static CStr FromACP ( const char *  string,
size_t  length = (size_t)-1 
)
static
MaxSDK::Util::MaxStringCastUTF8 ToUTF8 ( size_t length = NULL) const
static CStr FromUTF8 ( const char *  string,
size_t  length = (size_t)-1 
)
static
MaxSDK::Util::MaxStringCast<WCHAR> ToOLESTR ( size_t length = NULL) const
static CStr FromOLESTR ( const wchar_t *  string,
size_t  length = (size_t)-1 
)
static
MaxSDK::Util::MaxStringCast<WCHAR> ToUTF16 ( size_t length = NULL) const
static CStr FromUTF16 ( const wchar_t *  string,
size_t  length = (size_t)-1 
)
static
MaxSDK::Util::MaxStringCast<unsigned int> ToUTF32 ( size_t length = NULL) const
static CStr FromUTF32 ( const unsigned int string,
size_t  length = (size_t)-1 
)
static
MaxSDK::Util::MaxString ToMaxString ( ) const
void ToMaxString ( MaxSDK::Util::MaxString ) const
static CStr FromMaxString ( MaxSDK::Util::MaxString string)
inlinestatic
283 { return CStr(string); }
CStr ToCStr ( ) const
inline
285 { return *this; }
static CStr FromCStr ( const CStr string)
inlinestatic
286 { return CStr(string); }
WStr ToWStr ( ) const
static CStr FromWStr ( const WStr string)
static
MaxSDK::Util::MaxStringCast<wchar_t> ToMCHAR ( size_t length = NULL) const
inline
292 { return ToUTF16(length); }
int length() const
Definition: strclass.h:116
MaxSDK::Util::MaxStringCast< WCHAR > ToUTF16(size_t *length=NULL) const
static CStr FromMCHAR ( const wchar_t *  string,
size_t  length = (size_t)-1 
)
inlinestatic
293 { return FromUTF16(string, length); }
static CStr FromUTF16(const wchar_t *string, size_t length=(size_t)-1)
int length() const
Definition: strclass.h:116
WStr ToMSTR ( ) const
static CStr FromMSTR ( const WStr string)
static
bool EndsWith ( const CStr s,
bool  caseSensitive = true 
)

Returns true if the string ends with s; otherwise returns false.

Parameters
sThe string to be searched.
caseSensitiveIf it is true(default), the search is case sensitive; otherwise the search is case insensitive.
bool EndsWith ( char  c,
bool  caseSensitive = true 
)

Returns true if the string ends with c; otherwise returns false.

This function overloads EndsWith().

Parameters
cThe character to be searched.
caseSensitiveIf it is true(default), the search is case sensitive; otherwise the search is case insensitive.
bool StartsWith ( const CStr s,
bool  caseSensitive = true 
)

Returns true if the string starts with s; otherwise returns false.

Parameters
sThe string to be searched.
caseSensitiveIf it is true(default), the search is case sensitive; otherwise the search is case insensitive.
bool StartsWith ( char  c,
bool  caseSensitive = true 
)

Returns true if the string starts with c; otherwise returns false.

Parameters
cThe character to be searched.
caseSensitiveIf it is true(default), the search is case sensitive; otherwise the search is case insensitive.
size_t NumberOfLines ( ) const

Returns the number of line feeds inside a string.

size_t Replace ( const char *  pFind,
const char *  pReplaceBy,
bool  firstOnly = false,
size_t  startPosition = 0 
)

Replaces one substring with another in this string.

Parameters
pFindThe substring to be replaced.
pReplaceByThe string the substring is to be replaced with.
firstOnlyIf true, only first occurrence of 'from' is replaced, otherwise all occurrences are replaced
startPositionThe character position to start searching from
Returns
Returns the number of replacements made.