StringVar Class Reference

Extends its String base class to become self-sizing. This means that we need to override only functions in StringBase that try to create or extend the text allocation. More...

#include <varstr.h>

Inheritance diagram for StringVar:

StringBase CCObject SimpleCCObject List of all members.

Public Member Functions

 StringVar (const TCHAR *psz=TEXT(""))
 Constructs a string of the appropriate maximum length.
 StringVar (const StringBase &other)
 Constructs a string of the appropriate maximum length.
 StringVar (UINT32 resID, UINT32 hinst=0)
 Constructs a string of the appropriate maximum length.
 StringVar (const wxString &wxs)
 Constructs a string of the appropriate maximum length.
StringVaroperator+= (const StringBase &rhs)
 Append string y onto the end of string x, eg. x = TEXT("hello"); cout << (x += TEXT(" world!")) << endl;.
StringVaroperator+= (const TCHAR *s)
 Append string y onto the end of string x, eg. x = TEXT("hello"); cout << (x += TEXT(" world!")) << endl;.
StringVaroperator+= (const TCHAR ch)
 Appends this character to the end of this string.
StringVaroperator= (const StringBase &rhs)
 Assignment (set equal to) operator, eg. StringBase x("rhubarb"); x = "custard";.
StringVaroperator= (const TCHAR *s)
 Assignment (set equal to) operator, eg. StringBase x("rhubarb"); x = "custard";.
StringVaroperator= (const TCHAR ch)
 Assigns the single character to this string.
virtual void MakePercent (INT32 Value)
 Set the string equal to the given argument + a % character, or however the current locale wants it Example: String_32 Str; Str.MakePercent(23); For UK, Str now equals "23%".
virtual void MakePercent (double Value)
virtual void MakePercent (TCHAR *Value)

Static Public Member Functions

static BOOL Init ()
 Test StringVar functionality in debug builds. Scope: Public.

Private Member Functions

void SafeCat (const TCHAR *string)
 Appends string to the end of this.
void SafeCopy (const TCHAR *string, UINT32 maxlen=0)
 Assigns string to this carefully avoiding buffer overflow.
void EnsureAlloc (UINT32 newlen)
 Appends string to the end of this.

Detailed Description

Extends its String base class to become self-sizing. This means that we need to override only functions in StringBase that try to create or extend the text allocation.

class StringVar : public StringBase

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
30th January 2006

Definition at line 116 of file varstr.h.


Constructor & Destructor Documentation

StringVar::StringVar const TCHAR psz = TEXT("")  ) 
 

Constructs a string of the appropriate maximum length.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
30th January 2006
Parameters:
An optional pointer to a (constant) Unicode character array - if [INPUTS] omitted then the String becomes empty (NOT the null pointer).
Returns:
Errors: ASSERTion failure if the passed character array is too long for this length of string. Scope: Public

Definition at line 161 of file varstr.cpp.

00162 {
00163     text = NULL;
00164     length = 0;
00165 
00166     UINT32 newlen = camStrlen(psz);
00167     EnsureAlloc(newlen);
00168     SafeCopy(psz, newlen);
00169 }

StringVar::StringVar const StringBase other  ) 
 

Constructs a string of the appropriate maximum length.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
30th January 2006
Parameters:
An optional pointer to a (constant) Unicode character array - if [INPUTS] omitted then the String becomes empty (NOT the null pointer).
Returns:
Errors: ASSERTion failure if the passed character array is too long for this length of string, or if psz is NULL. Scope: Public

Definition at line 186 of file varstr.cpp.

00187 {
00188     text = NULL;
00189     length = 0;
00190 
00191     UINT32 newlen = other.Length();
00192     EnsureAlloc(newlen);
00193     SafeCopy((const TCHAR*)other, newlen);
00194 }

StringVar::StringVar UINT32  resID,
UINT32  hinst = 0
 

