Skip to content

Commit

Permalink
Merge pull request thestk#445 from ERSUCC/master
Browse files Browse the repository at this point in the history
Replaced deprecated string conversion function
  • Loading branch information
garyscavone authored Nov 6, 2024
2 parents 7c2f054 + 57a45cf commit 7aba116
Showing 1 changed file with 16 additions and 3 deletions.
19 changes: 16 additions & 3 deletions RtAudio.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -45,10 +45,10 @@
#include <iostream>
#include <cstdlib>
#include <cstring>
#include <cwchar>
#include <climits>
#include <cmath>
#include <algorithm>
#include <codecvt>
#include <locale>

#if defined(_WIN32)
Expand All @@ -74,9 +74,9 @@ std::string convertCharPointerToStdString(const char *text)
template<> inline
std::string convertCharPointerToStdString(const wchar_t* text)
{
#if defined(_MSC_VER)
if (!text)
return std::string();
#if defined(_MSC_VER)
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);
Expand All @@ -88,7 +88,20 @@ std::string convertCharPointerToStdString(const wchar_t* text)
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);
std::string result;
char dest[MB_CUR_MAX];
// get number of wide characters in text
const size_t length = wcslen(text);
for (size_t i = 0; i < length; i++) {
// get number of converted bytes
const int bytes = wctomb(dest, text[i]);
// protect against buffer overflow from conversion errors,
// or if the buffer is full and therefore not null-terminated
for (int j = 0; j < bytes; j++) {
result += dest[j];
}
}
return result;
#endif
}

Expand Down

0 comments on commit 7aba116

Please sign in to comment.