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.

612 lines
14 KiB

  1. //+--------------------------------------------------------------------------
  2. //
  3. // Copyright (c) 1996 Microsoft Corporation
  4. //
  5. // File: convert.cxx
  6. //
  7. // Synopsis: Functions for converting between LPSTR, LPWSTR, LPTSTR, and
  8. // LPOLESTR
  9. //
  10. // Functions: CopyString (all versions)
  11. //
  12. // TStringToOleString
  13. // WStringToOleString
  14. // AStringToOleString
  15. // OleStringToTString
  16. // OleStringToWString
  17. // OleStringToAString
  18. //
  19. // History: 01-Aug-96 MikeW Created
  20. // 31-Oct-96 MikeW Re-wrote to be DBCS aware
  21. // and less code duplication
  22. //
  23. //---------------------------------------------------------------------------
  24. #include <ctolerpc.h>
  25. #pragma hdrstop
  26. //
  27. // NOTE! There are seven functions called CopyString. Collectively
  28. // they handle copying and converting strings composed of signed,
  29. // unsigned, and wide chars. C++ polymorphism serves to distinguish
  30. // them from one another.
  31. //
  32. // Three of the CopyString functions are implemented as inline
  33. // thunks defined in olestr.h
  34. //
  35. #ifndef WIN16
  36. //+--------------------------------------------------------------------------
  37. //
  38. // Function: CopyString
  39. //
  40. // Synopsis: Convert a wide (Unicode) string to a multibyte string
  41. //
  42. // Parameters: [pszSource] -- The wide string
  43. // [ppszDest] -- Where to put the multibyte string
  44. //
  45. // Returns: S_OK if all went well
  46. //
  47. // History: 31-Oct-96 MikeW Created
  48. //
  49. //---------------------------------------------------------------------------
  50. HRESULT CopyString(LPCWSTR pszSource, LPSTR *ppszDest)
  51. {
  52. int bufferSize;
  53. HRESULT hr = S_OK;
  54. *ppszDest = NULL;
  55. //
  56. // Find the length of the buffer needed for the multibyte string
  57. //
  58. bufferSize = WideCharToMultiByte(
  59. CP_ACP,
  60. 0,
  61. pszSource,
  62. -1,
  63. *ppszDest,
  64. 0,
  65. NULL,
  66. NULL);
  67. if (0 == bufferSize)
  68. {
  69. hr = HRESULT_FROM_WIN32(GetLastError());
  70. }
  71. //
  72. // Allocate the buffer
  73. //
  74. if(S_OK == hr)
  75. {
  76. *ppszDest = new char[bufferSize];
  77. if (NULL == *ppszDest)
  78. {
  79. hr = E_OUTOFMEMORY;
  80. }
  81. }
  82. //
  83. // Do the conversion
  84. //
  85. if (S_OK == hr)
  86. {
  87. bufferSize = WideCharToMultiByte(
  88. CP_ACP,
  89. 0,
  90. pszSource,
  91. -1,
  92. *ppszDest,
  93. bufferSize,
  94. NULL,
  95. NULL);
  96. if (0 == bufferSize)
  97. {
  98. hr = HRESULT_FROM_WIN32(GetLastError());
  99. }
  100. }
  101. //
  102. // Clean up if there's an error
  103. //
  104. if (S_OK != hr && NULL != *ppszDest)
  105. {
  106. delete [] *ppszDest;
  107. *ppszDest = NULL;
  108. }
  109. return hr;
  110. }
  111. //+--------------------------------------------------------------------------
  112. //
  113. // Function: CopyString
  114. //
  115. // Synopsis: Convert a multibyte string to a wide (Unicode) string
  116. //
  117. // Parameters: [pszSource] -- The multibyte string
  118. // [ppszDest] -- Where to put the wide string
  119. //
  120. // Returns: S_OK if all went well
  121. //
  122. // History: 31-Oct-96 MikeW Created
  123. //
  124. //---------------------------------------------------------------------------
  125. HRESULT CopyString(LPCSTR pszSource, LPWSTR *ppszDest)
  126. {
  127. int bufferSize;
  128. HRESULT hr = S_OK;
  129. *ppszDest = NULL;
  130. //
  131. // Find the length of the buffer needed for the multibyte string
  132. //
  133. bufferSize = MultiByteToWideChar(
  134. CP_ACP,
  135. 0,
  136. pszSource,
  137. -1,
  138. *ppszDest,
  139. 0);
  140. if (0 == bufferSize)
  141. {
  142. hr = HRESULT_FROM_WIN32(GetLastError());
  143. }
  144. //
  145. // Allocate the buffer
  146. //
  147. if(S_OK == hr)
  148. {
  149. *ppszDest = new WCHAR[bufferSize];
  150. if (NULL == *ppszDest)
  151. {
  152. hr = E_OUTOFMEMORY;
  153. }
  154. }
  155. //
  156. // Do the conversion
  157. //
  158. if (S_OK == hr)
  159. {
  160. bufferSize = MultiByteToWideChar(
  161. CP_ACP,
  162. 0,
  163. pszSource,
  164. -1,
  165. *ppszDest,
  166. bufferSize);
  167. if (0 == bufferSize)
  168. {
  169. hr = HRESULT_FROM_WIN32(GetLastError());
  170. }
  171. }
  172. //
  173. // Clean up if there's an error
  174. //
  175. if (S_OK != hr && NULL != *ppszDest)
  176. {
  177. delete [] *ppszDest;
  178. *ppszDest = NULL;
  179. }
  180. return hr;
  181. }
  182. //+--------------------------------------------------------------------------
  183. //
  184. // Function: CopyString
  185. //
  186. // Synopsis: Copy a wide (Unicode) string
  187. //
  188. // Parameters: [pszSource] -- The original string
  189. // [ppszDest] -- The copy
  190. //
  191. // Returns: S_OK if all went well
  192. //
  193. // History: 31-Oct-96 MikeW Created
  194. //
  195. //---------------------------------------------------------------------------
  196. HRESULT CopyString(LPCWSTR pszSource, LPWSTR *ppszDest)
  197. {
  198. int bufferSize;
  199. HRESULT hr = S_OK;
  200. *ppszDest = NULL;
  201. //
  202. // Find the length of the original string
  203. //
  204. bufferSize = wcslen(pszSource) + 1;
  205. //
  206. // Allocate the buffer
  207. //
  208. *ppszDest = new WCHAR[bufferSize];
  209. if (NULL == *ppszDest)
  210. {
  211. hr = E_OUTOFMEMORY;
  212. }
  213. //
  214. // Copy the string
  215. //
  216. if (S_OK == hr)
  217. {
  218. wcscpy(*ppszDest, pszSource);
  219. }
  220. return hr;
  221. }
  222. //+--------------------------------------------------------------------------
  223. //
  224. // Function: CopyString
  225. //
  226. // Synopsis: Convert a wide (Unicode) string to a multibyte string
  227. // cchSource can be -1 which means source is null terminated.
  228. //
  229. // Parameters: [pszSource] -- The wide string
  230. // [pszDest] -- Where to put the multibyte string
  231. // [cchSource] -- count of characters of source
  232. // [cchDest] -- count of characters of destination
  233. //
  234. // Returns: S_OK if all went well
  235. //
  236. // History: 31-Oct-96 MikeW Created
  237. //
  238. //---------------------------------------------------------------------------
  239. HRESULT CopyString(LPCWSTR pszSource, LPSTR pszDest,
  240. int cchSource, int cchDest)
  241. {
  242. int bufferSize;
  243. HRESULT hr = S_OK;
  244. //
  245. // Find the length of the buffer needed for the multibyte string
  246. //
  247. bufferSize = WideCharToMultiByte(
  248. CP_ACP,
  249. 0,
  250. pszSource,
  251. cchSource,
  252. pszDest,
  253. 0,
  254. NULL,
  255. NULL);
  256. if (0 == bufferSize)
  257. {
  258. hr = HRESULT_FROM_WIN32(GetLastError());
  259. }
  260. if (bufferSize > cchDest)
  261. {
  262. hr = E_INVALIDARG;
  263. }
  264. //
  265. // Do the conversion
  266. //
  267. if (S_OK == hr)
  268. {
  269. bufferSize = WideCharToMultiByte(
  270. CP_ACP,
  271. 0,
  272. pszSource,
  273. cchSource,
  274. pszDest,
  275. cchDest,
  276. NULL,
  277. NULL);
  278. if (0 == bufferSize)
  279. {
  280. hr = HRESULT_FROM_WIN32(GetLastError());
  281. }
  282. }
  283. return hr;
  284. }
  285. //+--------------------------------------------------------------------------
  286. //
  287. // Function: CopyString
  288. //
  289. // Synopsis: Convert a multibyte string to a wide (Unicode) string
  290. // cchSource can be -1 which means source is null terminated.
  291. //
  292. // Parameters: [pszSource] -- The multibyte string
  293. // [pwszDest] -- Where to put the wide string
  294. // [cchSource] -- count of characters of source
  295. // [cchDest] -- count of characters of destination
  296. //
  297. // Returns: S_OK if all went well
  298. //
  299. // History: 31-Oct-96 MikeW Created
  300. //
  301. //---------------------------------------------------------------------------
  302. HRESULT CopyString(LPCSTR pszSource, LPWSTR pwszDest,
  303. int cchSource, int cchDest)
  304. {
  305. int bufferSize;
  306. HRESULT hr = S_OK;
  307. //
  308. // Find the length of the buffer needed for the multibyte string
  309. //
  310. bufferSize = MultiByteToWideChar(
  311. CP_ACP,
  312. 0,
  313. pszSource,
  314. cchSource,
  315. pwszDest,
  316. 0);
  317. if (0 == bufferSize)
  318. {
  319. hr = HRESULT_FROM_WIN32(GetLastError());
  320. }
  321. if (bufferSize > cchDest)
  322. {
  323. hr = E_INVALIDARG;
  324. }
  325. //
  326. // Do the conversion
  327. //
  328. if (S_OK == hr)
  329. {
  330. bufferSize = MultiByteToWideChar(
  331. CP_ACP,
  332. 0,
  333. pszSource,
  334. cchSource,
  335. pwszDest,
  336. cchDest);
  337. if (0 == bufferSize)
  338. {
  339. hr = HRESULT_FROM_WIN32(GetLastError());
  340. }
  341. }
  342. return hr;
  343. }
  344. #endif
  345. //+--------------------------------------------------------------------------
  346. //
  347. // Function: CopyString
  348. //
  349. // Synopsis: Copy a multibyte string
  350. //
  351. // Parameters: [pszSource] -- The original string
  352. // [ppszDest] -- The copy
  353. //
  354. // Returns: S_OK if all went well
  355. //
  356. // History: 31-Oct-96 MikeW Created
  357. //
  358. //---------------------------------------------------------------------------
  359. HRESULT CopyString(LPCSTR pszSource, LPSTR *ppszDest)
  360. {
  361. int bufferSize;
  362. HRESULT hr = S_OK;
  363. *ppszDest = NULL;
  364. //
  365. // Find the length of the original string (in bytes for DBCS)
  366. //
  367. bufferSize = strlen(pszSource) + 1;
  368. //
  369. // Allocate the buffer
  370. //
  371. *ppszDest = new char[bufferSize];
  372. if (NULL == *ppszDest)
  373. {
  374. hr = E_OUTOFMEMORY;
  375. }
  376. //
  377. // Copy the string
  378. //
  379. if (S_OK == hr)
  380. {
  381. strcpy(*ppszDest, pszSource);
  382. }
  383. return hr;
  384. }
  385. //+----------------------------------------------------------------------------
  386. //
  387. // -X-StringTo-Y-String functions
  388. //
  389. // Synopsis: Convert from a 'T' string to an Ole string
  390. //
  391. // Parameters: [pszSource] -- The T string
  392. // [ppszDest] -- Where to put the Ole string
  393. //
  394. // Returns: S_OK if all went well
  395. //
  396. // Notes: The implementation of these functions are all identical.
  397. // C++ polymorphism serves to make sure the right version
  398. // of CopyString gets called.
  399. //
  400. // If *ppszDest is non NULL when this function returns, the
  401. // caller is responsible for freeing the memory allocated at
  402. // [*ppszDest]
  403. //
  404. // For the XStringToYString functions involving Ole types:
  405. //
  406. // If system and OLE use the same string type (e.g UNICODE on
  407. // NT; CHAR on MAC & Win16) , then this function allocates memory
  408. // for the new string and does a simple string copy.
  409. //
  410. // If UNICODE is NOT defined, but OLE is unicode (as in Win95),
  411. // a CHAR to WCHAR conversion is performed.
  412. //
  413. // If UNICODE is defined, but OLE is NOT unicode (not in any
  414. // system at present), a WCHAR to CHAR conversion is performed.
  415. //
  416. //-----------------------------------------------------------------------------
  417. HRESULT TStringToOleString(LPCTSTR pszSource, LPOLESTR *ppszDest)
  418. {
  419. if (NULL == pszSource)
  420. {
  421. *ppszDest = NULL;
  422. return S_OK;
  423. }
  424. else
  425. {
  426. return CopyString(pszSource, ppszDest);
  427. }
  428. }
  429. HRESULT OleStringToTString(LPCOLESTR pszSource, LPTSTR *ppszDest)
  430. {
  431. if (NULL == pszSource)
  432. {
  433. *ppszDest = NULL;
  434. return S_OK;
  435. }
  436. else
  437. {
  438. return CopyString(pszSource, ppszDest);
  439. }
  440. }
  441. HRESULT AStringToOleString(LPCSTR pszSource, LPOLESTR *ppszDest)
  442. {
  443. if (NULL == pszSource)
  444. {
  445. *ppszDest = NULL;
  446. return S_OK;
  447. }
  448. else
  449. {
  450. return CopyString(pszSource, ppszDest);
  451. }
  452. }
  453. HRESULT OleStringToAString(LPCOLESTR pszSource, LPSTR *ppszDest)
  454. {
  455. if (NULL == pszSource)
  456. {
  457. *ppszDest = NULL;
  458. return S_OK;
  459. }
  460. else
  461. {
  462. return CopyString(pszSource, ppszDest);
  463. }
  464. }
  465. HRESULT AStringToTString(LPCSTR pszSource, LPTSTR *ppszDest)
  466. {
  467. if (NULL == pszSource)
  468. {
  469. *ppszDest = NULL;
  470. return S_OK;
  471. }
  472. else
  473. {
  474. return CopyString(pszSource, ppszDest);
  475. }
  476. }
  477. HRESULT TStringToAString(LPCTSTR pszSource, LPSTR *ppszDest)
  478. {
  479. if (NULL == pszSource)
  480. {
  481. *ppszDest = NULL;
  482. return S_OK;
  483. }
  484. else
  485. {
  486. return CopyString(pszSource, ppszDest);
  487. }
  488. }
  489. #ifndef WIN16
  490. HRESULT WStringToOleString(LPCWSTR pszSource, LPOLESTR *ppszDest)
  491. {
  492. if (NULL == pszSource)
  493. {
  494. *ppszDest = NULL;
  495. return S_OK;
  496. }
  497. else
  498. {
  499. return CopyString(pszSource, ppszDest);
  500. }
  501. }
  502. HRESULT OleStringToWString(LPCOLESTR pszSource, LPWSTR *ppszDest)
  503. {
  504. if (NULL == pszSource)
  505. {
  506. *ppszDest = NULL;
  507. return S_OK;
  508. }
  509. else
  510. {
  511. return CopyString(pszSource, ppszDest);
  512. }
  513. }
  514. #endif