26#if defined(G_MINGW) && !defined(WC_ERR_INVALID_CHARS)
27#define WC_ERR_INVALID_CHARS 0
34 std::string message(
const std::string & context , DWORD e ,
const std::string & ascii )
36 std::ostringstream ss ;
37 ss << context << (context.empty()?
"":
": ") << e <<
": [" << ascii <<
"]" ;
43std::wstring G::Convert::widen(
const std::string & s ,
bool is_utf8 ,
const std::string & context )
45 unsigned int codepage = is_utf8 ? CP_UTF8 : CP_ACP ;
49 DWORD flags = MB_ERR_INVALID_CHARS ;
50 int n = MultiByteToWideChar( codepage , flags , s.c_str() ,
static_cast<int>(s.size()) ,
nullptr , 0 ) ;
53 DWORD e = GetLastError() ;
57 std::vector<wchar_t> buffer(
static_cast<std::size_t
>(n) ) ;
58 n = MultiByteToWideChar( codepage , flags , s.c_str() ,
static_cast<int>(s.size()) , &buffer[0] , n ) ;
61 DWORD e = GetLastError() ;
64 result = std::wstring( &buffer[0] , n ) ;
69std::string G::Convert::narrow(
const std::wstring & s ,
bool is_utf8 ,
const std::string & context )
71 unsigned int codepage = is_utf8 ? CP_UTF8 : CP_ACP ;
75 DWORD flags = is_utf8 ? WC_ERR_INVALID_CHARS : 0 ;
76 BOOL defaulted = FALSE ;
77 int n = WideCharToMultiByte( codepage , flags , s.c_str() ,
static_cast<int>(s.size()) ,
nullptr , 0 ,
78 nullptr , is_utf8 ?
nullptr : &defaulted ) ;
79 if( n <= 0 || defaulted )
81 DWORD e = n == 0 ? GetLastError() : 0 ;
85 std::vector<char> buffer(
static_cast<std::size_t
>(n) ) ;
86 n = WideCharToMultiByte( codepage , flags , s.c_str() ,
static_cast<int>(s.size()) , &buffer[0] , n ,
87 nullptr , is_utf8 ?
nullptr : &defaulted ) ;
88 if( n == 0 || defaulted )
90 DWORD e = n == 0 ? GetLastError() : 0 ;
93 result = std::string( &buffer[0] , n ) ;
static std::string toPrintableAscii(const std::string &in, char escape='\\')
Returns a 7-bit printable representation of the given input string.