Skip to content

Commit

Permalink
Merge pull request #427 from g40/fix_codecvt2
Browse files Browse the repository at this point in the history
Fix convertCharPointerToStdString with MSC_VER guard
  • Loading branch information
garyscavone committed Apr 18, 2024
2 parents cb03db3 + b8d09ec commit f92c4bc
Showing 1 changed file with 16 additions and 1 deletion.
17 changes: 16 additions & 1 deletion RtAudio.cpp
Expand Up @@ -72,9 +72,24 @@ std::string convertCharPointerToStdString(const char *text)
}

template<> inline
std::string convertCharPointerToStdString(const wchar_t *text)
std::string convertCharPointerToStdString(const wchar_t* text)
{
#if defined(_MSC_VER)
if (!text)
return std::string();
const int wchars = (int)wcslen(text);
// how many characters are required after conversion?
const int nchars = WideCharToMultiByte(CP_UTF8, 0, text, wchars, 0, 0, 0, 0);
if (!nchars)
return std::string();
// create buffer
std::string nret(nchars, 0);
// do the conversion
WideCharToMultiByte(CP_UTF8, 0, text, wchars, &nret[0], nchars, 0, 0);
return nret;
#else
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>{}.to_bytes(text);
#endif
}

#if defined(_MSC_VER)
Expand Down

0 comments on commit f92c4bc

Please sign in to comment.