summaryrefslogtreecommitdiff
path: root/src/third_party/boost-1.69.0/boost/system/detail/system_category_win32.hpp
blob: 8a86e8180ea8ceb8ba42f07b2840a56165ed7dc5 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
// Windows implementation of system_error_category
//
// Copyright Beman Dawes 2002, 2006
// Copyright (c) Microsoft Corporation 2014
// Copyright 2018 Peter Dimov
//
// Distributed under the Boost Software License, Version 1.0. (See accompanying
// file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
//
// See library home page at http://www.boost.org/libs/system

#include <boost/winapi/error_codes.hpp>
#include <boost/winapi/error_handling.hpp>
#include <boost/winapi/character_code_conversion.hpp>
#include <boost/winapi/local_memory.hpp>
#include <cstdio>

//

namespace boost
{

namespace system
{

namespace detail
{

#if ( defined(_MSC_VER) && _MSC_VER < 1900 ) || ( defined(__MINGW32__) && !defined(__MINGW64_VERSION_MAJOR) )

inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len )
{
# if defined( BOOST_MSVC )
#  pragma warning( push )
#  pragma warning( disable: 4996 )
# endif

    _snprintf( buffer, len - 1, "Unknown error (%d)", ev );

    buffer[ len - 1 ] = 0;
    return buffer;

# if defined( BOOST_MSVC )
#  pragma warning( pop )
# endif
}

#else

inline char const * unknown_message_win32( int ev, char * buffer, std::size_t len )
{
    std::snprintf( buffer, len, "Unknown error (%d)", ev );
    return buffer;
}

#endif

inline boost::winapi::UINT_ message_cp_win32()
{
#if defined(BOOST_SYSTEM_USE_UTF8)

    return boost::winapi::CP_UTF8_;

#else

    return boost::winapi::CP_ACP_;

#endif
}

inline char const * system_category_message_win32( int ev, char * buffer, std::size_t len ) BOOST_NOEXCEPT
{
    if( len == 0 )
    {
        return buffer;
    }

    if( len == 1 )
    {
        buffer[0] = 0;
        return buffer;
    }

#if defined(__GNUC__)
# define BOOST_SYSTEM_ALLOCA __builtin_alloca
#else
# define BOOST_SYSTEM_ALLOCA _alloca
#endif

    wchar_t * wbuffer = static_cast<wchar_t*>( BOOST_SYSTEM_ALLOCA( len * sizeof( wchar_t ) ) );

#undef BOOST_SYSTEM_ALLOCA

    using namespace boost::winapi;

    DWORD_ retval = boost::winapi::FormatMessageW(
        FORMAT_MESSAGE_FROM_SYSTEM_ | FORMAT_MESSAGE_IGNORE_INSERTS_,
        NULL,
        ev,
        MAKELANGID_( LANG_NEUTRAL_, SUBLANG_DEFAULT_ ), // Default language
        wbuffer,
        static_cast<DWORD_>( len ),
        NULL
    );

    if( retval == 0 )
    {
        return unknown_message_win32( ev, buffer, len );
    }

    UINT_ const code_page = message_cp_win32();

    int r = boost::winapi::WideCharToMultiByte( code_page, 0, wbuffer, -1, buffer, static_cast<int>( len ), NULL, NULL );

    if( r == 0 )
    {
        return unknown_message_win32( ev, buffer, len );
    }

    --r; // exclude null terminator

    while( r > 0 && ( buffer[ r-1 ] == '\n' || buffer[ r-1 ] == '\r' ) )
    {
        buffer[ --r ] = 0;
    }

    if( r > 0 && buffer[ r-1 ] == '.' )
    {
        buffer[ --r ] = 0;
    }

    return buffer;
}

struct local_free
{
    void * p_;

