cmProcessOutput.cxx 4.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. /* Distributed under the OSI-approved BSD 3-Clause License. See accompanying
  2. file Copyright.txt or https://cmake.org/licensing for details. */
  3. #include "cmProcessOutput.h"
  4. #if defined(_WIN32)
  5. # include <cm/memory>
  6. # include <windows.h>
  7. unsigned int cmProcessOutput::defaultCodepage =
  8. KWSYS_ENCODING_DEFAULT_CODEPAGE;
  9. #endif
  10. cm::optional<cmProcessOutput::Encoding> cmProcessOutput::FindEncoding(
  11. std::string const& name)
  12. {
  13. cm::optional<Encoding> encoding;
  14. if ((name == "UTF8") || (name == "UTF-8")) {
  15. encoding = UTF8;
  16. } else if (name == "NONE") {
  17. encoding = None;
  18. } else if (name == "ANSI") {
  19. encoding = ANSI;
  20. } else if (name == "AUTO") {
  21. encoding = Auto;
  22. } else if (name == "OEM") {
  23. encoding = OEM;
  24. }
  25. return encoding;
  26. }
  27. cmProcessOutput::cmProcessOutput(Encoding encoding, unsigned int maxSize)
  28. {
  29. #if defined(_WIN32)
  30. codepage = 0;
  31. bufferSize = maxSize;
  32. if (encoding == None) {
  33. codepage = defaultCodepage;
  34. } else if (encoding == Auto) {
  35. codepage = GetConsoleCP();
  36. } else if (encoding == UTF8) {
  37. codepage = CP_UTF8;
  38. } else if (encoding == OEM) {
  39. codepage = GetOEMCP();
  40. }
  41. if (!codepage || encoding == ANSI) {
  42. codepage = GetACP();
  43. }
  44. #else
  45. static_cast<void>(encoding);
  46. static_cast<void>(maxSize);
  47. #endif
  48. }
  49. bool cmProcessOutput::DecodeText(std::string raw, std::string& decoded,
  50. size_t id)
  51. {
  52. #if !defined(_WIN32)
  53. static_cast<void>(id);
  54. decoded.swap(raw);
  55. return true;
  56. #else
  57. bool success = true;
  58. decoded = raw;
  59. if (id > 0) {
  60. if (rawparts.size() < id) {
  61. rawparts.reserve(id);
  62. while (rawparts.size() < id)
  63. rawparts.push_back(std::string());
  64. }
  65. raw = rawparts[id - 1] + raw;
  66. rawparts[id - 1].clear();
  67. decoded = raw;
  68. }
  69. if (raw.size() > 0 && codepage != defaultCodepage) {
  70. success = false;
  71. CPINFOEXW cpinfo;
  72. if (id > 0 && bufferSize > 0 && raw.size() == bufferSize &&
  73. GetCPInfoExW(codepage, 0, &cpinfo) == 1 && cpinfo.MaxCharSize > 1) {
  74. if (cpinfo.MaxCharSize == 2 && cpinfo.LeadByte[0] != 0) {
  75. LPSTR prevChar =
  76. CharPrevExA(codepage, raw.c_str(), raw.c_str() + raw.size(), 0);
  77. bool isLeadByte =
  78. (*(prevChar + 1) == 0) && IsDBCSLeadByteEx(codepage, *prevChar);
  79. if (isLeadByte) {
  80. rawparts[id - 1] += *(raw.end() - 1);
  81. raw.resize(raw.size() - 1);
  82. }
  83. success = DoDecodeText(raw, decoded, nullptr);
  84. } else {
  85. bool restoreDecoded = false;
  86. std::string firstDecoded = decoded;
  87. wchar_t lastChar = 0;
  88. for (UINT i = 0; i < cpinfo.MaxCharSize; i++) {
  89. success = DoDecodeText(raw, decoded, &lastChar);
  90. if (success && lastChar != 0) {
  91. if (i == 0) {
  92. firstDecoded = decoded;
  93. }
  94. if (lastChar == cpinfo.UnicodeDefaultChar) {
  95. restoreDecoded = true;
  96. rawparts[id - 1] = *(raw.end() - 1) + rawparts[id - 1];
  97. raw.resize(raw.size() - 1);
  98. } else {
  99. restoreDecoded = false;
  100. break;
  101. }
  102. } else {
  103. break;
  104. }
  105. }
  106. if (restoreDecoded) {
  107. decoded = firstDecoded;
  108. rawparts[id - 1].clear();
  109. }
  110. }
  111. } else {
  112. success = DoDecodeText(raw, decoded, nullptr);
  113. }
  114. }
  115. return success;
  116. #endif
  117. }
  118. bool cmProcessOutput::DecodeText(const char* data, size_t length,
  119. std::string& decoded, size_t id)
  120. {
  121. return this->DecodeText(std::string(data, length), decoded, id);
  122. }
  123. bool cmProcessOutput::DecodeText(std::vector<char> raw,
  124. std::vector<char>& decoded, size_t id)
  125. {
  126. std::string str;
  127. const bool success =
  128. this->DecodeText(std::string(raw.begin(), raw.end()), str, id);
  129. decoded.assign(str.begin(), str.end());
  130. return success;
  131. }
  132. #if defined(_WIN32)
  133. bool cmProcessOutput::DoDecodeText(std::string raw, std::string& decoded,
  134. wchar_t* lastChar)
  135. {
  136. bool success = false;
  137. const int wlength =
  138. MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()), nullptr, 0);
  139. auto wdata = cm::make_unique<wchar_t[]>(wlength);
  140. int r = MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()),
  141. wdata.get(), wlength);
  142. if (r > 0) {
  143. if (lastChar) {
  144. *lastChar = 0;
  145. if ((wlength >= 2 && wdata[wlength - 2] != wdata[wlength - 1]) ||
  146. wlength >= 1) {
  147. *lastChar = wdata[wlength - 1];
  148. }
  149. }
  150. int length = WideCharToMultiByte(defaultCodepage, 0, wdata.get(), wlength,
  151. nullptr, 0, nullptr, nullptr);
  152. auto data = cm::make_unique<char[]>(length);
  153. r = WideCharToMultiByte(defaultCodepage, 0, wdata.get(), wlength,
  154. data.get(), length, nullptr, nullptr);
  155. if (r > 0) {
  156. decoded = std::string(data.get(), length);
  157. success = true;
  158. }
  159. }
  160. return success;
  161. }
  162. #endif