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.

362 lines
8.9 KiB

  1. //+-------------------------------------------------------------
  2. // Microsoft OLE
  3. // Copyright (C) Microsoft Corporation, 1994-1995.
  4. //
  5. // File: convert.cxx
  6. //
  7. // Contents: Conversion functions for various things.
  8. //
  9. // Classes:
  10. //
  11. // Functions: TStrToWStr
  12. // WStrToTStr
  13. // MakeUnicodeString
  14. // MakeSingleByteString
  15. //
  16. // History: 20-Feb-95 AlexE Created
  17. // 29-Jan-98 FarzanaR Ported from ctoleui tree
  18. //---------------------------------------------------------------
  19. #include <dfheader.hxx>
  20. #pragma hdrstop
  21. // Debug Object declaration
  22. DH_DECLARE;
  23. //+-------------------------------------------------------------------------
  24. //
  25. // Function: TStrToWStr
  26. //
  27. // Synopsis: Converts a TCHAR string to a WCHAR string
  28. //
  29. // Arguments: [pszSource] -- The string to convert, NULL is valid
  30. // [ppszDest] -- The location to store the new string
  31. //
  32. // Returns: S_OK if the conversion was successful, another HRESULT
  33. // if it was not.
  34. //
  35. // History: 4-20-95 kennethm Created
  36. //
  37. // Notes: If unicode is defined this function allocates memory for
  38. // the new string and does a simple 'strcpy'.
  39. //
  40. // If unicode is NOT defined, a CHAR to WCHAR conversion is
  41. // performed.
  42. //
  43. // If *ppszDest is non NULL when this function returns, the
  44. // caller is responsible for freeing the memory allocated at
  45. // [*ppszDest]
  46. //
  47. //--------------------------------------------------------------------------
  48. HRESULT TStrToWStr(LPCTSTR pszSource, LPWSTR *ppszDest)
  49. {
  50. //
  51. // Make sure the destination is a valid address
  52. //
  53. if (IsBadWritePtr(ppszDest, sizeof(ppszDest)))
  54. {
  55. DH_ASSERT(!"TStrToWStr(): Bad destination pointer");
  56. return E_INVALIDARG;
  57. }
  58. if (IsBadReadPtr(pszSource, sizeof(TCHAR)))
  59. {
  60. DH_ASSERT(!"TStrToWStr(): Bad source pointer");
  61. return E_INVALIDARG;
  62. }
  63. if ('\0' == *pszSource)
  64. {
  65. *ppszDest = NULL;
  66. DH_ASSERT(!"TStrToWStr(): Source string is empty");
  67. return E_INVALIDARG ;
  68. }
  69. #ifdef UNICODE
  70. //
  71. // If we're in a unicode world then this whole thing was in vain
  72. // but we allocate memory and copy the string anyhow
  73. //
  74. *ppszDest = new WCHAR[lstrlen(pszSource) + 1];
  75. if (NULL == *ppszDest)
  76. {
  77. return E_OUTOFMEMORY;
  78. }
  79. lstrcpy(*ppszDest, pszSource);
  80. return S_OK;
  81. #else
  82. //
  83. // Otherwise we do the conversion
  84. //
  85. return MakeUnicodeString(pszSource, ppszDest);
  86. #endif
  87. }
  88. //+-------------------------------------------------------------------------
  89. //
  90. // Function: WStrToTStr
  91. //
  92. // Synopsis: Converts a WCHAR string into a TCHAR string
  93. //
  94. // Arguments: [pszSource] -- The string to convert, NULL is valid
  95. // [ppszDest] -- The location to store the new string
  96. //
  97. // Returns: S_OK if the conversion was successful, another HRESULT
  98. // if not.
  99. //
  100. // History: 12-May-1995 alexe Created
  101. //
  102. // Notes: If unicode is defined this function allocates memory for
  103. // the new string and does a simple 'strcpy'.
  104. //
  105. // If unicode is NOT defined, a WCHAR to CHAR conversion is
  106. // performed.
  107. //
  108. // If *ppszDest is non NULL when this function returns, the
  109. // caller is responsible for freeing the memory allocated at
  110. // [*ppszDest]
  111. //
  112. //--------------------------------------------------------------------------
  113. HRESULT WStrToTStr(LPCWSTR pszSource, LPTSTR *ppszDest)
  114. {
  115. //
  116. // Make sure the destination is a valid address
  117. //
  118. if (IsBadWritePtr(ppszDest, sizeof(ppszDest)))
  119. {
  120. DH_ASSERT(!"WStrToTStr(): Bad destination pointer");
  121. return E_INVALIDARG;
  122. }
  123. if (IsBadReadPtr(pszSource, sizeof(WCHAR)))
  124. {
  125. DH_ASSERT(!"WStrToTStr(): Bad source pointer");
  126. return E_INVALIDARG;
  127. }
  128. if ('\0' == *pszSource)
  129. {
  130. *ppszDest = NULL;
  131. DH_ASSERT(!"WStrToTStr(): Source string is empty");
  132. return E_INVALIDARG ;
  133. }
  134. #ifdef UNICODE
  135. //
  136. // If we're in a unicode world then this whole thing was in vain
  137. // but we allocate memory and copy the string anyhow
  138. //
  139. *ppszDest = new WCHAR[lstrlen(pszSource) + 1];
  140. if (NULL == *ppszDest)
  141. {
  142. return E_OUTOFMEMORY;
  143. }
  144. lstrcpy(*ppszDest, pszSource);
  145. return S_OK;
  146. #else
  147. //
  148. // Otherwise we do the conversion
  149. //
  150. return MakeSingleByteString(pszSource, ppszDest);
  151. #endif
  152. }
  153. //+-------------------------------------------------------------------
  154. // Member: MakeUnicodeString
  155. //
  156. // Synopsis: Converts an LPCSTR into an LPWSTR. This function is
  157. // intended to be a helper function for other functions,
  158. // TStrToWStr() in particular, so it does NOT do any
  159. // parameter validation.
  160. //
  161. // Arguments: [pszSource] - The string to convert.
  162. //
  163. // [ppszDest] - A place to put pointer to new string.
  164. //
  165. // Returns: S_OK if all goes well, another HRESULT if not.
  166. //
  167. // Remarks: The user of this function is responsible for freeing
  168. // the memory allocated by this function using the
  169. // current 'delete' operator implementation.
  170. //
  171. // History: 07-Mar-95 AlexE Created
  172. // 28-Mar-95 AlexE Moved from strhelp.cxx
  173. //--------------------------------------------------------------------
  174. HRESULT MakeUnicodeString(LPCSTR pszSource, LPWSTR *ppszDest)
  175. {
  176. HRESULT hr = S_OK ;
  177. INT nLen = 0 ;
  178. LPWSTR pszTmp = NULL ;
  179. //
  180. // Find the length of the string in UNICODE
  181. //
  182. SetLastError(0) ;
  183. nLen = MultiByteToWideChar(
  184. CP_ACP,
  185. 0,
  186. pszSource,
  187. -1,
  188. NULL,
  189. 0) ;
  190. DH_ASSERT(0 != nLen) ;
  191. pszTmp = new WCHAR [ nLen + 1 ] ;
  192. if (NULL == pszTmp)
  193. {
  194. hr = E_OUTOFMEMORY ;
  195. }
  196. if (S_OK == hr)
  197. {
  198. SetLastError(0) ;
  199. if (nLen != MultiByteToWideChar(
  200. CP_ACP,
  201. 0,
  202. pszSource,
  203. -1,
  204. pszTmp,
  205. nLen))
  206. {
  207. hr = HRESULT_FROM_WIN32(GetLastError()) ;
  208. DH_ASSERT(S_OK == hr) ;
  209. }
  210. }
  211. if (S_OK == hr)
  212. {
  213. *ppszDest = pszTmp ;
  214. }
  215. else
  216. {
  217. if (NULL != pszTmp)
  218. {
  219. delete pszTmp ;
  220. }
  221. }
  222. return hr ;
  223. }
  224. //+-------------------------------------------------------------------
  225. // Member: MakeSingleByteString
  226. //
  227. // Synopsis: Converts an LPCWSTR into an LPSTR. This function is
  228. // intended to be a helper function for other functions,
  229. // WStrToTStr() in particular, so it does NOT do any
  230. // parameter validation.
  231. //
  232. // Arguments: [pszSource] - The string to convert.
  233. //
  234. // [ppszDest] - A place to put pointer to new string.
  235. //
  236. // Returns: S_OK if all goes well, another HRESULT if not.
  237. //
  238. // Remarks: The user of this function is responsible for freeing
  239. // the memory allocated by this function using the
  240. // current 'delete' operator implementation.
  241. //
  242. // History: 07-Mar-95 AlexE Created
  243. // 28-Mar-95 AlexE Moved from strhelp.cxx
  244. //--------------------------------------------------------------------
  245. HRESULT MakeSingleByteString(LPCWSTR pszSource, LPSTR *ppszDest)
  246. {
  247. HRESULT hr = S_OK ;
  248. INT nLen ;
  249. LPSTR pszTmp ;
  250. SetLastError(0) ;
  251. nLen = WideCharToMultiByte(
  252. CP_ACP,
  253. 0,
  254. pszSource,
  255. -1,
  256. NULL,
  257. 0,
  258. NULL,
  259. NULL) ;
  260. hr = HRESULT_FROM_WIN32(GetLastError()) ;
  261. pszTmp = new CHAR[ nLen + 1 ] ;
  262. if (NULL == pszTmp)
  263. {
  264. hr = E_OUTOFMEMORY ;
  265. }
  266. if (S_OK == hr)
  267. {
  268. SetLastError(0) ;
  269. if (nLen != WideCharToMultiByte(
  270. CP_ACP,
  271. 0,
  272. pszSource,
  273. -1,
  274. pszTmp,
  275. nLen,
  276. NULL,
  277. NULL))
  278. {
  279. hr = HRESULT_FROM_WIN32(GetLastError()) ;
  280. DH_ASSERT(S_OK == hr) ;
  281. }
  282. }
  283. if (S_OK == hr)
  284. {
  285. *ppszDest = pszTmp ;
  286. }
  287. else
  288. {
  289. if (NULL != pszTmp)
  290. {
  291. delete pszTmp ;
  292. }
  293. }
  294. return hr ;
  295. }