source: NonGTP/Boost/boost/iostreams/detail/system_failure.hpp @ 857

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