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.

379 lines
8.2 KiB

  1. #include "srheader.hxx"
  2. //+---------------------------------------------------------------------------
  3. //
  4. // Method: SrTstStrCat
  5. //
  6. // Synopsis: Concatenates Str1 and Str1 and puts resulting string in
  7. // allocated buf returns address to the buf in plpDest
  8. // (char version)
  9. //
  10. // Arguments: plpDest: pointer to pointer to the concat. string
  11. // szStr1 : pointer to the 1st string
  12. // szStr2 : pointer to the 2nd string
  13. //
  14. // Returns: HRESULT
  15. //
  16. // History: 07-28-2000 a-robog Created
  17. // 09-24-2000 ristern Moved out of the class
  18. // 10-01-2000 ristern Moved to strutils
  19. // 04-May-2001 weiyouc Copied to StrUtils.cxx
  20. //
  21. // Notes:
  22. //
  23. //----------------------------------------------------------------------------
  24. HRESULT SrTstStrCat(IN LPCSTR szStr1,
  25. IN LPCSTR szStr2,
  26. OUT LPSTR *ppDest)
  27. {
  28. HRESULT hr = S_OK;
  29. DWORD dwSize = 0;
  30. DH_VDATEPTROUT(ppDest, LPSTR);
  31. DH_VDATEPTRIN (szStr1,char);
  32. DH_VDATEPTRIN (szStr2,char);
  33. dwSize = strlen(szStr1)+strlen(szStr2)+1;
  34. *ppDest = new char[dwSize];
  35. DH_ABORTIF(NULL == *ppDest,
  36. E_OUTOFMEMORY,
  37. TEXT("new char[...]"));
  38. strcpy(*ppDest,szStr1);
  39. strcat(*ppDest,szStr2);
  40. ErrReturn:
  41. return hr;
  42. }
  43. //+---------------------------------------------------------------------------
  44. //
  45. // Method: SrTstTStrCat
  46. //
  47. // Synopsis: Concatenates Str1 and Str1 and puts resulting string in
  48. // allocated buf returns address to the buf in plpDest
  49. // (TCHAR version)
  50. //
  51. // Arguments: plpDest: pointer to pointer to the concat. string
  52. // szStr1 : pointer to the 1st string
  53. // szStr2 : pointer to the 2nd string
  54. //
  55. // Returns: HRESULT
  56. //
  57. // History: 07-28-2000 a-robog Created
  58. // 09-24-2000 ristern Moved out of the class
  59. // 09-30-2000 ristern Converted to TCHAR
  60. // 10-01-2000 ristern Moved to strutils
  61. // 04-May-2001 weiyouc Copied to StrUtils.cxx
  62. //
  63. // Notes:
  64. //
  65. //----------------------------------------------------------------------------
  66. HRESULT SrTstTStrCat(IN LPCTSTR szStr1,
  67. IN LPCTSTR szStr2,
  68. OUT LPTSTR *ppDest)
  69. {
  70. HRESULT hr = S_OK;
  71. DWORD dwSize = 0;
  72. DH_VDATEPTROUT(ppDest, LPTSTR);
  73. DH_VDATEPTRIN (szStr1, TCHAR);
  74. DH_VDATEPTRIN (szStr2, TCHAR);
  75. dwSize = _tcslen(szStr1)+_tcslen(szStr2)+1;
  76. *ppDest = new TCHAR[dwSize];
  77. DH_ABORTIF(NULL == *ppDest,
  78. E_OUTOFMEMORY,
  79. TEXT("new TCHAR[...]"));
  80. _tcscpy(*ppDest,szStr1);
  81. _tcscat(*ppDest,szStr2);
  82. ErrReturn:
  83. return hr;
  84. }
  85. //+--------------------------------------------------------------------------
  86. //
  87. // Function: CopyString
  88. //
  89. // Synopsis: Copy a wide (Unicode) string
  90. //
  91. // Parameters: [pszSource] -- The original string
  92. // [ppszDest] -- The copy
  93. //
  94. // Returns: S_OK if all went well
  95. //
  96. // History: 31-Oct-96 MikeW Created
  97. //
  98. //---------------------------------------------------------------------------
  99. HRESULT CopyString(LPCWSTR pszSource, LPWSTR *ppszDest)
  100. {
  101. size_t bufferSize;
  102. HRESULT hr = S_OK;
  103. *ppszDest = NULL;
  104. //
  105. // Find the length of the original string
  106. //
  107. bufferSize = wcslen(pszSource) + 1;
  108. //
  109. // Allocate the buffer
  110. //
  111. *ppszDest = new WCHAR[bufferSize];
  112. if (NULL == *ppszDest)
  113. {
  114. hr = E_OUTOFMEMORY;
  115. }
  116. //
  117. // Copy the string
  118. //
  119. if (S_OK == hr)
  120. {
  121. wcscpy(*ppszDest, pszSource);
  122. }
  123. return hr;
  124. }
  125. //+--------------------------------------------------------------------------
  126. //
  127. // Function: CopyString
  128. //
  129. // Synopsis: Copy a multibyte string
  130. //
  131. // Parameters: [pszSource] -- The original string
  132. // [ppszDest] -- The copy
  133. //
  134. // Returns: S_OK if all went well
  135. //
  136. // History: 31-Oct-96 MikeW Created
  137. //
  138. //---------------------------------------------------------------------------
  139. HRESULT CopyString(LPCSTR pszSource, LPSTR *ppszDest)
  140. {
  141. size_t bufferSize;
  142. HRESULT hr = S_OK;
  143. *ppszDest = NULL;
  144. //
  145. // Find the length of the original string (in bytes for DBCS)
  146. //
  147. bufferSize = strlen(pszSource) + 1;
  148. //
  149. // Allocate the buffer
  150. //
  151. *ppszDest = new char[bufferSize];
  152. if (NULL == *ppszDest)
  153. {
  154. hr = E_OUTOFMEMORY;
  155. }
  156. //
  157. // Copy the string
  158. //
  159. if (S_OK == hr)
  160. {
  161. strcpy(*ppszDest, pszSource);
  162. }
  163. return hr;
  164. }
  165. //+--------------------------------------------------------------------------
  166. //
  167. // Function: CopyString
  168. //
  169. // Synopsis: Convert a wide (Unicode) string to a multibyte string
  170. //
  171. // Parameters: [pszSource] -- The wide string
  172. // [ppszDest] -- Where to put the multibyte string
  173. //
  174. // Returns: S_OK if all went well
  175. //
  176. // History: 31-Oct-96 MikeW Created
  177. //
  178. //---------------------------------------------------------------------------
  179. HRESULT CopyString(LPCWSTR pszSource, LPSTR *ppszDest)
  180. {
  181. int bufferSize;
  182. HRESULT hr = S_OK;
  183. *ppszDest = NULL;
  184. //
  185. // Find the length of the buffer needed for the multibyte string
  186. //
  187. bufferSize = WideCharToMultiByte(
  188. CP_ACP,
  189. 0,
  190. pszSource,
  191. -1,
  192. *ppszDest,
  193. 0,
  194. NULL,
  195. NULL);
  196. if (0 == bufferSize)
  197. {
  198. hr = HRESULT_FROM_WIN32(GetLastError());
  199. }
  200. //
  201. // Allocate the buffer
  202. //
  203. if(S_OK == hr)
  204. {
  205. *ppszDest = new char[bufferSize];
  206. if (NULL == *ppszDest)
  207. {
  208. hr = E_OUTOFMEMORY;
  209. }
  210. }
  211. //
  212. // Do the conversion
  213. //
  214. if (S_OK == hr)
  215. {
  216. bufferSize = WideCharToMultiByte(
  217. CP_ACP,
  218. 0,
  219. pszSource,
  220. -1,
  221. *ppszDest,
  222. bufferSize,
  223. NULL,
  224. NULL);
  225. if (0 == bufferSize)
  226. {
  227. hr = HRESULT_FROM_WIN32(GetLastError());
  228. }
  229. }
  230. //
  231. // Clean up if there's an error
  232. //
  233. if (S_OK != hr && NULL != *ppszDest)
  234. {
  235. delete [] *ppszDest;
  236. *ppszDest = NULL;
  237. }
  238. return hr;
  239. }
  240. //+--------------------------------------------------------------------------
  241. //
  242. // Function: CopyString
  243. //
  244. // Synopsis: Convert a multibyte string to a wide (Unicode) string
  245. //
  246. // Parameters: [pszSource] -- The multibyte string
  247. // [ppszDest] -- Where to put the wide string
  248. //
  249. // Returns: S_OK if all went well
  250. //
  251. // History: 31-Oct-96 MikeW Created
  252. //
  253. //---------------------------------------------------------------------------
  254. HRESULT CopyString(LPCSTR pszSource, LPWSTR *ppszDest)
  255. {
  256. int bufferSize;
  257. HRESULT hr = S_OK;
  258. *ppszDest = NULL;
  259. //
  260. // Find the length of the buffer needed for the multibyte string
  261. //
  262. bufferSize = MultiByteToWideChar(
  263. CP_ACP,
  264. 0,
  265. pszSource,
  266. -1,
  267. *ppszDest,
  268. 0);
  269. if (0 == bufferSize)
  270. {
  271. hr = HRESULT_FROM_WIN32(GetLastError());
  272. }
  273. //
  274. // Allocate the buffer
  275. //
  276. if(S_OK == hr)
  277. {
  278. *ppszDest = new WCHAR[bufferSize];
  279. if (NULL == *ppszDest)
  280. {
  281. hr = E_OUTOFMEMORY;
  282. }
  283. }
  284. //
  285. // Do the conversion
  286. //
  287. if (S_OK == hr)
  288. {
  289. bufferSize = MultiByteToWideChar(
  290. CP_ACP,
  291. 0,
  292. pszSource,
  293. -1,
  294. *ppszDest,
  295. bufferSize);
  296. if (0 == bufferSize)
  297. {
  298. hr = HRESULT_FROM_WIN32(GetLastError());
  299. }
  300. }
  301. //
  302. // Clean up if there's an error
  303. //
  304. if (S_OK != hr && NULL != *ppszDest)
  305. {
  306. delete [] *ppszDest;
  307. *ppszDest = NULL;
  308. }
  309. return hr;
  310. }