Constructs a string of the appropriate maximum length.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
30th January 2006
Parameters:
An optional pointer to a (constant) Unicode character array - if [INPUTS] omitted then the String becomes empty (NOT the null pointer).
Returns:
Errors: ASSERTion failure if the passed character array is too long for this length of string, or if psz is NULL. Scope: Public

Definition at line 211 of file varstr.cpp.

00212 {
00213     text = NULL;
00214     length = 0;
00215 
00216     const TCHAR* pResText = CamResource::GetText(resID);
00217     UINT32 len = camStrlen(pResText);
00218 
00219     EnsureAlloc(len);
00220     SafeCopy(pResText, len);
00221 }

StringVar::StringVar const wxString &  wxs  ) 
 

Constructs a string of the appropriate maximum length.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
30th January 2006
Parameters:
An optional pointer to a (constant) Unicode character array - if [INPUTS] omitted then the String becomes empty (NOT the null pointer).
Returns:
Errors: ASSERTion failure if the passed character array is too long for this length of string, or if psz is NULL. Scope: Public

Definition at line 238 of file varstr.cpp.

00239 {
00240     text = NULL;
00241     length = 0;
00242 
00243     UINT32 newlen = wxs.Len();
00244     EnsureAlloc(newlen);
00245     SafeCopy((const TCHAR*)wxs, newlen);
00246 }


Member Function Documentation

void StringVar::EnsureAlloc UINT32  newlen  )  [private]
 

Appends string to the end of this.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
31st January 2006
Parameters:
string 0-terminated destination string [INPUTS]
- [OUTPUTS]
Returns:
-
See also:
StringBase::Alloc, StringVar::Alloc

Definition at line 485 of file varstr.cpp.

00486 {
00487     // Note: length includes zero termination character
00488     if (length==0 || newlen > (length-1))       // length includes term
00489     {
00490         // We need to reallocate
00491         TCHAR* pOldText = text;
00492         text = NULL;
00493 
00494         // Allocate room for the new string plus extra room to avoid repeated reallocation
00495         // And round to a 64 char boundary so that allocations slot together more efficiently.
00496         // And ensure that we always allocate >0
00497         // (Remember Alloc adds 1 character for the terminator so we subtract 1 from newlen)
00498         newlen = (newlen/64 + 1)*64 - 1;
00499         Alloc(newlen);
00500 
00501         if (text!=NULL && pOldText!=NULL)
00502             camStrcpy(text, pOldText);
00503     }
00504 }

BOOL StringVar::Init void   )  [static]
 

Test StringVar functionality in debug builds. Scope: Public.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
31st January 2006
Parameters:
- [INPUTS]
TRUE if this class initialises OK [OUTPUTS] FALSE otherwise

Reimplemented from SimpleCCObject.

Definition at line 116 of file varstr.cpp.

00117 {
00118 #if defined(_DEBUG) && 0
00119     StringVar t1(_T("Test string\n"));
00120     StringVar t2(_T("0123456789012345678901234567890123456789") // 8*40 = 320 chars
00121                 _T("0123456789012345678901234567890123456789")
00122                 _T("0123456789012345678901234567890123456789")
00123                 _T("0123456789012345678901234567890123456789")
00124                 _T("0123456789012345678901234567890123456789")
00125                 _T("0123456789012345678901234567890123456789")
00126                 _T("0123456789012345678901234567890123456789")
00127                 _T("0123456789012345678901234567890123456789")
00128                 );
00129     String_32 st32(_T(" Woohoo!\n"));
00130 
00131     StringVar t3;
00132     t3 = st32;
00133     t3 += t1;
00134     t3 += t2;
00135     TRACEUSER("Phil", _T("StringVar Test 1 = %s"), (TCHAR*)t3);
00136 
00137     StringVar t4(st32);
00138     StringVar t5(_R(IDS_MANY));
00139     t4 += TCHAR('-') + t5;      // Implicit cast?
00140     TRACEUSER("Phil", _T("StringVar Test 2 = %s\n"), (TCHAR*)t4);
00141 #endif
00142 
00143     return TRUE;
00144 }

