Chris@16
|
1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
|
Chris@16
|
2 // (C) Copyright 2005-2007 Jonathan Turkanis
|
Chris@16
|
3 // (C) Copyright Jonathan Graehl 2004.
|
Chris@16
|
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
|
Chris@16
|
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
|
Chris@16
|
6
|
Chris@16
|
7 // See http://www.boost.org/libs/iostreams for documentation.
|
Chris@16
|
8
|
Chris@16
|
9 // Used by mapped_file.cpp.
|
Chris@16
|
10
|
Chris@16
|
11 #ifndef BOOST_IOSTREAMS_DETAIL_SYSTEM_FAILURE_HPP_INCLUDED
|
Chris@16
|
12 #define BOOST_IOSTREAMS_DETAIL_SYSTEM_FAILURE_HPP_INCLUDED
|
Chris@16
|
13
|
Chris@16
|
14 #if defined(_MSC_VER) && (_MSC_VER >= 1020)
|
Chris@16
|
15 # pragma once
|
Chris@16
|
16 #endif
|
Chris@16
|
17
|
Chris@16
|
18 #include <cstring>
|
Chris@16
|
19 #include <string>
|
Chris@16
|
20 #include <boost/config.hpp>
|
Chris@16
|
21 #include <boost/throw_exception.hpp>
|
Chris@16
|
22 #include <boost/iostreams/detail/config/windows_posix.hpp>
|
Chris@16
|
23 #include <boost/iostreams/detail/ios.hpp> // failure.
|
Chris@16
|
24
|
Chris@16
|
25 #if defined(BOOST_NO_STDC_NAMESPACE) && !defined(__LIBCOMO__)
|
Chris@16
|
26 namespace std { using ::strlen; }
|
Chris@16
|
27 #endif
|
Chris@16
|
28
|
Chris@16
|
29 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
30 # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
|
Chris@16
|
31 # include <windows.h>
|
Chris@16
|
32 #else
|
Chris@16
|
33 # include <errno.h>
|
Chris@16
|
34 # include <string.h>
|
Chris@16
|
35 #endif
|
Chris@16
|
36
|
Chris@16
|
37 namespace boost { namespace iostreams { namespace detail {
|
Chris@16
|
38
|
Chris@16
|
39 inline BOOST_IOSTREAMS_FAILURE system_failure(const char* msg)
|
Chris@16
|
40 {
|
Chris@16
|
41 std::string result;
|
Chris@16
|
42 #ifdef BOOST_IOSTREAMS_WINDOWS
|
Chris@16
|
43 DWORD err;
|
Chris@16
|
44 LPVOID lpMsgBuf;
|
Chris@16
|
45 if ( (err = ::GetLastError()) != NO_ERROR &&
|
Chris@16
|
46 ::FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER |
|
Chris@16
|
47 FORMAT_MESSAGE_FROM_SYSTEM,
|
Chris@16
|
48 NULL,
|
Chris@16
|
49 err,
|
Chris@16
|
50 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
|
Chris@16
|
51 (LPSTR) &lpMsgBuf,
|
Chris@16
|
52 0,
|
Chris@16
|
53 NULL ) != 0 )
|
Chris@16
|
54 {
|
Chris@16
|
55 result.reserve(std::strlen(msg) + 2 + std::strlen((LPSTR)lpMsgBuf));
|
Chris@16
|
56 result.append(msg);
|
Chris@16
|
57 result.append(": ");
|
Chris@16
|
58 result.append((LPSTR) lpMsgBuf);
|
Chris@16
|
59 ::LocalFree(lpMsgBuf);
|
Chris@16
|
60 } else {
|
Chris@16
|
61 result += msg;
|
Chris@16
|
62 }
|
Chris@16
|
63 #else
|
Chris@16
|
64 const char* system_msg = errno ? strerror(errno) : "";
|
Chris@16
|
65 result.reserve(std::strlen(msg) + 2 + std::strlen(system_msg));
|
Chris@16
|
66 result.append(msg);
|
Chris@16
|
67 result.append(": ");
|
Chris@16
|
68 result.append(system_msg);
|
Chris@16
|
69 #endif
|
Chris@16
|
70 return BOOST_IOSTREAMS_FAILURE(result);
|
Chris@16
|
71 }
|
Chris@16
|
72
|
Chris@16
|
73 inline BOOST_IOSTREAMS_FAILURE system_failure(const std::string& msg)
|
Chris@16
|
74 { return system_failure(msg.c_str()); }
|
Chris@16
|
75
|
Chris@16
|
76 inline void throw_system_failure(const char* msg)
|
Chris@16
|
77 { boost::throw_exception(system_failure(msg)); }
|
Chris@16
|
78
|
Chris@16
|
79 inline void throw_system_failure(const std::string& msg)
|
Chris@16
|
80 { boost::throw_exception(system_failure(msg)); }
|
Chris@16
|
81
|
Chris@16
|
82 } } } // End namespaces detail, iostreams, boost.
|
Chris@16
|
83
|
Chris@16
|
84 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_SYSTEM_FAILURE_HPP_INCLUDED
|