Planet
navi homePPSaboutscreenshotsdownloaddevelopmentforum

source: downloads/boost_1_34_1/libs/filesystem/src/exception.cpp @ 47

Last change on this file since 47 was 29, checked in by landauf, 17 years ago

updated boost from 1_33_1 to 1_34_1

File size: 5.0 KB
Line 
1//  Exception implementation file  -------------------------------------------//
2
3//  Copyright 2002 Beman Dawes
4//  Copyright 2001 Dietmar Kuehl
5//  Use, modification, and distribution is subject to the Boost Software
6//  License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy
7//  at http://www.boost.org/LICENSE_1_0.txt)
8
9//  See library home page at http://www.boost.org/libs/filesystem
10
11//----------------------------------------------------------------------------//
12
13// define BOOST_FILESYSTEM_SOURCE so that <boost/filesystem/config.hpp> knows
14// the library is being built (possibly exporting rather than importing code)
15#define BOOST_FILESYSTEM_SOURCE
16
17#include <boost/filesystem/config.hpp>
18#include <boost/filesystem/path.hpp>
19#include <boost/filesystem/cerrno.hpp>
20
21namespace fs = boost::filesystem;
22
23#include <cstring> // SGI MIPSpro compilers need this
24
25# ifdef BOOST_NO_STDC_NAMESPACE
26    namespace std { using ::strerror; }
27# endif
28
29
30# if defined( BOOST_WINDOWS_API )
31#   include "windows.h"
32# endif
33
34//----------------------------------------------------------------------------//
35
36namespace
37{
38#ifdef BOOST_WINDOWS_API
39  struct ec_xlate { fs::system_error_type sys_ec; fs::errno_type ec; };
40  const ec_xlate ec_table[] =
41  {
42    // see WinError.h comments for descriptions of errors
43   
44    // most common errors first to speed sequential search
45    { ERROR_FILE_NOT_FOUND, ENOENT },
46    { ERROR_PATH_NOT_FOUND, ENOENT },
47
48    // alphabetical for easy maintenance
49    { 0, 0 }, // no error
50    { ERROR_ACCESS_DENIED, EACCES },
51    { ERROR_ALREADY_EXISTS, EEXIST },
52    { ERROR_BAD_UNIT, ENODEV },
53    { ERROR_BUFFER_OVERFLOW, ENAMETOOLONG },
54    { ERROR_BUSY, EBUSY },
55    { ERROR_BUSY_DRIVE, EBUSY },
56    { ERROR_CANNOT_MAKE, EACCES },
57    { ERROR_CANTOPEN, EIO },
58    { ERROR_CANTREAD, EIO },
59    { ERROR_CANTWRITE, EIO },
60    { ERROR_CURRENT_DIRECTORY, EACCES },
61    { ERROR_DEV_NOT_EXIST, ENODEV },
62    { ERROR_DEVICE_IN_USE, EBUSY },
63    { ERROR_DIR_NOT_EMPTY, ENOTEMPTY },
64    { ERROR_DIRECTORY, EINVAL }, // WinError.h: "The directory name is invalid"
65    { ERROR_DISK_FULL, ENOSPC },
66    { ERROR_FILE_EXISTS, EEXIST },
67    { ERROR_HANDLE_DISK_FULL, ENOSPC },
68    { ERROR_INVALID_ACCESS, EACCES },
69    { ERROR_INVALID_DRIVE, ENODEV },
70    { ERROR_INVALID_FUNCTION, ENOSYS },
71    { ERROR_INVALID_HANDLE, EBADHANDLE },
72    { ERROR_INVALID_NAME, EINVAL },
73    { ERROR_LOCK_VIOLATION, EACCES },
74    { ERROR_LOCKED, EACCES },
75    { ERROR_NOACCESS, EACCES },
76    { ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
77    { ERROR_NOT_READY, EAGAIN },
78    { ERROR_NOT_SAME_DEVICE, EXDEV },
79    { ERROR_OPEN_FAILED, EIO },
80    { ERROR_OPEN_FILES, EBUSY },
81    { ERROR_OUTOFMEMORY, ENOMEM },
82    { ERROR_READ_FAULT, EIO },
83    { ERROR_SEEK, EIO },
84    { ERROR_SHARING_VIOLATION, EACCES },
85    { ERROR_TOO_MANY_OPEN_FILES, ENFILE },
86    { ERROR_WRITE_FAULT, EIO },
87    { ERROR_WRITE_PROTECT, EROFS },
88    { 0,EOTHER }
89  };
90#endif
91
92} // unnamed namespace
93
94namespace boost
95{
96  namespace filesystem
97  {
98# ifdef BOOST_WINDOWS_API
99
100    BOOST_FILESYSTEM_DECL
101    errno_type lookup_errno( system_error_type sys_err_code )
102    {
103      for ( const ec_xlate * cur = &ec_table[0];
104        cur != ec_table
105          + sizeof(ec_table)/sizeof(ec_xlate); ++cur )
106      {
107        if ( sys_err_code == cur->sys_ec ) return cur->ec;
108      }
109      return EOTHER;
110    }
111
112    BOOST_FILESYSTEM_DECL void
113    system_message( system_error_type sys_err_code, std::string & target )
114    {
115      LPVOID lpMsgBuf;
116      ::FormatMessageA( 
117          FORMAT_MESSAGE_ALLOCATE_BUFFER | 
118          FORMAT_MESSAGE_FROM_SYSTEM | 
119          FORMAT_MESSAGE_IGNORE_INSERTS,
120          NULL,
121          sys_err_code,
122          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
123          (LPSTR) &lpMsgBuf,
124          0,
125          NULL 
126      );
127      target += static_cast<LPCSTR>(lpMsgBuf);
128      ::LocalFree( lpMsgBuf ); // free the buffer
129      while ( target.size()
130        && (target[target.size()-1] == '\n' || target[target.size()-1] == '\r') )
131          target.erase( target.size()-1 );
132    }
133
134#  ifndef BOOST_FILESYSTEM_NARROW_ONLY
135    BOOST_FILESYSTEM_DECL void
136    system_message( system_error_type sys_err_code, std::wstring & target )
137    {
138      LPVOID lpMsgBuf;
139      ::FormatMessageW( 
140          FORMAT_MESSAGE_ALLOCATE_BUFFER | 
141          FORMAT_MESSAGE_FROM_SYSTEM | 
142          FORMAT_MESSAGE_IGNORE_INSERTS,
143          NULL,
144          sys_err_code,
145          MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
146          (LPWSTR) &lpMsgBuf,
147          0,
148          NULL 
149      );
150      target += static_cast<LPCWSTR>(lpMsgBuf);
151      ::LocalFree( lpMsgBuf ); // free the buffer
152      while ( target.size()
153        && (target[target.size()-1] == L'\n' || target[target.size()-1] == L'\r') )
154          target.erase( target.size()-1 );
155    }
156#  endif
157# else
158    void
159    system_message( system_error_type sys_err_code, std::string & target )
160    {
161      target += std::strerror( sys_err_code );
162    }
163# endif
164
165  } // namespace filesystem
166} // namespace boost
Note: See TracBrowser for help on using the repository browser.