diff --git a/src/Wideconvert.cpp b/src/Wideconvert.cpp index 9a41dda..21b6ed2 100644 --- a/src/Wideconvert.cpp +++ b/src/Wideconvert.cpp @@ -20,14 +20,32 @@ #include +/* + * 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 + #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 @@ -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 @@ -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 }