EncodingC.c 1.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing#kwsys for details. */
  3. #include "kwsysPrivate.h"
  4. #include KWSYS_HEADER(Encoding.h)
  5. /* Work-around CMake dependency scanning limitation. This must
  6. duplicate the above list of headers. */
  7. #if 0
  8. #include "Encoding.h.in"
  9. #endif
  10. #include <stdlib.h>
  11. #ifdef _WIN32
  12. #include <windows.h>
  13. #endif
  14. size_t kwsysEncoding_mbstowcs(wchar_t* dest, const char* str, size_t n)
  15. {
  16. if (str == 0) {
  17. return (size_t)-1;
  18. }
  19. #ifdef _WIN32
  20. return MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str, -1, dest,
  21. (int)n) -
  22. 1;
  23. #else
  24. return mbstowcs(dest, str, n);
  25. #endif
  26. }
  27. wchar_t* kwsysEncoding_DupToWide(const char* str)
  28. {
  29. wchar_t* ret = NULL;
  30. size_t length = kwsysEncoding_mbstowcs(NULL, str, 0) + 1;
  31. if (length > 0) {
  32. ret = (wchar_t*)malloc((length) * sizeof(wchar_t));
  33. if (ret) {
  34. ret[0] = 0;
  35. kwsysEncoding_mbstowcs(ret, str, length);
  36. }
  37. }
  38. return ret;
  39. }
  40. size_t kwsysEncoding_wcstombs(char* dest, const wchar_t* str, size_t n)
  41. {
  42. if (str == 0) {
  43. return (size_t)-1;
  44. }
  45. #ifdef _WIN32
  46. return WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str, -1, dest,
  47. (int)n, NULL, NULL) -
  48. 1;
  49. #else
  50. return wcstombs(dest, str, n);
  51. #endif
  52. }
  53. char* kwsysEncoding_DupToNarrow(const wchar_t* str)
  54. {
  55. char* ret = NULL;
  56. size_t length = kwsysEncoding_wcstombs(0, str, 0) + 1;
  57. if (length > 0) {
  58. ret = (char*)malloc(length);
  59. if (ret) {
  60. ret[0] = 0;
  61. kwsysEncoding_wcstombs(ret, str, length);
  62. }
  63. }
  64. return ret;
  65. }