EncodingCXX.cxx 7.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277
  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 <stdlib.h>
  18. #include <string.h>
  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] = KWSYS_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] = KWSYS_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]) : KWSYS_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]) : KWSYS_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 = MultiByteToWideChar(
  111. KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(), int(str.size()), NULL, 0);
  112. if (wlength > 0) {
  113. wchar_t* wdata = new wchar_t[wlength];
  114. int r = MultiByteToWideChar(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.data(),
  115. int(str.size()), wdata, wlength);
  116. if (r > 0) {
  117. wstr = std::wstring(wdata, wlength);
  118. }
  119. delete[] wdata;
  120. }
  121. #else
  122. size_t pos = 0;
  123. size_t nullPos = 0;
  124. do {
  125. if (pos < str.size() && str.at(pos) != '\0') {
  126. wstr += ToWide(str.c_str() + pos);
  127. }
  128. nullPos = str.find('\0', pos);
  129. if (nullPos != std::string::npos) {
  130. pos = nullPos + 1;
  131. wstr += wchar_t('\0');
  132. }
  133. } while (nullPos != std::string::npos);
  134. #endif
  135. return wstr;
  136. }
  137. std::string Encoding::ToNarrow(const std::wstring& str)
  138. {
  139. std::string nstr;
  140. #if defined(_WIN32)
  141. int length =
  142. WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
  143. int(str.size()), NULL, 0, NULL, NULL);
  144. if (length > 0) {
  145. char* data = new char[length];
  146. int r =
  147. WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(),
  148. int(str.size()), data, length, NULL, NULL);
  149. if (r > 0) {
  150. nstr = std::string(data, length);
  151. }
  152. delete[] data;
  153. }
  154. #else
  155. size_t pos = 0;
  156. size_t nullPos = 0;
  157. do {
  158. if (pos < str.size() && str.at(pos) != '\0') {
  159. nstr += ToNarrow(str.c_str() + pos);
  160. }
  161. nullPos = str.find(wchar_t('\0'), pos);
  162. if (nullPos != std::string::npos) {
  163. pos = nullPos + 1;
  164. nstr += '\0';
  165. }
  166. } while (nullPos != std::string::npos);
  167. #endif
  168. return nstr;
  169. }
  170. std::wstring Encoding::ToWide(const char* cstr)
  171. {
  172. std::wstring wstr;
  173. size_t length = kwsysEncoding_mbstowcs(KWSYS_NULLPTR, cstr, 0) + 1;
  174. if (length > 0) {
  175. std::vector<wchar_t> wchars(length);
  176. if (kwsysEncoding_mbstowcs(&wchars[0], cstr, length) > 0) {
  177. wstr = &wchars[0];
  178. }
  179. }
  180. return wstr;
  181. }
  182. std::string Encoding::ToNarrow(const wchar_t* wcstr)
  183. {
  184. std::string str;
  185. size_t length = kwsysEncoding_wcstombs(KWSYS_NULLPTR, wcstr, 0) + 1;
  186. if (length > 0) {
  187. std::vector<char> chars(length);
  188. if (kwsysEncoding_wcstombs(&chars[0], wcstr, length) > 0) {
  189. str = &chars[0];
  190. }
  191. }
  192. return str;
  193. }
  194. #if defined(_WIN32)
  195. // Convert local paths to UNC style paths
  196. std::wstring Encoding::ToWindowsExtendedPath(std::string const& source)
  197. {
  198. std::wstring wsource = Encoding::ToWide(source);
  199. // Resolve any relative paths
  200. DWORD wfull_len;
  201. /* The +3 is a workaround for a bug in some versions of GetFullPathNameW that
  202. * won't return a large enough buffer size if the input is too small */
  203. wfull_len = GetFullPathNameW(wsource.c_str(), 0, NULL, NULL) + 3;
  204. std::vector<wchar_t> wfull(wfull_len);
  205. GetFullPathNameW(wsource.c_str(), wfull_len, &wfull[0], NULL);
  206. /* This should get the correct size without any extra padding from the
  207. * previous size workaround. */
  208. wfull_len = static_cast<DWORD>(wcslen(&wfull[0]));
  209. if (wfull_len >= 2 && isalpha(wfull[0]) &&
  210. wfull[1] == L':') { /* C:\Foo\bar\FooBar.txt */
  211. return L"\\\\?\\" + std::wstring(&wfull[0]);
  212. } else if (wfull_len >= 2 && wfull[0] == L'\\' &&
  213. wfull[1] == L'\\') { /* Starts with \\ */
  214. if (wfull_len >= 4 && wfull[2] == L'?' &&
  215. wfull[3] == L'\\') { /* Starts with \\?\ */
  216. if (wfull_len >= 8 && wfull[4] == L'U' && wfull[5] == L'N' &&
  217. wfull[6] == L'C' &&
  218. wfull[7] == L'\\') { /* \\?\UNC\Foo\bar\FooBar.txt */
  219. return std::wstring(&wfull[0]);
  220. } else if (wfull_len >= 6 && isalpha(wfull[4]) &&
  221. wfull[5] == L':') { /* \\?\C:\Foo\bar\FooBar.txt */
  222. return std::wstring(&wfull[0]);
  223. } else if (wfull_len >= 5) { /* \\?\Foo\bar\FooBar.txt */
  224. return L"\\\\?\\UNC\\" + std::wstring(&wfull[4]);
  225. }
  226. } else if (wfull_len >= 4 && wfull[2] == L'.' &&
  227. wfull[3] == L'\\') { /* Starts with \\.\ a device name */
  228. if (wfull_len >= 6 && isalpha(wfull[4]) &&
  229. wfull[5] == L':') { /* \\.\C:\Foo\bar\FooBar.txt */
  230. return L"\\\\?\\" + std::wstring(&wfull[4]);
  231. } else if (wfull_len >=
  232. 5) { /* \\.\Foo\bar\ Device name is left unchanged */
  233. return std::wstring(&wfull[0]);
  234. }
  235. } else if (wfull_len >= 3) { /* \\Foo\bar\FooBar.txt */
  236. return L"\\\\?\\UNC\\" + std::wstring(&wfull[2]);
  237. }
  238. }
  239. // If this case has been reached, then the path is invalid. Leave it
  240. // unchanged
  241. return Encoding::ToWide(source);
  242. }
  243. #endif
  244. #endif // KWSYS_STL_HAS_WSTRING
  245. } // namespace KWSYS_NAMESPACE