| 54 | } |
| 55 | |
| 56 | bool cmProcessOutput::DecodeText(std::string raw, std::string& decoded, |
| 57 | size_t id) |
| 58 | { |
| 59 | #if !defined(_WIN32) |
| 60 | static_cast<void>(id); |
| 61 | decoded.swap(raw); |
| 62 | return true; |
| 63 | #else |
| 64 | bool success = true; |
| 65 | decoded = raw; |
| 66 | if (id > 0) { |
| 67 | if (rawparts.size() < id) { |
| 68 | rawparts.reserve(id); |
| 69 | while (rawparts.size() < id) |
| 70 | rawparts.push_back(std::string()); |
| 71 | } |
| 72 | raw = rawparts[id - 1] + raw; |
| 73 | rawparts[id - 1].clear(); |
| 74 | decoded = raw; |
| 75 | } |
| 76 | if (raw.size() > 0 && codepage != defaultCodepage) { |
| 77 | success = false; |
| 78 | CPINFOEXW cpinfo; |
| 79 | if (id > 0 && bufferSize > 0 && raw.size() == bufferSize && |
| 80 | GetCPInfoExW(codepage, 0, &cpinfo) == 1 && cpinfo.MaxCharSize > 1) { |
| 81 | if (cpinfo.MaxCharSize == 2 && cpinfo.LeadByte[0] != 0) { |
| 82 | LPSTR prevChar = |
| 83 | CharPrevExA(codepage, raw.c_str(), raw.c_str() + raw.size(), 0); |
| 84 | bool isLeadByte = |
| 85 | (*(prevChar + 1) == 0) && IsDBCSLeadByteEx(codepage, *prevChar); |
| 86 | if (isLeadByte) { |
| 87 | rawparts[id - 1] += *(raw.end() - 1); |
| 88 | raw.resize(raw.size() - 1); |
| 89 | } |
| 90 | success = DoDecodeText(raw, decoded, nullptr); |
| 91 | } else { |
| 92 | bool restoreDecoded = false; |
| 93 | std::string firstDecoded = decoded; |
| 94 | wchar_t lastChar = 0; |
| 95 | for (UINT i = 0; i < cpinfo.MaxCharSize; i++) { |
| 96 | success = DoDecodeText(raw, decoded, &lastChar); |
| 97 | if (success && lastChar != 0) { |
| 98 | if (i == 0) { |
| 99 | firstDecoded = decoded; |
| 100 | } |
| 101 | if (lastChar == cpinfo.UnicodeDefaultChar) { |
| 102 | restoreDecoded = true; |
| 103 | rawparts[id - 1] = *(raw.end() - 1) + rawparts[id - 1]; |
| 104 | raw.resize(raw.size() - 1); |
| 105 | } else { |
| 106 | restoreDecoded = false; |
| 107 | break; |
| 108 | } |
| 109 | } else { |
| 110 | break; |
| 111 | } |
| 112 | } |
| 113 | if (restoreDecoded) { |