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

Windows Kernel

Development Platform:

Visual C++

  1. /////////////////////////////////////////////////////////////////////////////
  2. // STRCLASS.CPP
  3. //
  4. // Implementation of CString.
  5. //
  6. // History:
  7. //
  8. // Author   Date        Description
  9. // ------   ----        -----------
  10. // jaym     07/26/96    Created
  11. // jaym     08/26/96    Added LoadString
  12. /////////////////////////////////////////////////////////////////////////////
  13. #include "precomp.h"
  14. #include "strclass.h"
  15. /////////////////////////////////////////////////////////////////////////////
  16. // Design constants
  17. /////////////////////////////////////////////////////////////////////////////
  18. #define MAX_LOADSTRING_LEN  128
  19.     // Maximum length of a LoadString string. NOTE: The Windows imposed max
  20.     // string length is 4K. But, for performance reasons, we have choosen a
  21.     // much smaller number which relates to this applications largest string
  22.     // resource. [jm]
  23. /////////////////////////////////////////////////////////////////////////////
  24. // CString
  25. /////////////////////////////////////////////////////////////////////////////
  26. CString::CString()
  27. {
  28.     Initialize();
  29. }
  30. CString::CString
  31. (
  32.     int cSize
  33. )
  34. {
  35.     Initialize();
  36.     if (cSize > 0)
  37.         Initialize(cSize);
  38. }
  39. CString::CString
  40. (
  41.     char * str
  42. )
  43. {
  44.     Initialize();
  45.     if (str)
  46.     {
  47.         int cSize = lstrlen(str);
  48.         if (Initialize(cSize))
  49.             lstrcpy(m_str, str);
  50.     }
  51. }
  52. CString::CString
  53. (
  54.     BSTR & bstr
  55. )
  56. {
  57.     int cSize = ::SysStringLen(bstr)*2;
  58.     Initialize();
  59.     if (Initialize(cSize))
  60.         ::WideCharToMultiByte(CP_ACP, 0, bstr, -1, m_str, cSize+1, NULL, NULL);
  61. }
  62. CString::CString
  63. (
  64.     CString & str
  65. )
  66. {
  67.     int cSize = str.GetSize();
  68.     Initialize();
  69.     if (Initialize(cSize))
  70.         str.GetString(m_str);
  71. }
  72. /////////////////////////////////////////////////////////////////////////////
  73. // CString::Initialize
  74. /////////////////////////////////////////////////////////////////////////////
  75. BOOL CString::Initialize
  76. (
  77.     int cSize
  78. )
  79. {
  80.     if (m_cLen > 0)
  81.         Empty();
  82.     m_str = new char [cSize+1];
  83.     if (m_str)
  84.     {
  85.         m_cLen = cSize+1;
  86.         return TRUE;
  87.     }
  88.     else
  89.         Initialize();
  90.     return FALSE;
  91. }
  92. /////////////////////////////////////////////////////////////////////////////
  93. // CString::operator= (CString)
  94. /////////////////////////////////////////////////////////////////////////////
  95. CString & CString::operator=
  96. (
  97.     CString & str
  98. )
  99. {
  100.     Empty();
  101.     int cSize = str.GetSize();
  102.     if (Initialize(cSize))
  103.         str.GetString(m_str);
  104.     return *this;
  105. }
  106. /////////////////////////////////////////////////////////////////////////////
  107. // CString::operator= (BSTR)
  108. /////////////////////////////////////////////////////////////////////////////
  109. CString & CString::operator=
  110. (
  111.     BSTR & bstr
  112. )
  113. {
  114.     Empty();
  115.     int cSize = ::SysStringLen(bstr)*2;
  116.     if (Initialize(cSize))
  117.         ::WideCharToMultiByte(CP_ACP, 0, bstr, -1, m_str, cSize+1, NULL, NULL);
  118.     return *this;
  119. }
  120. /////////////////////////////////////////////////////////////////////////////
  121. // CString::operator= (char *)
  122. /////////////////////////////////////////////////////////////////////////////
  123. CString & CString::operator=
  124. (
  125.     char * psz
  126. )
  127. {
  128.     EVAL(SetString(psz));
  129.     return *this;
  130. }
  131. /////////////////////////////////////////////////////////////////////////////
  132. // CString::operator= (const char *)
  133. /////////////////////////////////////////////////////////////////////////////
  134. CString & CString::operator=
  135. (
  136.     const char * psz
  137. )
  138. {
  139.     EVAL(SetString(psz));
  140.     return *this;
  141. }
  142. /////////////////////////////////////////////////////////////////////////////
  143. // CString::operator+=
  144. /////////////////////////////////////////////////////////////////////////////
  145. CString & CString::operator+=
  146. (
  147.     LPCTSTR psz 
  148. )
  149. {
  150.     int     cAddLen;
  151.     char *  pNew;
  152.     if ((cAddLen = lstrlen(psz)) == 0)
  153.         return *this;
  154.     pNew = new char [m_cLen+cAddLen+1];
  155.     if (NULL == pNew)
  156.         return *this;
  157.     if (m_str != NULL)
  158.     {
  159.         lstrcpy(pNew, m_str);
  160.         delete [] m_str;
  161.     }
  162.     lstrcat(pNew, psz);
  163.     m_cLen += cAddLen+1;
  164.     m_str = pNew;
  165.     return *this;
  166. }
  167. /////////////////////////////////////////////////////////////////////////////
  168. // CString::operator+=
  169. /////////////////////////////////////////////////////////////////////////////
  170. CString & CString::operator+=
  171. (
  172.     char ch
  173. )
  174. {
  175.     char * pNew;
  176.     pNew = new char [m_cLen+1];
  177.     if (m_str != NULL)
  178.     {
  179.         lstrcpy(pNew, m_str);
  180.         delete [] m_str;
  181.     }
  182.     else
  183.         return *this;
  184.     pNew[m_cLen++] = ch;
  185.     pNew[m_cLen] = '';
  186.     m_str = pNew;
  187.     return *this;
  188. }
  189. /////////////////////////////////////////////////////////////////////////////
  190. // CString::Empty
  191. /////////////////////////////////////////////////////////////////////////////
  192. void CString::Empty
  193. (
  194. )
  195. {
  196.     if (m_cLen > 0)
  197.         delete [] m_str;
  198.     Initialize();
  199. }
  200. /////////////////////////////////////////////////////////////////////////////
  201. // CString::SetString
  202. /////////////////////////////////////////////////////////////////////////////
  203. BOOL CString::SetString
  204. (
  205.     const char * strIn
  206. )
  207. {
  208.     Empty();
  209.     int cSize = lstrlen(strIn);
  210.     if (Initialize(cSize))
  211.     {
  212.             lstrcpy(m_str, strIn);
  213.             return TRUE;
  214.     }
  215.     else
  216.         return FALSE;
  217. }
  218. /////////////////////////////////////////////////////////////////////////////
  219. // CString::AllocSysString
  220. /////////////////////////////////////////////////////////////////////////////
  221. BSTR CString::AllocSysString
  222. (
  223. ) const
  224. {
  225. #if defined(_UNICODE) || defined(OLE2ANSI)
  226.     BSTR bstr = ::SysAllocString(m_str);
  227. #else
  228.     int nLen = ::MultiByteToWideChar(CP_ACP, 0, m_str, m_cLen-1, NULL, NULL);
  229.     BSTR bstr = ::SysAllocStringLen(NULL, nLen);
  230.     if (bstr != NULL)
  231.         ::MultiByteToWideChar(CP_ACP, 0, m_str, m_cLen-1, bstr, nLen);
  232. #endif
  233.     return bstr;
  234. }
  235. /////////////////////////////////////////////////////////////////////////////
  236. // CString::SetSysString
  237. /////////////////////////////////////////////////////////////////////////////
  238. BSTR CString::SetSysString
  239. (
  240.     BSTR * pbstr
  241. ) const
  242. {
  243. #if defined(_UNICODE) || defined(OLE2ANSI)
  244.     ::SysReAllocString(pbstr, m_str))
  245. #else
  246.     int nLen = ::MultiByteToWideChar(CP_ACP, 0, m_str, m_cLen-1, NULL, NULL);
  247.     if (::SysReAllocStringLen(pbstr, NULL, nLen))
  248.         MultiByteToWideChar(CP_ACP, 0, m_str, m_cLen-1, *pbstr, nLen);
  249. #endif
  250.     ASSERT(*pbstr != NULL);
  251.     return *pbstr;
  252. }
  253. /////////////////////////////////////////////////////////////////////////////
  254. // CString::LoadString
  255. /////////////////////////////////////////////////////////////////////////////
  256. BOOL CString::LoadString
  257. (
  258.     UINT nID
  259. )
  260. {
  261.     Initialize(MAX_LOADSTRING_LEN);
  262.     return (::LoadString(   _pModule->GetResourceInstance(),
  263.                             nID,
  264.                             m_str,
  265.                             MAX_LOADSTRING_LEN) != 0);
  266. }
  267. /////////////////////////////////////////////////////////////////////////////
  268. // CString::GetBuffer
  269. /////////////////////////////////////////////////////////////////////////////
  270. LPTSTR CString::GetBuffer
  271. (
  272.     int nMinBufLength
  273. )
  274. {
  275.     ASSERT(nMinBufLength >= 0);
  276.     if (nMinBufLength > m_cLen)
  277.     {
  278.         // We have to grow the buffer
  279.         char *  pOldData = m_str;
  280.         int     nOldLen = m_cLen;
  281.         if (nMinBufLength < nOldLen)
  282.             nMinBufLength = nOldLen;
  283.         m_str = new char [nMinBufLength+1];
  284.         if (NULL == m_str)
  285.         {
  286.             m_str = pOldData;
  287.             return NULL;
  288.         }
  289.         if (pOldData != NULL)
  290.         {
  291.             memcpy(m_str, pOldData, (nOldLen)*sizeof(TCHAR));
  292.             delete [] pOldData;
  293.         }
  294.         m_cLen = nMinBufLength+1;
  295.     }
  296.     return m_str;
  297. }
  298. /////////////////////////////////////////////////////////////////////////////
  299. // CString::ReleaseBuffer
  300. /////////////////////////////////////////////////////////////////////////////
  301. void CString::ReleaseBuffer
  302. (
  303.     int nNewLength
  304. )
  305. {
  306.     if (nNewLength == -1)
  307.         nNewLength = lstrlen(m_str); // Zero terminated
  308.     ASSERT(nNewLength <= m_cLen);
  309.     m_str[nNewLength] = '';
  310. }
  311. /////////////////////////////////////////////////////////////////////////////
  312. // Helper functions
  313. /////////////////////////////////////////////////////////////////////////////
  314. /////////////////////////////////////////////////////////////////////////////
  315. // MakeWideStrFromAnsi
  316. /////////////////////////////////////////////////////////////////////////////
  317. LPWSTR MakeWideStrFromAnsi
  318. (
  319.     LPSTR psz,
  320.     BYTE  bType
  321. )
  322. {
  323.     LPWSTR pwsz;
  324.     int i;
  325.     if (psz == NULL)
  326.         return NULL;
  327.     // Compute the length of the required BSTR
  328.     if ((i =  MultiByteToWideChar(CP_ACP, 0, psz, -1, NULL, 0)) <= 0)
  329.         return NULL;
  330.     // Allocate the widestr, +1 for terminating null
  331.     switch (bType) 
  332.     {
  333.         case STR_BSTR:
  334.         {
  335.             pwsz = (LPWSTR) SysAllocStringLen(NULL, i-1); // SysAllocStringLen adds
  336.             break;
  337.         }
  338.         case STR_OLESTR:
  339.         {
  340.             pwsz = (LPWSTR) CoTaskMemAlloc(i * sizeof(WCHAR));
  341.             break;
  342.         }
  343.         default:
  344.         {
  345.             TraceMsg(DM_ERROR, "Bogus String Type!");
  346.             return NULL;
  347.         }
  348.     }
  349.     if (pwsz == NULL)
  350.         return NULL;
  351.     MultiByteToWideChar(CP_ACP, 0, psz, -1, pwsz, i);
  352.     pwsz[i - 1] = 0;
  353.     return pwsz;
  354. }