cmProcessOutput.cxx 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  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 <windows.h>
  6. unsigned int cmProcessOutput::defaultCodepage =
  7. KWSYS_ENCODING_DEFAULT_CODEPAGE;
  8. #endif
  9. cmProcessOutput::Encoding cmProcessOutput::FindEncoding(
  10. std::string const& name)
  11. {
  12. Encoding encoding = Auto;
  13. if ((name == "UTF8") || (name == "UTF-8")) {
  14. encoding = UTF8;
  15. } else if (name == "NONE") {
  16. encoding = None;
  17. } else if (name == "ANSI") {
  18. encoding = ANSI;
  19. } else if (name == "OEM") {
  20. encoding = OEM;
  21. }
  22. return encoding;
  23. }
  24. cmProcessOutput::cmProcessOutput(Encoding encoding, unsigned int maxSize)
  25. {
  26. #if defined(_WIN32)
  27. codepage = 0;
  28. bufferSize = maxSize;
  29. if (encoding == None) {
  30. codepage = defaultCodepage;
  31. } else if (encoding == Auto) {
  32. codepage = GetConsoleCP();
  33. } else if (encoding == UTF8) {
  34. codepage = CP_UTF8;
  35. } else if (encoding == OEM) {
  36. codepage = GetOEMCP();
  37. }
  38. if (!codepage || encoding == ANSI) {
  39. codepage = GetACP();
  40. }
  41. #else
  42. static_cast<void>(encoding);
  43. static_cast<void>(maxSize);
  44. #endif
  45. }
  46. cmProcessOutput::~cmProcessOutput()
  47. {
  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, NULL);
  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, NULL);
  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 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. 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()), NULL, 0);
  139. wchar_t* wdata = new wchar_t[wlength];
  140. int r = MultiByteToWideChar(codepage, 0, raw.c_str(), int(raw.size()), wdata,
  141. 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, wlength, NULL,
  151. 0, NULL, NULL);
  152. char* data = new char[length];
  153. r = WideCharToMultiByte(defaultCodepage, 0, wdata, wlength, data, length,
  154. NULL, NULL);
  155. if (r > 0) {
  156. decoded = std::string(data, length);
  157. success = true;
  158. }
  159. delete[] data;
  160. }
  161. delete[] wdata;
  162. return success;
  163. }
  164. #endif