CString

Provides MFC-like string support, without MFC.

Archived content: This source code is currently inactive and may be outdated or no longer maintained or functional.

Jedes umfangreichere C++-Framework stellt Funktionen zum einfachen Umgang mit Zeichenketten bereit, so z. B. MFC mit der CString-Klasse, STL mit der string-Klasse oder Qt mit der QString-Klasse. Die CString-Klasse in Microsofts MFC-Framework für C++ erleichtert den Umgang mit Zeichenketten deutlich. Sie stellt die meisten Operationen objektorientiert zur Verfügung und nimmt die Arbeit der Prüfungen und Speicherverwaltung ab. Diesen Komfort gibt es mit der hier angebotenen CString-Klasse auch für nicht-MFC-Anwendungen.

Note on the code quality: The content on this page is possibly a bit outdated. I’m not using C++ anymore for quite some time, but I would like to keep the existing and once useful functions available.

Download

CString.7z3.9 KiBArchiv mit CPP- und Header-Datei der CString-Klasse

string.cpp12.0 KiBQuelltext der CString-Klasse

string.h5.9 KiBHeader der CString-Klasse

Contents of the header file string.h:

// Provides MFC-like string support
//
// Web: http://unclassified.software/source/cstring

#ifndef _STRING_H_INC_
#define _STRING_H_INC_

#include <windows.h>
#include <stdarg.h>

/*
 *  class CString
 *
 *  Provides MFC-like CStrings
 *
 */

class CString
{
    public:
        // Constructors & destructors

        // Main constructor
        // Reserves memory and cleans content
        CString();
        // copy constructor
        CString(const CString& strSrc);
        // from a single character
        CString(TCHAR ch, int nRepeat = 1);
        // from ANSI string
        CString(LPCSTR lpsz);
        // from unsigned char's
        CString(const unsigned char* psz);
        // from integer
        CString(int i);
        // from integer
        CString(long l);
        // from integer
        CString(unsigned long ul);

        // Main destructor
        // Frees memory
        ~CString();

        // Attributes & Operators

        // get data length
        int GetLength();
        // TRUE if zero length
        BOOL IsEmpty();
        // clear contents to empty
        void Empty();
        // return single character at zero-based index
        TCHAR GetAt(int nIndex);
        // return single character at zero-based index
        TCHAR operator[](int nIndex);
        // set a single character at zero-based index
        void SetAt(int nIndex, TCHAR ch);

        // Assignment

        // copy from another CString
        const CString& operator=(const CString& strSrc);
        // set string content to single character
        const CString& operator=(TCHAR ch);
        // copy string content from ANSI string (converts to TCHAR)
        const CString& operator=(LPCSTR lpsz);
        // copy string content from unsigned chars
        const CString& operator=(const unsigned char* psz);
        // copy string content from integer
        const CString& operator=(int i);
        // copy string content from integer
        const CString& operator=(long l);
        // copy string content from integer
        const CString& operator=(unsigned long ul);

        // return pointer to const string
        operator LPCTSTR();

        // Concatenation

        // concatenate from another CString
        const CString& operator+=(const CString& string);
        // concatenate a single character
        const CString& operator+=(TCHAR ch);
        // concatenate unsigned chars
        const CString& operator+=(const unsigned char* psz);

        friend CString __stdcall operator+(const CString& string1, const CString& string2);
        friend CString __stdcall operator+(const CString& string, TCHAR ch);
        friend CString __stdcall operator+(TCHAR ch, const CString& string);
        friend CString __stdcall operator+(const CString& string, LPCTSTR lpsz);
        friend CString __stdcall operator+(LPCTSTR lpsz, const CString& string);

        // String comparison

        // straight character comparison
        int Compare(LPCTSTR lpsz);
        // compare ignoring case
        int CompareNoCase(LPCTSTR lpsz);
        // compare unsigned chars using ==
        BOOL operator==(const char* psz);
        // compare unsigned chars using !=
        BOOL operator!=(const char* psz);

        // Sub-string extraction

        // return nCount characters starting at zero-based nFirst
        CString Mid(int nFirst, int nCount);
        // return all characters starting at zero-based nFirst
        CString Mid(int nFirst);
        // return first nCount characters in string
        CString Left(int nCount);
        // return nCount characters from end of string
        CString Right(int nCount);
        // return nth part
        CString Part(char cDelimiter, int nIndex);
        // return number of parts
        int PartCount(char cDelimiter);
        // return start position of nth part
        int PartBegin(char cDelimiter, int nIndex);

        // upper/lower/reverse conversion

        // NLS aware conversion to uppercase
        void MakeUpper();
        // NLS aware conversion to lowercase
        void MakeLower();
        // reverse string right-to-left
        void MakeReverse();

        // ansi/oem/escape conversion

        // convert string from ANSI to OEM (DOS/ASCII)
        void AnsiToOem();
        // convert string from OEM (DOS/ASCII) to ANSI
        void OemToAnsi();
        // escape string
        void Escape();
        // un-escape string
        void UnEscape();

        // trimming whitespace (either side)

        // remove whitespace starting from right edge
        void TrimRight();
        // remove whitespace starting from left side
        void TrimLeft();
        // remove whitespace from any side
        void Trim();

        // advanced manipulation

        // replace occurrences of chOld with chNew
        int Replace(TCHAR chOld, TCHAR chNew);
        // replace occurrences of substring lpszOld with lpszNew;
        // empty lpszNew removes instances of lpszOld
        int Replace(LPCTSTR lpszOld, LPCTSTR lpszNew);
        // remove occurrences of chRemove
        int Remove(TCHAR chRemove);
        // insert character at zero-based index; concatenates
        // if index is past end of string
        int Insert(int nIndex, TCHAR ch);
        // insert substring at zero-based index; concatenates
        // if index is past end of string
        int Insert(int nIndex, LPCTSTR pstr);
        // delete nCount characters starting at zero-based index
        int Delete(int nIndex, int nCount = 1);

        // searching

        // find character starting at left, -1 if not found
        int Find(TCHAR ch);
        // find character starting at right
        int ReverseFind(TCHAR ch);
        // find character starting at zero-based index and going right
        int Find(TCHAR ch, int nStart);
        // find first instance of substring
        int Find(LPCTSTR lpszSub);
        // find first instance of substring starting at zero-based index
        int Find(LPCTSTR lpszSub, int nStart);

        // formatting

        void Format(const char *pcFormat, ...);

        // Access to string implementation buffer as "C" character array

        // get pointer to modifiable buffer at least as long as nMinBufLength (or as string length if -1)
        LPTSTR GetBuffer(int nMinBufLength = -1);
        // release buffer, setting length to nNewLength (or to first nul if -1)
        void ReleaseBuffer(int nNewLength = -1);

    private:
        LPSTR m_pchData;
        int m_iLen;

        // get amount of allocated memory
        int GetAllocSize();
        // alloc's/frees memory if needed
        void SetAllocSize(int nSize);
        // copies bytes
        void CopyMem(void *pBuffer, int nLength);
        // append-copies bytes
        void AppendMem(void *pBuffer, int nLength);
        // sets terminating NULL
        void SetLength(int nLength);
};

#endif  // ifndef _STRING_H_INC_

Licence and terms of use

This software is freely available as source code and compiled version, without restrictions (“public domain”). There is no warranty, not even for merchantability or fitness for a particular purpose. I am not liable for any damage caused through appropriate or inappropriate use.

Statistic data

  • Created on 2007-04-02.
  • Ca. 350 lines of code, estimated development costs: 350 - 1 400 €