Leaked source code of windows server 2003
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.

325 lines
6.3 KiB

  1. #include <pch.cpp>
  2. #pragma hdrstop
  3. #include "tfc.h"
  4. #define __dwFILE__ __dwFILE_CERTLIB_CSTRING_CPP__
  5. extern HINSTANCE g_hInstance;
  6. //////////////////////////////////////////////////////////////////////////////////
  7. //////////////////////////////////////////////////////////////////////////////////
  8. // CString
  9. //////////////////////////////////////////////////////////////////////////////////
  10. //////////////////////////////////////////////////////////////////////////////////
  11. CString::CString()
  12. {
  13. Init();
  14. }
  15. CString::CString(const CString& stringSrc)
  16. {
  17. Init();
  18. *this = stringSrc;
  19. }
  20. CString::CString(LPCSTR lpsz)
  21. {
  22. Init();
  23. *this = lpsz;
  24. }
  25. CString::CString(LPCWSTR lpsz)
  26. {
  27. Init();
  28. *this = lpsz;
  29. }
  30. CString::~CString()
  31. {
  32. if (szData)
  33. {
  34. LocalFree(szData);
  35. szData = NULL;
  36. }
  37. dwDataLen = 0;
  38. }
  39. // called to initialize cstring
  40. void CString::Init()
  41. {
  42. szData = NULL;
  43. dwDataLen = 0;
  44. }
  45. // called to make cstring empty
  46. void CString::Empty()
  47. {
  48. if (szData)
  49. {
  50. // Allow us to use ReAlloc
  51. szData[0]=L'\0';
  52. dwDataLen = sizeof(WCHAR);
  53. }
  54. else
  55. dwDataLen = 0;
  56. }
  57. BOOL CString::IsEmpty() const
  58. {
  59. return ((NULL == szData) || (szData[0] == L'\0'));
  60. }
  61. LPWSTR CString::GetBuffer(DWORD cch)
  62. {
  63. // get buffer of at least cch CHARS
  64. cch ++; // incl null term
  65. cch *= sizeof(WCHAR); // cb
  66. if (cch > dwDataLen)
  67. {
  68. LPWSTR szTmp;
  69. if (szData)
  70. szTmp = (LPWSTR)LocalReAlloc(szData, cch, LMEM_MOVEABLE);
  71. else
  72. szTmp = (LPWSTR)LocalAlloc(LMEM_FIXED, cch);
  73. if (!szTmp)
  74. {
  75. LocalFree(szData);
  76. dwDataLen = 0;
  77. }
  78. else
  79. {
  80. dwDataLen = cch;
  81. }
  82. szData = szTmp;
  83. }
  84. return szData;
  85. }
  86. BSTR CString::AllocSysString() const
  87. {
  88. return SysAllocStringLen(szData, (dwDataLen-1)/sizeof(WCHAR));
  89. }
  90. DWORD CString::GetLength() const
  91. {
  92. // return # chars in string (not incl NULL term)
  93. return ((dwDataLen > 0) ? wcslen(szData) : 0);
  94. }
  95. // warning: insertion strings cannot exceed MAX_PATH chars
  96. void CString::Format(LPCWSTR lpszFormat, ...)
  97. {
  98. Empty();
  99. DWORD cch = wcslen(lpszFormat) + MAX_PATH;
  100. GetBuffer(cch); // chars (don't count NULL term)
  101. if (szData != NULL)
  102. {
  103. DWORD dwformatted;
  104. int cPrint;
  105. va_list argList;
  106. va_start(argList, lpszFormat);
  107. cPrint = _vsnwprintf(szData, cch, lpszFormat, argList);
  108. if(-1 == cPrint)
  109. {
  110. szData[cch-1] = L'\0';
  111. dwformatted = cch-1;
  112. }
  113. else
  114. {
  115. dwformatted = cPrint;
  116. }
  117. va_end(argList);
  118. dwformatted = (dwformatted+1)*sizeof(WCHAR); // cvt to bytes
  119. VERIFY (dwformatted <= dwDataLen);
  120. dwDataLen = dwformatted;
  121. }
  122. else
  123. {
  124. ASSERT(dwDataLen == 0);
  125. dwDataLen = 0;
  126. }
  127. }
  128. BOOL CString::LoadString(UINT iRsc)
  129. {
  130. WCHAR *pwszResource = myLoadResourceStringNoCache(g_hInstance, iRsc);
  131. if (NULL == pwszResource)
  132. return FALSE;
  133. Attach(pwszResource);
  134. return TRUE;
  135. }
  136. BOOL CString::FromWindow(HWND hWnd)
  137. {
  138. Empty();
  139. INT iCh = (INT)SendMessage(hWnd, WM_GETTEXTLENGTH, 0, 0);
  140. GetBuffer(iCh);
  141. if (NULL == szData)
  142. return FALSE;
  143. if (dwDataLen != (DWORD)SendMessage(hWnd, WM_GETTEXT, (WPARAM)(dwDataLen/sizeof(WCHAR)), (LPARAM)szData))
  144. {
  145. // truncation!
  146. }
  147. return TRUE;
  148. }
  149. BOOL CString::ToWindow(HWND hWnd)
  150. {
  151. return (BOOL)SendMessage(hWnd, WM_SETTEXT, 0, (LPARAM)szData);
  152. }
  153. void CString::SetAt(int nIndex, WCHAR ch)
  154. {
  155. ASSERT(nIndex <= (int)(dwDataLen / sizeof(WCHAR)) );
  156. if (nIndex <= (int)(dwDataLen / sizeof(WCHAR)) )
  157. szData[nIndex] = ch;
  158. }
  159. // test
  160. BOOL CString::IsEqual(LPCWSTR sz)
  161. {
  162. if ((szData == NULL) || (szData[0] == L'\0'))
  163. return ((sz == NULL) || (sz[0] == L'\0'));
  164. if (sz == NULL)
  165. return FALSE;
  166. return (0 == lstrcmp(sz, szData));
  167. }
  168. // assignmt
  169. const CString& CString::operator=(const CString& stringSrc)
  170. {
  171. if (stringSrc.IsEmpty())
  172. Empty();
  173. else
  174. {
  175. GetBuffer( stringSrc.GetLength() );
  176. if (szData != NULL)
  177. {
  178. CopyMemory(szData, stringSrc.szData, sizeof(WCHAR)*(stringSrc.GetLength()+1));
  179. }
  180. }
  181. return *this;
  182. }
  183. // W Const
  184. const CString& CString::operator=(LPCWSTR lpsz)
  185. {
  186. if (lpsz == NULL)
  187. Empty();
  188. else
  189. {
  190. GetBuffer(wcslen(lpsz));
  191. if (szData != NULL)
  192. {
  193. CopyMemory(szData, lpsz, sizeof(WCHAR)*(wcslen(lpsz)+1));
  194. }
  195. }
  196. return *this;
  197. }
  198. // W
  199. const CString& CString::operator=(LPWSTR lpsz)
  200. {
  201. *this = (LPCWSTR)lpsz;
  202. return *this;
  203. }
  204. // A Const
  205. const CString& CString::operator=(LPCSTR lpsz)
  206. {
  207. if (lpsz == NULL)
  208. Empty();
  209. else
  210. {
  211. DWORD cch;
  212. cch = ::MultiByteToWideChar(CP_ACP, 0, lpsz, -1, NULL, 0);
  213. GetBuffer(cch-1);
  214. if (szData != NULL)
  215. {
  216. ::MultiByteToWideChar(CP_ACP, 0, lpsz, -1, szData, cch);
  217. }
  218. }
  219. return *this;
  220. }
  221. // A
  222. const CString& CString::operator=(LPSTR lpsz)
  223. {
  224. *this = (LPCSTR)lpsz;
  225. return *this;
  226. }
  227. // concat
  228. const CString& CString::operator+=(LPCWSTR lpsz)
  229. {
  230. if (IsEmpty())
  231. {
  232. *this = lpsz;
  233. return *this;
  234. }
  235. if (lpsz != NULL)
  236. {
  237. GetBuffer(wcslen(lpsz) + GetLength() );
  238. if (szData != NULL)
  239. {
  240. wcscat(szData, lpsz);
  241. }
  242. }
  243. return *this;
  244. }
  245. const CString& CString::operator+=(const CString& string)
  246. {
  247. if (IsEmpty())
  248. {
  249. *this = string;
  250. return *this;
  251. }
  252. if (!string.IsEmpty())
  253. {
  254. GetBuffer( string.GetLength() + GetLength() ); // don't count NULL terms
  255. if (szData != NULL)
  256. {
  257. wcscat(szData, string.szData);
  258. }
  259. }
  260. return *this;
  261. }
  262. void CString::Attach(LPWSTR pcwszSrc)
  263. {
  264. if(szData)
  265. {
  266. LocalFree(szData);
  267. }
  268. dwDataLen = sizeof(WCHAR)*(wcslen(pcwszSrc)+1);
  269. szData = pcwszSrc;
  270. }