StringHelper.h 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384
  1. /* -*- mode: c; tab-width: 4; c-basic-offset: 4; c-file-style: "linux" -*- */
  2. //
  3. // StringHelper.h: UWP support library for SDLPal.
  4. // Author: Lou Yihua @ 2016
  5. //
  6. // Copyright (c) 2009-2011, Wei Mingzhi <whistler_wmz@users.sf.net>.
  7. // Copyright (c) 2011-2017 SDLPAL development team.
  8. // All rights reserved.
  9. //
  10. // This file is part of SDLPAL.
  11. //
  12. // SDLPAL is free software: you can redistribute it and/or modify
  13. // it under the terms of the GNU General Public License as published by
  14. // the Free Software Foundation, either version 3 of the License, or
  15. // (at your option) any later version.
  16. //
  17. // This program is distributed in the hope that it will be useful,
  18. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  19. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  20. // GNU General Public License for more details.
  21. //
  22. // You should have received a copy of the GNU General Public License
  23. // along with this program. If not, see <http://www.gnu.org/licenses/>.
  24. //
  25. #pragma once
  26. #include <wrl.h>
  27. #include <string>
  28. static void ConvertString(Platform::String^ src, std::string& dst)
  29. {
  30. int len = WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, nullptr, 0, nullptr, nullptr);
  31. dst.resize(len - 1);
  32. WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, (char*)dst.data(), len, nullptr, nullptr);
  33. }
  34. static std::string ConvertString(Platform::String^ src)
  35. {
  36. int len = WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, nullptr, 0, nullptr, nullptr);
  37. std::string dst(len - 1, ' ');
  38. WideCharToMultiByte(CP_ACP, 0, src->Begin(), -1, (char*)dst.data(), len, nullptr, nullptr);
  39. return dst;
  40. }
  41. static std::string ConvertString(const wchar_t* src)
  42. {
  43. int len = WideCharToMultiByte(CP_ACP, 0, src, -1, nullptr, 0, nullptr, nullptr);
  44. std::string dst(len - 1, ' ');
  45. WideCharToMultiByte(CP_ACP, 0, src, -1, (char*)dst.data(), len, nullptr, nullptr);
  46. return dst;
  47. }
  48. static std::string ConvertString(const std::wstring& src)
  49. {
  50. return ConvertString(src.c_str());
  51. }
  52. static void ConvertString(const std::string& src, std::wstring& dst)
  53. {
  54. int len = MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, nullptr, 0);
  55. dst.resize(len - 1);
  56. MultiByteToWideChar(CP_ACP, 0, src.c_str(), -1, (wchar_t*)dst.data(), len);
  57. }
  58. static Platform::String^ ConvertString(const char* src)
  59. {
  60. if (src)
  61. {
  62. int len = MultiByteToWideChar(CP_ACP, 0, src, -1, nullptr, 0);
  63. auto wc = new wchar_t[len];
  64. MultiByteToWideChar(CP_ACP, 0, src, -1, wc, len);
  65. auto dst = ref new Platform::String(wc);
  66. delete[] wc;
  67. return dst;
  68. }
  69. else
  70. return "";
  71. }
  72. static Platform::String^ ConvertString(const std::string& src)
  73. {
  74. return ConvertString(src.c_str());
  75. }