Skip to content

Commit 7aba116

Browse files
authored
Merge pull request #445 from ERSUCC/master
Replaced deprecated string conversion function
2 parents 7c2f054 + 57a45cf commit 7aba116

1 file changed

Lines changed: 16 additions & 3 deletions

File tree

RtAudio.cpp

Lines changed: 16 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,10 +45,10 @@
4545
#include <iostream>
4646
#include <cstdlib>
4747
#include <cstring>
48+
#include <cwchar>
4849
#include <climits>
4950
#include <cmath>
5051
#include <algorithm>
51-
#include <codecvt>
5252
#include <locale>
5353

5454
#if defined(_WIN32)
@@ -74,9 +74,9 @@ std::string convertCharPointerToStdString(const char *text)
7474
template<> inline
7575
std::string convertCharPointerToStdString(const wchar_t* text)
7676
{
77-
#if defined(_MSC_VER)
7877
if (!text)
7978
return std::string();
79+
#if defined(_MSC_VER)
8080
const int wchars = (int)wcslen(text);
8181
// how many characters are required after conversion?
8282
const int nchars = WideCharToMultiByte(CP_UTF8, 0, text, wchars, 0, 0, 0, 0);
@@ -88,7 +88,20 @@ std::string convertCharPointerToStdString(const wchar_t* text)
8888
WideCharToMultiByte(CP_UTF8, 0, text, wchars, &nret[0], nchars, 0, 0);
8989
return nret;
9090
#else
91-
return std::wstring_convert<std::codecvt_utf8_utf16<wchar_t>>{}.to_bytes(text);
91+
std::string result;
92+
char dest[MB_CUR_MAX];
93+
// get number of wide characters in text
94+
const size_t length = wcslen(text);
95+
for (size_t i = 0; i < length; i++) {
96+
// get number of converted bytes
97+
const int bytes = wctomb(dest, text[i]);
98+
// protect against buffer overflow from conversion errors,
99+
// or if the buffer is full and therefore not null-terminated
100+
for (int j = 0; j < bytes; j++) {
101+
result += dest[j];
102+
}
103+
}
104+
return result;
92105
#endif
93106
}
94107

0 commit comments

Comments
 (0)