void StringVar::MakePercent TCHAR Value  )  [virtual]
 

Reimplemented from StringBase.

Definition at line 535 of file varstr.cpp.

00536 {
00537     EnsureAlloc(32);
00538     StringBase::MakePercent(Value);
00539 }

void StringVar::MakePercent double  Value  )  [virtual]
 

Reimplemented from StringBase.

Definition at line 529 of file varstr.cpp.

00530 {
00531     EnsureAlloc(32);
00532     StringBase::MakePercent(Value);
00533 }

void StringVar::MakePercent INT32  Value  )  [virtual]
 

Set the string equal to the given argument + a % character, or however the current locale wants it Example: String_32 Str; Str.MakePercent(23); For UK, Str now equals "23%".

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
31st January 2006
Parameters:
Takes a number / string [INPUTS]

Reimplemented from StringBase.

Definition at line 523 of file varstr.cpp.

00524 {   
00525     EnsureAlloc(32);
00526     StringBase::MakePercent(Value);
00527 }

StringVar & StringVar::operator+= const TCHAR  ch  ) 
 

Appends this character to the end of this string.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
31st January 2006
Parameters:
A character to add to the end of this String. [INPUTS]
Returns:
A reference to the lhs, ie. this StringVar as an lvalue.

Reimplemented from StringBase.

Definition at line 307 of file varstr.cpp.

00308 {
00309     ERROR3IF(this==NULL, "StringVar this pointer is NULL!");
00310     ERROR3IF(text==NULL, "Call to String::operator+= for an unALLOCated String");
00311 
00312     if (text && ch!=0)
00313     {
00314         TCHAR tch[2];
00315         tch[0] = ch;
00316         tch[1] = 0;
00317         SafeCat(tch);
00318     }   
00319 
00320     return *this;
00321 }

StringVar & StringVar::operator+= const TCHAR s  )  [inline]
 

Append string y onto the end of string x, eg. x = TEXT("hello"); cout << (x += TEXT(" world!")) << endl;.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
31st January 2006
Parameters:
The C-style character "string" to add to the end of this String. [INPUTS]
Returns:
A reference to the lhs, ie. this StringBase as an lvalue.

Reimplemented from StringBase.

Definition at line 285 of file varstr.cpp.

00286 {
00287     ERROR3IF(this==NULL, "StringVar this pointer is NULL!");
00288     ERROR3IF(text==NULL || s==NULL, "Call to String::operator+= for an unALLOCated String");
00289 
00290     if (text && s)
00291         SafeCat(s);
00292 
00293     return *this;
00294 }

StringVar & StringVar::operator+= const StringBase rhs  )  [inline]
 

Append string y onto the end of string x, eg. x = TEXT("hello"); cout << (x += TEXT(" world!")) << endl;.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
31st January 2006
Parameters:
The string to add to the end of this string. [INPUTS]
Returns:
A reference to the lhs, ie. this StringVar as an lvalue.

Reimplemented from StringBase.

Definition at line 261 of file varstr.cpp.

00262 {
00263     ERROR3IF(this==NULL, "StringVar this pointer is NULL!");
00264     ERROR3IF(text==NULL || (const TCHAR*)rhs==NULL, "Call to StringVar::operator+= for an unALLOCated String");
00265 
00266     if (text && (const TCHAR*)rhs)
00267         SafeCat((const TCHAR*)rhs);
00268 
00269     return *this;
00270 }

StringVar & StringVar::operator= const TCHAR  ch  )  [inline]
 

Assigns the single character to this string.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
1st February 2006
Parameters:
ch - a single character [INPUTS]
Returns:
A reference to this.

Reimplemented from StringBase.

Definition at line 389 of file varstr.cpp.

