StringConvert.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107
  1. /*
  2. ** Command & Conquer Generals(tm)
  3. ** Copyright 2025 Electronic Arts Inc.
  4. **
  5. ** This program is free software: you can redistribute it and/or modify
  6. ** it under the terms of the GNU General Public License as published by
  7. ** the Free Software Foundation, either version 3 of the License, or
  8. ** (at your option) any later version.
  9. **
  10. ** This program is distributed in the hope that it will be useful,
  11. ** but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. ** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. ** GNU General Public License for more details.
  14. **
  15. ** You should have received a copy of the GNU General Public License
  16. ** along with this program. If not, see <http://www.gnu.org/licenses/>.
  17. */
  18. /******************************************************************************
  19. *
  20. * FILE
  21. * $Archive: $
  22. *
  23. * DESCRIPTION
  24. * Perform ANSI <-> Unicode string conversions
  25. *
  26. * PROGRAMMER
  27. * Denzil E. Long, Jr.
  28. * $Author: $
  29. *
  30. * VERSION INFO
  31. * $Modtime: $
  32. * $Revision: $
  33. *
  34. ******************************************************************************/
  35. #include "StringConvert.h"
  36. #include "UString.h"
  37. #include <windows.h>
  38. #include <Debug\DebugPrint.h>
  39. #include <assert.h>
  40. /******************************************************************************
  41. *
  42. * NAME
  43. * UStringToANSI
  44. *
  45. * DESCRIPTION
  46. * Convert UString to an ANSI string
  47. *
  48. * INPUTS
  49. * String - String to convert
  50. * Buffer - Pointer to buffer to receive conversion.
  51. * BufferLength - Length of buffer
  52. *
  53. * RESULT
  54. * ANSI - Pointer to ANSI string
  55. *
  56. ******************************************************************************/
  57. Char* UStringToANSI(const UString& string, Char* buffer, UInt bufferLength)
  58. {
  59. return UnicodeToANSI(string.Get(), buffer, bufferLength);
  60. }
  61. /******************************************************************************
  62. *
  63. * NAME
  64. * UnicodeToANSI
  65. *
  66. * DESCRIPTION
  67. * Convert Unicode string to an ANSI string
  68. *
  69. * INPUTS
  70. * String - Unicode string to convert
  71. * Buffer - Pointer to buffer to receive conversion.
  72. * BufferLength - Length of buffer
  73. *
  74. * RESULT
  75. * ANSI - Pointer to ANSI string
  76. *
  77. ******************************************************************************/
  78. Char* UnicodeToANSI(const WChar* string, Char* buffer, UInt bufferLength)
  79. {
  80. if ((string == NULL) || (buffer == NULL))
  81. {
  82. return NULL;
  83. }
  84. #ifdef _DEBUG
  85. int result =
  86. #endif
  87. WideCharToMultiByte(CP_ACP, 0, string, -1, buffer, bufferLength,
  88. NULL, NULL);
  89. #ifdef _DEBUG
  90. if (result == 0)
  91. {
  92. PrintWin32Error("ConvertToANSI() Failed");
  93. assert(false);
  94. }
  95. #endif
  96. return buffer;
  97. }