Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

167 lines
3.4 KiB

  1. //+---------------------------------------------------------------------------
  2. //
  3. // Microsoft Windows
  4. // Copyright (C) Microsoft Corporation, 2000.
  5. //
  6. // File: U S T R I N G . C P P
  7. //
  8. // Contents: Simple string class
  9. //
  10. // Notes:
  11. //
  12. // Author: mbend 17 Aug 2000
  13. //
  14. //----------------------------------------------------------------------------
  15. #include <pch.h>
  16. #pragma hdrstop
  17. #include "UString.h"
  18. #include "ComUtility.h"
  19. HRESULT CUString::HrAssign(const wchar_t * sz)
  20. {
  21. if(!sz)
  22. {
  23. Clear();
  24. return S_OK;
  25. }
  26. HRESULT hr = S_OK;
  27. wchar_t * szTemp = new wchar_t[lstrlen(sz) + 1];
  28. if(!szTemp)
  29. {
  30. hr = E_OUTOFMEMORY;
  31. }
  32. if(SUCCEEDED(hr))
  33. {
  34. lstrcpy(szTemp, sz);
  35. delete [] m_sz;
  36. m_sz = szTemp;
  37. }
  38. TraceHr(ttidError, FAL, hr, FALSE, "CUString::HrAssign");
  39. return hr;
  40. }
  41. HRESULT CUString::HrAssign(const char * sz)
  42. {
  43. if(!sz)
  44. {
  45. Clear();
  46. return S_OK;
  47. }
  48. HRESULT hr = S_OK;
  49. long nLength = MultiByteToWideChar(CP_ACP, 0, sz, -1, NULL, 0);
  50. wchar_t * szTemp = new wchar_t[nLength];
  51. if(!szTemp)
  52. {
  53. hr = E_OUTOFMEMORY;
  54. }
  55. if(SUCCEEDED(hr))
  56. {
  57. MultiByteToWideChar(CP_ACP, 0, sz, -1, szTemp, nLength);
  58. delete [] m_sz;
  59. m_sz = szTemp;
  60. }
  61. TraceHr(ttidError, FAL, hr, FALSE, "CUString::HrAssign");
  62. return hr;
  63. }
  64. HRESULT CUString::HrAppend(const wchar_t * sz)
  65. {
  66. // Ignore appending a blank/null string
  67. if(!sz)
  68. {
  69. return S_OK;
  70. }
  71. HRESULT hr = S_OK;
  72. long nLength = GetLength() + (sz?lstrlen(sz):0) + 1;
  73. wchar_t * szTemp = new wchar_t[nLength];
  74. if(!szTemp)
  75. {
  76. hr = E_OUTOFMEMORY;
  77. }
  78. if(SUCCEEDED(hr))
  79. {
  80. *szTemp = 0;
  81. if(m_sz)
  82. {
  83. lstrcpy(szTemp, m_sz);
  84. }
  85. if(sz)
  86. {
  87. lstrcat(szTemp, sz);
  88. }
  89. delete [] m_sz;
  90. m_sz = szTemp;
  91. }
  92. TraceHr(ttidError, FAL, hr, FALSE, "CUString::HrAppend");
  93. return hr;
  94. }
  95. HRESULT CUString::HrPrintf(const wchar_t * szFormat, ...)
  96. {
  97. va_list val;
  98. va_start(val, szFormat);
  99. wchar_t buf[1024];
  100. wvsprintf(buf, szFormat, val);
  101. va_end(val);
  102. HRESULT hr = HrAssign(buf);
  103. TraceHr(ttidError, FAL, hr, FALSE, "CUString::HrPrintf");
  104. return hr;
  105. }
  106. long CUString::CcbGetMultiByteLength() const
  107. {
  108. if(!GetLength())
  109. {
  110. return 1;
  111. }
  112. return WideCharToMultiByte(CP_ACP, 0, m_sz, -1, NULL, 0, NULL, NULL);
  113. }
  114. HRESULT CUString::HrGetMultiByte(char * szBuf, long ccbLength) const
  115. {
  116. HRESULT hr = S_OK;
  117. if(!GetLength())
  118. {
  119. *szBuf = '\0';
  120. }
  121. else
  122. {
  123. if(!WideCharToMultiByte(CP_ACP, 0, m_sz, -1, szBuf, ccbLength, NULL, NULL))
  124. {
  125. hr = HrFromLastWin32Error();
  126. }
  127. }
  128. TraceHr(ttidError, FAL, hr, FALSE, "CUString::HrGetMultiByte");
  129. return hr;
  130. }
  131. HRESULT CUString::HrGetMultiByteWithAlloc(char ** pszBuf) const
  132. {
  133. HRESULT hr = S_OK;
  134. long ccbLength = CcbGetMultiByteLength();
  135. char * szBuf = new char[ccbLength];
  136. if(!szBuf)
  137. {
  138. hr = E_OUTOFMEMORY;
  139. }
  140. if(SUCCEEDED(hr))
  141. {
  142. hr = HrGetMultiByte(szBuf, ccbLength);
  143. if(FAILED(hr))
  144. {
  145. delete [] szBuf;
  146. }
  147. if(SUCCEEDED(hr))
  148. {
  149. *pszBuf = szBuf;
  150. }
  151. }
  152. TraceHr(ttidError, FAL, hr, FALSE, "CUString::HrGetMultiByteWithAlloc");
  153. return hr;
  154. }