utils.cpp 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141
  1. #include "utils.h"
  2. #include <flutter_windows.h>
  3. #include <io.h>
  4. #include <stdio.h>
  5. #include <windows.h>
  6. #include <iostream>
  7. void CreateAndAttachConsole()
  8. {
  9. if (::AllocConsole())
  10. {
  11. FILE *unused;
  12. if (freopen_s(&unused, "CONOUT$", "w", stdout))
  13. {
  14. _dup2(_fileno(stdout), 1);
  15. }
  16. if (freopen_s(&unused, "CONOUT$", "w", stderr))
  17. {
  18. _dup2(_fileno(stdout), 2);
  19. }
  20. std::ios::sync_with_stdio();
  21. FlutterDesktopResyncOutputStreams();
  22. }
  23. }
  24. std::vector<std::string> GetCommandLineArguments()
  25. {
  26. // Convert the UTF-16 command line arguments to UTF-8 for the Engine to use.
  27. int argc;
  28. wchar_t **argv = ::CommandLineToArgvW(::GetCommandLineW(), &argc);
  29. if (argv == nullptr)
  30. {
  31. return std::vector<std::string>();
  32. }
  33. std::vector<std::string> command_line_arguments;
  34. // Skip the first argument as it's the binary name.
  35. for (int i = 1; i < argc; i++)
  36. {
  37. command_line_arguments.push_back(Utf8FromUtf16(argv[i]));
  38. }
  39. ::LocalFree(argv);
  40. return command_line_arguments;
  41. }
  42. std::string Utf8FromUtf16(const wchar_t *utf16_string)
  43. {
  44. if (utf16_string == nullptr)
  45. {
  46. return std::string();
  47. }
  48. int target_length = ::WideCharToMultiByte(
  49. CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
  50. -1, nullptr, 0, nullptr, nullptr);
  51. std::string utf8_string;
  52. if (target_length == 0 || target_length > utf8_string.max_size())
  53. {
  54. return utf8_string;
  55. }
  56. utf8_string.resize(target_length);
  57. int converted_length = ::WideCharToMultiByte(
  58. CP_UTF8, WC_ERR_INVALID_CHARS, utf16_string,
  59. -1, utf8_string.data(),
  60. target_length, nullptr, nullptr);
  61. if (converted_length == 0)
  62. {
  63. return std::string();
  64. }
  65. return utf8_string;
  66. }
  67. std::wstring Utf16FromUtf8(const char *utf8_string)
  68. {
  69. if (utf8_string == nullptr)
  70. {
  71. return std::wstring();
  72. }
  73. int target_length = ::MultiByteToWideChar(CP_UTF8, 0, utf8_string, static_cast<int>(strlen(utf8_string)), nullptr, 0);
  74. std::wstring utf16_string;
  75. if (target_length == 0 || target_length > utf16_string.max_size())
  76. {
  77. return utf16_string;
  78. }
  79. utf16_string.resize(target_length);
  80. int converted_length = ::MultiByteToWideChar(CP_UTF8, 0, utf8_string, static_cast<int>(strlen(utf8_string)), utf16_string.data(), target_length);
  81. if (converted_length == 0)
  82. {
  83. return std::wstring();
  84. }
  85. return utf16_string;
  86. }
  87. std::string utf8_encode(const std::wstring &wstr)
  88. {
  89. if (wstr.empty())
  90. return std::string();
  91. int size_needed = WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), NULL, 0, NULL, NULL);
  92. std::string strTo(size_needed, 0);
  93. WideCharToMultiByte(CP_UTF8, 0, &wstr[0], (int)wstr.size(), &strTo[0], size_needed, NULL, NULL);
  94. return strTo;
  95. }
  96. std::wstring utf8_decode(const std::string &str)
  97. {
  98. if (str.empty())
  99. return std::wstring();
  100. int size_needed = MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), NULL, 0);
  101. std::wstring wstrTo(size_needed, 0);
  102. MultiByteToWideChar(CP_UTF8, 0, &str[0], (int)str.size(), &wstrTo[0], size_needed);
  103. return wstrTo;
  104. }
  105. bool _isntspace(const char &ch)
  106. {
  107. return !isspace(ch);
  108. }
  109. const std::string ltrim(const std::string &s)
  110. {
  111. std::string::const_iterator iter = find_if(s.begin(), s.end(), _isntspace);
  112. return std::string(iter, s.end());
  113. }
  114. const std::string rtrim(const std::string &s)
  115. {
  116. std::string::const_iterator iter = find_if(s.rbegin(), s.rend(), _isntspace).base();
  117. return std::string(s.begin(), iter);
  118. }
  119. const std::string trim(const std::string &s)
  120. {
  121. std::string::const_iterator iter1 = find_if(s.begin(), s.end(), _isntspace);
  122. std::string::const_iterator iter2 = find_if(s.rbegin(), s.rend(), _isntspace).base();
  123. return iter1 < iter2 ? std::string(iter1, iter2) : std::string("");
  124. }