1 // (C) Copyright 2008 CodeRage, LLC (turkanis at coderage dot com)
2 // (C) Copyright 2005-2007 Jonathan Turkanis
3 // (C) Copyright Jonathan Graehl 2004.
4 // Distributed under the Boost Software License, Version 1.0. (See accompanying
5 // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt.)
7 // See http://www.boost.org/libs/iostreams for documentation.
9 // Used by mapped_file.cpp.
11 #ifndef BOOST_IOSTREAMS_DETAIL_SYSTEM_FAILURE_HPP_INCLUDED
12 #define BOOST_IOSTREAMS_DETAIL_SYSTEM_FAILURE_HPP_INCLUDED
20 #include <boost/config.hpp>
21 #include <boost/throw_exception.hpp>
22 #include <boost/iostreams/detail/config/windows_posix.hpp>
23 #include <boost/iostreams/detail/ios.hpp> // failure.
25 #if defined(BOOST_NO_STDC_NAMESPACE) && !defined(__LIBCOMO__)
26 namespace std { using ::strlen; }
29 #ifdef BOOST_IOSTREAMS_WINDOWS
30 # define WIN32_LEAN_AND_MEAN // Exclude rarely-used stuff from Windows headers
37 namespace boost { namespace iostreams { namespace detail {
39 inline BOOST_IOSTREAMS_FAILURE system_failure(const char* msg)
42 #ifdef BOOST_IOSTREAMS_WINDOWS
45 if ( (err = ::GetLastError()) != NO_ERROR &&
46 ::FormatMessageA( FORMAT_MESSAGE_ALLOCATE_BUFFER |
47 FORMAT_MESSAGE_FROM_SYSTEM,
50 MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
55 result.reserve(std::strlen(msg) + 2 + std::strlen((LPSTR)lpMsgBuf));
58 result.append((LPSTR) lpMsgBuf);
59 ::LocalFree(lpMsgBuf);
64 const char* system_msg = errno ? strerror(errno) : "";
65 result.reserve(std::strlen(msg) + 2 + std::strlen(system_msg));
68 result.append(system_msg);
70 return BOOST_IOSTREAMS_FAILURE(result);
73 inline BOOST_IOSTREAMS_FAILURE system_failure(const std::string& msg)
74 { return system_failure(msg.c_str()); }
76 inline void throw_system_failure(const char* msg)
77 { boost::throw_exception(system_failure(msg)); }
79 inline void throw_system_failure(const std::string& msg)
80 { boost::throw_exception(system_failure(msg)); }
82 } } } // End namespaces detail, iostreams, boost.
84 #endif // #ifndef BOOST_IOSTREAMS_DETAIL_SYSTEM_FAILURE_HPP_INCLUDED