CSTRING.H

//////////////////////////////////////////////////////////////////////////////// 
// string.h
//
// Copyright (C) 1987-1997 By Microsoft Corp. All rights reserved.
// Copyright (C) 1997 Metawise Computing, Inc. All rights reserved.
//
////////////////////////////////////////////////////////////////////////////////


#ifndef _STRING_H_
#define _STRING_H_

#ifndef __wtypes_h__
#include <wtypes.h>
#endif

#ifndef _INC_TCHAR
#include <tchar.h>
#endif
#include <assert.h>

extern TCHAR* g_NULL_STRING;
extern int _wcstombsz (char* mbstr, const wchar_t* wcstr, size_t count);
#ifndef NO_BSTR_SUPPORT
extern int _mbstowcsz (wchar_t* wcstr, const char* mbstr, size_t count);
#endif

////////////////////////////////////////////////////////////////////////////////
// String definition similar to CString in MFC

class CString
{
public:
// Constructors/Destructor
CString ();
CString (LPCSTR lpsz);
CString (LPCWSTR lpsz);
CString (const unsigned char* psz);
CString (const CString& str);
CString (REFGUID guid);
~CString ();

// Attributes & Operations
int GetLength () const;
BOOL IsEmpty () const;
void Empty ();// free up data
int Copy// return size of original string
(
OUT LPTSTR lpsz,// buffer to copy string to
IN int nSize// size of buffer
);

operator LPCTSTR() const;
LPTSTR Detach ();// return the string and detach it from this class

TCHAR GetAt (int nIndex) const;// 0 based

LPTSTR GetBuffer (int nMinLength);
LPTSTR GetBufferSetLength (int nNewLength);
void ReleaseBuffer (int nNewLength = -1);

void TrimLeft ();
void TrimRight ();
void TruncateAt (int n);

CString Left (int nCount) const;
CString Mid (int nFirst, int nCount) const;
CString Right (int nCount) const;

int Find (TCHAR ch) const;
int ReverseFind (TCHAR ch) const;

void MakeUpper ();
void MakeLower ();
void ReplaceBy (TCHAR chFrom, TCHAR chTo);

void AllocCopy (CString& dest, int nCopyLen, int nCopyIndex, int nExtraLen) const;

// Assignment operations
void AssignCopy (int nSrcLen, LPCTSTR lpszSrcData);
const CString& operator= (const CString& str);
const CString& operator= (TCHAR ch);
#ifdef _UNICODE
const CString& operator= (char ch);
#endif
const CString& operator= (LPCSTR lpsz);
const CString& operator= (LPCWSTR lpsz);
const CString& operator= (const unsigned char* psz);

// Concatenation operations
void ConcatCopy (int len1, LPCTSTR lpsz1, int len2, LPCTSTR lpsz2);
void ConcatInPlace (int len, LPCTSTR lpsz);
const CString& operator+= (const CString& str);
const CString& operator+= (TCHAR ch);
#ifdef _UNICODE
const CString& operator+= (char ch);
#endif
const CString& operator+= (LPCTSTR lpsz);

friend CString operator+ (const CString& str1, const CString& str2);

// Comparison operations
int Compare (LPCTSTR lpsz) const;
int CompareNoCase (LPCTSTR lpsz) const;

#ifndef NO_BSTR_SUPPORT
BSTR AllocSysString () const;
#endif

protected:
LPTSTR m_pchData;// buffer pointer
int m_nDataLength;// data length in TCHAR, excluding 0 terminator
int m_nBuffLength;// buffer length in TCHAR

void Init ();
void AllocBuffer
(
IN int nLen// #TCHARs to allocate
);

LPTSTR GetData () const;// return m_pchData if its not zero, else return s_NULL_STRING

// helper functions
static int PASCAL SafeStrlen// return #bytes (ANSI) or #Characters(Unicode), excluding terminating 0
(
IN LPCTSTR lpsz// could be 0
);
};


////////////////////////////////////////////////////////////////////////////////
// Compare helpers

