Click here to Skip to main content
65,938 articles
CodeProject is changing. Read more.
Articles
(untagged)

Non-MFC String Class for ATL

0.00/5 (No votes)
11 Aug 2000 2  
A Non-MFC String Class for use in ATL components
  • Download demo project - 22 Kb
  • Download source files - 6 Kb
  • Introduction

    When writing ATL components, I have found that one of the hardest things for Windows programmers to overcome is the dependency on the MFC library. For dates and strings, MFC simplifies the programmer's job by handling memory allocations and type conversions within its class encapsulation. However, with that simplification comes the price of having to distribute the MFC DLL's with your component.

    This article focuses on the issues surrounding the use of strings in an ATL component. If you don't include MFC support in your ATL component, you obviously won't have the CString available to use. There have been several articles published that address this issue. Several of the ones that I have read are:

    • NonMFC:CString (posted on www.worldofatl.com) - This article provides a new CString class based on the standard template library (STL).
    • Add MFC's CString to ATL w/ No MFC dependencies (by K. Shane Triem (www.codeguru.com) - This article illustrates how to use a Visual Studio macro to extract the CString class source code from the MFC library and then use this class in your ATL component.

    CCOMString

    While both of these articles are viable alternatives to the MFC CString class, I wanted a class that would compile under both ANSI and Unicode and had no dependencies whatsoever: neither MFC or STL. My alternative was to write a string handling class entitled CCOMString which is based entirely on Visual C++'s TCHAR datatype.

    The TCHAR datatype is defined as follows:

    • As a char when _MBCS is defined in your program.
    • As a wchar_t when _UNICODE is defined in your program.
    • As a char when neither _MBCS or _UNICODE is defined in your program.

    Therefore, by using the TCHAR datatype, CCOMString is compatible with both ANSI and Unicode compile modes. Please note that there is one caveat to this class in its present state of development: it does not currently include exception handling for the memory allocation functions. I am currently trapping any memory allocation errors with the ATLASSERT macro, but these will compile away in Release mode (I'm still researching a way to handle these errors without adding the exception handling overhead to the class).

    The current version of the CCOMString includes the following functionality. Please note, if there are any functions in the CCOMString class that you download that are not listed in the text below, they are considered to be undocumented and, therefore, possibly untest as well:

    Constructors

  • CCOMString() - Constructs an empty string.
  • CCOMString(CCOMString&) - Constructs a string from another CCOMString.
  • CCOMString(LPCTSTR) - Constructs a string from a LPCTSTR (i.e., const TCHAR*).
  • CCOMString(BSTR) - Constructs a string from a BSTR.
  • CCOMString(TCHAR, int) - Constructs a string from a character repeated an indicated number of times.
  • Assignment Operations

  • operator=(CCOMString&) - Copies another CCOMString object to the CCOMString object.
  • operator=(LPCTSTR) - Copies a LPCTSTR to the CCOMString object.
  • operator=(BSTR) - Copies a BSTR to the CCOMString object.
  • operator LPCTSTR() - Returns a const TCHAR* from the CCOMString object.
  • TCHAR* GetString() - Obtains a pointer to the string contained in the CCOMString object.
  • BSTR AllocSysString() - Allocates a BSTR from the CCOMString object.
  • Concatenation

  • <code>operator+=(CCOMString&) - Concatenates a CCOMString object to the end of the CCOMString object.
  • operator+=(LPCTSTR) - Concatenates a const TCHAR* to the end of the CCOMString object.
  • operator+=(BSTR) - Concatenates a BSTR to the end of the CCOMString object.
  • operator+=(TCHAR) - Concatenates a TCHAR to the end of the CCOMString object.
  • operator+(CCOMString&, CCOMString&) - Concatenates two CCOMString objects together.
  • operator+(CCOMString&, LPCTSTR) - Concatenates a CCOMString object and a const TCHAR* together.
  • operator+(LPCTSTR, CCOMString&) - Concatenates a const TCHAR* and CCOMString object together.
  • operator+(CCOMString&, BSTR) - Concatenates a CCOMString object and a BSTR together.
  • operator+(BSTR, CCOMString&) - Concatenates a BSTR and a CCOMString object together.
  • Accessors for the String as an Array

  • GetLength() - Returns the length of the CCOMString string as an integer.
  • IsEmpty() - Returns TRUE or FALSE depending on whether or not the CCOMString string is empty.
  • Empty() - Sets the CCOMString string to an empty string (i.e., _T("")).
  • GetAt(int) - Returns a TCHAR character from the CCOMString string at the specified location.
  • operator[] (int) - Same functionality as GetAt(int).
  • SetAt(int, TCHAR) - Sets the character at the specified location of the CCOMString string to the specified character.
  • Conversions

  • MakeUpper() - Converts the CCOMString string into all uppercase characters.
  • MakeLower() - Converts the CCOMString string into all lowercase characters.
  • MakeReverse() - Reverses the sequence of the characters contained in the CCOMString string.
  • TrimLeft() - Removes all spaces from the left-hand side of the CCOMString string.
  • TrimRight() - Removes all spaces from the right-hand side of the CCOMString string.
  • Searching

  • Find(TCHAR) - Returns the first position in the CCOMString string of the specified character.
  • Find(TCHAR, int nStart) - Returns the first position after the nStart position in the CCOMString string of the specified character.
  • Find(LPCTSTR lpszSub) - Returns the first position in the CCOMString string of the specified character string.
  • Find(LPCTSTR lpszSub, int nStart) - Returns the first position after the nStart position in the CCOMString string of the specified character string.
  • Extraction

  • Mid(int) - Returns a CCOMString object containing the character starting at the specified position to the end of the CCOMString string.
  • Mid(int, int) - Returns a CCOMString object containing the character starting at the specified position and extending for the specified length of the CCOMString string.
  • Left(int nCount) - Returns nCount characters starting at the left-hand side of the CCOMString string.
  • Right(int nCount) - Returns nCount characters starting at the right-hand side of the CCOMString string.
  • Replacing

  • Replace(TCHAR chOld, TCHAR chNew) - Replaces all the chOld characters in the CCOMString string with the chNew character.
  • Replace(LPCTSTR lpszOld, LPCTSTR lpszNew) - Replaces all the lpszOld strings in the CCOMString string with the lpszNew string.
  • Comparison

  • Compare(CCOMString&) - Compares the current CCOMString string with the specified CCOMString string and returns zero if equal and non-zero if not equal. This comparision is case sensitive.
  • Compare(LPCTSTR) - Compares the current CCOMString string with the specified const TCHAR* and returns zero if equal and non-zero if not equal. This comparision is case sensitive.
  • operator==(const CCOMString&, const CCOMString&) - Same functionality as Compare(CCOMString&).
  • operator==(const CCOMString&, LPCTSTR) - Same functionality as Compare(LPCTSTR).
  • operator==(LPCTSTR, const CCOMString&) - Same functionality as Compare(LPCTSTR).
  • operator!=(const CCOMString&, const CCOMString&) - Compares the current CCOMString string with the specified CCOMString string and returns zero if not equal and non-zero if equal. This comparision is case sensitive.
  • operator!=(const CCOMString&, LPCTSTR) - Compares the current CCOMString string with the specified const TCHAR* and returns zero if not equal and non-zero if equal.This comparision is case sensitive.
  • operator!=(LPCTSTR, const CCOMString&) - Same functionality as operator !=(const CCOMString&, LPCTSTR).
  • CompareNoCase(CCOMString&) - Compares the current CCOMString string with the specified CCOMString string and returns zero if equal and non-zero if not equal. This comparision is not case sensitive.
  • CompareNoCase(LPCTSTR) - Compares the current CCOMString string with the specified const TCHAR* and returns zero if equal and non-zero if not equal. This comparision is not case sensitive.
  • Formatting

  • Format(LPCTSTR, ...) - Formats the string similar to the C function printf().
  • History

    28 June 2000 - memory leak fix in source

    12 August 2000 - updated source ZIP file

    License

    This article has no explicit license attached to it but may contain usage terms in the article text or the download files themselves. If in doubt please contact the author via the discussion board below.

    A list of licenses authors might use can be found here