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.

168 lines
2.5 KiB

  1. /*++
  2. Copyright (c) 1990-2003 Microsoft Corporation
  3. Module Name:
  4. widechar.c
  5. Abstract:
  6. This module contains all NLS unicode / ansi translation code
  7. Author:
  8. 18-Nov-1993 Thu 08:21:37 created
  9. [Environment:]
  10. GDI Device Driver - Plotter.
  11. [Notes:]
  12. Revision History:
  13. --*/
  14. #include "precomp.h"
  15. #pragma hdrstop
  16. LPWSTR
  17. str2Wstr(
  18. LPWSTR pwStr,
  19. size_t cchDest,
  20. LPSTR pbStr
  21. )
  22. /*++
  23. Routine Description:
  24. This function copy a ansi string to the equvlent of unicode string which
  25. also include the NULL teiminator
  26. Arguments:
  27. pwStr - Point to the unicode string location, it must have the size of
  28. (strlen(pstr) + 1) * sizeof(WCHAR)
  29. pbStr - a null teiminated string
  30. Return Value:
  31. pwcs
  32. Author:
  33. 18-Nov-1993 Thu 08:36:00 created
  34. Revision History:
  35. --*/
  36. {
  37. size_t cch;
  38. if (NULL == pbStr || NULL == pwStr)
  39. {
  40. return NULL;
  41. }
  42. //
  43. // Make sure that the size of dest buffer is large enough.
  44. //
  45. if (SUCCEEDED(StringCchLengthA(pbStr, cchDest, &cch)))
  46. {
  47. //
  48. // cch returned above does not include the null terminator.
  49. // So we need to add 1 to cch to make sure destination string is
  50. // null terminated.
  51. //
  52. AnsiToUniCode(pbStr, pwStr, cch+1);
  53. return pwStr;
  54. }
  55. else
  56. {
  57. return NULL;
  58. }
  59. }
  60. LPSTR
  61. WStr2Str(
  62. LPSTR pbStr,
  63. size_t cchDest,
  64. LPWSTR pwStr
  65. )
  66. /*++
  67. Routine Description:
  68. This function convert a UNICODE string to the ANSI string, assume that
  69. pbStr has same character count memory as pwStr
  70. Arguments:
  71. pbStr - Point to the ANSI string which has size of wcslen(pwStr) + 1
  72. pwStr - Point to the UNICODE string
  73. Return Value:
  74. pbStr
  75. Author:
  76. 06-Dec-1993 Mon 13:06:12 created
  77. Revision History:
  78. --*/
  79. {
  80. size_t cch;
  81. if (NULL == pbStr || NULL == pwStr)
  82. {
  83. return NULL;
  84. }
  85. if (SUCCEEDED(StringCchLengthW(pwStr, cchDest, &cch)))
  86. {
  87. //
  88. // cch returned above does not include the null terminator.
  89. // So we need to add 1 to cch to make sure destination string is
  90. // null terminated.
  91. //
  92. UniCodeToAnsi(pbStr, pwStr, cch+1);
  93. return pbStr;
  94. }
  95. else
  96. {
  97. return NULL;
  98. }
  99. }