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.

291 lines
6.1 KiB

  1. #include "dvosal.h"
  2. //#include "osind.h"
  3. #include "dndbg.h"
  4. #define DVOSAL_DEFAULT_CHAR "-"
  5. volatile BOOL g_fUnicode;
  6. #undef DPF_MODNAME
  7. #define DPF_MODNAME "OSAL_Initialize"
  8. HRESULT OSAL_Initialize()
  9. {
  10. g_fUnicode = OSAL_CheckIsUnicodePlatform();
  11. return S_OK;
  12. }
  13. #undef DPF_MODNAME
  14. #define DPF_MODNAME "OSAL_DeInitialize"
  15. HRESULT OSAL_DeInitialize()
  16. {
  17. return S_OK;
  18. }
  19. #undef DPF_MODNAME
  20. #define DPF_MODNAME "OSAL_IsUnicodePlatform"
  21. BOOL OSAL_IsUnicodePlatform()
  22. {
  23. return g_fUnicode;
  24. }
  25. #undef DPF_MODNAME
  26. #define DPF_MODNAME "OSAL_CheckIsUnicodePlatform"
  27. BOOL OSAL_CheckIsUnicodePlatform()
  28. {
  29. OSVERSIONINFOA ver;
  30. BOOL bReturn = FALSE;
  31. // Clear our structure since it's on the stack
  32. memset(&ver, 0, sizeof(OSVERSIONINFOA));
  33. ver.dwOSVersionInfoSize = sizeof(OSVERSIONINFOA);
  34. // Just always call the ANSI function
  35. if(!GetVersionExA(&ver))
  36. {
  37. DPF( DVF_ERRORLEVEL, "Unable to determinte platform -- setting flag to ANSI");
  38. bReturn = FALSE;
  39. }
  40. else
  41. {
  42. switch(ver.dwPlatformId)
  43. {
  44. case VER_PLATFORM_WIN32_WINDOWS:
  45. DPF(DVF_ERRORLEVEL, "Platform detected as non-NT -- setting flag to ANSI");
  46. bReturn = FALSE;
  47. break;
  48. case VER_PLATFORM_WIN32_NT:
  49. DPF(DVF_ERRORLEVEL, "Platform detected as NT -- setting flag to Unicode");
  50. bReturn = TRUE;
  51. break;
  52. default:
  53. DPF(DVF_ERRORLEVEL, "Unable to determine platform -- setting flag to ANSI");
  54. bReturn = FALSE;
  55. break;
  56. }
  57. }
  58. // Keep the compiler happy
  59. return bReturn;
  60. } // OS_IsUnicodePlatform
  61. #undef DPF_MODNAME
  62. #define DPF_MODNAME "OSAL_AllocAndConvertToANSI"
  63. /*
  64. ** GetAnsiString
  65. *
  66. * CALLED BY: Everywhere
  67. *
  68. * PARAMETERS: *ppszAnsi - pointer to string
  69. * lpszWide - string to copy
  70. *
  71. * DESCRIPTION: handy utility function
  72. * allocs space for and converts lpszWide to ansi
  73. *
  74. * RETURNS: string length
  75. *
  76. */
  77. HRESULT OSAL_AllocAndConvertToANSI(LPSTR * ppszAnsi,LPCWSTR lpszWide)
  78. {
  79. int iStrLen;
  80. DNASSERT(ppszAnsi);
  81. if (!lpszWide)
  82. {
  83. *ppszAnsi = NULL;
  84. return S_OK;
  85. }
  86. // call wide to ansi to find out how big +1 for terminating NULL
  87. iStrLen = OSAL_WideToAnsi(NULL,lpszWide,0) + 1;
  88. DNASSERT(iStrLen > 0);
  89. *ppszAnsi = new char[iStrLen];
  90. if (!*ppszAnsi)
  91. {
  92. DPF(DVF_ERRORLEVEL, "could not get ansi string -- out of memory");
  93. return E_OUTOFMEMORY;
  94. }
  95. OSAL_WideToAnsi(*ppszAnsi,lpszWide,iStrLen);
  96. return S_OK;
  97. } // GetAnsiString
  98. #undef DPF_MODNAME
  99. #define DPF_MODNAME "OSAL_WideToAnsi"
  100. /*
  101. ** WideToAnsi
  102. *
  103. * CALLED BY: everywhere
  104. *
  105. * PARAMETERS: lpStr - destination string
  106. * lpWStr - string to convert
  107. * cchStr - size of dest buffer
  108. *
  109. * DESCRIPTION:
  110. * converts unicode lpWStr to ansi lpStr.
  111. * fills in unconvertable chars w/ DPLAY_DEFAULT_CHAR "-"
  112. *
  113. *
  114. * RETURNS: if cchStr is 0, returns the size required to hold the string
  115. * otherwise, returns the number of chars converted
  116. *
  117. */
  118. int OSAL_WideToAnsi(LPSTR lpStr,LPCWSTR lpWStr,int cchStr)
  119. {
  120. int rval;
  121. //PREFIX: using uninitialized memory 'bDefault', Mill Bug#129165
  122. // bDefault is passed by reference to WideCharToMultiByte, but we will keep prefix happy
  123. BOOL bDefault = 0x0;
  124. if (!lpWStr && cchStr)
  125. {
  126. // can't call us w/ null pointer & non-zero cch
  127. DNASSERT(FALSE);
  128. return 0;
  129. }
  130. // use the default code page (CP_ACP)
  131. // -1 indicates WStr must be null terminated
  132. rval = WideCharToMultiByte(CP_ACP,0,lpWStr,-1,lpStr,cchStr,
  133. DVOSAL_DEFAULT_CHAR,&bDefault);
  134. if (bDefault)
  135. {
  136. DPF(DVF_WARNINGLEVEL,"!!! WARNING - used default string in WideToAnsi conversion.!!!");
  137. DPF(DVF_WARNINGLEVEL,"!!! Possible bad unicode string - (you're not hiding ansi in there are you?) !!! ");
  138. }
  139. return rval;
  140. } // WideToAnsi
  141. #undef DPF_MODNAME
  142. #define DPF_MODNAME "OSAL_AnsiToWide"
  143. /*
  144. ** AnsiToWide
  145. *
  146. * CALLED BY: everywhere
  147. *
  148. * PARAMETERS: lpWStr - dest string
  149. * lpStr - string to convert
  150. * cchWstr - size of dest buffer
  151. *
  152. * DESCRIPTION: converts Ansi lpStr to Unicode lpWstr
  153. *
  154. *
  155. * RETURNS: if cchStr is 0, returns the size required to hold the string
  156. * otherwise, returns the number of chars converted
  157. *
  158. */
  159. int OSAL_AnsiToWide(LPWSTR lpWStr,LPCSTR lpStr,int cchWStr)
  160. {
  161. int rval;
  162. if (!lpStr && cchWStr)
  163. {
  164. // can't call us w/ null pointer & non-zero cch
  165. DNASSERT(FALSE);
  166. return 0;
  167. }
  168. rval = MultiByteToWideChar(CP_ACP,0,lpStr,-1,lpWStr,cchWStr);
  169. return rval;
  170. } // AnsiToWide
  171. /*
  172. ** WideToTChar
  173. *
  174. * CALLED BY: everywhere
  175. *
  176. * PARAMETERS: lpTStr - destination string
  177. * lpWStr - string to convert
  178. * cchTStr - size of dest buffer
  179. *
  180. * DESCRIPTION:
  181. * converts unicode lpWStr to TCHAR lpTStr.
  182. * fills in unconvertable chars w/ DPLAY_DEFAULT_CHAR "-"
  183. *
  184. *
  185. * RETURNS: if cchTStr is 0, returns the size required to hold the string
  186. * otherwise, returns the number of chars converted
  187. *
  188. */
  189. int OSAL_WideToTChar(LPTSTR lpTStr,LPCWSTR lpWStr,int cchTStr)
  190. {
  191. #if defined(UNICODE)
  192. // no conversion required, just copy the string over
  193. if (!lpWStr && cchTStr)
  194. {
  195. // can't call us w/ null pointer & non-zero cch
  196. DNASSERT(FALSE);
  197. return 0;
  198. }
  199. if (cchTStr == 0)
  200. {
  201. return (wcslen(lpWStr)+1)*sizeof(TCHAR);
  202. }
  203. wcsncpy(lpTStr, lpWStr, cchTStr/sizeof(TCHAR));
  204. return (wcslen(lpTStr)+1)*sizeof(TCHAR);
  205. #else
  206. // call the conversion function
  207. return OSAL_WideToAnsi(lpTStr, lpWStr, cchTStr);
  208. #endif
  209. } // WideToTChar
  210. /*
  211. ** TCharToWide
  212. *
  213. * CALLED BY: everywhere
  214. *
  215. * PARAMETERS: lpWStr - destination string
  216. * lpTStr - string to convert
  217. * cchWStr - size of dest buffer
  218. *
  219. * DESCRIPTION:
  220. * converts TCHAR lpTStr to unicode lpWStr.
  221. *
  222. *
  223. * RETURNS: if cchWStr is 0, returns the size required to hold the string
  224. * otherwise, returns the number of chars converted
  225. *
  226. */
  227. int OSAL_TCharToWide(LPWSTR lpWStr,LPCTSTR lpTStr,int cchWStr)
  228. {
  229. #if defined(UNICODE)
  230. // no conversion required, just copy the string over
  231. if (!lpTStr && cchWStr)
  232. {
  233. // can't call us w/ null pointer & non-zero cch
  234. DNASSERT(FALSE);
  235. return 0;
  236. }
  237. if (cchWStr == 0)
  238. {
  239. return (wcslen(lpTStr)+1)*sizeof(WCHAR);
  240. }
  241. wcsncpy(lpWStr, lpTStr, cchWStr/sizeof(WCHAR));
  242. return (wcslen(lpWStr)+1)*sizeof(WCHAR);
  243. #else
  244. // call the conversion function
  245. return OSAL_AnsiToWide(lpWStr, lpTStr, cchWStr);
  246. #endif
  247. } // TCharToWide