E-MailRelay
gconvert_win32.cpp
Go to the documentation of this file.
1//
2// Copyright (C) 2001-2023 Graeme Walker <graeme_walker@users.sourceforge.net>
3//
4// This program is free software: you can redistribute it and/or modify
5// it under the terms of the GNU General Public License as published by
6// the Free Software Foundation, either version 3 of the License, or
7// (at your option) any later version.
8//
9// This program is distributed in the hope that it will be useful,
10// but WITHOUT ANY WARRANTY; without even the implied warranty of
11// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12// GNU General Public License for more details.
13//
14// You should have received a copy of the GNU General Public License
15// along with this program. If not, see <http://www.gnu.org/licenses/>.
16// ===
17///
18/// \file gconvert_win32.cpp
19///
20
21#include "gdef.h"
22#include "gconvert.h"
23#include "gstr.h"
24#include <vector>
25
26#if defined(G_MINGW) && !defined(WC_ERR_INVALID_CHARS)
27#define WC_ERR_INVALID_CHARS 0
28#endif
29
30namespace G
31{
32 namespace ConvertImp
33 {
34 std::string message( const std::string & context , DWORD e , const std::string & ascii )
35 {
36 std::ostringstream ss ;
37 ss << context << (context.empty()?"":": ") << e << ": [" << ascii << "]" ;
38 return ss.str() ;
39 }
40 }
41}
42
43std::wstring G::Convert::widen( const std::string & s , bool is_utf8 , const std::string & context )
44{
45 unsigned int codepage = is_utf8 ? CP_UTF8 : CP_ACP ;
46 std::wstring result ;
47 if( ! s.empty() )
48 {
49 DWORD flags = MB_ERR_INVALID_CHARS ;
50 int n = MultiByteToWideChar( codepage , flags , s.c_str() , static_cast<int>(s.size()) , nullptr , 0 ) ;
51 if( n <= 0 )
52 {
53 DWORD e = GetLastError() ;
54 throw Convert::Error( ConvertImp::message(context,e,Str::toPrintableAscii(s)) ) ;
55 }
56
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 ) ;
59 if( n == 0 )
60 {
61 DWORD e = GetLastError() ;
62 throw Convert::Error( ConvertImp::message(context,e,Str::toPrintableAscii(s)) ) ;
63 }
64 result = std::wstring( &buffer[0] , n ) ;
65 }
66 return result ;
67}
68
69std::string G::Convert::narrow( const std::wstring & s , bool is_utf8 , const std::string & context )
70{
71 unsigned int codepage = is_utf8 ? CP_UTF8 : CP_ACP ;
72 std::string result ;
73 if( ! s.empty() )
74 {
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 )
80 {
81 DWORD e = n == 0 ? GetLastError() : 0 ;
82 throw Convert::Error( ConvertImp::message(context,e,Str::toPrintableAscii(s)) ) ;
83 }
84
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 )
89 {
90 DWORD e = n == 0 ? GetLastError() : 0 ;
91 throw Convert::Error( ConvertImp::message(context,e,Str::toPrintableAscii(s)) ) ;
92 }
93 result = std::string( &buffer[0] , n ) ;
94 }
95 return result ;
96}
97
static std::string toPrintableAscii(const std::string &in, char escape='\\')
Returns a 7-bit printable representation of the given input string.
Definition: gstr.cpp:934
Low-level classes.
Definition: garg.h:30