| 157 | } |
| 158 | |
| 159 | std::string Encoding::ToNarrow(std::wstring const& str) |
| 160 | { |
| 161 | std::string nstr; |
| 162 | #if defined(_WIN32) |
| 163 | int length = |
| 164 | WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(), |
| 165 | int(str.size()), nullptr, 0, nullptr, nullptr); |
| 166 | if (length > 0) { |
| 167 | char* data = new char[length]; |
| 168 | int r = |
| 169 | WideCharToMultiByte(KWSYS_ENCODING_DEFAULT_CODEPAGE, 0, str.c_str(), |
| 170 | int(str.size()), data, length, nullptr, nullptr); |
| 171 | if (r > 0) { |
| 172 | nstr = std::string(data, length); |
| 173 | } |
| 174 | delete[] data; |
| 175 | } |
| 176 | #else |
| 177 | size_t pos = 0; |
| 178 | size_t nullPos = 0; |
| 179 | do { |
| 180 | if (pos < str.size() && str.at(pos) != '\0') { |
| 181 | nstr += ToNarrow(str.c_str() + pos); |
| 182 | } |
| 183 | nullPos = str.find(wchar_t('\0'), pos); |
| 184 | if (nullPos != std::string::npos) { |
| 185 | pos = nullPos + 1; |
| 186 | nstr += '\0'; |
| 187 | } |
| 188 | } while (nullPos != std::string::npos); |
| 189 | #endif |
| 190 | return nstr; |
| 191 | } |
| 192 | |
| 193 | std::wstring Encoding::ToWide(char const* cstr) |
| 194 | { |