strclass.h
Upload User: xhy777
Upload Date: 2007-02-14
Package Size: 24088k
Code Size: 3k
Category:

Windows Kernel

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // STRCLASS.H
  3. //
  4. // Declaration of CString.
  5. //
  6. // History:
  7. //
  8. // Author   Date        Description
  9. // ------   ----        -----------
  10. // jaym     07/26/96    Created
  11. // jaym     06/10/97    Added TCharSysAllocString and MakeWideStrFromAnsi
  12. /////////////////////////////////////////////////////////////////////////////
  13. #ifndef __STRCLASS_H__
  14. #define __STRCLASS_H__
  15. /////////////////////////////////////////////////////////////////////////////
  16. // CString
  17. /////////////////////////////////////////////////////////////////////////////
  18. class CString
  19. {
  20. // Construction/Destruction
  21. public:
  22.     CString();
  23.     CString(int cSize);
  24.     CString(char * str);
  25.     CString(BSTR & bstr);
  26.     CString(CString & str);
  27.     virtual ~CString()
  28.     { Empty(); }
  29. // Data
  30. protected:
  31.     int     m_cLen;
  32.     char *  m_str;
  33. public:
  34. // Operators
  35.     CString & operator=(CString & str);
  36.     CString & operator=(BSTR & bstr);
  37.     CString & operator=(char * psz);
  38.     CString & operator=(const char * pcsz);
  39.     CString & operator+=(LPCTSTR lpsz);
  40.     CString & operator+=(char ch);
  41.     char operator[](int nIndex)
  42.     {
  43.         ASSERT(nIndex >= 0);
  44.         ASSERT(nIndex < lstrlen(m_str));
  45.         return m_str[nIndex];
  46.     }
  47.     BOOL operator==(CString & str)
  48.     { return (lstrcmpi(m_str, str.m_str) == 0); }
  49. // Casts
  50.     operator LPCTSTR() const
  51.     { return (LPCTSTR)m_str; }
  52.     operator LPTSTR() const
  53.     { return (LPTSTR)m_str; }
  54. // String functions
  55.     void GetString(char * strOut)
  56.     { lstrcpy(strOut, m_str); }
  57.     void GetString(char * strOut, int cSize)
  58.     { lstrcpyn(strOut, m_str, cSize); }
  59.     BOOL SetString(const char * strIn);
  60.     BSTR SetSysString(BSTR * pbstr) const;
  61.     BSTR AllocSysString() const;
  62.     BOOL LoadString(UINT nID);
  63.     LPTSTR GetBuffer(int nMinBufLength);
  64.     void ReleaseBuffer(int nNewLength = -1);
  65.     int GetLength()
  66.     { return ((m_str != NULL) ? lstrlen(m_str) : 0); }
  67.     int GetSize()
  68.     { return m_cLen; }
  69.     void Empty();
  70. // Utility functions
  71. private:
  72.     void Initialize()
  73.     { m_cLen = 0; m_str = NULL; }
  74.     BOOL Initialize(int length);
  75.     CString & ConcatStr(LPCTSTR psz, int cAddLen);
  76. };
  77. /////////////////////////////////////////////////////////////////////////////
  78. // Helper functions
  79. /////////////////////////////////////////////////////////////////////////////
  80. LPWSTR MakeWideStrFromAnsi(LPSTR psz, BYTE bType);
  81. #ifdef UNICODE
  82. #define TCharSysAllocString(psz) (BSTR)SysAllocString((psz))
  83. #else
  84. #define TCharSysAllocString(psz) (BSTR)MakeWideStrFromAnsi((psz), STR_BSTR)
  85. #endif
  86. #endif  // __STRCLASS_H__