Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Replaced deprecated string conversion function #445

Merged
merged 3 commits into from
Nov 6, 2024
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Loading