    ~local_free()
    {
        boost::winapi::LocalFree( p_ );
    }
};

inline std::string unknown_message_win32( int ev )
{
    char buffer[ 38 ];
    return unknown_message_win32( ev, buffer, sizeof( buffer ) );
}

inline std::string system_category_message_win32( int ev )
{
    using namespace boost::winapi;

    wchar_t * lpMsgBuf = 0;

    DWORD_ retval = boost::winapi::FormatMessageW(
        FORMAT_MESSAGE_ALLOCATE_BUFFER_ | FORMAT_MESSAGE_FROM_SYSTEM_ | FORMAT_MESSAGE_IGNORE_INSERTS_,
        NULL,
        ev,
        MAKELANGID_( LANG_NEUTRAL_, SUBLANG_DEFAULT_ ), // Default language
        (LPWSTR_) &lpMsgBuf,
        0,
        NULL
    );

    if( retval == 0 )
    {
        return unknown_message_win32( ev );
    }

    local_free lf_ = { lpMsgBuf };
    (void)lf_;

    UINT_ const code_page = message_cp_win32();

    int r = boost::winapi::WideCharToMultiByte( code_page, 0, lpMsgBuf, -1, 0, 0, NULL, NULL );

    if( r == 0 )
    {
        return unknown_message_win32( ev );
    }

    std::string buffer( r, char() );

    r = boost::winapi::WideCharToMultiByte( code_page, 0, lpMsgBuf, -1, &buffer[0], r, NULL, NULL );

    if( r == 0 )
    {
        return unknown_message_win32( ev );
    }

    --r; // exclude null terminator

    while( r > 0 && ( buffer[ r-1 ] == '\n' || buffer[ r-1 ] == '\r' ) )
    {
        --r;
    }

    if( r > 0 && buffer[ r-1 ] == '.' )
    {
        --r;
    }

    buffer.resize( r );

    return buffer;
}

inline error_condition system_category_default_error_condition_win32( int ev ) BOOST_NOEXCEPT
{
    // When using the Windows Runtime, most system errors are reported as HRESULTs.
    // We want to map the common Win32 errors to their equivalent error condition,
    // whether or not they are reported via an HRESULT.

#define BOOST_SYSTEM_FAILED(hr)           ((hr) < 0)
#define BOOST_SYSTEM_HRESULT_FACILITY(hr) (((hr) >> 16) & 0x1fff)
#define BOOST_SYSTEM_HRESULT_CODE(hr)     ((hr) & 0xFFFF)
#define BOOST_SYSTEM_FACILITY_WIN32       7

    if( BOOST_SYSTEM_FAILED( ev ) && BOOST_SYSTEM_HRESULT_FACILITY( ev ) == BOOST_SYSTEM_FACILITY_WIN32 )
    {
        ev = BOOST_SYSTEM_HRESULT_CODE( ev );
    }

#undef BOOST_SYSTEM_FAILED
#undef BOOST_SYSTEM_HRESULT_FACILITY
#undef BOOST_SYSTEM_HRESULT_CODE
#undef BOOST_SYSTEM_FACILITY_WIN32

    using namespace boost::winapi;
    using namespace errc;

    // Windows system -> posix_errno decode table
    // see WinError.h comments for descriptions of errors

    switch ( ev )
    {
    case 0: return make_error_condition( success );

    case ERROR_ACCESS_DENIED_: return make_error_condition( permission_denied );
    case ERROR_ALREADY_EXISTS_: return make_error_condition( file_exists );
    case ERROR_BAD_UNIT_: return make_error_condition( no_such_device );
    case ERROR_BUFFER_OVERFLOW_: return make_error_condition( filename_too_long );
    case ERROR_BUSY_: return make_error_condition( device_or_resource_busy );
    case ERROR_BUSY_DRIVE_: return make_error_condition( device_or_resource_busy );
    case ERROR_CANNOT_MAKE_: return make_error_condition( permission_denied );
    case ERROR_CANTOPEN_: return make_error_condition( io_error );
    case ERROR_CANTREAD_: return make_error_condition( io_error );
    case ERROR_CANTWRITE_: return make_error_condition( io_error );
    case ERROR_CURRENT_DIRECTORY_: return make_error_condition( permission_denied );
    case ERROR_DEV_NOT_EXIST_: return make_error_condition( no_such_device );
    case ERROR_DEVICE_IN_USE_: return make_error_condition( device_or_resource_busy );
    case ERROR_DIR_NOT_EMPTY_: return make_error_condition( directory_not_empty );
    case ERROR_DIRECTORY_: return make_error_condition( invalid_argument ); // WinError.h: "The directory name is invalid"
    case ERROR_DISK_FULL_: return make_error_condition( no_space_on_device );
    case ERROR_FILE_EXISTS_: return make_error_condition( file_exists );
    case ERROR_FILE_NOT_FOUND_: return make_error_condition( no_such_file_or_directory );
    case ERROR_HANDLE_DISK_FULL_: return make_error_condition( no_space_on_device );
    case ERROR_INVALID_ACCESS_: return make_error_condition( permission_denied );
    case ERROR_INVALID_DRIVE_: return make_error_condition( no_such_device );
    case ERROR_INVALID_FUNCTION_: return make_error_condition( function_not_supported );
    case ERROR_INVALID_HANDLE_: return make_error_condition( invalid_argument );
    case ERROR_INVALID_NAME_: return make_error_condition( invalid_argument );
    case ERROR_LOCK_VIOLATION_: return make_error_condition( no_lock_available );
    case ERROR_LOCKED_: return make_error_condition( no_lock_available );
    case ERROR_NEGATIVE_SEEK_: return make_error_condition( invalid_argument );
    case ERROR_NOACCESS_: return make_error_condition( permission_denied );
    case ERROR_NOT_ENOUGH_MEMORY_: return make_error_condition( not_enough_memory );
    case ERROR_NOT_READY_: return make_error_condition( resource_unavailable_try_again );
    case ERROR_NOT_SAME_DEVICE_: return make_error_condition( cross_device_link );
    case ERROR_OPEN_FAILED_: return make_error_condition( io_error );
    case ERROR_OPEN_FILES_: return make_error_condition( device_or_resource_busy );
    case ERROR_OPERATION_ABORTED_: return make_error_condition( operation_canceled );
    case ERROR_OUTOFMEMORY_: return make_error_condition( not_enough_memory );
    case ERROR_PATH_NOT_FOUND_: return make_error_condition( no_such_file_or_directory );
    case ERROR_READ_FAULT_: return make_error_condition( io_error );
    case ERROR_RETRY_: return make_error_condition( resource_unavailable_try_again );
    case ERROR_SEEK_: return make_error_condition( io_error );
    case ERROR_SHARING_VIOLATION_: return make_error_condition( permission_denied );
    case ERROR_TOO_MANY_OPEN_FILES_: return make_error_condition( too_many_files_open );
    case ERROR_WRITE_FAULT_: return make_error_condition( io_error );
    case ERROR_WRITE_PROTECT_: return make_error_condition( permission_denied );
    case WSAEACCES_: return make_error_condition( permission_denied );
    case WSAEADDRINUSE_: return make_error_condition( address_in_use );
    case WSAEADDRNOTAVAIL_: return make_error_condition( address_not_available );
    case WSAEAFNOSUPPORT_: return make_error_condition( address_family_not_supported );
    case WSAEALREADY_: return make_error_condition( connection_already_in_progress );
    case WSAEBADF_: return make_error_condition( bad_file_descriptor );
    case WSAECONNABORTED_: return make_error_condition( connection_aborted );
    case WSAECONNREFUSED_: return make_error_condition( connection_refused );
    case WSAECONNRESET_: return make_error_condition( connection_reset );
    case WSAEDESTADDRREQ_: return make_error_condition( destination_address_required );
    case WSAEFAULT_: return make_error_condition( bad_address );
    case WSAEHOSTUNREACH_: return make_error_condition( host_unreachable );
    case WSAEINPROGRESS_: return make_error_condition( operation_in_progress );
    case WSAEINTR_: return make_error_condition( interrupted );
    case WSAEINVAL_: return make_error_condition( invalid_argument );
    case WSAEISCONN_: return make_error_condition( already_connected );
    case WSAEMFILE_: return make_error_condition( too_many_files_open );
    case WSAEMSGSIZE_: return make_error_condition( message_size );
    case WSAENAMETOOLONG_: return make_error_condition( filename_too_long );
    case WSAENETDOWN_: return make_error_condition( network_down );
    case WSAENETRESET_: return make_error_condition( network_reset );
    case WSAENETUNREACH_: return make_error_condition( network_unreachable );
    case WSAENOBUFS_: return make_error_condition( no_buffer_space );
    case WSAENOPROTOOPT_: return make_error_condition( no_protocol_option );
    case WSAENOTCONN_: return make_error_condition( not_connected );
    case WSAENOTSOCK_: return make_error_condition( not_a_socket );
    case WSAEOPNOTSUPP_: return make_error_condition( operation_not_supported );
    case WSAEPROTONOSUPPORT_: return make_error_condition( protocol_not_supported );
    case WSAEPROTOTYPE_: return make_error_condition( wrong_protocol_type );
    case WSAETIMEDOUT_: return make_error_condition( timed_out );
    case WSAEWOULDBLOCK_: return make_error_condition( operation_would_block );

    default: return error_condition( ev, system_category() );
    }
}

} // namespace detail

} // namespace system

} // namespace boost