| 146 | |
| 147 | |
| 148 | std::string wstring2string(std::wstring& wide_string) { |
| 149 | if (wide_string.empty()) { |
| 150 | return ""; |
| 151 | } |
| 152 | |
| 153 | // Determine the size needed for the UTF-8 buffer |
| 154 | int size_needed = WideCharToMultiByte(CP_UTF8, 0, wide_string.c_str(), -1, nullptr, 0, nullptr, nullptr); |
| 155 | if (size_needed <= 0) { |
| 156 | throw std::runtime_error("Failed to calculate size for UTF-8 string."); |
| 157 | } |
| 158 | |
| 159 | // Allocate the buffer and perform the conversion |
| 160 | std::string utf8_string(size_needed - 1, '\0'); // Exclude the null terminator |
| 161 | WideCharToMultiByte(CP_UTF8, 0, wide_string.c_str(), -1, &utf8_string[0], size_needed, nullptr, nullptr); |
| 162 | |
| 163 | return utf8_string; |
| 164 | } |
| 165 | |
| 166 | |
| 167 | /* |