Skip to content

Commit

Permalink
unsupported C11 funktions: mbstowcs_s/wcstombs_s
Browse files Browse the repository at this point in the history
The new secure C11 funktion set for multibyte character string
conversion is not supported by the GNU C library.

Error was:

.../src/wideconvert.cpp:45:56: error:
   ‘mbstowcs_s’ was not declared in this scope
   mbstowcs_s(&bufferSize, NULL, 0, ns.c_str(), ns.size());
                                                        ^
.../src/wideconvert.cpp:78:56: error:
   ‘wcstombs_s’ was not declared in this scope
   wcstombs_s(&bufferSize, NULL, 0, ws.c_str(), ws.size());
                                                        ^

Signed-off-by: Stephan Linz <[email protected]>
  • Loading branch information
rexut committed Nov 28, 2016
1 parent d6e6f5b commit 12bed31
Showing 1 changed file with 47 additions and 3 deletions.
50 changes: 47 additions & 3 deletions src/Wideconvert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,14 +20,32 @@

#include <algorithm>

/*
* NOTE: from http://en.cppreference.com/w/c/string/multibyte/mbstowcs
* and from http://en.cppreference.com/w/c/string/multibyte/wcstombs
*
* As all bounds-checked functions, mbstowcs_s (wcstombs_s) is only
* guaranteed to be available if __STDC_LIB_EXT1__ is defined by the
* implementation and if the user defines __STDC_WANT_LIB_EXT1__ to
* the integer constant 1 before including stdlib.h.
*/
#define __STDC_WANT_LIB_EXT1__ 1
#include <stdlib.h>

#include "Wideconvert.hpp"

// FIXME: use the C++11
// see: http://stackoverflow.com/a/18374698
// http://stackoverflow.com/q/15615136
// http://stackoverflow.com/q/24802133
// http://en.cppreference.com/w/cpp/header/codecvt

std::wstring LibUSB::Util::StringToWString( const std::string& ns )
{

#if defined(__STDC_LIB_EXT1__) || defined(_WIN32)
size_t bufferSize;

// first call to wcstombs_s to get the target buffer size
// first call to mbstowcs_s to get the target buffer size
mbstowcs_s(&bufferSize, NULL, 0, ns.c_str(), ns.size());

// create target buffer with required size
Expand All @@ -39,12 +57,25 @@ std::wstring LibUSB::Util::StringToWString( const std::string& ns )
std::wstring result(buffer, bufferSize);
delete[] buffer;
return result;
#else
// first call to wcstombs to get the target buffer size
size_t bufferSize = mbstowcs (NULL, ns.c_str(), 0);

// create target buffer with required size
wchar_t* buffer = new wchar_t[bufferSize];

// second call to do the actual conversion
mbstowcs(buffer, ns.c_str(), bufferSize);

std::wstring result(buffer, bufferSize);
delete[] buffer;
return result;
#endif
}

std::string LibUSB::Util::WStringToString( const std::wstring& ws )
{

#if defined(__STDC_LIB_EXT1__) || defined(_WIN32)
size_t bufferSize;

// first call to wcstombs_s to get the target buffer size
Expand All @@ -59,5 +90,18 @@ std::string LibUSB::Util::WStringToString( const std::wstring& ws )
std::string result(buffer, bufferSize);
delete[] buffer;
return result;
#else
// first call to wcstombs to get the target buffer size
size_t bufferSize = wcstombs(NULL, ws.c_str(), 0);

// create target buffer with required size
char* buffer = new char[bufferSize];

// second call to do the actual conversion
wcstombs(buffer, ws.c_str(), bufferSize);

std::string result(buffer, bufferSize);
delete[] buffer;
return result;
#endif
}

0 comments on commit 12bed31

Please sign in to comment.