BOOL operator== (const CString& str1, const CString& str2);
BOOL operator== (const CString& str1, LPCTSTR lpsz2);
BOOL operator== (LPCTSTR lpsz1, const CString& str2);
BOOL operator!= (const CString& str1, const CString& str2);
BOOL operator!= (const CString& str1, LPCTSTR lpsz2);
BOOL operator!= (LPCTSTR lpsz1, const CString& str2);
BOOL operator < (const CString& str1, const CString& str2);
BOOL operator < (const CString& str1, LPCTSTR lpsz2);
BOOL operator < (LPCTSTR lpsz1, const CString& str2);
BOOL operator > (const CString& str1, const CString& str2);
BOOL operator > (const CString& str1, LPCTSTR lpsz2);
BOOL operator > (LPCTSTR lpsz1, const CString& str2);
BOOL operator<= (const CString& str1, const CString& str2);
BOOL operator<= (const CString& str1, LPCTSTR lpsz2);
BOOL operator<= (LPCTSTR lpsz1, const CString& str2);
BOOL operator>= (const CString& str1, const CString& str2);
BOOL operator>= (const CString& str1, LPCTSTR lpsz2);
BOOL operator>= (LPCTSTR lpsz1, const CString& str2);


#ifndef _DEBUG

inline CString::~CString ()
{
Empty ();
}

inline void CString::Init ()
{
m_pchData = 0;
m_nDataLength = 0;
m_nBuffLength = 0;
}

inline BOOL CString::IsEmpty () const
{
return (0 == m_nDataLength);
}

inline int CString::GetLength () const
{
return m_nDataLength;
}

inline int PASCAL CString::SafeStrlen
(
IN LPCTSTR lpsz// could be 0
)
{
return (0 == lpsz) ? 0 : lstrlen(lpsz);
}

inline CString::operator LPCTSTR () const
{
return (LPCTSTR) GetData ();
}

inline TCHAR CString::GetAt (int nIndex) const
{
assert(nIndex >= 0);
assert(nIndex < m_nDataLength);
return GetData()[nIndex];
}

inline LPTSTR CString::GetData () const
{
return (m_pchData ? m_pchData : g_NULL_STRING);
}


////////////////////////////////////////////////////////////////////////////////
// Compare helpers

inline BOOL operator== (const CString& str1, const CString& str2)
{
return str1.Compare (str2) == 0;
}

inline BOOL operator== (const CString& str1, LPCTSTR lpsz2)
{
return str1.Compare (lpsz2) == 0;
}

inline BOOL operator== (LPCTSTR lpsz1, const CString& str2)
{
return str2.Compare (lpsz1) == 0;
}

inline BOOL operator!= (const CString& str1, const CString& str2)
{
return str1.Compare (str2) != 0;
}

inline BOOL operator!= (const CString& str1, LPCTSTR lpsz2)
{
return str1.Compare (lpsz2) != 0;
}

inline BOOL operator!= (LPCTSTR lpsz1, const CString& str2)
{
return str2.Compare (lpsz1) != 0;
}

inline BOOL operator < (const CString& str1, const CString& str2)
{
return str1.Compare (str2) < 0;
}

inline BOOL operator < (const CString& str1, LPCTSTR lpsz2)
{
return str1.Compare (lpsz2) < 0;
}

inline BOOL operator < (LPCTSTR lpsz1, const CString& str2)
{
return str2.Compare (lpsz1) > 0;
}

inline BOOL operator > (const CString& str1, const CString& str2)
{
return str1.Compare (str2) > 0;
}

inline BOOL operator > (const CString& str1, LPCTSTR lpsz2)
{
return str1.Compare (lpsz2) > 0;
}

inline BOOL operator > (LPCTSTR lpsz1, const CString& str2)
{
return str2.Compare (lpsz1) < 0;
}

inline BOOL operator<= (const CString& str1, const CString& str2)
{
return str1.Compare (str2) <= 0;
}

inline BOOL operator<= (const CString& str1, LPCTSTR lpsz2)
{
return str1.Compare (lpsz2) <= 0;
}

inline BOOL operator<= (LPCTSTR lpsz1, const CString& str2)
{
return str2.Compare (lpsz1) >= 0;
}

inline BOOL operator>= (const CString& str1, const CString& str2)
{
return str1.Compare (str2) >= 0;
}

inline BOOL operator>= (const CString& str1, LPCTSTR lpsz2)
{
return str1.Compare (lpsz2) >= 0;
}

inline BOOL operator>= (LPCTSTR lpsz1, const CString& str2)
{
return str2.Compare (lpsz1) <= 0;
}

inline int CString::Compare (LPCTSTR lpsz) const
{
return _tcscmp(GetData(), lpsz);
}

inline int CString::CompareNoCase (LPCTSTR lpsz) const
{
return _tcsicmp(GetData(), lpsz);
}

#endif // _DEBUG

#endif // _STRING_H_