00390 {
00391     ERROR3IF(this==NULL, "StringVar this pointer is NULL!");
00392     ERROR3IF(text==NULL, "Call to String::operator= for an unALLOCated String");
00393 
00394     EnsureAlloc(1);
00395     if (text && length>=2)      // length includes term
00396     {
00397         text[0] = ch;
00398         text[1] = 0;
00399     }
00400 
00401     return *this;
00402 }

StringVar & StringVar::operator= const TCHAR s  ) 
 

Assignment (set equal to) operator, eg. StringBase x("rhubarb"); x = "custard";.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
1st February 2006
Parameters:
A pointer to a constant C-style (possibly wide-character) string. [INPUTS]
Returns:
A reference to this.

Reimplemented from StringBase.

Definition at line 363 of file varstr.cpp.

00364 {
00365     ERROR3IF(this==NULL, "StringVar this pointer is NULL!");
00366     ERROR3IF(text==NULL || s==NULL, "Call to String::operator= for an unALLOCated String or 0 char*");
00367 
00368     if (text && s)
00369     {
00370         UINT32 newlen = camStrlen(s);
00371         EnsureAlloc(newlen);
00372         SafeCopy(s, newlen);
00373     }
00374 
00375     return *this;
00376 }

StringVar & StringVar::operator= const StringBase other  ) 
 

Assignment (set equal to) operator, eg. StringBase x("rhubarb"); x = "custard";.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
1st February 2006
Parameters:
A reference to a constant String. [INPUTS]
Returns:
A reference to this.

Reimplemented from StringBase.

Definition at line 336 of file varstr.cpp.

00337 {
00338     ERROR3IF(this==NULL, "StringVar this pointer is NULL!");
00339     ERROR3IF(text==NULL || (const TCHAR*)other==NULL, "Call to String::operator= for an unALLOCated String");
00340 
00341     if (text && (const TCHAR*)other)
00342     {
00343         UINT32 newlen = other.Length();
00344         EnsureAlloc(newlen);
00345         SafeCopy((const TCHAR*)other, newlen);
00346     }
00347     return *this;
00348 }

void StringVar::SafeCat const TCHAR string  )  [private]
 

Appends string to the end of this.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
31st January 2006
Parameters:
string 0-terminated source string [INPUTS]
- [OUTPUTS]
Returns:
-
See also:
StringBase::Alloc, StringVar::Alloc

Definition at line 422 of file varstr.cpp.

00423 {
00424     ERROR3IF(this==NULL || string==NULL, "StringBase::SafeCat given NULL params");
00425     if (string==NULL)
00426         return;
00427 
00428     // Are we going to overflow ?
00429     UINT32 newlen = camStrlen(text) + camStrlen(string);
00430     EnsureAlloc(newlen);
00431 
00432     // Now we can concatenate the specified string, if we allocated the buffer correcetly
00433     if (text && length>0 && (length-1) >= newlen)
00434         camStrcat(text, string);
00435 }

void StringVar::SafeCopy const TCHAR string,
UINT32  maxlen = 0
[private]
 

Assigns string to this carefully avoiding buffer overflow.

Author:
Phil_Martin (Xara Group Ltd) <camelotdev@xara.com>
Date:
1st February 2006
Parameters:
string 0-terminated source string [INPUTS]
- [OUTPUTS]
Returns:
-

Definition at line 453 of file varstr.cpp.

00454 {
00455     ERROR3IF(this==NULL || string==NULL, "StringBase::SafeCopy given NULL params");
00456 
00457     if (text!=NULL && length>0 && string!=NULL)
00458     {
00459         if (maxlen==0)
00460             maxlen = camStrlen(string);
00461 
00462         maxlen = ((length-1)<maxlen) ? length-1 : maxlen;       // length includes term
00463 
00464         camStrncpy(text, string, maxlen+1);     // Copy the string AND the terminating 0 character
00465     }
00466 }


The documentation for this class was generated from the following files:
Generated on Sat Nov 10 04:01:31 2007 for Camelot by  doxygen 1.4.4