CString Extensions for Resource Loading and String Formatting

The following classes were developed to simplify my life and clean up my code and perhaps they can do the same for you. They are simple classes, but allow an easy load of a string resource ( important for internationalization ) as well as an easy way to format strings.

Class Definitions

// Load resource
class CResString : public CString
{
public:	
 CResString(): CString() {}; 
	 
 CResString(UINT nResID) 
 { 
  Init();
  VERIFY(LoadString(nResID) != 0);
  _ASSERTE( m_pchData );
 };

 void operator () (UINT nResID)
 {
  Init();
  VERIFY(LoadString(nResID) != 0);
  _ASSERTE( m_pchData );
 }
};

// Format 
class CFmtString : public CString
{
public:	
 CFmtString(): CString() {}; 

 CFmtString( char *szFormat, ... ) 
 { 
  Init();
  va_list argList;
  va_start( argList, szFormat );
  FormatV( szFormat, argList );
 };

 CFmtString( UINT uResID, ... ) 
 { 
  Init();
  va_list argList;
  CResString sFormat( uResID );
  va_start( argList, sFormat );
  FormatV( sFormat, argList );
 };

 void operator () ( char *szFormat, ... )
 {
  Init();
  va_list argList;
  va_start( argList, szFormat );
  FormatV( szFormat, argList );
 }

 void operator () ( UINT uResID, ... )
 {
  Init();
  va_list argList;
  CResString sFormat( uResID );
  va_start( argList, sFormat );
  FormatV( sFormat, argList );
 }
};

Examples of Using the CResString Class

CResString sSection(IDS_DATAOBJECTSECTION);
	
// Use sSection

// ...

// Load another string

sSection(IDS_INVALIDOBJECT);

Example of Using the CFmtString Class

CFmtString sCommand(_T("SELECT %s FROM %s WHERE %s = %d"),
                    sColumn1, sTable, sColumn2, nID );
	
// Use sQuery

// ...

// Format another
	
sCommand(_T("UPDATE %s SET %s = %d WHERE %s = %d"),  
         sTable, sColumn1, nValue, sColumn2, nID );

Downloads

Download source - 1 Kb


Comments

  • Update for .NET?

    Posted by Legacy on 01/10/2003 12:00am

    Originally posted by: HJG

    Seems that the Init() and Release() methods aren't available in Visual Studio .NET, and the protected data member m_pchData isn't avaiable either.

    Anyone have a modified version of CResString for .NET?

    Thanks.

    Reply
  • Release your leak

    Posted by Legacy on 10/16/2001 12:00am

    Originally posted by: Josh Reed

    Calling Release() prior to reuse will cure your leak.
    How appropriate.
    I had thought that Init() would've handled this, but
    apparently not.
    Have another pint on me.

    void operator () (UINT nResID)
    {
    Release();
    Init();
    VERIFY(LoadString(nResID) != 0);
    _ASSERTE( m_pchData );
    }

    void operator () ( char *szFormat, ... )
    {
    Release();
    Init();
    va_list argList;
    va_start( argList, szFormat );
    FormatV( szFormat, argList );
    }

    void operator () ( UINT uResID, ... )
    {
    Release();
    Init();
    va_list argList;
    CResString sFormat( uResID );
    va_start( argList, 0 );
    FormatV( sFormat, argList );
    }

    Reply
  • Great but i'm leaking like a man with ten pints of beer in his belly

    Posted by Legacy on 08/29/2001 12:00am

    Originally posted by: Tim Stubbs

    CResString sSection(IDS_DATAOBJECTSECTION);
    

    // Use sSection

    // ...

    // Load another string

    sSection(IDS_INVALIDOBJECT);

    From your example...

    Problem - I'm leaking memory when i load a 2nd resource
    string into the same object... Still working out why
    atm but i thought i'd mention it in the interim. Works
    fine if you don't do the "Load another string" section
    above :(


    LATER:

    Also same problem if you reformat the same object i.e.

    CFmtString hello(_T("%s"), _T("Hello"));
    hello(_T("%s"), _T("You"));

    Reply
  • Great class, just one thing

    Posted by Legacy on 05/03/2001 12:00am

    Originally posted by: Ron van Gelderen

    This class is a great help for dealing with strings.

    There's one bug in the code that causes trouble in the CFmtString class:

    CFmtString( UINT uResID, ... )
    {
    Init();
    va_list argList;
    CResString sFormat( uResID );
    // va_start( argList, sFormat ); // this line should be replaced...
    va_start( argList, uResID); // with this line!
    FormatV( sFormat, argList );
    };

    void operator () ( UINT uResID, ... )
    {
    Init();
    va_list argList;
    CResString sFormat( uResID );
    // va_start( argList, sFormat ); // this line should be replaced...
    va_start( argList, uResID); // with this line!

    FormatV( sFormat, argList );
    }


    -Ron

    Reply
Leave a Comment
  • Your email address will not be published. All fields are required.

Top White Papers and Webcasts

  • Live Event Date: September 19, 2014 @ 2:00 p.m. ET / 11:00 a.m. PT In response to the rising number of data breaches and the regulatory and legal impact that can occur as a result of these incidents, leading analysts at Forrester Research have developed five important design principles that will help security professionals reduce their attack surface and mitigate vulnerabilities. Check out this upcoming eSeminar and join Chris Sherman of Forrester Research to learn how to deal with the influx of new device …

  • Adaptation and evolution are fundamental requirements of survival -- not only in nature, but also in business. Our world has changed dramatically in a short amount of time. Many businesses are fueling and capitalizing on this change, while others are desperately clinging to a bygone era. Who is left standing in the years and decades ahead should come as no surprise. This edition of Unleashing IT highlights the companies that are embracing new circumstances, new methods, and new opportunities. By downloading …

Most Popular Programming Stories

More for Developers

Latest Developer Headlines

RSS Feeds