EncodingCXX.cxx 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288
  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. #ifdef __osf__
  4. # define _OSF_SOURCE
  5. # define _POSIX_C_SOURCE 199506L
  6. # define _XOPEN_SOURCE_EXTENDED
  7. #endif
  8. #include "kwsysPrivate.h"
  9. #include KWSYS_HEADER(Encoding.hxx)
  10. #include KWSYS_HEADER(Encoding.h)
  11. // Work-around CMake dependency scanning limitation. This must
  12. // duplicate the above list of headers.
  13. #if 0
  14. # include "Encoding.h.in"
  15. # include "Encoding.hxx.in"
  16. #endif
  17. #include <cstdlib>
  18. #include <cstring>
  19. #include <vector>
  20. #ifdef _MSC_VER
  21. # pragma warning(disable : 4786)
  22. #endif
  23. // Windows API.
  24. #if defined(_WIN32)
  25. # include <windows.h>
  26. # include <ctype.h>
  27. # include <shellapi.h>
  28. #endif
  29. namespace KWSYS_NAMESPACE {
  30. Encoding::CommandLineArguments Encoding::CommandLineArguments::Main(
  31. int argc, char const* const* argv)
  32. {
  33. #ifdef _WIN32
  34. (void)argc;
  35. (void)argv;
  36. int ac;
  37. LPWSTR* w_av = CommandLineToArgvW(GetCommandLineW(), &ac);
  38. std::vector<std::string> av1(ac);
  39. std::vector<char const*> av2(ac);
  40. for (int i = 0; i < ac; i++) {
  41. av1[i] = ToNarrow(w_av[i]);
  42. av2[i] = av1[i].c_str();
  43. }
  44. LocalFree(w_av);
  45. return CommandLineArguments(ac, &av2[0]);
  46. #else
  47. return CommandLineArguments(argc, argv);
  48. #endif
  49. }
  50. Encoding::CommandLineArguments::CommandLineArguments(int ac,
  51. char const* const* av)
  52. {
  53. this->argv_.resize(ac + 1);
  54. for (int i = 0; i < ac; i++) {
  55. this->argv_[i] = strdup(av[i]);
  56. }
  57. this->argv_[ac] = nullptr;
  58. }
  59. Encoding::CommandLineArguments::CommandLineArguments(int ac,
  60. wchar_t const* const* av)
  61. {
  62. this->argv_.resize(ac + 1);
  63. for (int i = 0; i < ac; i++) {
  64. this->argv_[i] = kwsysEncoding_DupToNarrow(av[i]);
  65. }
  66. this->argv_[ac] = nullptr;
  67. }
  68. Encoding::CommandLineArguments::~CommandLineArguments()
  69. {
  70. for (size_t i = 0; i < this->argv_.size(); i++) {
  71. free(argv_[i]);
  72. }
  73. }
  74. Encoding::CommandLineArguments::CommandLineArguments(
  75. const CommandLineArguments& other)
  76. {
  77. this->argv_.resize(other.argv_.size());
  78. for (size_t i = 0; i < this->argv_.size(); i++) {
  79. this->argv_[i] = other.argv_[i] ? strdup(other.argv_[i]) : nullptr;
  80. }
  81. }
  82. Encoding::CommandLineArguments& Encoding::CommandLineArguments::operator=(
  83. const CommandLineArguments& other)
  84. {
  85. if (this != &other) {
  86. size_t i;
  87. for (i = 0; i < this->argv_.size(); i++) {
  88. free(this->argv_[i]);
  89. }
  90. this->argv_.resize(other.argv_.size());
  91. for (i = 0; i < this->argv_.size(); i++) {
  92. this->argv_[i] = other.argv_[i] ? strdup(other.argv_[i]) : nullptr;
  93. }
  94. }
  95. return *this;
  96. }
  97. int Encoding::CommandLineArguments::argc() const
  98. {
  99. return static_cast<int>(this->argv_.size() - 1);
  100. }
  101. char const* const* Encoding::CommandLineArguments::argv() const
  102. {
  103. return &this->argv_[0];
  104. }
  105. #if KWSYS_STL_HAS_WSTRING
  106. std::wstring Encoding::ToWide(const std::string& str)
  107. {
  108. std::wstring wstr;
  109. # if defined(_WIN32)
  110. const int wlength =
  111. MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(),
  112. int(str.size()), nullptr, 0);
  113. if (wlength > 0) {
  114. wchar_t* wdata = new wchar_t[wlength];
  115. int r = MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(),
  116. int(str.size()), wdata, wlength);
  117. if (r > 0) {
  118. wstr = std::wstring(wdata, wlength);
  119. }
  120. delete[] wdata;
  121. }
  122. # else
  123. size_t pos = 0;
  124. size_t nullPos = 0;
  125. do {
  126. if (pos < str.size() && str.at(pos) != '\0') {
  127. wstr += ToWide(str.c_str() + pos);
  128. }
  129. nullPos = str.find('\0', pos);
  130. if (nullPos != std::string::npos) {
  131. pos = nullPos + 1;
  132. wstr += wchar_t('\0');
  133. }
  134. } while (nullPos != std::string::npos);
  135. # endif
  136. return wstr;
  137. }
  138. std::string Encoding::ToNarrow(const std::wstring& str)
  139. {
  140. std::string nstr;
  141. # if defined(_WIN32)
  142. int length =
  143. WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
  144. int(str.size()), nullptr, 0, nullptr, nullptr);
  145. if (length > 0) {
  146. char* data = new char[length];
  147. int r =
  148. WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
  149. int(str.size()), data, length, nullptr, nullptr);
  150. if (r > 0) {
  151. nstr = std::string(data, length);
  152. }
  153. delete[] data;
  154. }
  155. # else
  156. size_t pos = 0;
  157. size_t nullPos = 0;
  158. do {
  159. if (pos < str.size() && str.at(pos) != '\0') {
  160. nstr += ToNarrow(str.c_str() + pos);
  161. }
  162. nullPos = str.find(wchar_t('\0'), pos);
  163. if (nullPos != std::string::npos) {
  164. pos = nullPos + 1;
  165. nstr += '\0';
  166. }
  167. } while (nullPos != std::string::npos);
  168. # endif
  169. return nstr;
  170. }
  171. std::wstring Encoding::ToWide(const char* cstr)
  172. {
  173. std::wstring wstr;
  174. size_t length = kwsysEncoding_mbstowcs(nullptr, cstr, 0) + 1;
  175. if (length > 0) {
  176. std::vector<wchar_t> wchars(length);
  177. if (kwsysEncoding_mbstowcs(&wchars[0], cstr, length) > 0) {
  178. wstr = &wchars[0];
  179. }
  180. }
  181. return wstr;
  182. }
  183. std::string Encoding::ToNarrow(const wchar_t* wcstr)
  184. {
  185. std::string str;
  186. size_t length = kwsysEncoding_wcstombs(nullptr, wcstr, 0) + 1;
  187. if (length > 0) {
  188. std::vector<char> chars(length);
  189. if (kwsysEncoding_wcstombs(&chars[0], wcstr, length) > 0) {
  190. str = &chars[0];
  191. }
  192. }
  193. return str;
  194. }
  195. # if defined(_WIN32)
  196. // Convert local paths to UNC style paths
  197. std::wstring Encoding::ToWindowsExtendedPath(std::string const& source)
  198. {
  199. return ToWindowsExtendedPath(ToWide(source));
  200. }
  201. // Convert local paths to UNC style paths
  202. std::wstring Encoding::ToWindowsExtendedPath(const char* source)
  203. {
  204. return ToWindowsExtendedPath(ToWide(source));
  205. }
  206. // Convert local paths to UNC style paths
  207. std::wstring Encoding::ToWindowsExtendedPath(std::wstring const& wsource)
  208. {
  209. // Resolve any relative paths
  210. DWORD wfull_len;
  211. /* The +3 is a workaround for a bug in some versions of GetFullPathNameW that
  212. * won't return a large enough buffer size if the input is too small */
  213. wfull_len = GetFullPathNameW(wsource.c_str(), 0, nullptr, nullptr) + 3;
  214. std::vector<wchar_t> wfull(wfull_len);
  215. GetFullPathNameW(wsource.c_str(), wfull_len, &wfull[0], nullptr);
  216. /* This should get the correct size without any extra padding from the
  217. * previous size workaround. */
  218. wfull_len = static_cast<DWORD>(wcslen(&wfull[0]));
  219. if (wfull_len >= 2 && isalpha(wfull[0]) &&
  220. wfull[1] == L':') { /* C:\Foo\bar\FooBar.txt */
  221. return L"\\\\?\\" + std::wstring(&wfull[0]);
  222. } else if (wfull_len >= 2 && wfull[0] == L'\\' &&
  223. wfull[1] == L'\\') { /* Starts with \\ */
  224. if (wfull_len >= 4 && wfull[2] == L'?' &&
  225. wfull[3] == L'\\') { /* Starts with \\?\ */
  226. if (wfull_len >= 8 && wfull[4] == L'U' && wfull[5] == L'N' &&
  227. wfull[6] == L'C' &&
  228. wfull[7] == L'\\') { /* \\?\UNC\Foo\bar\FooBar.txt */
  229. return std::wstring(&wfull[0]);
  230. } else if (wfull_len >= 6 && isalpha(wfull[4]) &&
  231. wfull[5] == L':') { /* \\?\C:\Foo\bar\FooBar.txt */
  232. return std::wstring(&wfull[0]);
  233. } else if (wfull_len >= 5) { /* \\?\Foo\bar\FooBar.txt */
  234. return L"\\\\?\\UNC\\" + std::wstring(&wfull[4]);
  235. }
  236. } else if (wfull_len >= 4 && wfull[2] == L'.' &&
  237. wfull[3] == L'\\') { /* Starts with \\.\ a device name */
  238. if (wfull_len >= 6 && isalpha(wfull[4]) &&
  239. wfull[5] == L':') { /* \\.\C:\Foo\bar\FooBar.txt */
  240. return L"\\\\?\\" + std::wstring(&wfull[4]);
  241. } else if (wfull_len >=
  242. 5) { /* \\.\Foo\bar\ Device name is left unchanged */
  243. return std::wstring(&wfull[0]);
  244. }
  245. } else if (wfull_len >= 3) { /* \\Foo\bar\FooBar.txt */
  246. return L"\\\\?\\UNC\\" + std::wstring(&wfull[2]);
  247. }
  248. }
  249. // If this case has been reached, then the path is invalid. Leave it
  250. // unchanged
  251. return wsource;
  252. }
  253. # endif
  254. #endif // KWSYS_STL_HAS_WSTRING
  255. } // namespace KWSYS_NAMESPACE