|
Boost-Commit : |
From: bdawes_at_[hidden]
Date: 2007-09-09 10:59:16
Author: bemandawes
Date: 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
New Revision: 39173
URL: http://svn.boost.org/trac/boost/changeset/39173
Log:
Merge system and filesystem branches, bringing them in sync with N2415. Several filesystem bugs fixed, and current_path setter added.
Added:
trunk/libs/system/test/header_only_test.cpp
- copied unchanged from r39167, /branches/libs/system/system/test/header_only_test.cpp
Removed:
trunk/boost/filesystem/cerrno.hpp
trunk/libs/system/build/Jamfile
Text files modified:
trunk/boost/cerrno.hpp | 14
trunk/boost/filesystem/convenience.hpp | 35 +
trunk/boost/filesystem/operations.hpp | 15
trunk/boost/filesystem/path.hpp | 84 +++-
trunk/boost/system/config.hpp | 2
trunk/boost/system/error_code.hpp | 637 +++++++++++++++++++++++++++++++++++----
trunk/boost/system/system_error.hpp | 63 +--
trunk/libs/filesystem/build/Jamfile.v2 | 4
trunk/libs/filesystem/src/operations.cpp | 208 ++++++++----
trunk/libs/filesystem/src/path.cpp | 55 ++
trunk/libs/filesystem/test/Jamfile.v2 | 4
trunk/libs/filesystem/test/convenience_test.cpp | 96 +++++
trunk/libs/filesystem/test/operations_test.cpp | 70 ++-
trunk/libs/filesystem/test/path_test.cpp | 2
trunk/libs/system/doc/error_code.html | 7
trunk/libs/system/src/error_code.cpp | 499 +++++++++++++++++--------------
trunk/libs/system/test/Jamfile.v2 | 3
trunk/libs/system/test/error_code_test.cpp | 202 +++++++++---
trunk/libs/system/test/error_code_user_test.cpp | 377 +++++++++++++++++++++--
trunk/libs/system/test/system_error_test.cpp | 44 +-
20 files changed, 1811 insertions(+), 610 deletions(-)
Modified: trunk/boost/cerrno.hpp
==============================================================================
--- trunk/boost/cerrno.hpp (original)
+++ trunk/boost/cerrno.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -1,14 +1,14 @@
-// Boost Filesystem cerrno.hpp header --------------------------------------//
+// Boost cerrno.hpp header -------------------------------------------------//
-// © Copyright Beman Dawes 2005.
+// Copyright Beman Dawes 2005.
// Use, modification, and distribution is subject to 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/filesystem
+// See library home page at http://www.boost.org/libs/system
-#ifndef BOOST_FILESYSTEM_CERRNO_HPP
-#define BOOST_FILESYSTEM_CERRNO_HPP
+#ifndef BOOST_CERRNO_HPP
+#define BOOST_CERRNO_HPP
#include <cerrno>
@@ -190,8 +190,4 @@
#define EILSEQ 9945
#endif
-#define EBADHANDLE 9998 // bad handle
-#define EOTHER 9999 // Other error not translatable
- // to a POSIX errno value
-
#endif // include guard
Deleted: trunk/boost/filesystem/cerrno.hpp
==============================================================================
--- trunk/boost/filesystem/cerrno.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
+++ (empty file)
@@ -1,23 +0,0 @@
-// Boost Filesystem cerrno.hpp header --------------------------------------//
-
-// Copyright Beman Dawes 2005.
-// Use, modification, and distribution is subject to 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/filesystem
-
-#ifndef BOOST_FILESYSTEM_CERRNO_HPP
-#define BOOST_FILESYSTEM_CERRNO_HPP
-
-#include <cerrno>
-
-#if defined __BORLANDC__
-#define ENOSYS 9997
-#endif
-
-#define EBADHANDLE 9998 // bad handle
-#define EOTHER 9999 // Other error not translatable
- // to a POSIX errno value
-
-#endif // include guard
Modified: trunk/boost/filesystem/convenience.hpp
==============================================================================
--- trunk/boost/filesystem/convenience.hpp (original)
+++ trunk/boost/filesystem/convenience.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -1,7 +1,7 @@
// boost/filesystem/convenience.hpp ----------------------------------------//
-// © Copyright Beman Dawes, 2002-2005
-// © Copyright Vladimir Prus, 2002
+// Copyright Beman Dawes, 2002-2005
+// Copyright Vladimir Prus, 2002
// Use, modification, and distribution is subject to 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)
@@ -14,7 +14,7 @@
#define BOOST_FILESYSTEM_CONVENIENCE_HPP
#include <boost/filesystem/operations.hpp>
-#include <boost/cerrno.hpp>
+#include <boost/system/error_code.hpp>
#include <vector>
#include <stack>
@@ -45,7 +45,8 @@
if ( !ph.empty() && !is_directory(ph) )
boost::throw_exception( basic_filesystem_error<Path>(
"boost::filesystem::create_directories", ph,
- boost::system::error_code(EEXIST, boost::system::errno_ecat) ) );
+ boost::system::make_error_code(
+ boost::system::posix::file_exists ) ) );
return false;
}
@@ -188,6 +189,7 @@
bool equal( const basic_recursive_directory_iterator & rhs ) const
{ return m_imp == rhs.m_imp; }
+
};
typedef basic_recursive_directory_iterator<path> recursive_directory_iterator;
@@ -204,6 +206,8 @@
: m_imp( new detail::recur_dir_itr_imp<Path> )
{
m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path ) );
+ if ( m_imp->m_stack.top () == basic_directory_iterator<Path>() )
+ { m_imp.reset (); }
}
template<class Path>
@@ -212,8 +216,10 @@
system::error_code & ec )
: m_imp( new detail::recur_dir_itr_imp<Path> )
{
- m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path, std::nothrow ) );
m_imp->m_no_throw = true;
+ m_imp->m_stack.push( basic_directory_iterator<Path>( dir_path, ec ) );
+ if ( m_imp->m_stack.top () == basic_directory_iterator<Path>() )
+ { m_imp.reset (); }
}
// increment
@@ -224,15 +230,15 @@
static const basic_directory_iterator<Path> end_itr;
- if ( m_imp->m_no_push ) m_imp->m_no_push = false;
+ if ( m_imp->m_no_push )
+ { m_imp->m_no_push = false; }
else if ( is_directory( m_imp->m_stack.top()->status() ) )
{
system::error_code ec;
m_imp->m_stack.push(
m_imp->m_no_throw
? basic_directory_iterator<Path>( *m_imp->m_stack.top(), ec )
- : basic_directory_iterator<Path>( *m_imp->m_stack.top() )
- );
+ : basic_directory_iterator<Path>( *m_imp->m_stack.top() ) );
if ( m_imp->m_stack.top() != end_itr )
{
++m_imp->m_level;
@@ -258,8 +264,17 @@
BOOST_ASSERT( m_imp.get() && "pop on end iterator" );
BOOST_ASSERT( m_imp->m_level > 0 && "pop with level < 1" );
- m_imp->m_stack.pop();
- --m_imp->m_level;
+ static const basic_directory_iterator<Path> end_itr;
+
+ do
+ {
+ m_imp->m_stack.pop();
+ --m_imp->m_level;
+ }
+ while ( !m_imp->m_stack.empty()
+ && ++m_imp->m_stack.top() == end_itr );
+
+ if ( m_imp->m_stack.empty() ) m_imp.reset(); // done, so make end iterator
}
} // namespace filesystem
Modified: trunk/boost/filesystem/operations.hpp
==============================================================================
--- trunk/boost/filesystem/operations.hpp (original)
+++ trunk/boost/filesystem/operations.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -160,6 +160,8 @@
last_write_time_api( const std::string & ph, std::time_t new_value );
BOOST_FILESYSTEM_DECL system::error_code
get_current_path_api( std::string & ph );
+ BOOST_FILESYSTEM_DECL system::error_code
+ set_current_path_api( const std::string & ph );
BOOST_FILESYSTEM_DECL query_pair
create_directory_api( const std::string & ph );
BOOST_FILESYSTEM_DECL system::error_code
@@ -200,6 +202,8 @@
last_write_time_api( const std::wstring & ph, std::time_t new_value );
BOOST_FILESYSTEM_DECL system::error_code
get_current_path_api( std::wstring & ph );
+ BOOST_FILESYSTEM_DECL system::error_code
+ set_current_path_api( const std::wstring & ph );
BOOST_FILESYSTEM_DECL query_pair
create_directory_api( const std::wstring & ph );
# ifdef BOOST_FS_HARD_LINK
@@ -506,6 +510,15 @@
}
template< class Path >
+ void current_path( const Path & ph )
+ {
+ system::error_code ec( detail::set_current_path_api( ph.string() ) );
+ if ( ec )
+ boost::throw_exception( basic_filesystem_error<Path>(
+ "boost::filesystem::current_path", ph, ec ) );
+ }
+
+ template< class Path >
const Path & initial_path()
{
static Path init_path;
@@ -991,7 +1004,7 @@
m_symlink_status = symlink_st;
}
- const Path & path() const { return m_path; }
+ const Path & path() const { return m_path; }
file_status status() const;
file_status status( system::error_code & ec ) const;
file_status symlink_status() const;
Modified: trunk/boost/filesystem/path.hpp
==============================================================================
--- trunk/boost/filesystem/path.hpp (original)
+++ trunk/boost/filesystem/path.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -1,6 +1,6 @@
// boost/filesystem/path.hpp -----------------------------------------------//
-// Copyright Beman Dawes 2002-2005
+// Copyright Beman Dawes 2002-2005
// 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)
@@ -566,6 +566,53 @@
}
# endif
+ // basic_filesystem_error helpers --------------------------------------//
+
+ // Originally choice of implementation was done via specialization of
+ // basic_filesystem_error::what(). Several compilers (GCC, aCC, etc.)
+ // couldn't handle that, so the choice is now accomplished by overloading.
+
+ namespace detail
+ {
+ // BOOST_FILESYSTEM_DECL version works for VC++ but not GCC. Go figure!
+ inline
+ const char * what( const char * sys_err_what,
+ const path & path1, const path & path2, std::string & target )
+ {
+ try
+ {
+ if ( target.empty() )
+ {
+ target = sys_err_what;
+ if ( !path1.empty() )
+ {
+ target += ": \"";
+ target += path1.file_string();
+ target += "\"";
+ }
+ if ( !path2.empty() )
+ {
+ target += ", \"";
+ target += path2.file_string();
+ target += "\"";
+ }
+ }
+ return target.c_str();
+ }
+ catch (...)
+ {
+ return sys_err_what;
+ }
+ }
+
+ template<class Path>
+ const char * what( const char * sys_err_what,
+ const Path & path1, const Path & path2, std::string & target )
+ {
+ return sys_err_what;
+ }
+ }
+
// basic_filesystem_error ----------------------------------------------//
template<class Path>
@@ -599,7 +646,13 @@
return m_imp_ptr.get() ? m_imp_ptr->m_path2 : empty_path ;
}
- const char * what() const throw() { return system_error::what(); }
+ const char * what() const throw()
+ {
+ if ( !m_imp_ptr.get() )
+ return system_error::what();
+ return detail::what( system_error::what(), m_imp_ptr->m_path1,
+ m_imp_ptr->m_path2, m_imp_ptr->m_what );
+ }
private:
struct m_imp
@@ -611,33 +664,6 @@
boost::shared_ptr<m_imp> m_imp_ptr;
};
-// This specialization is causing problems with GCC, aCC (HP-UX)
-// and cxx on Alpha platforms.
-#if !(defined(__GNUC__) || defined(__HP_aCC) || \
- (defined(__DECCXX) && defined(__alpha)))
- template<> const char * basic_filesystem_error<path>::what() const throw()
- {
- if ( !m_imp_ptr.get() ) return system_error::what();
- if ( m_imp_ptr->m_what.empty() )
- {
- m_imp_ptr->m_what = system_error::what();
- if ( !path1().empty() )
- {
- m_imp_ptr->m_what += ": \"";
- m_imp_ptr->m_what += path1().file_string();
- m_imp_ptr->m_what += "\"";
- }
- if ( !path2().empty() )
- {
- m_imp_ptr->m_what += ", \"";
- m_imp_ptr->m_what += path2().file_string();
- m_imp_ptr->m_what += "\"";
- }
- }
- return m_imp_ptr->m_what.c_str();
- }
-#endif
-
typedef basic_filesystem_error<path> filesystem_error;
# ifndef BOOST_FILESYSTEM_NARROW_ONLY
Modified: trunk/boost/system/config.hpp
==============================================================================
--- trunk/boost/system/config.hpp (original)
+++ trunk/boost/system/config.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -28,7 +28,7 @@
// enable dynamic linking on Windows ---------------------------------------//
//# if (defined(BOOST_ALL_DYN_LINK) || defined(BOOST_SYSTEM_DYN_LINK)) && defined(__BORLANDC__) && defined(__WIN32__)
-//# error Dynamic linking Boost.Filesystem does not work for Borland; use static linking instead
+//# error Dynamic linking Boost.System does not work for Borland; use static linking instead
//# endif
#ifdef BOOST_HAS_DECLSPEC // defined in config system
Modified: trunk/boost/system/error_code.hpp
==============================================================================
--- trunk/boost/system/error_code.hpp (original)
+++ trunk/boost/system/error_code.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -1,22 +1,33 @@
// boost/system/error_code.hpp ---------------------------------------------//
-// Copyright Beman Dawes 2006
+// Copyright Beman Dawes 2006, 2007
+// Copyright Christoper Kohlhoff 2007
// 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/filesystem
+// See library home page at http://www.boost.org/libs/system
-#ifndef BOOST_SYSTEM_ERROR_CODE_HPP
-#define BOOST_SYSTEM_ERROR_CODE_HPP
+#ifndef BOOST_ERROR_CODE_HPP
+#define BOOST_ERROR_CODE_HPP
#include <boost/system/config.hpp>
#include <boost/cstdint.hpp>
#include <boost/assert.hpp>
#include <boost/operators.hpp>
-#include <boost/detail/identifier.hpp>
+#include <boost/noncopyable.hpp>
+#include <boost/utility/enable_if.hpp>
+#include <ostream>
#include <string>
#include <stdexcept>
+#include <functional>
+
+// TODO: undef these macros if not already defined
+#include <boost/cerrno.hpp>
+
+# ifdef BOOST_WINDOWS_API
+# include <winerror.h>
+# endif
#include <boost/config/abi_prefix.hpp> // must be the last #include
@@ -24,125 +35,623 @@
{
namespace system
{
-# ifndef BOOST_NO_STD_WSTRING // workaround Cygwin's lack of wstring_t
- typedef std::wstring wstring_t;
-# else
- typedef std::basic_string<wchar_t> wstring_t;
-# endif
class error_code;
+ class error_condition;
- // typedefs for registering additional decoders -------------------------//
+ // "Concept" helpers ---------------------------------------------------//
- typedef int (*errno_decoder)( const error_code & );
- typedef std::string (*message_decoder)( const error_code & );
- typedef wstring_t (*wmessage_decoder)( const error_code & );
+ template< class T >
+ struct is_error_code_enum { static const bool value = false; };
+
+ template< class T >
+ struct is_error_condition_enum { static const bool value = false; };
+
+ // portable error_conditions -------------------------------------------//
+
+ namespace posix
+ {
+
+ enum posix_errno
+ {
+ success = 0,
+ address_family_not_supported = EAFNOSUPPORT,
+ address_in_use = EADDRINUSE,
+ address_not_available = EADDRNOTAVAIL,
+ already_connected = EISCONN,
+ argument_list_too_long = E2BIG,
+ argument_out_of_domain = EDOM,
+ bad_address = EFAULT,
+ bad_file_descriptor = EBADF,
+ bad_message = EBADMSG,
+ broken_pipe = EPIPE,
+ connection_aborted = ECONNABORTED,
+ connection_already_in_progress = EALREADY,
+ connection_refused = ECONNREFUSED,
+ connection_reset = ECONNRESET,
+ cross_device_link = EXDEV,
+ destination_address_required = EDESTADDRREQ,
+ device_or_resource_busy = EBUSY,
+ directory_not_empty = ENOTEMPTY,
+ executable_format_error = ENOEXEC,
+ file_exists = EEXIST,
+ file_too_large = EFBIG,
+ filename_too_long = ENAMETOOLONG,
+ function_not_supported = ENOSYS,
+ host_unreachable = EHOSTUNREACH,
+ identifier_removed = EIDRM,
+ illegal_byte_sequence = EILSEQ,
+ inappropriate_io_control_operation = ENOTTY,
+ interrupted = EINTR,
+ invalid_argument = EINVAL,
+ invalid_seek = ESPIPE,
+ io_error = EIO,
+ is_a_directory = EISDIR,
+ message_size = EMSGSIZE,
+ network_down = ENETDOWN,
+ network_reset = ENETRESET,
+ network_unreachable = ENETUNREACH,
+ no_buffer_space = ENOBUFS,
+ no_child_process = ECHILD,
+ no_link = ENOLINK,
+ no_lock_available = ENOLCK,
+ no_message_available = ENODATA,
+ no_message = ENOMSG,
+ no_protocol_option = ENOPROTOOPT,
+ no_space_on_device = ENOSPC,
+ no_stream_resources = ENOSR,
+ no_such_device_or_address = ENXIO,
+ no_such_device = ENODEV,
+ no_such_file_or_directory = ENOENT,
+ no_such_process = ESRCH,
+ not_a_directory = ENOTDIR,
+ not_a_socket = ENOTSOCK,
+ not_a_stream = ENOSTR,
+ not_connected = ENOTCONN,
+ not_enough_memory = ENOMEM,
+ not_supported = ENOTSUP,
+ operation_canceled = ECANCELED,
+ operation_in_progress = EINPROGRESS,
+ operation_not_permitted = EPERM,
+ operation_not_supported = EOPNOTSUPP,
+ operation_would_block = EWOULDBLOCK,
+ owner_dead = EOWNERDEAD,
+ permission_denied = EACCES,
+ protocol_error = EPROTO,
+ protocol_not_supported = EPROTONOSUPPORT,
+ read_only_file_system = EROFS,
+ resource_deadlock_would_occur = EDEADLK,
+ resource_unavailable_try_again = EAGAIN,
+ result_out_of_range = ERANGE,
+ state_not_recoverable = ENOTRECOVERABLE,
+ stream_timeout = ETIME,
+ text_file_busy = ETXTBSY,
+ timed_out = ETIMEDOUT,
+ too_many_files_open_in_system = ENFILE,
+ too_many_files_open = EMFILE,
+ too_many_links = EMLINK,
+ too_many_synbolic_link_levels = ELOOP,
+ value_too_large = EOVERFLOW,
+ wrong_protocol_type = EPROTOTYPE,
+ };
+
+ } // namespace posix
+
+ template<> struct is_error_condition_enum<posix::posix_errno>
+ { static const bool value = true; };
// class error_category ------------------------------------------------//
- class BOOST_SYSTEM_DECL error_category
- : public boost::detail::identifier< uint_least32_t, error_category >
+ class error_category : public noncopyable
{
public:
- error_category()
- : boost::detail::identifier< uint_least32_t, error_category >(0){}
- explicit error_category( value_type v )
- : boost::detail::identifier< uint_least32_t, error_category >(v){}
+ virtual ~error_category(){}
+ virtual const char * name() const; // see implementation note below
+ virtual std::string message( int ev ) const; // see implementation note below
+ virtual error_condition default_error_condition( int ev ) const;
+ virtual bool equivalent( int code, const error_condition & condition ) const;
+ virtual bool equivalent( const error_code & code, int condition ) const;
+
+ bool operator==(const error_category & rhs) const { return this == &rhs; }
+ bool operator!=(const error_category & rhs) const { return this != &rhs; }
+ bool operator<( const error_category & rhs ) const
+ {
+ return std::less<const error_category*>()( this, &rhs );
+ }
};
// predefined error categories -----------------------------------------//
- const error_category errno_ecat(0); // unspecified value
+ BOOST_SYSTEM_DECL extern const error_category & posix_category;
+ BOOST_SYSTEM_DECL extern const error_category & system_category;
-# ifdef BOOST_WINDOWS_API
- const error_category native_ecat(1); // unspecified value
-# else
- const error_category native_ecat(0); // unspecified value
-# endif
+ // deprecated synonyms
+ BOOST_SYSTEM_DECL extern const error_category & errno_ecat; // posix_category
+ BOOST_SYSTEM_DECL extern const error_category & native_ecat; // system_category
- // class error_code ----------------------------------------------------//
+ // class error_condition -----------------------------------------------//
+
+ // error_conditions are portable, error_codes are system or lib specific
- class BOOST_SYSTEM_DECL error_code
+ class error_condition
{
public:
- typedef boost::int_least32_t value_type;
// constructors:
- error_code()
- : m_value(0), m_category(errno_ecat) {}
- error_code( value_type val, error_category cat )
- : m_value(val), m_category(cat) {}
+ error_condition() : m_val(0), m_cat(&posix_category) {}
+ error_condition( int val, const error_category & cat ) : m_val(val), m_cat(&cat) {}
+
+ template <class ConditionEnum>
+ error_condition(ConditionEnum e,
+ typename boost::enable_if<is_error_condition_enum<ConditionEnum> >::type* = 0)
+ {
+ *this = make_error_condition(e);
+ }
+
+ // modifiers:
+
+ void assign( int val, const error_category & cat )
+ {
+ m_val = val;
+ m_cat = &cat;
+ }
+
+ template<typename ConditionEnum>
+ typename boost::enable_if<is_error_condition_enum<ConditionEnum>, error_condition>::type &
+ operator=( ConditionEnum val )
+ {
+ *this = make_error_condition(val);
+ return *this;
+ }
+ void clear()
+ {
+ m_val = 0;
+ m_cat = &posix_category;
+ }
// observers:
- value_type value() const { return m_value; }
- error_category category() const { return m_category; }
- int to_errno() const; // name chosen to limit surprises
- // see Kohlhoff Jun 28 '06
- std::string message() const;
- wstring_t wmessage() const;
+ int value() const { return m_val; }
+ const error_category & category() const { return *m_cat; }
+ std::string message() const { return m_cat->message(value()); }
+
+ typedef void (*unspecified_bool_type)();
+ static void unspecified_bool_true() {}
- void assign( value_type val, const error_category & cat )
+ operator unspecified_bool_type() const // true if error
{
- m_value = val;
- m_category = cat;
+ return m_val == 0 ? 0 : unspecified_bool_true;
+ }
+
+ bool operator!() const // true if no error
+ {
+ return m_val == 0;
}
// relationals:
- bool operator==( const error_code & rhs ) const
+ // the more symmetrical non-member syntax allows enum
+ // conversions work for both rhs and lhs.
+ inline friend bool operator==( const error_condition & lhs,
+ const error_condition & rhs )
+ {
+ return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
+ }
+
+ inline friend bool operator<( const error_condition & lhs,
+ const error_condition & rhs )
+ // the more symmetrical non-member syntax allows enum
+ // conversions work for both rhs and lhs.
{
- return value() == rhs.value() && category() == rhs.category();
+ return lhs.m_cat < rhs.m_cat
+ || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
}
- bool operator!=( const error_code & rhs ) const
+
+ private:
+ int m_val;
+ const error_category * m_cat;
+
+ };
+
+ // class error_code ----------------------------------------------------//
+
+ // We want error_code to be a value type that can be copied without slicing
+ // and without requiring heap allocation, but we also want it to have
+ // polymorphic behavior based on the error category. This is achieved by
+ // abstract base class error_category supplying the polymorphic behavior,
+ // and error_code containing a pointer to an object of a type derived
+ // from error_category.
+ class error_code
+ {
+ public:
+
+ // constructors:
+ error_code() : m_val(0), m_cat(&system_category) {}
+ error_code( int val, const error_category & cat ) : m_val(val), m_cat(&cat) {}
+
+ template <class CodeEnum>
+ error_code(CodeEnum e,
+ typename boost::enable_if<is_error_code_enum<CodeEnum> >::type* = 0)
{
- return !(*this == rhs);
+ *this = make_error_code(e);
+ }
+
+ // modifiers:
+ void assign( int val, const error_category & cat )
+ {
+ m_val = val;
+ m_cat = &cat;
+ }
+
+ template<typename CodeEnum>
+ typename boost::enable_if<is_error_code_enum<CodeEnum>, error_code>::type &
+ operator=( CodeEnum val )
+ {
+ *this = make_error_code(val);
+ return *this;
}
- bool operator<( const error_code & rhs ) const
+
+ void clear()
{
- return category() < rhs.category()
- || ( category() == rhs.category() && value() < rhs.value() );
+ m_val = 0;
+ m_cat = &system_category;
}
- bool operator<=( const error_code & rhs ) const { return *this == rhs || *this < rhs; }
- bool operator> ( const error_code & rhs ) const { return !(*this <= rhs); }
- bool operator>=( const error_code & rhs ) const { return !(*this < rhs); }
+
+ // observers:
+ int value() const { return m_val; }
+ const error_category & category() const { return *m_cat; }
+ error_condition default_error_condition() const { return m_cat->default_error_condition(value()); }
+ std::string message() const { return m_cat->message(value()); }
typedef void (*unspecified_bool_type)();
static void unspecified_bool_true() {}
operator unspecified_bool_type() const // true if error
{
- return m_value == value_type() ? 0 : unspecified_bool_true;
+ return m_val == 0 ? 0 : unspecified_bool_true;
}
bool operator!() const // true if no error
{
- return m_value == value_type();
+ return m_val == 0;
}
- // statics:
- static error_category new_category( errno_decoder ed = 0,
- message_decoder md = 0, wmessage_decoder wmd = 0 );
- static bool get_decoders( error_category cat, errno_decoder & ed,
- message_decoder & md, wmessage_decoder & wmd );
+ // relationals:
+ inline friend bool operator==( const error_code & lhs,
+ const error_code & rhs )
+ // the more symmetrical non-member syntax allows enum
+ // conversions work for both rhs and lhs.
+ {
+ return lhs.m_cat == rhs.m_cat && lhs.m_val == rhs.m_val;
+ }
+
+ inline friend bool operator<( const error_code & lhs,
+ const error_code & rhs )
+ // the more symmetrical non-member syntax allows enum
+ // conversions work for both rhs and lhs.
+ {
+ return lhs.m_cat < rhs.m_cat
+ || (lhs.m_cat == rhs.m_cat && lhs.m_val < rhs.m_val);
+ }
+
+ private:
+ int m_val;
+ const error_category * m_cat;
- private:
- value_type m_value;
- error_category m_category;
};
// non-member functions ------------------------------------------------//
+ inline bool operator!=( const error_code & lhs,
+ const error_code & rhs )
+ {
+ return !(lhs == rhs);
+ }
+
+ inline bool operator!=( const error_condition & lhs,
+ const error_condition & rhs )
+ {
+ return !(lhs == rhs);
+ }
+
+ inline bool operator==( const error_code & code,
+ const error_condition & condition )
+ {
+ return code.category().equivalent( code.value(), condition )
+ || condition.category().equivalent( code, condition.value() );
+ }
+
+ inline bool operator!=( const error_code & lhs,
+ const error_condition & rhs )
+ {
+ return !(lhs == rhs);
+ }
+
+ inline bool operator==( const error_condition & condition,
+ const error_code & code )
+ {
+ return condition.category().equivalent( code, condition.value() )
+ || code.category().equivalent( code.value(), condition );
+ }
+
+ inline bool operator!=( const error_condition & lhs,
+ const error_code & rhs )
+ {
+ return !(lhs == rhs);
+ }
+
+ // TODO: both of these may move elsewhere, but the LWG hasn't spoken yet.
+
+ template <class charT, class traits>
+ inline std::basic_ostream<charT,traits>&
+ operator<< (std::basic_ostream<charT,traits>& os, error_code ec)
+ {
+ os << ec.category().name() << ':' << ec.value();
+ return os;
+ }
+
inline std::size_t hash_value( const error_code & ec )
{
return static_cast<std::size_t>(ec.value())
- + (static_cast<std::size_t>(ec.category().value()) << 16 );
+ + reinterpret_cast<std::size_t>(&ec.category());
+ }
+
+ // make_* functions for posix::posix_errno -----------------------------//
+
+ // explicit conversion:
+ inline error_code make_error_code( posix::posix_errno e )
+ { return error_code( e, posix_category ); }
+
+ // implicit conversion:
+ inline error_condition make_error_condition( posix::posix_errno e )
+ { return error_condition( e, posix_category ); }
+
+ // error_category default implementation -------------------------------//
+
+ inline error_condition error_category::default_error_condition( int ev ) const
+ {
+ return error_condition( ev, *this );
+ }
+
+ inline bool error_category::equivalent( int code,
+ const error_condition & condition ) const
+ {
+ return default_error_condition( code ) == condition;
+ }
+
+ inline bool error_category::equivalent( const error_code & code,
+ int condition ) const
+ {
+ return *this == code.category() && code.value() == condition;
}
+ // error_category implementation note: VC++ 8.0 objects to name() and
+ // message() being pure virtual functions. Thus these implementations.
+ inline const char * error_category::name() const
+ {
+ return "error: should never be called";
+ }
+
+ inline std::string error_category::message( int ev ) const
+ {
+ static std::string s("error: should never be called");
+ return s;
+ }
+
+ // ----------------------------------------------------------------------//
+
+ // Operating system specific interfaces --------------------------------//
+
+
+ // The interface is divided into general and system-specific portions to
+ // meet these requirements:
+ //
+ // * Code calling an operating system API can create an error_code with
+ // a single category (system_category), even for POSIX-like operating
+ // systems that return some POSIX errno values and some native errno
+ // values. This code should not have to pay the cost of distinguishing
+ // between categories, since it is not yet known if that is needed.
+ //
+ // * Users wishing to write system-specific code should be given enums for
+ // at least the common error cases.
+ //
+ // * System specific code should fail at compile time if moved to another
+ // operating system.
+
+#ifdef BOOST_POSIX_API
+
+ // POSIX-based systems -------------------------------------------------//
+
+ // To construct an error_code after a API error:
+ //
+ // error_code( errno, system_category )
+
+ // User code should use the portable "posix" enums for POSIX errors; this
+ // allows such code to be portable to non-POSIX systems. For the non-POSIX
+ // errno values that POSIX-based systems typically provide in addition to
+ // POSIX values, use the system specific enums below.
+
+# ifdef __CYGWIN__
+
+ namespace cygwin
+ {
+ enum cygwin_errno
+ {
+ no_net = ENONET,
+ no_package = ENOPKG,
+ no_share = ENOSHARE,
+ };
+ } // namespace cygwin
+
+ template<> struct is_error_code_enum<cygwin::cygwin_errno>
+ { static const bool value = true; };
+
+ inline error_code make_error_code(cygwin::cygwin_errno e)
+ { return error_code( e, system_category ); }
+
+# elif defined(linux) || defined(__linux) || defined(__linux__)
+
+ namespace Linux // linux lowercase name preempted by use as predefined macro
+ {
+ enum linux_error
+ {
+ advertise_error = EADV,
+ bad_exchange = EBADE,
+ bad_file_number = EBADFD,
+ bad_font_format = EBFONT,
+ bad_request_code = EBADRQC,
+ bad_request_descriptor = EBADR,
+ bad_slot = EBADSLT,
+ channel_range = ECHRNG,
+ communication_error = ECOMM,
+ dot_dot_error = EDOTDOT,
+ exchange_full = EXFULL,
+ host_down = EHOSTDOWN,
+ is_named_file_type= EISNAM,
+ key_expired = EKEYEXPIRED,
+ key_rejected = EKEYREJECTED,
+ key_revoked = EKEYREVOKED,
+ level2_halt= EL2HLT,
+ level2_no_syncronized= EL2NSYNC,
+ level3_halt = EL3HLT,
+ level3_reset = EL3RST,
+ link_range = ELNRNG,
+ medium_type = EMEDIUMTYPE,
+ no_anode= ENOANO,
+ no_block_device = ENOTBLK,
+ no_csi = ENOCSI,
+ no_key = ENOKEY,
+ no_medium = ENOMEDIUM,
+ no_network = ENONET,
+ no_package = ENOPKG,
+ not_avail = ENAVAIL,
+ not_named_file_type= ENOTNAM,
+ not_recoverable = ENOTRECOVERABLE,
+ not_unique = ENOTUNIQ,
+ owner_dead = EOWNERDEAD,
+ protocol_no_supported = EPFNOSUPPORT,
+ remote_address_changed = EREMCHG,
+ remote_io_error = EREMOTEIO,
+ remote_object = EREMOTE,
+ restart_needed = ERESTART,
+ shared_library_access = ELIBACC,
+ shared_library_bad = ELIBBAD,
+ shared_library_execute = ELIBEXEC,
+ shared_library_max_ = ELIBMAX,
+ shared_library_section= ELIBSCN,
+ shutdown = ESHUTDOWN,
+ socket_type_not_supported = ESOCKTNOSUPPORT,
+ srmount_error = ESRMNT,
+ stream_pipe_error = ESTRPIPE,
+ too_many_references = ETOOMANYREFS,
+ too_many_users = EUSERS,
+ unattached = EUNATCH,
+ unclean = EUCLEAN,
+ };
+ } // namespace Linux
+
+ template<> struct is_error_code_enum<Linux::linux_error>
+ { static const bool value = true; };
+
+ inline error_code make_error_code(Linux::linux_error e)
+ { return error_code( e, system_category ); }
+
+# endif
+
+ // TODO: Add more POSIX-based operating systems here
+
+
+#elif defined(BOOST_WINDOWS_API)
+
+ // Microsoft Windows ---------------------------------------------------//
+
+ // To construct an error_code after a API error:
+ //
+ // error_code( ::GetLastError(), system_category )
+
+ namespace windows
+ {
+ enum windows_error
+ {
+ success = 0,
+ // These names and values are based on Windows winerror.h
+ invalid_function = ERROR_INVALID_FUNCTION,
+ file_not_found = ERROR_FILE_NOT_FOUND,
+ path_not_found = ERROR_PATH_NOT_FOUND,
+ too_many_open_files = ERROR_TOO_MANY_OPEN_FILES,
+ access_denied = ERROR_ACCESS_DENIED,
+ invalid_handle = ERROR_INVALID_HANDLE,
+ arena_trashed = ERROR_ARENA_TRASHED,
+ not_enough_memory = ERROR_NOT_ENOUGH_MEMORY,
+ invalid_block = ERROR_INVALID_BLOCK,
+ bad_environment = ERROR_BAD_ENVIRONMENT,
+ bad_format = ERROR_BAD_FORMAT,
+ invalid_access = ERROR_INVALID_ACCESS,
+ outofmemory = ERROR_OUTOFMEMORY,
+ invalid_drive = ERROR_INVALID_DRIVE,
+ current_directory = ERROR_CURRENT_DIRECTORY,
+ not_same_device = ERROR_NOT_SAME_DEVICE,
+ no_more_files = ERROR_NO_MORE_FILES,
+ write_protect = ERROR_WRITE_PROTECT,
+ bad_unit = ERROR_BAD_UNIT,
+ not_ready = ERROR_NOT_READY,
+ bad_command = ERROR_BAD_COMMAND,
+ crc = ERROR_CRC,
+ bad_length = ERROR_BAD_LENGTH,
+ seek = ERROR_SEEK,
+ not_dos_disk = ERROR_NOT_DOS_DISK,
+ sector_not_found = ERROR_SECTOR_NOT_FOUND,
+ out_of_paper = ERROR_OUT_OF_PAPER,
+ write_fault = ERROR_WRITE_FAULT,
+ read_fault = ERROR_READ_FAULT,
+ gen_failure = ERROR_GEN_FAILURE,
+ sharing_violation = ERROR_SHARING_VIOLATION,
+ lock_violation = ERROR_LOCK_VIOLATION,
+ wrong_disk = ERROR_WRONG_DISK,
+ sharing_buffer_exceeded = ERROR_SHARING_BUFFER_EXCEEDED,
+ handle_eof = ERROR_HANDLE_EOF,
+ handle_disk_full= ERROR_HANDLE_DISK_FULL,
+ rem_not_list = ERROR_REM_NOT_LIST,
+ dup_name = ERROR_DUP_NAME,
+ bad_net_path = ERROR_BAD_NETPATH,
+ network_busy = ERROR_NETWORK_BUSY,
+ // ...
+ file_exists = ERROR_FILE_EXISTS,
+ cannot_make = ERROR_CANNOT_MAKE,
+ // ...
+ broken_pipe = ERROR_BROKEN_PIPE,
+ open_failed = ERROR_OPEN_FAILED,
+ buffer_overflow = ERROR_BUFFER_OVERFLOW,
+ disk_full= ERROR_DISK_FULL,
+ // ...
+ lock_failed = ERROR_LOCK_FAILED,
+ busy = ERROR_BUSY,
+ cancel_violation = ERROR_CANCEL_VIOLATION,
+ already_exists = ERROR_ALREADY_EXISTS
+ // ...
+
+ // TODO: add more Windows errors
+ };
+ } // namespace windows
+
+ template<> struct is_error_code_enum<windows::windows_error>
+ { static const bool value = true; };
+
+ inline error_code make_error_code(windows::windows_error e)
+ { return error_code( e, system_category ); }
+
+#else
+# error BOOST_POSIX_API or BOOST_WINDOWS_API must be defined
+#endif
+
} // namespace system
} // namespace boost
#include <boost/config/abi_suffix.hpp> // pops abi_prefix.hpp pragmas
-#endif // BOOST_SYSTEM_ERROR_CODE_HPP
+# ifdef BOOST_ERROR_CODE_HEADER_ONLY
+# include <boost/../libs/system/src/error_code.cpp>
+# endif
+
+#endif // BOOST_ERROR_CODE_HPP
Modified: trunk/boost/system/system_error.hpp
==============================================================================
--- trunk/boost/system/system_error.hpp (original)
+++ trunk/boost/system/system_error.hpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -17,58 +17,51 @@
{
namespace system
{
- enum message_action { append_message, no_message };
-
// class system_error --------------------------------------------------//
class system_error : public std::runtime_error
{
public:
- explicit system_error( error_code ec )
- : std::runtime_error(std::string()), m_error_code(ec),
- m_append_message(true) {}
-
- system_error( error_code ec, const std::string & what_arg,
- message_action ma = append_message )
- : std::runtime_error(what_arg), m_error_code(ec),
- m_append_message(ma==append_message) {}
-
- system_error( error_code::value_type ev, error_category ecat )
- : std::runtime_error(std::string()), m_error_code(ev,ecat),
- m_append_message(true) {}
-
- system_error( error_code::value_type ev, error_category ecat,
- const std::string & what_arg, message_action ma = append_message )
- : std::runtime_error(what_arg), m_error_code(ev,ecat),
- m_append_message(ma==append_message) {}
+ system_error( error_code ec )
+ : std::runtime_error(""), m_error_code(ec) {}
+ system_error( error_code ec, const std::string & what_arg )
+ : std::runtime_error(what_arg), m_error_code(ec) {}
+ system_error( int ev, const error_category & ecat,
+ const std::string & what_arg )
+ : std::runtime_error(what_arg), m_error_code(ev,ecat) {}
+ system_error( int ev, const error_category & ecat )
+ : std::runtime_error(""), m_error_code(ev,ecat) {}
virtual ~system_error() throw() {}
- const error_code & code() const throw() { return m_error_code; }
+ const error_code & code() const throw() { return m_error_code; }
+ const char * what() const throw();
- const char * what() const throw()
- // see http://www.boost.org/more/error_handling.html for lazy build rationale
+ private:
+ error_code m_error_code;
+ mutable std::string m_what;
+ };
+
+ // implementation ------------------------------------------------------//
+
+ inline const char * system_error::what() const throw()
+ // see http://www.boost.org/more/error_handling.html for lazy build rationale
+ {
+ if ( m_what.empty() )
{
- if ( !m_error_code || !m_append_message ) return runtime_error::what();
- if ( m_what.empty() )
+ try
{
- try
+ m_what = this->runtime_error::what();
+ if ( m_error_code )
{
- m_what = runtime_error::what();
if ( !m_what.empty() ) m_what += ": ";
m_what += m_error_code.message();
}
- catch (...) { return runtime_error::what(); }
}
- return m_what.c_str();
+ catch (...) { return runtime_error::what(); }
}
-
-
- private:
- error_code m_error_code;
- mutable std::string m_what;
- bool m_append_message;
- };
+ return m_what.c_str();
+ }
} // namespace system
} // namespace boost
Modified: trunk/libs/filesystem/build/Jamfile.v2
==============================================================================
--- trunk/libs/filesystem/build/Jamfile.v2 (original)
+++ trunk/libs/filesystem/build/Jamfile.v2 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -21,8 +21,8 @@
$(SOURCES).cpp
../../system/build//boost_system
:
- <link>shared:<define>BOOST_FILESYSTEM_DYN_LINK=1 # tell source we're building dll's
- <link>static:<define>BOOST_FILESYSTEM_STATIC_LINK=1 # tell source we're building static lib's
+ <link>shared:<define>BOOST_ALL_DYN_LINK=1 # tell source we're building dll's
+ <link>static:<define>BOOST_All_STATIC_LINK=1 # tell source we're building static lib's
;
install dist-lib
Modified: trunk/libs/filesystem/src/operations.cpp
==============================================================================
--- trunk/libs/filesystem/src/operations.cpp (original)
+++ trunk/libs/filesystem/src/operations.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -16,7 +16,10 @@
#define _POSIX_PTHREAD_SEMANTICS // Sun readdir_r() needs this
+#if !(defined(__HP_aCC) && defined(_ILP32) && \
+ !defined(_STATVFS_ACPP_PROBLEMS_FIXED))
#define _FILE_OFFSET_BITS 64 // at worst, these defines may have no effect,
+#endif
#define __USE_FILE_OFFSET64 // but that is harmless on Windows and on POSIX
// 64-bit systems or on 32-bit systems which don't have files larger
// than can be represented by a traditional POSIX/UNIX off_t type.
@@ -42,35 +45,38 @@
namespace fs = boost::filesystem;
using boost::system::error_code;
-using boost::system::errno_ecat;
-using boost::system::native_ecat;
+using boost::system::system_category;
# if defined(BOOST_WINDOWS_API)
-# include "windows.h"
+# include <windows.h>
# if defined(__BORLANDC__) || defined(__MWERKS__)
# if defined(__BORLANDC__)
using std::time_t;
# endif
-# include "utime.h"
+# include <utime.h>
# else
-# include "sys/utime.h"
+# include <sys/utime.h>
# endif
# else // BOOST_POSIX_API
# include <sys/types.h>
-# ifndef __APPLE__
+# if !defined(__APPLE__) && !defined(__OpenBSD__)
# include <sys/statvfs.h>
# define BOOST_STATVFS statvfs
# define BOOST_STATVFS_F_FRSIZE vfs.f_frsize
# else
+#ifdef __OpenBSD__
+# include <sys/param.h>
+#endif
# include <sys/mount.h>
# define BOOST_STATVFS statfs
# define BOOST_STATVFS_F_FRSIZE static_cast<boost::uintmax_t>( vfs.f_bsize )
# endif
-# include "dirent.h"
-# include "unistd.h"
-# include "fcntl.h"
-# include "utime.h"
+# include <dirent.h>
+# include <unistd.h>
+# include <fcntl.h>
+# include <utime.h>
+# include "limits.h"
# endif
// BOOST_FILESYSTEM_STATUS_CACHE enables file_status cache in
@@ -99,7 +105,7 @@
namespace
{
- static const fs::directory_iterator end_itr;
+ const fs::directory_iterator end_itr;
bool is_empty_directory( const std::string & dir_path )
{
return fs::directory_iterator(fs::path(dir_path)) == end_itr;
@@ -120,7 +126,7 @@
inline DWORD get_file_attributes( const wchar_t * ph )
{ return ::GetFileAttributesW( ph ); }
- static const fs::wdirectory_iterator wend_itr;
+ const fs::wdirectory_iterator wend_itr;
bool is_empty_directory( const std::wstring & dir_path )
{
return fs::wdirectory_iterator(fs::wpath(dir_path)) == wend_itr;
@@ -143,6 +149,9 @@
inline DWORD get_current_directory( DWORD sz, wchar_t * buf )
{ return ::GetCurrentDirectoryW( sz, buf ); }
+ inline bool set_current_directory( const wchar_t * buf )
+ { return ::SetCurrentDirectoryW( buf ) != 0 ; }
+
inline bool get_free_disk_space( const std::wstring & ph,
PULARGE_INTEGER avail, PULARGE_INTEGER total, PULARGE_INTEGER free )
{ return ::GetDiskFreeSpaceExW( ph.c_str(), avail, total, free ) != 0; }
@@ -178,7 +187,7 @@
DWORD attr( get_file_attributes( ph.c_str() ) );
if ( attr == 0xFFFFFFFF )
{
- ec = error_code( ::GetLastError(), native_ecat );
+ ec = error_code( ::GetLastError(), system_category );
if ((ec.value() == ERROR_FILE_NOT_FOUND)
|| (ec.value() == ERROR_PATH_NOT_FOUND)
|| (ec.value() == ERROR_INVALID_NAME) // "tools/jam/src/:sys:stat.h", "//foo"
@@ -206,7 +215,7 @@
{
WIN32_FILE_ATTRIBUTE_DATA fad;
if ( get_file_attributes_ex( ph.c_str(), fad ) == 0 )
- return std::make_pair( error_code( ::GetLastError(), native_ecat ), false );
+ return std::make_pair( error_code( ::GetLastError(), system_category ), false );
return std::make_pair( error_code(),
( fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
? is_empty_directory( ph )
@@ -276,14 +285,14 @@
{ return std::make_pair( error_code(), false ); }
assert( p1.handle == INVALID_HANDLE_VALUE
&& p2.handle == INVALID_HANDLE_VALUE );
- { return std::make_pair( error_code( error1, errno_ecat), false ); }
+ { return std::make_pair( error_code( error1, system_category), false ); }
}
// at this point, both handles are known to be valid
BY_HANDLE_FILE_INFORMATION info1, info2;
if ( !::GetFileInformationByHandle( p1.handle, &info1 ) )
- { return std::make_pair( error_code( ::GetLastError(), native_ecat ), false ); }
+ { return std::make_pair( error_code( ::GetLastError(), system_category ), false ); }
if ( !::GetFileInformationByHandle( p2.handle, &info2 ) )
- { return std::make_pair( error_code( ::GetLastError(), native_ecat ), false ); }
+ { return std::make_pair( error_code( ::GetLastError(), system_category ), false ); }
// In theory, volume serial numbers are sufficient to distinguish between
// devices, but in practice VSN's are sometimes duplicated, so last write
// time and file size are also checked.
@@ -306,9 +315,9 @@
WIN32_FILE_ATTRIBUTE_DATA fad;
// by now, intmax_t is 64-bits on all Windows compilers
if ( get_file_attributes_ex( ph.c_str(), fad ) == 0 )
- return std::make_pair( error_code( ::GetLastError(), native_ecat ), 0 );
+ return std::make_pair( error_code( ::GetLastError(), system_category ), 0 );
if ( (fad.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) !=0 )
- return std::make_pair( error_code( ERROR_FILE_NOT_FOUND, native_ecat), 0 );
+ return std::make_pair( error_code( ERROR_FILE_NOT_FOUND, system_category), 0 );
return std::make_pair( error_code(),
(static_cast<boost::uintmax_t>(fad.nFileSizeHigh)
<< (sizeof(fad.nFileSizeLow)*8))
@@ -340,7 +349,7 @@
}
else
{
- result.first = error_code( ::GetLastError(), native_ecat );
+ result.first = error_code( ::GetLastError(), system_category );
result.second.capacity = result.second.free
= result.second.available = 0;
}
@@ -361,11 +370,22 @@
typedef typename String::value_type value_type;
boost::scoped_array<value_type> buf( new value_type[sz] );
if ( get_current_directory( sz, buf.get() ) == 0 )
- return error_code( ::GetLastError(), native_ecat );
+ return error_code( ::GetLastError(), system_category );
ph = buf.get();
return error_code();
}
+ inline bool set_current_directory( const char * buf )
+ { return ::SetCurrentDirectoryA( buf ) != 0; }
+
+ template< class String >
+ error_code
+ set_current_path_template( const String & ph )
+ {
+ return error_code( set_current_directory( ph.c_str() )
+ ? 0 : ::GetLastError(), system_category );
+ }
+
inline std::size_t get_full_path_name(
const std::string & ph, std::size_t len, char * buf, char ** p )
{
@@ -384,13 +404,13 @@
typename String::value_type * pfn;
std::size_t len = get_full_path_name( ph,
buf_size , buf, &pfn );
- if ( len == 0 ) return error_code( ::GetLastError(), native_ecat );
+ if ( len == 0 ) return error_code( ::GetLastError(), system_category );
if ( len > buf_size )
{
typedef typename String::value_type value_type;
boost::scoped_array<value_type> big_buf( new value_type[len] );
if ( (len=get_full_path_name( ph, len , big_buf.get(), &pfn ))
- == 0 ) return error_code( ::GetLastError(), native_ecat );
+ == 0 ) return error_code( ::GetLastError(), system_category );
big_buf[len] = '\0';
target = big_buf.get();
return error_code();
@@ -409,9 +429,9 @@
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 ) );
if ( hw.handle == INVALID_HANDLE_VALUE )
- return error_code( ::GetLastError(), native_ecat );
+ return error_code( ::GetLastError(), system_category );
return error_code( ::GetFileTime( hw.handle, 0, 0, &last_write_time ) != 0
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
template<class String>
@@ -423,9 +443,9 @@
FILE_SHARE_DELETE | FILE_SHARE_READ | FILE_SHARE_WRITE, 0,
OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0 ) );
if ( hw.handle == INVALID_HANDLE_VALUE )
- return error_code( ::GetLastError(), native_ecat );
+ return error_code( ::GetLastError(), system_category );
return error_code( ::SetFileTime( hw.handle, 0, 0, &last_write_time ) != 0
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
// these constants come from inspecting some Microsoft sample code
@@ -490,11 +510,11 @@
if ( fs::is_directory( sf ) )
{
if ( !remove_directory( ph ) )
- return error_code(::GetLastError(), native_ecat);
+ return error_code(::GetLastError(), system_category);
}
else
{
- if ( !delete_file( ph ) ) return error_code(::GetLastError(), native_ecat);
+ if ( !delete_file( ph ) ) return error_code(::GetLastError(), system_category);
}
return error_code();
}
@@ -508,7 +528,7 @@
{
error_code error, dummy;
if ( create_directory( dir_ph ) ) return std::make_pair( error, true );
- error = error_code( ::GetLastError(), native_ecat );
+ error = error_code( ::GetLastError(), system_category );
// an error here may simply mean the postcondition is already met
if ( error.value() == ERROR_ALREADY_EXISTS
&& fs::is_directory( fs::detail::status_api( dir_ph, dummy ) ) )
@@ -529,7 +549,7 @@
const String & from_ph )
{
return error_code( create_hard_link( to_ph.c_str(), from_ph.c_str() )
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
#endif
@@ -548,9 +568,9 @@
BOOST_FILESYSTEM_DECL error_code not_found_error()
{
# ifdef BOOST_WINDOWS_API
- return error_code(ERROR_PATH_NOT_FOUND, native_ecat);
+ return error_code(ERROR_PATH_NOT_FOUND, system_category);
# else
- return error_code(ENOENT, errno_ecat);
+ return error_code(ENOENT, system_category);
# endif
}
@@ -601,6 +621,11 @@
get_current_path_api( std::wstring & ph )
{ return get_current_path_template( ph ); }
+ BOOST_FILESYSTEM_DECL
+ error_code
+ set_current_path_api( const std::wstring & ph )
+ { return set_current_path_template( ph ); }
+
BOOST_FILESYSTEM_DECL error_code
get_full_path_name_api( const std::wstring & ph, std::wstring & target )
{ return get_full_path_name_template( ph, target ); }
@@ -627,7 +652,7 @@
BOOST_FILESYSTEM_DECL error_code
create_symlink_api( const std::wstring & to_ph,
const std::wstring & from_ph )
- { return error_code( ERROR_NOT_SUPPORTED, native_ecat ); }
+ { return error_code( ERROR_NOT_SUPPORTED, system_category ); }
BOOST_FILESYSTEM_DECL error_code
remove_api( const std::wstring & ph ) { return remove_template( ph ); }
@@ -636,14 +661,14 @@
rename_api( const std::wstring & from, const std::wstring & to )
{
return error_code( ::MoveFileW( from.c_str(), to.c_str() )
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
BOOST_FILESYSTEM_DECL error_code
copy_file_api( const std::wstring & from, const std::wstring & to )
{
return error_code( ::CopyFileW( from.c_str(), to.c_str(), /*fail_if_exists=*/true )
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
BOOST_FILESYSTEM_DECL bool create_file_api( const std::wstring & ph,
@@ -719,7 +744,7 @@
{
handle = 0;
return error_code( ::GetLastError() == ERROR_FILE_NOT_FOUND
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
target = data.cFileName;
if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
@@ -737,7 +762,7 @@
{
int error = ::GetLastError();
dir_itr_close( handle );
- return error_code( error == ERROR_NO_MORE_FILES ? 0 : error, native_ecat );
+ return error_code( error == ERROR_NO_MORE_FILES ? 0 : error, system_category );
}
target = data.cFileName;
if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
@@ -774,6 +799,11 @@
get_current_path_api( std::string & ph )
{ return get_current_path_template( ph ); }
+ BOOST_FILESYSTEM_DECL
+ error_code
+ set_current_path_api( const std::string & ph )
+ { return set_current_path_template( ph ); }
+
BOOST_FILESYSTEM_DECL error_code
get_full_path_name_api( const std::string & ph, std::string & target )
{ return get_full_path_name_template( ph, target ); }
@@ -802,7 +832,7 @@
BOOST_FILESYSTEM_DECL error_code
create_symlink_api( const std::string & to_ph,
const std::string & from_ph )
- { return error_code( ERROR_NOT_SUPPORTED, native_ecat ); }
+ { return error_code( ERROR_NOT_SUPPORTED, system_category ); }
BOOST_FILESYSTEM_DECL error_code
remove_api( const std::string & ph ) { return remove_template( ph ); }
@@ -811,14 +841,14 @@
rename_api( const std::string & from, const std::string & to )
{
return error_code( ::MoveFileA( from.c_str(), to.c_str() )
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
BOOST_FILESYSTEM_DECL error_code
copy_file_api( const std::string & from, const std::string & to )
{
return error_code( ::CopyFileA( from.c_str(), to.c_str(), /*fail_if_exists=*/true )
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
BOOST_FILESYSTEM_DECL error_code
@@ -840,7 +870,7 @@
{
handle = 0;
return error_code( ::GetLastError() == ERROR_FILE_NOT_FOUND
- ? 0 : ::GetLastError(), native_ecat );
+ ? 0 : ::GetLastError(), system_category );
}
target = data.cFileName;
if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
@@ -856,7 +886,7 @@
{
bool ok = ::FindClose( handle ) != 0;
handle = 0;
- return error_code( ok ? 0 : ::GetLastError(), native_ecat );
+ return error_code( ok ? 0 : ::GetLastError(), system_category );
}
return error_code();
}
@@ -870,7 +900,7 @@
{
int error = ::GetLastError();
dir_itr_close( handle );
- return error_code( error == ERROR_NO_MORE_FILES ? 0 : error, native_ecat );
+ return error_code( error == ERROR_NO_MORE_FILES ? 0 : error, system_category );
}
target = data.cFileName;
if ( data.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY )
@@ -892,7 +922,7 @@
ec = error_code();
return fs::file_status( fs::file_not_found );
}
- ec = error_code( errno, errno_ecat );
+ ec = error_code( errno, system_category );
return fs::file_status( fs::status_unknown );
}
ec = error_code();
@@ -922,7 +952,7 @@
ec = error_code();
return fs::file_status( fs::file_not_found );
}
- ec = error_code( errno, errno_ecat );
+ ec = error_code( errno, system_category );
return fs::file_status( fs::status_unknown );
}
ec = error_code();
@@ -957,7 +987,7 @@
{
struct stat path_stat;
if ( (::stat( ph.c_str(), &path_stat )) != 0 )
- return std::make_pair( error_code( errno, errno_ecat ), false );
+ return std::make_pair( error_code( errno, system_category ), false );
return std::make_pair( error_code(), S_ISDIR( path_stat.st_mode )
? is_empty_directory( ph )
: path_stat.st_size == 0 );
@@ -971,7 +1001,7 @@
struct stat s1;
int e1( ::stat( ph1.c_str(), &s1 ) );
if ( e1 != 0 || e2 != 0 )
- return std::make_pair( error_code( e1 != 0 && e2 != 0 ? errno : 0, errno_ecat ), false );
+ return std::make_pair( error_code( e1 != 0 && e2 != 0 ? errno : 0, system_category ), false );
// at this point, both stats are known to be valid
return std::make_pair( error_code(),
s1.st_dev == s2.st_dev
@@ -988,9 +1018,9 @@
{
struct stat path_stat;
if ( ::stat( ph.c_str(), &path_stat ) != 0 )
- return std::make_pair( error_code( errno, errno_ecat ), 0 );
+ return std::make_pair( error_code( errno, system_category ), 0 );
if ( !S_ISREG( path_stat.st_mode ) )
- return std::make_pair( error_code( EPERM, errno_ecat ), 0 );
+ return std::make_pair( error_code( EPERM, system_category ), 0 );
return std::make_pair( error_code(),
static_cast<boost::uintmax_t>(path_stat.st_size) );
}
@@ -1002,7 +1032,7 @@
space_pair result;
if ( ::BOOST_STATVFS( ph.c_str(), &vfs ) != 0 )
{
- result.first = error_code( errno, errno_ecat );
+ result.first = error_code( errno, system_category );
result.second.capacity = result.second.free
= result.second.available = 0;
}
@@ -1024,7 +1054,7 @@
{
struct stat path_stat;
if ( ::stat( ph.c_str(), &path_stat ) != 0 )
- return std::make_pair( error_code( errno, errno_ecat ), 0 );
+ return std::make_pair( error_code( errno, system_category ), 0 );
return std::make_pair( error_code(), path_stat.st_mtime );
}
@@ -1033,11 +1063,11 @@
{
struct stat path_stat;
if ( ::stat( ph.c_str(), &path_stat ) != 0 )
- return error_code( errno, errno_ecat );
+ return error_code( errno, system_category );
::utimbuf buf;
buf.actime = path_stat.st_atime; // utime() updates access time too:-(
buf.modtime = new_value;
- return error_code( ::utime( ph.c_str(), &buf ) != 0 ? errno : 0, errno_ecat );
+ return error_code( ::utime( ph.c_str(), &buf ) != 0 ? errno : 0, system_category );
}
BOOST_FILESYSTEM_DECL error_code
@@ -1054,7 +1084,7 @@
# if defined(__MSL__) && (defined(macintosh) || defined(__APPLE__) || defined(__APPLE_CC__))
&& errno != 0
# endif
- ) return error_code( errno, errno_ecat );
+ ) return error_code( errno, system_category );
}
else
{
@@ -1065,6 +1095,13 @@
return error_code();
}
+ BOOST_FILESYSTEM_DECL error_code
+ set_current_path_api( std::string & ph )
+ {
+ return error_code( ::chdir( ph.c_str() )
+ ? errno : 0, system_category );
+ }
+
BOOST_FILESYSTEM_DECL fs::detail::query_pair
create_directory_api( const std::string & ph )
{
@@ -1074,7 +1111,7 @@
error_code dummy;
if ( ec != EEXIST
|| !fs::is_directory( status_api( ph, dummy ) ) )
- { return std::make_pair( error_code( ec, errno_ecat ), false ); }
+ { return std::make_pair( error_code( ec, system_category ), false ); }
return std::make_pair( error_code(), false );
}
@@ -1083,7 +1120,7 @@
const std::string & from_ph )
{
return error_code( ::link( to_ph.c_str(), from_ph.c_str() ) == 0
- ? 0 : errno, errno_ecat );
+ ? 0 : errno, system_category );
}
BOOST_FILESYSTEM_DECL error_code
@@ -1091,7 +1128,7 @@
const std::string & from_ph )
{
return error_code( ::symlink( to_ph.c_str(), from_ph.c_str() ) == 0
- ? 0 : errno, errno_ecat );
+ ? 0 : errno, system_category );
}
BOOST_FILESYSTEM_DECL error_code
@@ -1116,7 +1153,7 @@
// shall fail and set errno to EEXIST or ENOTEMPTY."
// Linux uses ENOTEMPTY, Solaris uses EEXIST.
if ( error == EEXIST ) error = ENOTEMPTY;
- return error_code( error, errno_ecat );
+ return error_code( error, system_category );
}
return error_code();
}
@@ -1127,9 +1164,9 @@
// POSIX is too permissive so must check
error_code dummy;
if ( fs::exists( status_api( to, dummy ) ) )
- return error_code( EEXIST, errno_ecat );
+ return error_code( EEXIST, system_category );
return error_code( std::rename( from.c_str(), to.c_str() ) != 0
- ? errno : 0, errno_ecat );
+ ? errno : 0, system_category );
}
BOOST_FILESYSTEM_DECL error_code
@@ -1149,7 +1186,7 @@
from_stat.st_mode )) < 0 )
{
if ( infile >= 0 ) ::close( infile );
- return error_code( errno, errno_ecat );
+ return error_code( errno, system_category );
}
ssize_t sz, sz_read=1, sz_write;
@@ -1174,7 +1211,32 @@
if ( ::close( infile) < 0 ) sz_read = -1;
if ( ::close( outfile) < 0 ) sz_read = -1;
- return error_code( sz_read < 0 ? errno : 0, errno_ecat );
+ return error_code( sz_read < 0 ? errno : 0, system_category );
+ }
+
+ // this code is based on Stevens and Rago, Advanced Programming in the
+ // UNIX envirnment, 2nd Ed., ISBN 0-201-43307-9, page 49
+ error_code path_max( std::size_t & result )
+ {
+# ifdef PATH_MAX
+ static std::size_t max = PATH_MAX;
+# else
+ static std::size_t max = 0;
+# endif
+ if ( max == 0 )
+ {
+ errno = 0;
+ long tmp = ::pathconf( "/", _PC_NAME_MAX );
+ if ( tmp < 0 )
+ {
+ if ( errno == 0 ) // indeterminate
+ max = 4096; // guess
+ else return error_code( errno, system_category );
+ }
+ else max = static_cast<std::size_t>( tmp + 1 ); // relative root
+ }
+ result = max;
+ return error_code();
}
BOOST_FILESYSTEM_DECL error_code
@@ -1182,15 +1244,17 @@
const std::string & dir, std::string & target,
file_status &, file_status & )
{
- static const std::string dummy_first_name( "." );
if ( (handle = ::opendir( dir.c_str() )) == 0 )
- return error_code( errno, errno_ecat );
- target = dummy_first_name;
- long pc_name_max( ::pathconf( dir.c_str(), _PC_NAME_MAX ) );
- if ( pc_name_max == -1L ) return error_code( errno, errno_ecat );
+ return error_code( errno, system_category );
+ target = std::string( "." ); // string was static but caused trouble
+ // when iteration called from dtor, after
+ // static had already been destroyed
+ std::size_t path_size;
+ error_code ec = path_max( path_size );
+ if ( ec ) return ec;
dirent de;
buffer = std::malloc( (sizeof(dirent) - sizeof(de.d_name))
- + static_cast<std::size_t>( pc_name_max ) + 1 );
+ + path_size + 1 ); // + 1 for "/0"
return error_code();
}
@@ -1202,7 +1266,7 @@
if ( handle == 0 ) return error_code();
DIR * h( static_cast<DIR*>(handle) );
handle = 0;
- return error_code( ::closedir( h ) == 0 ? 0 : errno, errno_ecat );
+ return error_code( ::closedir( h ) == 0 ? 0 : errno, system_category );
}
// warning: the only dirent member updated is d_name
@@ -1215,7 +1279,7 @@
&& defined(_POSIX_THREAD_SAFE_FUNCTIONS) \
&& defined(_SC_THREAD_SAFE_FUNCTIONS) \
&& (_POSIX_THREAD_SAFE_FUNCTIONS+0 >= 0) \
- && (!defined(__hpux) || (defined(__hpux) && defined(_REENTRANT)))
+ && (!defined(__HP_aCC) || (defined(__HP_aCC) && defined(_REENTRANT)))
if ( ::sysconf( _SC_THREAD_SAFE_FUNCTIONS ) >= 0 )
{ return ::readdir_r( dirp, entry, result ); }
# endif
@@ -1238,7 +1302,7 @@
dirent * result;
int return_code;
if ( (return_code = readdir_r_simulator( static_cast<DIR*>(handle),
- entry, &result )) != 0 ) return error_code( errno, errno_ecat );
+ entry, &result )) != 0 ) return error_code( errno, system_category );
if ( result == 0 ) return dir_itr_close( handle, buffer );
target = entry->d_name;
# ifdef BOOST_FILESYSTEM_STATUS_CACHE
Modified: trunk/libs/filesystem/src/path.cpp
==============================================================================
--- trunk/libs/filesystem/src/path.cpp (original)
+++ trunk/libs/filesystem/src/path.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -36,10 +36,16 @@
return lc;
}
- const std::codecvt<wchar_t, char, std::mbstate_t> *
- converter(
+ const std::codecvt<wchar_t, char, std::mbstate_t> *&
+ converter()
+ {
+ static const std::codecvt<wchar_t, char, std::mbstate_t> *
+ cvtr(
&std::use_facet<std::codecvt<wchar_t, char, std::mbstate_t> >
( loc() ) );
+ return cvtr;
+ }
+
bool locked(false);
} // unnamed namespace
@@ -52,7 +58,7 @@
if ( locked ) return false;
locked = true;
loc() = new_loc;
- converter = &std::use_facet
+ converter() = &std::use_facet
<std::codecvt<wchar_t, char, std::mbstate_t> >( loc() );
return true;
}
@@ -62,9 +68,42 @@
if ( locked ) boost::throw_exception(
wfilesystem_error(
"boost::filesystem::wpath_traits::imbue() after lockdown",
- system::error_code(EOTHER, system::errno_ecat) ) );
+ system::make_error_code( system::posix::not_supported ) ) );
imbue( new_loc, std::nothrow );
}
+
+ //namespace detail
+ //{
+ // BOOST_FILESYSTEM_DECL
+ // const char * what( const char * sys_err_what,
+ // const path & path1, const path & path2, std::string & target)
+ // {
+ // try
+ // {
+ // if ( target.empty() )
+ // {
+ // target = sys_err_what;
+ // if ( !path1.empty() )
+ // {
+ // target += ": \"";
+ // target += path1.file_string();
+ // target += "\"";
+ // }
+ // if ( !path2.empty() )
+ // {
+ // target += ", \"";
+ // target += path2.file_string();
+ // target += "\"";
+ // }
+ // }
+ // return target.c_str();
+ // }
+ // catch (...)
+ // {
+ // return sys_err_what;
+ // }
+ // }
+ //}
# ifdef BOOST_POSIX_API
@@ -81,12 +120,12 @@
std::mbstate_t state;
const internal_string_type::value_type * from_next;
external_string_type::value_type * to_next;
- if ( converter->out(
+ if ( converter()->out(
state, src.c_str(), src.c_str()+src.size(), from_next, work.get(),
work.get()+work_size, to_next ) != std::codecvt_base::ok )
boost::throw_exception( boost::filesystem::wfilesystem_error(
"boost::filesystem::wpath::to_external conversion error",
- ph, system::error_code( EINVAL, system::errno_ecat ) ) );
+ ph, system::posix::invalid_argument ) );
*to_next = '\0';
return external_string_type( work.get() );
}
@@ -100,12 +139,12 @@
std::mbstate_t state;
const external_string_type::value_type * from_next;
internal_string_type::value_type * to_next;
- if ( converter->in(
+ if ( converter()->in(
state, src.c_str(), src.c_str()+src.size(), from_next, work.get(),
work.get()+work_size, to_next ) != std::codecvt_base::ok )
boost::throw_exception( boost::filesystem::wfilesystem_error(
"boost::filesystem::wpath::to_internal conversion error",
- system::error_code( EINVAL, system::errno_ecat ) ) );
+ system::posix::invalid_argument ) );
*to_next = L'\0';
return internal_string_type( work.get() );
}
Modified: trunk/libs/filesystem/test/Jamfile.v2
==============================================================================
--- trunk/libs/filesystem/test/Jamfile.v2 (original)
+++ trunk/libs/filesystem/test/Jamfile.v2 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -16,7 +16,7 @@
<runtime-link>static <link>static
]
[ run path_test.cpp ../../system/build
- : : : <define>BOOST_FILESYSTEM_DYN_LINK
+ : : : <define>BOOST_FILESYSTEM_DYN_LINK <define>BOOST_SYSTEM_DYN_LINK
: path_test_dll
]
[ run operations_test.cpp
@@ -24,7 +24,7 @@
<runtime-link>static <link>static
]
[ run operations_test.cpp ../../system/build
- : : : <define>BOOST_FILESYSTEM_DYN_LINK
+ : : : <define>BOOST_FILESYSTEM_DYN_LINK <define>BOOST_SYSTEM_DYN_LINK
: operations_test_dll
]
[ run fstream_test.cpp
Modified: trunk/libs/filesystem/test/convenience_test.cpp
==============================================================================
--- trunk/libs/filesystem/test/convenience_test.cpp (original)
+++ trunk/libs/filesystem/test/convenience_test.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -18,6 +18,7 @@
#include <boost/filesystem/convenience.hpp>
namespace fs = boost::filesystem;
using fs::path;
+namespace sys = boost::system;
#include <boost/test/minimal.hpp>
#include <boost/bind.hpp>
@@ -45,7 +46,15 @@
}
return false;
}
+
+ void create_recursive_iterator( const fs::path & ph )
+ {
+ fs::recursive_directory_iterator it( ph );
+ }
}
+
+// --------------------------------------------------------------------------//
+
int test_main( int, char*[] )
{
path::default_name_check( fs::no_check ); // names below not valid on all O/S's
@@ -63,13 +72,13 @@
BOOST_CHECK( fs::exists( "xx" ) );
BOOST_CHECK( fs::is_directory( "xx" ) );
- BOOST_CHECK( fs::create_directories( "xx/ww/zz" ) );
+ BOOST_CHECK( fs::create_directories( "xx/yy/zz" ) );
BOOST_CHECK( fs::exists( "xx" ) );
- BOOST_CHECK( fs::exists( "xx/ww" ) );
- BOOST_CHECK( fs::exists( "xx/ww/zz" ) );
+ BOOST_CHECK( fs::exists( "xx/yy" ) );
+ BOOST_CHECK( fs::exists( "xx/yy/zz" ) );
BOOST_CHECK( fs::is_directory( "xx" ) );
- BOOST_CHECK( fs::is_directory( "xx/ww" ) );
- BOOST_CHECK( fs::is_directory( "xx/ww/zz" ) );
+ BOOST_CHECK( fs::is_directory( "xx/yy" ) );
+ BOOST_CHECK( fs::is_directory( "xx/yy/zz" ) );
path is_a_file( "xx/uu" );
{
@@ -109,5 +118,82 @@
// see the rationale in html docs for explanation why this works
BOOST_CHECK( fs::change_extension("", ".png").string() == ".png" );
+// recursive_directory_iterator tests ----------------------------------------//
+
+ sys::error_code ec;
+ fs::recursive_directory_iterator it( "/no-such-path", ec );
+ BOOST_CHECK( ec );
+ BOOST_CHECK( throws_fs_error(
+ boost::bind( create_recursive_iterator, "/no-such-path" ) ) );
+
+ fs::remove( "xx/uu" );
+
+#ifdef BOOST_WINDOWS_API
+ // These tests depends on ordering of directory entries, and that's guaranteed
+ // on Windows but not necessarily on other operating systems
+ {
+ std::ofstream f( "xx/yya" );
+ BOOST_CHECK( !!f );
+ }
+
+ for ( it = fs::recursive_directory_iterator( "xx" );
+ it != fs::recursive_directory_iterator(); ++it )
+ { std::cout << *it << '\n'; }
+
+ it = fs::recursive_directory_iterator( "xx" );
+ BOOST_CHECK( it->path() == "xx/yy" );
+ BOOST_CHECK( it.level() == 0 );
+ ++it;
+ BOOST_CHECK( it->path() == "xx/yy/zz" );
+ BOOST_CHECK( it.level() == 1 );
+ it.pop();
+ BOOST_CHECK( it->path() == "xx/yya" );
+ BOOST_CHECK( it.level() == 0 );
+ it++;
+ BOOST_CHECK( it == fs::recursive_directory_iterator() );
+
+ it = fs::recursive_directory_iterator( "xx" );
+ BOOST_CHECK( it->path() == "xx/yy" );
+ it.no_push();
+ ++it;
+ BOOST_CHECK( it->path() == "xx/yya" );
+ ++it;
+ BOOST_CHECK( it == fs::recursive_directory_iterator() );
+
+ fs::remove( "xx/yya" );
+#endif
+
+ it = fs::recursive_directory_iterator( "xx/yy/zz" );
+ BOOST_CHECK( it == fs::recursive_directory_iterator() );
+
+ it = fs::recursive_directory_iterator( "xx" );
+ BOOST_CHECK( it->path() == "xx/yy" );
+ BOOST_CHECK( it.level() == 0 );
+ ++it;
+ BOOST_CHECK( it->path() == "xx/yy/zz" );
+ BOOST_CHECK( it.level() == 1 );
+ it++;
+ BOOST_CHECK( it == fs::recursive_directory_iterator() );
+
+ it = fs::recursive_directory_iterator( "xx" );
+ BOOST_CHECK( it->path() == "xx/yy" );
+ it.no_push();
+ ++it;
+ BOOST_CHECK( it == fs::recursive_directory_iterator() );
+
+ it = fs::recursive_directory_iterator( "xx" );
+ BOOST_CHECK( it->path() == "xx/yy" );
+ ++it;
+ it.pop();
+ BOOST_CHECK( it == fs::recursive_directory_iterator() );
+
+
+
+ // nothrow wrong. see imp. Make sure failed basic_directory_iterator
+ // ctor creates the end iterator.
+
+
+
+
return 0;
}
Modified: trunk/libs/filesystem/test/operations_test.cpp
==============================================================================
--- trunk/libs/filesystem/test/operations_test.cpp (original)
+++ trunk/libs/filesystem/test/operations_test.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -16,7 +16,7 @@
#include <boost/filesystem/operations.hpp>
#include <boost/filesystem/convenience.hpp>
-#include <boost/filesystem/cerrno.hpp>
+#include <boost/cerrno.hpp>
namespace fs = boost::filesystem;
#include <boost/config.hpp>
@@ -26,7 +26,7 @@
using boost::bind;
using boost::system::error_code;
-using boost::system::errno_ecat;
+using boost::system::system_category;
using boost::system::system_error;
#include <fstream>
@@ -71,7 +71,7 @@
std::ofstream f( ph.file_string().c_str() );
if ( !f )
throw fs::filesystem_error( "operations_test create_file",
- ph, error_code(errno, errno_ecat) );
+ ph, error_code(errno, system_category) );
if ( !contents.empty() ) f << contents;
}
@@ -80,7 +80,7 @@
std::ifstream f( ph.file_string().c_str() );
if ( !f )
throw fs::filesystem_error( "operations_test verify_file",
- ph, error_code(errno, errno_ecat) );
+ ph, error_code(errno, system_category) );
std::string contents;
f >> contents;
if ( contents != expected )
@@ -101,10 +101,10 @@
std::cout << "\n" << ex.what() << "\n";
}
if ( en == 0
- || en == ex.code().to_errno() ) return true;
+ || en == ex.code().default_error_condition().value() ) return true;
std::cout
<< "\nWarning: line " << line
- << " exception reports iso() " << ex.code().to_errno()
+ << " exception reports default_error_condition().value() " << ex.code().default_error_condition().value()
<< ", should be " << en
<< "\n value() is " << ex.code().value()
<< std::endl;
@@ -160,7 +160,7 @@
if ( report_throws ) std::cout << x.what() << std::endl;
if ( platform == "Windows" )
BOOST_CHECK( std::strcmp( x.what(),
- "boost::filesystem::create_directory: The system cannot find the path specified." ) == 0 );
+ "boost::filesystem::create_directory: The system cannot find the path specified" ) == 0 );
}
BOOST_CHECK( exception_thrown );
@@ -176,8 +176,12 @@
if ( platform == "Windows" )
{
bool ok ( std::strcmp( x.what(),
- "boost::filesystem::create_directory: The system cannot find the path specified.: \"no-such-dir\\foo\\bar\"" ) == 0 );
+ "boost::filesystem::create_directory: The system cannot find the path specified: \"no-such-dir\\foo\\bar\"" ) == 0 );
BOOST_CHECK( ok );
+ if ( !ok )
+ {
+ std::cout << "what returns \"" << x.what() << "\"" << std::endl;
+ }
}
}
BOOST_CHECK( exception_thrown );
@@ -194,8 +198,12 @@
if ( platform == "Windows" )
{
bool ok ( std::strcmp( x.what(),
- "boost::filesystem::create_directory: The system cannot find the path specified.: \"no-such-dir\\foo\\bar\"" ) == 0 );
+ "boost::filesystem::create_directory: The system cannot find the path specified: \"no-such-dir\\foo\\bar\"" ) == 0 );
BOOST_CHECK( ok );
+ if ( !ok )
+ {
+ std::cout << "what returns \"" << x.what() << "\"" << std::endl;
+ }
}
}
BOOST_CHECK( exception_thrown );
@@ -222,7 +230,7 @@
? "Windows"
: "POSIX";
# endif
- std::cout << "API is " << platform << '\n';
+ std::cout << "API is " << platform << std::endl;
exception_tests();
@@ -325,26 +333,12 @@
{
BOOST_CHECK( ex.path1().string() == " no-way, Jose" );
}
-
- BOOST_CHECK( fs::create_directory( dir ) );
-
// several functions give unreasonable results if uintmax_t isn't 64-bits
std::cout << "sizeof(boost::uintmax_t) = " << sizeof(boost::uintmax_t) << '\n';
BOOST_CHECK( sizeof( boost::uintmax_t ) >= 8 );
- // make some reasonable assuptions for testing purposes
- fs::space_info spi( fs::space( dir ) );
- BOOST_CHECK( spi.capacity > 1000000 );
- BOOST_CHECK( spi.free > 1000 );
- BOOST_CHECK( spi.capacity > spi.free );
- BOOST_CHECK( spi.free >= spi.available );
-
- // it is convenient to display space, but older VC++ versions choke
-# if !defined(BOOST_MSVC) || _MSC_VER >= 1300 // 1300 == VC++ 7.0
- std::cout << " capacity = " << spi.capacity << '\n';
- std::cout << " free = " << spi.free << '\n';
- std::cout << "available = " << spi.available << '\n';
-# endif
+ // create a directory, then check it for consistency
+ BOOST_CHECK( fs::create_directory( dir ) );
BOOST_CHECK( fs::exists( dir ) );
BOOST_CHECK( BOOST_FS_IS_EMPTY( dir ) );
@@ -359,6 +353,30 @@
BOOST_CHECK( !fs::is_other( stat ) );
BOOST_CHECK( !fs::is_symlink( stat ) );
+ // set the current directory, then check it for consistency
+ fs::path original_dir = fs::current_path<fs::path>();
+ BOOST_CHECK( dir != original_dir );
+ fs::current_path( dir );
+ BOOST_CHECK( fs::current_path<fs::path>() == dir );
+ BOOST_CHECK( fs::current_path<fs::path>() != original_dir );
+ fs::current_path( original_dir );
+ BOOST_CHECK( fs::current_path<fs::path>() == original_dir );
+ BOOST_CHECK( fs::current_path<fs::path>() != dir );
+
+ // make some reasonable assuptions for testing purposes
+ fs::space_info spi( fs::space( dir ) );
+ BOOST_CHECK( spi.capacity > 1000000 );
+ BOOST_CHECK( spi.free > 1000 );
+ BOOST_CHECK( spi.capacity > spi.free );
+ BOOST_CHECK( spi.free >= spi.available );
+
+ // it is convenient to display space, but older VC++ versions choke
+# if !defined(BOOST_MSVC) || _MSC_VER >= 1300 // 1300 == VC++ 7.0
+ std::cout << " capacity = " << spi.capacity << '\n';
+ std::cout << " free = " << spi.free << '\n';
+ std::cout << "available = " << spi.available << '\n';
+# endif
+
if ( platform == "Windows" )
BOOST_CHECK( CHECK_EXCEPTION( bind( BOOST_BND(fs::file_size), dir ),
ENOENT ) );
Modified: trunk/libs/filesystem/test/path_test.cpp
==============================================================================
--- trunk/libs/filesystem/test/path_test.cpp (original)
+++ trunk/libs/filesystem/test/path_test.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -180,7 +180,7 @@
void exception_tests()
{
const std::string str_1("string-1");
- boost::system::error_code ec( 12345, boost::system::errno_ecat);
+ boost::system::error_code ec( 12345, boost::system::system_category);
try { throw fs::filesystem_error( str_1, ec ); }
catch ( const fs::filesystem_error & ex )
{
Deleted: trunk/libs/system/build/Jamfile
==============================================================================
--- trunk/libs/system/build/Jamfile 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
+++ (empty file)
@@ -1,56 +0,0 @@
-# Boost System Library Build Jamfile
-
-# (C) Copyright Beman Dawes 2002, 2006
-
-# Distributed under the Boost Software License, Version 1.0.
-# (See accompanying file LICENSE_1_0.txt or www.boost.org/LICENSE_1_0.txt)
-
-# See library home page at http://www.boost.org/libs/system
-
-subproject libs/system/build ;
-
-SOURCES = error_code ;
-
-lib boost_system
- : ../src/$(SOURCES).cpp
- : # build requirements
- <define>BOOST_SYSTEM_STATIC_LINK
- <sysinclude>$(BOOST_AUX_ROOT) <sysinclude>$(BOOST_ROOT)
- # common-variant-tag ensures that the library will
- # be named according to the rules used by the install
- # and auto-link features:
- common-variant-tag
- : debug release # build variants
- ;
-
-dll boost_system
- : ../src/$(SOURCES).cpp
- : # build requirements
- <define>BOOST_SYSTEM_DYN_LINK=1 # tell source we're building dll's
- <runtime-link>dynamic # build only for dynamic runtimes
- <sysinclude>$(BOOST_AUX_ROOT) <sysinclude>$(BOOST_ROOT)
- # common-variant-tag ensures that the library will
- # be named according to the rules used by the install
- # and auto-link features:
- common-variant-tag
- : debug release # build variants
- ;
-
-install system lib
- : <lib>boost_system <dll>boost_system
- ;
-
-stage stage/lib : <lib>boost_system <dll>boost_system
- :
- # copy to a path rooted at BOOST_ROOT:
- <locate>$(BOOST_ROOT)
- # make sure the names of the libraries are correctly named:
- common-variant-tag
- # add this target to the "stage" and "all" psuedo-targets:
- <target>stage
- <target>all
- :
- debug release
- ;
-
-# end
Modified: trunk/libs/system/doc/error_code.html
==============================================================================
--- trunk/libs/system/doc/error_code.html (original)
+++ trunk/libs/system/doc/error_code.html 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -30,6 +30,11 @@
an identifier for a
particular kind of error code. Users or
third-parties may add additional error categories.</p>
+<h2>Builds</h2>
+<p>The system library is required by default. If the preprocessor macro name
+BOOST_ERROR_CODE_HEADER_ONLY is defined, no object library or shared/DLL library
+is required. Only one translation unit may defined BOOST_ERROR_CODE_HEADER_ONLY,
+otherwise symbols will be multiply defined.</p>
<h2><a name="Synopsis">Synopsis</a></h2>
<pre>namespace boost
{
@@ -254,7 +259,7 @@
Oleg Abrosimov.</p>
<hr>
<p>Last revised:
-<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->06 September, 2006<!--webbot bot="Timestamp" endspan i-checksum="39349" --></p>
+<!--webbot bot="Timestamp" s-type="EDITED" s-format="%d %B, %Y" startspan -->30 August, 2007<!--webbot bot="Timestamp" endspan i-checksum="34727" --></p>
<p>© Copyright Beman Dawes, 2006</p>
<p>Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at
Modified: trunk/libs/system/src/error_code.cpp
==============================================================================
--- trunk/libs/system/src/error_code.cpp (original)
+++ trunk/libs/system/src/error_code.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -30,15 +30,12 @@
#include <cassert>
using namespace boost::system;
+using namespace boost::system::posix;
#include <cstring> // for strerror/strerror_r
-# ifdef BOOST_NO_STDC_NAMESPACE
- namespace std { using ::strerror; }
-# endif
-
# if defined( BOOST_WINDOWS_API )
-# include "windows.h"
+# include <windows.h>
# ifndef ERROR_INCORRECT_SIZE
# define ERROR_INCORRECT_SIZE ERROR_BAD_ARGUMENTS
# endif
@@ -49,75 +46,282 @@
namespace
{
-#ifdef BOOST_WINDOWS_API
- struct native_to_errno_t
+ struct system_to_posix_t
{
- boost::int32_t native_value;
- int to_errno;
+ int system_value;
+ boost::system::posix::posix_errno posix_value;
};
- const native_to_errno_t native_to_errno[] =
+ const system_to_posix_t system_to_posix[] =
{
+
+#ifdef BOOST_POSIX_API
+ // POSIX-like O/S -> posix_errno decode table ----------------------------//
+
+ // most common errors first to speed sequential search
+ { ENOENT, no_such_file_or_directory },
+ { EACCES, permission_denied },
+ { EINVAL, invalid_argument },
+
+ // rest are alphabetical for easy maintenance
+ { 0, success },
+ { E2BIG, argument_list_too_long },
+ { EADDRINUSE, address_in_use },
+ { EADDRNOTAVAIL, address_not_available },
+ { EAFNOSUPPORT, address_family_not_supported },
+ { EAGAIN, resource_unavailable_try_again },
+ { EALREADY, connection_already_in_progress },
+ { EBADF, bad_file_descriptor },
+ { EBADMSG, bad_message },
+ { EBUSY, device_or_resource_busy },
+ { ECANCELED, operation_canceled },
+ { ECHILD, no_child_process },
+ { ECONNABORTED, connection_aborted },
+ { ECONNREFUSED, connection_refused },
+ { ECONNRESET, connection_reset },
+ { EDEADLK, resource_deadlock_would_occur },
+ { EDESTADDRREQ, destination_address_required },
+ { EDOM, argument_out_of_domain },
+ { EEXIST, file_exists },
+ { EFAULT, bad_address },
+ { EFBIG, file_too_large },
+ { EHOSTUNREACH, host_unreachable },
+ { EIDRM, identifier_removed },
+ { EILSEQ, illegal_byte_sequence },
+ { EINPROGRESS, operation_in_progress },
+ { EINTR, interrupted },
+ { EIO, io_error },
+ { EISCONN, already_connected },
+ { EISDIR, is_a_directory },
+ { ELOOP, too_many_synbolic_link_levels },
+ { EMFILE, too_many_files_open },
+ { EMLINK, too_many_links },
+ { EMSGSIZE, message_size },
+ { ENAMETOOLONG, filename_too_long },
+ { ENETDOWN, network_down },
+ { ENETRESET, network_reset },
+ { ENETUNREACH, network_unreachable },
+ { ENFILE, too_many_files_open_in_system },
+ { ENOBUFS, no_buffer_space },
+ { ENODATA, no_message_available },
+ { ENODEV, no_such_device },
+ { ENOEXEC, executable_format_error },
+ { ENOLCK, no_lock_available },
+ { ENOLINK, no_link },
+ { ENOMEM, not_enough_memory },
+ { ENOMSG, no_message },
+ { ENOPROTOOPT, no_protocol_option },
+ { ENOSPC, no_space_on_device },
+ { ENOSR, no_stream_resources },
+ { ENOSTR, not_a_stream },
+ { ENOSYS, function_not_supported },
+ { ENOTCONN, not_connected },
+ { ENOTDIR, not_a_directory },
+ { ENOTEMPTY, directory_not_empty },
+ { ENOTRECOVERABLE, state_not_recoverable },
+ { ENOTSOCK, not_a_socket },
+ { ENOTSUP, not_supported },
+ { ENOTTY, inappropriate_io_control_operation },
+ { ENXIO, no_such_device_or_address },
+ { EOPNOTSUPP, operation_not_supported },
+ { EOVERFLOW, value_too_large },
+ { EOWNERDEAD, owner_dead },
+ { EPERM, operation_not_permitted },
+ { EPIPE, broken_pipe },
+ { EPROTO, protocol_error },
+ { EPROTONOSUPPORT, protocol_not_supported },
+ { EPROTOTYPE, wrong_protocol_type },
+ { ERANGE, result_out_of_range },
+ { EROFS, read_only_file_system },
+ { ESPIPE, invalid_seek },
+ { ESRCH, no_such_process },
+ { ETIME, stream_timeout },
+ { ETIMEDOUT, timed_out },
+ { ETXTBSY, text_file_busy },
+ { EWOULDBLOCK, operation_would_block },
+ { EXDEV, cross_device_link }
+
+#else
+
+ // Windows system -> posix_errno decode table ----------------------------//
+
// see WinError.h comments for descriptions of errors
// most common errors first to speed sequential search
- { ERROR_FILE_NOT_FOUND, ENOENT },
- { ERROR_PATH_NOT_FOUND, ENOENT },
+ { ERROR_FILE_NOT_FOUND, no_such_file_or_directory },
+ { ERROR_PATH_NOT_FOUND, no_such_file_or_directory },
// rest are alphabetical for easy maintenance
- { 0, 0 }, // no error
- { ERROR_ACCESS_DENIED, EACCES },
- { ERROR_ALREADY_EXISTS, EEXIST },
- { ERROR_BAD_UNIT, ENODEV },
- { ERROR_BUFFER_OVERFLOW, ENAMETOOLONG },
- { ERROR_BUSY, EBUSY },
- { ERROR_BUSY_DRIVE, EBUSY },
- { ERROR_CANNOT_MAKE, EACCES },
- { ERROR_CANTOPEN, EIO },
- { ERROR_CANTREAD, EIO },
- { ERROR_CANTWRITE, EIO },
- { ERROR_CURRENT_DIRECTORY, EACCES },
- { ERROR_DEV_NOT_EXIST, ENODEV },
- { ERROR_DEVICE_IN_USE, EBUSY },
- { ERROR_DIR_NOT_EMPTY, ENOTEMPTY },
- { ERROR_DIRECTORY, EINVAL }, // WinError.h: "The directory name is invalid"
- { ERROR_DISK_FULL, ENOSPC },
- { ERROR_FILE_EXISTS, EEXIST },
- { ERROR_HANDLE_DISK_FULL, ENOSPC },
- { ERROR_INVALID_ACCESS, EACCES },
- { ERROR_INVALID_DRIVE, ENODEV },
- { ERROR_INVALID_FUNCTION, ENOSYS },
- { ERROR_INVALID_HANDLE, EBADHANDLE },
- { ERROR_INVALID_NAME, EINVAL },
- { ERROR_LOCK_VIOLATION, EACCES },
- { ERROR_LOCKED, EACCES },
- { ERROR_NEGATIVE_SEEK, EINVAL },
- { ERROR_NOACCESS, EACCES },
- { ERROR_NOT_ENOUGH_MEMORY, ENOMEM },
- { ERROR_NOT_READY, EAGAIN },
- { ERROR_NOT_SAME_DEVICE, EXDEV },
- { ERROR_OPEN_FAILED, EIO },
- { ERROR_OPEN_FILES, EBUSY },
- { ERROR_OUTOFMEMORY, ENOMEM },
- { ERROR_READ_FAULT, EIO },
- { ERROR_SEEK, EIO },
- { ERROR_SHARING_VIOLATION, EACCES },
- { ERROR_TOO_MANY_OPEN_FILES, ENFILE },
- { ERROR_WRITE_FAULT, EIO },
- { ERROR_WRITE_PROTECT, EROFS }
+ { 0, success },
+ { ERROR_ACCESS_DENIED, permission_denied },
+ { ERROR_ALREADY_EXISTS, file_exists },
+ { ERROR_BAD_UNIT, no_such_device },
+ { ERROR_BUFFER_OVERFLOW, filename_too_long },
+ { ERROR_BUSY, device_or_resource_busy },
+ { ERROR_BUSY_DRIVE, device_or_resource_busy },
+ { ERROR_CANNOT_MAKE, permission_denied },
+ { ERROR_CANTOPEN, io_error },
+ { ERROR_CANTREAD, io_error },
+ { ERROR_CANTWRITE, io_error },
+ { ERROR_CURRENT_DIRECTORY, permission_denied },
+ { ERROR_DEV_NOT_EXIST, no_such_device },
+ { ERROR_DEVICE_IN_USE, device_or_resource_busy },
+ { ERROR_DIR_NOT_EMPTY, directory_not_empty },
+ { ERROR_DIRECTORY, invalid_argument }, // WinError.h: "The directory name is invalid"
+ { ERROR_DISK_FULL, no_space_on_device },
+ { ERROR_FILE_EXISTS, file_exists },
+ { ERROR_HANDLE_DISK_FULL, no_space_on_device },
+ { ERROR_INVALID_ACCESS, permission_denied },
+ { ERROR_INVALID_DRIVE, no_such_device },
+ { ERROR_INVALID_FUNCTION, function_not_supported },
+ { ERROR_INVALID_HANDLE, invalid_argument },
+ { ERROR_INVALID_NAME, invalid_argument },
+ { ERROR_LOCK_VIOLATION, no_lock_available },
+ { ERROR_LOCKED, no_lock_available },
+ { ERROR_NEGATIVE_SEEK, invalid_argument },
+ { ERROR_NOACCESS, permission_denied },
+ { ERROR_NOT_ENOUGH_MEMORY, not_enough_memory },
+ { ERROR_NOT_READY, resource_unavailable_try_again },
+ { ERROR_NOT_SAME_DEVICE, cross_device_link },
+ { ERROR_OPEN_FAILED, io_error },
+ { ERROR_OPEN_FILES, device_or_resource_busy },
+ { ERROR_OUTOFMEMORY, not_enough_memory },
+ { ERROR_READ_FAULT, io_error },
+ { ERROR_SEEK, io_error },
+ { ERROR_SHARING_VIOLATION, permission_denied },
+ { ERROR_TOO_MANY_OPEN_FILES, too_many_files_open },
+ { ERROR_WRITE_FAULT, io_error },
+ { ERROR_WRITE_PROTECT, permission_denied }
+
+#endif
+ };
+
+ // standard error categories -------------------------------------------//
+
+ class posix_error_category : public error_category
+ {
+ public:
+ const char * name() const;
+ std::string message( int ev ) const;
};
- int windows_ed( const error_code & ec )
+ class system_error_category : public error_category
{
- const native_to_errno_t * cur = native_to_errno;
+ public:
+ const char * name() const;
+ posix::posix_errno posix( int ev ) const;
+ std::string message( int ev ) const;
+ error_condition default_error_condition( int ev ) const;
+ };
+
+ const posix_error_category posix_category_const;
+ const system_error_category system_category_const;
+
+ // posix_error_category implementation ---------------------------------//
+
+ const char * posix_error_category::name() const
+ {
+ return "POSIX";
+ }
+
+ std::string posix_error_category::message( int ev ) const
+ {
+ // strerror_r is preferred because it is always thread safe,
+ // however, we fallback to strerror in certain cases because:
+ // -- Windows doesn't provide strerror_r.
+ // -- HP and Sundo provide strerror_r on newer systems, but there is
+ // no way to tell if is available at runtime and in any case their
+ // versions of strerror are thread safe anyhow.
+ // -- Linux only sometimes provides strerror_r.
+ // -- Tru64 provides strerror_r only when compiled -pthread.
+ // -- VMS doesn't provide strerror_r, but on this platform, strerror is
+ // thread safe.
+ # if defined(BOOST_WINDOWS_API) || defined(__hpux) || defined(__sun)\
+ || (defined(__linux) && (!defined(__USE_XOPEN2K) || defined(BOOST_SYSTEM_USE_STRERROR)))\
+ || (defined(__osf__) && !defined(_REENTRANT))\
+ || (defined(__vms))
+ const char * c_str = std::strerror( ev );
+ return std::string( c_str ? c_str : "invalid_argument" );
+ # else
+ char buf[64];
+ char * bp = buf;
+ std::size_t sz = sizeof(buf);
+ # if defined(__CYGWIN__) || defined(__USE_GNU)
+ // Oddball version of strerror_r
+ const char * c_str = strerror_r( ev, bp, sz );
+ return std::string( c_str ? c_str : "invalid_argument" );
+ # else
+ // POSIX version of strerror_r
+ int result;
+ for (;;)
+ {
+ // strerror_r returns 0 on success, otherwise ERANGE if buffer too small,
+ // invalid_argument if ev not a valid error number
+ if ( (result = strerror_r( ev, bp, sz )) == 0 )
+ break;
+ else
+ {
+ # if defined(__linux)
+ // Linux strerror_r returns -1 on error, with error number in errno
+ result = errno;
+ # endif
+ if ( result != ERANGE ) break;
+ if ( sz > sizeof(buf) ) std::free( bp );
+ sz *= 2;
+ if ( (bp = static_cast<char*>(std::malloc( sz ))) == 0 )
+ return std::string( "ENOMEM" );
+ }
+ }
+ try
+ {
+ std::string msg( ( result == invalid_argument ) ? "invalid_argument" : bp );
+ if ( sz > sizeof(buf) ) std::free( bp );
+ sz = 0;
+ return msg;
+ }
+ catch(...)
+ {
+ if ( sz > sizeof(buf) ) std::free( bp );
+ throw;
+ }
+ # endif
+ # endif
+ }
+ // system_error_category implementation --------------------------------//
+
+ const char * system_error_category::name() const
+ {
+ return "system";
+ }
+
+ posix_errno system_error_category::posix( int ev ) const
+ {
+ const system_to_posix_t * cur = system_to_posix;
do
{
- if ( ec.value() == cur->native_value ) return cur->to_errno;
+ if ( ev == cur->system_value )
+ return cur->posix_value;
++cur;
- } while ( cur != native_to_errno + sizeof(native_to_errno)/sizeof(native_to_errno_t) );
- return EOTHER;
+ } while ( cur != system_to_posix
+ + sizeof(system_to_posix)/sizeof(system_to_posix_t) );
+ return static_cast<posix::posix_errno>(-1);
+ }
+
+ error_condition system_error_category::default_error_condition( int ev ) const
+ {
+ int tmp = posix(ev);
+ return tmp >= 0
+ ? error_condition( tmp, posix_category )
+ : error_condition( ev, system_category );
}
+# if !defined( BOOST_WINDOWS_API )
+
+ std::string system_error_category::message( int ev ) const
+ {
+ return posix_category.message( ev );
+ }
+# else
// TODO:
//Some quick notes on the implementation (sorry for the noise if
@@ -133,8 +337,7 @@
//
//Cheers,
//Chris
-
- std::string windows_md( const error_code & ec )
+ std::string system_error_category::message( int ev ) const
{
LPVOID lpMsgBuf;
::FormatMessageA(
@@ -142,7 +345,7 @@
FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL,
- ec.value(),
+ ev,
MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
(LPSTR) &lpMsgBuf,
0,
@@ -153,185 +356,31 @@
while ( str.size()
&& (str[str.size()-1] == '\n' || str[str.size()-1] == '\r') )
str.erase( str.size()-1 );
+ if ( str.size() && str[str.size()-1] == '.' )
+ { str.erase( str.size()-1 ); }
return str;
}
-
- wstring_t windows_wmd( const error_code & ec )
- {
- LPVOID lpMsgBuf;
- ::FormatMessageW(
- FORMAT_MESSAGE_ALLOCATE_BUFFER |
- FORMAT_MESSAGE_FROM_SYSTEM |
- FORMAT_MESSAGE_IGNORE_INSERTS,
- NULL,
- ec.value(),
- MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), // Default language
- (LPWSTR) &lpMsgBuf,
- 0,
- NULL
- );
- wstring_t str( static_cast<LPCWSTR>(lpMsgBuf) );
- ::LocalFree( lpMsgBuf ); // free the buffer
- while ( str.size()
- && (str[str.size()-1] == L'\n' || str[str.size()-1] == L'\r') )
- str.erase( str.size()-1 );
- return str;
- }
-
-#endif
-
- int errno_ed( const error_code & ec ) { return ec.value(); }
-
- std::string errno_md( const error_code & ec )
- {
- // strerror_r is preferred because it is always thread safe,
- // however, we fallback to strerror in certain cases because:
- // -- Windows doesn't provide strerror_r.
- // -- HP and Sundo provide strerror_r on newer systems, but there is
- // no way to tell if is available at runtime and in any case their
- // versions of strerror are thread safe anyhow.
- // -- Linux only sometimes provides strerror_r.
- // -- Tru64 provides strerror_r only when compiled -pthread.
- // -- VMS doesn't provide strerror_r, but on this platform, strerror is
- // thread safe.
-# if defined(BOOST_WINDOWS_API) || defined(__hpux) || defined(__sun)\
- || (defined(__linux) && (!defined(__USE_XOPEN2K) || defined(BOOST_SYSTEM_USE_STRERROR)))\
- || (defined(__osf__) && !defined(_REENTRANT))\
- || (defined(__vms))
- const char * c_str = std::strerror( ec.value() );
- return std::string( c_str ? c_str : "EINVAL" );
-# else
- char buf[64];
- char * bp = buf;
- std::size_t sz = sizeof(buf);
-# if defined(__CYGWIN__) || defined(__USE_GNU)
- // Oddball version of strerror_r
- const char * c_str = strerror_r( ec.value(), bp, sz );
- return std::string( c_str ? c_str : "EINVAL" );
-# else
- // POSIX version of strerror_r
- int result;
- for (;;)
- {
- // strerror_r returns 0 on success, otherwise ERANGE if buffer too small,
- // EINVAL if ec.value() not a valid error number
- if ( (result = strerror_r( ec.value(), bp, sz )) == 0 )
- break;
- else
- {
-# if defined(__linux)
- // Linux strerror_r returns -1 on error, with error number in errno
- result = errno;
-# endif
- if ( result != ERANGE ) break;
- if ( sz > sizeof(buf) ) std::free( bp );
- sz *= 2;
- if ( (bp = static_cast<char*>(std::malloc( sz ))) == 0 )
- return std::string( "ENOMEM" );
- }
- }
- try
- {
- std::string msg( ( result == EINVAL ) ? "EINVAL" : bp );
- if ( sz > sizeof(buf) ) std::free( bp );
- sz = 0;
- return msg;
- }
- catch(...)
- {
- if ( sz > sizeof(buf) ) std::free( bp );
- throw;
- }
-# endif
# endif
- }
-
- wstring_t errno_wmd( const error_code & ec )
- {
- // TODO: Implement this:
- assert( 0 && "sorry, not implemented yet" );
- wstring_t str;
- return str;
- }
- struct decoder_element
- {
- errno_decoder ed;
- message_decoder md;
- wmessage_decoder wmd;
-
- decoder_element( errno_decoder ed_,
- message_decoder md_, wmessage_decoder wmd_ )
- : ed(ed_), md(md_), wmd(wmd_) {}
-
- decoder_element() : ed(0), md(0), wmd(0) {}
- };
-
- typedef std::vector< decoder_element > decoder_vec_type;
-
- decoder_vec_type & decoder_vec()
- {
- static const decoder_element init_decoders[] =
-#ifdef BOOST_WINDOWS_API
- { decoder_element( errno_ed, errno_md, errno_wmd ),
- decoder_element( windows_ed, windows_md, windows_wmd) };
-#else
- { decoder_element( errno_ed, errno_md, errno_wmd ) };
-#endif
-
- static decoder_vec_type dv( init_decoders,
- init_decoders + sizeof(init_decoders)/sizeof(decoder_element));
- return dv;
- }
} // unnamed namespace
namespace boost
{
namespace system
{
- error_category error_code::new_category(
- errno_decoder ed, message_decoder md, wmessage_decoder wmd )
- {
- decoder_vec().push_back( decoder_element( ed, md, wmd ) );
- return error_category( static_cast<value_type>(decoder_vec().size()) - 1 );
- }
- bool error_code::get_decoders( error_category cat,
- errno_decoder & ed, message_decoder & md, wmessage_decoder & wmd )
- {
- if ( cat.value() < decoder_vec().size() )
- {
- ed = decoder_vec()[cat.value()].ed;
- md = decoder_vec()[cat.value()].md;
- wmd = decoder_vec()[cat.value()].wmd;
- return true;
- }
- return false;
- }
+ BOOST_SYSTEM_DECL const error_category & posix_category
+ = posix_category_const;
- int error_code::to_errno() const
- {
- return (m_category.value() < decoder_vec().size()
- && decoder_vec()[m_category.value()].ed)
- ? decoder_vec()[m_category.value()].ed( *this )
- : EOTHER;
- }
+ BOOST_SYSTEM_DECL const error_category & system_category
+ = system_category_const;
- std::string error_code::message() const
- {
- return (m_category.value() < decoder_vec().size()
- && decoder_vec()[m_category.value()].md)
- ? decoder_vec()[m_category.value()].md( *this )
- : std::string( "API error" );
- }
+ // deprecated synonyms
+ BOOST_SYSTEM_DECL const error_category & errno_ecat
+ = posix_category_const;
- wstring_t error_code::wmessage() const
- {
- return (m_category.value() < decoder_vec().size()
- && decoder_vec()[m_category.value()].wmd)
- ? decoder_vec()[m_category.value()].wmd( *this )
- : wstring_t( L"API error" );
- }
+ BOOST_SYSTEM_DECL const error_category & native_ecat
+ = system_category_const;
} // namespace system
} // namespace boost
Modified: trunk/libs/system/test/Jamfile.v2
==============================================================================
--- trunk/libs/system/test/Jamfile.v2 (original)
+++ trunk/libs/system/test/Jamfile.v2 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -38,4 +38,7 @@
: : : <define>BOOST_SYSTEM_DYN_LINK
: system_error_test_dll
]
+ [ run header_only_test.cpp
+ : : : <runtime-link>static <link>static
+ ]
;
Modified: trunk/libs/system/test/error_code_test.cpp
==============================================================================
--- trunk/libs/system/test/error_code_test.cpp (original)
+++ trunk/libs/system/test/error_code_test.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -10,7 +10,7 @@
//----------------------------------------------------------------------------//
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
-// can be cause buffer overruns or other possible security issues if misused.
+// can cause buffer overruns or other possible security issues if misused.
// See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
// But the wording of the warning is misleading and unsettling, there are no
// portable alternative functions, and VC++ 8.0's own libraries use the
@@ -20,6 +20,11 @@
#include <boost/test/minimal.hpp>
#include <boost/system/error_code.hpp>
+#include <iostream>
+#include <sstream>
+#include <string>
+#include <cstring>
+#include <boost/cerrno.hpp>
// Although using directives are not the best programming practice, testing
// with a boost::system using directive increases use scenario coverage.
@@ -27,86 +32,175 @@
# if defined( BOOST_WINDOWS_API )
# include "winerror.h"
-# include <boost/cerrno.hpp>
+# define BOOST_ACCESS_ERROR_MACRO ERROR_ACCESS_DENIED
+# elif defined( BOOST_POSIX_API )
+# define BOOST_ACCESS_ERROR_MACRO EACCES
+# else
+# error "Only supported for POSIX and Windows"
# endif
+namespace
+{
+ void check_ostream( error_code ec, const char * expected )
+ {
+ std::stringstream ss;
+ std::string s;
+
+ ss << ec;
+ ss >> s;
+ BOOST_CHECK( s == expected );
+ }
+}
+
// test_main ---------------------------------------------------------------//
-// TODO: supply a build jam file
-// TODO: supply a test jam file
-// TODO: same for bjam v2
-// TODO: add message decoder tests
+// TODO: add hash_value tests
int test_main( int, char ** )
{
- error_code ec;
- error_code ec_0_native( 0, native_ecat );
- error_code ec_0_errno( 0, errno_ecat );
- error_code ec_1_native( 1, native_ecat );
- error_code ec_1_errno( 1, errno_ecat );
+ std::cout << "General tests...\n";
+ // unit tests:
+
+ BOOST_CHECK( posix_category == posix_category );
+ BOOST_CHECK( system_category == system_category );
+ BOOST_CHECK( posix_category != system_category );
+ BOOST_CHECK( system_category != posix_category );
+ BOOST_CHECK( posix_category < system_category );
+ BOOST_CHECK( !(system_category < posix_category) );
+
+ error_code ec;
+ error_condition dec;
BOOST_CHECK( !ec );
BOOST_CHECK( ec.value() == 0 );
- BOOST_CHECK( ec.to_errno() == 0 );
-
- BOOST_CHECK( !ec_0_native );
- BOOST_CHECK( ec_0_native.value() == 0 );
- BOOST_CHECK( ec_0_native.to_errno() == 0 );
-
- BOOST_CHECK( !ec_0_errno );
- BOOST_CHECK( ec_0_errno.value() == 0 );
- BOOST_CHECK( ec_0_errno.to_errno() == 0 );
- BOOST_CHECK( ec == ec_0_errno );
- BOOST_CHECK( native_ecat != errno_ecat || ec_0_native == ec_0_errno );
- BOOST_CHECK( native_ecat == errno_ecat || ec_0_native != ec_0_errno );
-
- BOOST_CHECK( ec_1_native );
- BOOST_CHECK( ec_1_native.value() == 1 );
- BOOST_CHECK( ec_1_native.value() != 0 );
- BOOST_CHECK( ec_1_native.to_errno() != 0 );
- BOOST_CHECK( ec != ec_1_native );
- BOOST_CHECK( ec_0_native != ec_1_native );
- BOOST_CHECK( ec_0_errno != ec_1_native );
-
- BOOST_CHECK( ec_1_errno );
- BOOST_CHECK( ec_1_errno.value() == 1 );
- BOOST_CHECK( ec_1_errno.to_errno() == 1 );
- BOOST_CHECK( ec_1_errno.to_errno() != 0 );
- BOOST_CHECK( ec != ec_1_errno );
- BOOST_CHECK( ec_0_native != ec_1_errno );
- BOOST_CHECK( ec_0_errno != ec_1_errno );
+ dec = ec.default_error_condition();
+ BOOST_CHECK( dec.value() == 0 );
+ BOOST_CHECK( dec.category() == posix_category );
+ BOOST_CHECK( ec == posix::success );
+ BOOST_CHECK( ec.category() == system_category );
+ BOOST_CHECK( std::strcmp( ec.category().name(), "system") == 0 );
+ BOOST_CHECK( !(ec < error_code( 0, system_category )) );
+ BOOST_CHECK( !(error_code( 0, system_category ) < ec) );
+ BOOST_CHECK( ec < error_code( 1, system_category ) );
+ BOOST_CHECK( !(error_code( 1, system_category ) < ec) );
+
+ error_code ec_0_system( 0, system_category );
+ BOOST_CHECK( !ec_0_system );
+ BOOST_CHECK( ec_0_system.value() == 0 );
+ dec = ec_0_system.default_error_condition();
+ BOOST_CHECK( dec.value() == 0 );
+ BOOST_CHECK( dec.category() == posix_category );
+ BOOST_CHECK( ec_0_system == posix::success );
+ BOOST_CHECK( ec_0_system.category() == system_category );
+ BOOST_CHECK( std::strcmp( ec_0_system.category().name(), "system") == 0 );
+ check_ostream( ec_0_system, "system:0" );
+
+ BOOST_CHECK( ec_0_system == ec );
+
+ error_code ec_1_system( 1, system_category );
+ BOOST_CHECK( ec_1_system );
+ BOOST_CHECK( ec_1_system.value() == 1 );
+ BOOST_CHECK( ec_1_system.value() != 0 );
+ BOOST_CHECK( ec != ec_1_system );
+ BOOST_CHECK( ec_0_system != ec_1_system );
+ check_ostream( ec_1_system, "system:1" );
+
+ ec = error_code( BOOST_ACCESS_ERROR_MACRO, system_category );
+ BOOST_CHECK( ec );
+ BOOST_CHECK( ec.value() == BOOST_ACCESS_ERROR_MACRO );
+ dec = ec.default_error_condition();
+ BOOST_CHECK( dec.value() == static_cast<int>(posix::permission_denied) );
+ BOOST_CHECK( dec.category() == posix_category );
+ BOOST_CHECK( dec == error_condition( posix::permission_denied, posix_category ) );
+ BOOST_CHECK( dec == posix::permission_denied );
+ BOOST_CHECK( posix::permission_denied == dec );
+ BOOST_CHECK( ec == posix::permission_denied );
+ BOOST_CHECK( ec.category() == system_category );
+ BOOST_CHECK( std::strcmp( ec.category().name(), "system") == 0 );
+
+ // test the explicit make_error_code conversion for posix
+ ec = make_error_code( posix::bad_message );
+ BOOST_CHECK( ec );
+ BOOST_CHECK( ec == posix::bad_message );
+ BOOST_CHECK( posix::bad_message == ec );
+ BOOST_CHECK( ec != posix::permission_denied );
+ BOOST_CHECK( posix::permission_denied != ec );
+ BOOST_CHECK( ec.category() == posix_category );
+
+ // test the deprecated predefined error_category synonyms
+ BOOST_CHECK( &system_category == &native_ecat );
+ BOOST_CHECK( &posix_category == &errno_ecat );
+ BOOST_CHECK( system_category == native_ecat );
+ BOOST_CHECK( posix_category == errno_ecat );
#ifdef BOOST_WINDOWS_API
- BOOST_CHECK( ec != ec_0_native );
-
- // these tests probe the Windows to_errno decoder
+ std::cout << "Windows tests...\n";
+ // these tests probe the Windows posix decoder
// test the first entry in the decoder table:
- ec = error_code( ERROR_FILE_NOT_FOUND, native_ecat );
+ ec = error_code( ERROR_FILE_NOT_FOUND, system_category );
BOOST_CHECK( ec.value() == ERROR_FILE_NOT_FOUND );
- BOOST_CHECK( ec.to_errno() == ENOENT );
+ BOOST_CHECK( ec == posix::no_such_file_or_directory );
+ BOOST_CHECK( ec.default_error_condition().value() == posix::no_such_file_or_directory );
+ BOOST_CHECK( ec.default_error_condition().category() == posix_category );
// test the second entry in the decoder table:
- ec = error_code( ERROR_PATH_NOT_FOUND, native_ecat );
+ ec = error_code( ERROR_PATH_NOT_FOUND, system_category );
BOOST_CHECK( ec.value() == ERROR_PATH_NOT_FOUND );
- BOOST_CHECK( ec.to_errno() == ENOENT );
+ BOOST_CHECK( ec == posix::no_such_file_or_directory );
+ BOOST_CHECK( ec.default_error_condition().value() == posix::no_such_file_or_directory );
+ BOOST_CHECK( ec.default_error_condition().category() == posix_category );
// test the third entry in the decoder table:
- ec = error_code( ERROR_ACCESS_DENIED, native_ecat );
+ ec = error_code( ERROR_ACCESS_DENIED, system_category );
BOOST_CHECK( ec.value() == ERROR_ACCESS_DENIED );
- BOOST_CHECK( ec.to_errno() == EACCES );
+ BOOST_CHECK( ec == posix::permission_denied );
+ BOOST_CHECK( ec.default_error_condition().value() == posix::permission_denied );
+ BOOST_CHECK( ec.default_error_condition().category() == posix_category );
// test the last regular entry in the decoder table:
- ec = error_code( ERROR_WRITE_PROTECT, native_ecat );
+ ec = error_code( ERROR_WRITE_PROTECT, system_category );
BOOST_CHECK( ec.value() == ERROR_WRITE_PROTECT );
- BOOST_CHECK( ec.to_errno() == EROFS );
+ BOOST_CHECK( ec == posix::permission_denied );
+ BOOST_CHECK( ec.default_error_condition().value() == posix::permission_denied );
+ BOOST_CHECK( ec.default_error_condition().category() == posix_category );
// test not-in-table condition:
- ec = error_code( 1234567890, native_ecat );
+ ec = error_code( 1234567890, system_category );
BOOST_CHECK( ec.value() == 1234567890 );
- BOOST_CHECK( ec.to_errno() == EOTHER );
+ BOOST_CHECK( ec.default_error_condition().value() == 1234567890 );
+ BOOST_CHECK( ec.default_error_condition().category() == system_category );
+
+#else // POSIX
+
+ std::cout << "POSIX tests...\n";
+ ec = error_code( EACCES, system_category );
+ BOOST_CHECK( ec == error_code( posix::permission_denied, system_category ) );
+ BOOST_CHECK( error_code( posix::permission_denied, system_category ) == ec );
+ BOOST_CHECK( ec == posix::permission_denied );
+ BOOST_CHECK( posix::permission_denied == ec );
+ BOOST_CHECK( ec.default_error_condition().value() == posix::permission_denied );
+ BOOST_CHECK( ec.default_error_condition().category() == posix_category );
+
+# ifdef __CYGWIN__
+
+ std::cout << "Cygwin tests...\n";
+ ec = cygwin::no_package;
+ BOOST_CHECK( ec == cygwin::no_package );
+ BOOST_CHECK( ec == error_code( ENOPKG, system_category ) );
+ BOOST_CHECK( ec == error_code( cygwin::no_package, system_category ) );
+ BOOST_CHECK( ec.default_error_condition().category() == system_category );
+
+# elif defined(linux) || defined(__linux) || defined(__linux__)
+
+ std::cout << "Linux tests...\n";
+ ec = Linux::dot_dot_error;
+ BOOST_CHECK( ec == Linux::dot_dot_error );
+ BOOST_CHECK( ec == error_code( EDOTDOT, system_category ) );
+ BOOST_CHECK( ec == error_code( Linux::dot_dot_error, system_category ) );
+ BOOST_CHECK( ec.default_error_condition().category() == posix_category );
+
+# endif
-#else
- BOOST_CHECK( ec == ec_0_native );
#endif
return 0;
Modified: trunk/libs/system/test/error_code_user_test.cpp
==============================================================================
--- trunk/libs/system/test/error_code_user_test.cpp (original)
+++ trunk/libs/system/test/error_code_user_test.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -8,85 +8,384 @@
// See library home page at http://www.boost.org/libs/system
// ------------------------------------------------------------------------ //
-// This code demonstrates creation and use of a new category of error codes.
-// The motivation was a Boost posting by Christopher Kohlhoff on June 28, 2006.
+// This program demonstrates creation and use of new categories of error
+// codes. Several scenarios are demonstrated and tested.
+
+// Motivation was a Boost posting by Christopher Kohlhoff on June 28, 2006.
#include <boost/system/error_code.hpp>
#include <boost/cerrno.hpp>
+#include <string>
+#include <cstdio>
+#include <boost/test/minimal.hpp>
+
+#ifdef BOOST_POSIX_API
+# include <sys/stat.h>
+#else
+# include <windows.h>
+#endif
+
+// ------------------------------------------------------------------------ //
+
+// Library 1: User function passes through an error code from the
+// operating system.
+
+
+boost::system::error_code my_mkdir( const std::string & path )
+{
+ return boost::system::error_code(
+# ifdef BOOST_POSIX_API
+ ::mkdir( path.c_str(), S_IRWXU|S_IRWXG|S_IROTH|S_IXOTH ) == 0 ? 0 : errno,
+# else
+ ::CreateDirectoryA( path.c_str(), 0 ) != 0 ? 0 : ::GetLastError(),
+# endif
+ boost::system::system_category );
+}
// ------------------------------------------------------------------------ //
-// header asio.hpp
+// Library 2: User function passes through errno from the C-runtime.
+
+#include <cstdio>
+
+boost::system::error_code my_remove( const std::string & path )
+{
+ return boost::system::error_code(
+ ::remove( path.c_str() ) == 0 ? 0 : errno,
+ boost::system::posix_category ); // OK for both Windows and POSIX
+ // Alternatively, could use posix_category
+ // on Windows and system_category on
+ // POSIX-based systems.
+}
+
+// ------------------------------------------------------------------------ //
+
+// Library 3: Library uses enum to identify library specific errors.
+
+// This particular example is for a library within the parent namespace. For
+// an example of a library not within the parent namespace, see library 4.
+
+// header lib3.hpp:
-#define BOO_BOO 12345 // this could also be a constant; a macro is used for
- // illustration because many older API's define errors
- // via macro.
namespace boost
{
- namespace asio
+ namespace lib3
{
- // asio declares have its own error_category:
- extern system::error_category asio_error;
+ // lib3 has its own error_category:
+ extern const boost::system::error_category & lib3_error_category;
- namespace error
+ enum error
{
- extern boost::system::error_code boo_boo;
- }
+ boo_boo=123,
+ big_boo_boo
+ };
- void boo_boo( boost::system::error_code & ec );
}
+
+ namespace system
+ {
+ template<> struct is_error_code_enum<boost::lib3::error>
+ { static const bool value = true; };
+
+ inline error_code make_error_code(boost::lib3::error e)
+ { return error_code(e,boost::lib3::lib3_error_category); }
+ }
+
}
-// ------------------------------------------------------------------------ //
+// implementation file lib3.cpp:
-// implementation file asio.cpp:
+// #include <lib3.hpp>
namespace boost
{
- namespace asio
+ namespace lib3
{
+ class lib3_error_category_imp : public boost::system::error_category
+ {
+ public:
+ const char * name() const
+ {
+ return "lib3";
+ }
+
+ boost::system::error_condition default_error_condition( int ev ) const
+ {
+ return ev == boo_boo
+ ? boost::system::error_condition( boost::system::posix::io_error,
+ boost::system::posix_category )
+ : boost::system::error_condition( ev,
+ boost::lib3::lib3_error_category );
+ }
+
+ std::string message( int ev ) const
+ {
+ if ( ev == boo_boo ) return std::string("boo boo");
+ if ( ev == big_boo_boo ) return std::string("big boo boo");
+ return std::string("unknown error");
+ }
+ };
+
+ const lib3_error_category_imp lib3_error_category_const;
+
+ const boost::system::error_category & lib3_error_category
+ = lib3_error_category_const;
+ }
+}
+
+// ------------------------------------------------------------------------ //
+
+// Library 4: Library uses const error_code's to identify library specific
+// errors.
+
+// This particular example is for a library not within the parent namespace.
+// For an example of a library within the parent namespace, see library 3.
+
+// header lib4.hpp:
- system::error_category asio_error = system::error_code::new_category();
+namespace lib4
+{
+ // lib4 has its own error_category:
+ extern const boost::system::error_category & lib4_error_category;
+
+ extern const boost::system::error_code boo_boo;
+ extern const boost::system::error_code big_boo_boo;
+}
+
+// implementation file lib4.cpp:
- namespace error
+// #include <lib4.hpp>
+
+namespace lib4
+{
+ class lib4_error_category_imp : public boost::system::error_category
+ {
+ public:
+ const char * name() const
{
- boost::system::error_code boo_boo( BOO_BOO, asio_error );
+ return "lib4";
}
- // function sets ec arg to boo_boo
- void boo_boo( boost::system::error_code & ec )
+ boost::system::error_condition default_error_condition( int ev ) const
{
- ec = error::boo_boo;
+ return ev == boo_boo.value()
+ ? boost::system::error_condition( boost::system::posix::io_error,
+ boost::system::posix_category )
+ : boost::system::error_condition( ev, lib4::lib4_error_category );
}
- }
+
+ std::string message( int ev ) const
+ {
+ if ( ev == boo_boo.value() ) return std::string("boo boo");
+ if ( ev == big_boo_boo.value() ) return std::string("big boo boo");
+ return std::string("unknown error");
+ }
+ };
+
+ const lib4_error_category_imp lib4_error_category_const;
+
+ const boost::system::error_category & lib4_error_category
+ = lib4_error_category_const;
+
+ const boost::system::error_code boo_boo( 456, lib4_error_category );
+ const boost::system::error_code big_boo_boo( 789, lib4_error_category );
+
}
// ------------------------------------------------------------------------ //
-// a user program:
+// Chris Kolhoff's Test3, modified to work with error_code.hpp
+// Test3
+// =====
+// Define error classes to check for success, permission_denied and
+// out_of_memory, but add additional mappings for a user-defined error category.
+//
+
+//namespace test3 {
+
+// enum user_err
+// {
+// user_success = 0,
+// user_permission_denied,
+// user_out_of_memory
+// };
+//
+// class user_error_category_imp : public boost::system::error_category
+// {
+// public:
+// const std::string & name() const
+// {
+// static std::string s( "test3" );
+// return s;
+// }
+//
+// boost::system::error_code portable_error_code( int ev ) const
+// {
+// switch (ev)
+// {
+// case user_success:
+// return boost::system::error_code(boost::system::posix::success, boost::system::posix_category);
+// case user_permission_denied:
+// return boost::system::error_code(boost::system::posix::permission_denied, boost::system::posix_category);
+// case user_out_of_memory:
+// return boost::system::error_code(boost::system::posix::not_enough_memory, boost::system::posix_category);
+// default:
+// break;
+// }
+// return boost::system::error_code(boost::system::posix::no_posix_equivalent, boost::system::posix_category);
+// }
+//
+// };
+//
+// const user_error_category_imp user_error_category_const;
+//
+// const boost::system::error_category & user_error_category
+// = user_error_category_const;
+//
+// inline boost::system::error_code make_error_code(user_err e)
+// {
+// return boost::system::error_code(e, user_error_category);
+// }
+//
+// // test code
+//
+// void check_success(const boost::system::error_code& ec, bool expect)
+// {
+// BOOST_CHECK( (ec == boost::system::posix::success) == expect );
+// if (ec == boost::system::posix::success)
+// std::cout << "yes... " << (expect ? "ok" : "fail") << '\n';
+// else
+// std::cout << "no... " << (expect ? "fail" : "ok") << '\n';
+// }
+//
+// void check_permission_denied(const boost::system::error_code& ec, bool expect)
+// {
+// BOOST_CHECK( (ec == boost::system::posix::permission_denied) == expect );
+// if (ec == boost::system::posix::permission_denied)
+// std::cout << "yes... " << (expect ? "ok" : "fail") << '\n';
+// else
+// std::cout << "no... " << (expect ? "fail" : "ok") << '\n';
+// }
+//
+// void check_out_of_memory(const boost::system::error_code& ec, bool expect)
+// {
+// BOOST_CHECK( (ec == boost::system::posix::not_enough_memory) == expect );
+// if (ec == boost::system::posix::not_enough_memory)
+// std::cout << "yes... " << (expect ? "ok" : "fail") << '\n';
+// else
+// std::cout << "no... " << (expect ? "fail" : "ok") << '\n';
+// }
+//
+// void run()
+// {
+// printf("Test3\n");
+// printf("=====\n");
+// boost::system::error_code ec;
+// check_success(ec, true);
+// check_success(boost::system::posix::success, true);
+// check_success(boost::system::posix::permission_denied, false);
+// check_success(boost::system::posix::not_enough_memory, false);
+// check_success(user_success, true);
+// check_success(user_permission_denied, false);
+// check_success(user_out_of_memory, false);
+// check_permission_denied(ec, false);
+// check_permission_denied(boost::system::posix::success, false);
+// check_permission_denied(boost::system::posix::permission_denied, true);
+// check_permission_denied(boost::system::posix::not_enough_memory, false);
+// check_permission_denied(user_success, false);
+// check_permission_denied(user_permission_denied, true);
+// check_permission_denied(user_out_of_memory, false);
+// check_out_of_memory(ec, false);
+// check_out_of_memory(boost::system::posix::success, false);
+// check_out_of_memory(boost::system::posix::permission_denied, false);
+// check_out_of_memory(boost::system::posix::not_enough_memory, true);
+// check_out_of_memory(user_success, false);
+// check_out_of_memory(user_permission_denied, false);
+// check_out_of_memory(user_out_of_memory, true);
+//
+//# ifdef BOOST_WINDOWS_API
+// check_success(boost::system::windows::success, true);
+// check_success(boost::system::windows::access_denied, false);
+// check_success(boost::system::windows::not_enough_memory, false);
+// check_permission_denied(boost::system::windows::success, false);
+// check_permission_denied(boost::system::windows::access_denied, true);
+// check_permission_denied(boost::system::windows::not_enough_memory, false);
+// check_out_of_memory(boost::system::windows::success, false);
+// check_out_of_memory(boost::system::windows::access_denied, false);
+// check_out_of_memory(boost::system::windows::not_enough_memory, true);
+//# endif
+//
+// printf("\n");
+// }
+//
+//} // namespace test3
-// #include <asio.hpp>
-#include <boost/test/minimal.hpp>
+
+
+// ------------------------------------------------------------------------ //
int test_main( int, char *[] )
{
boost::system::error_code ec;
- boost::asio::boo_boo( ec );
+
+ // Library 1 tests:
+
+ ec = my_mkdir( "/no-such-file-or-directory/will-not-succeed" );
+ std::cout << "ec.value() is " << ec.value() << '\n';
+
+ BOOST_CHECK( ec );
+ BOOST_CHECK( ec == boost::system::posix::no_such_file_or_directory );
+ BOOST_CHECK( ec.category() == boost::system::system_category );
+
+ // Library 2 tests:
+
+ ec = my_remove( "/no-such-file-or-directory" );
+ std::cout << "ec.value() is " << ec.value() << '\n';
+
+ BOOST_CHECK( ec );
+ BOOST_CHECK( ec == boost::system::posix::no_such_file_or_directory );
+ BOOST_CHECK( ec.category() == boost::system::posix_category );
+
+ // Library 3 tests:
+
+ ec = boost::lib3::boo_boo;
+ std::cout << "ec.value() is " << ec.value() << '\n';
BOOST_CHECK( ec );
- BOOST_CHECK( ec == boost::asio::error::boo_boo );
- BOOST_CHECK( ec.value() == BOO_BOO );
- BOOST_CHECK( ec.category() == boost::asio::asio_error );
-
- // a real user can't rely on the value of an error_category object's value,
- // but in this test program that value is known, so test for it.
- BOOST_CHECK( ec.category().value() == boost::system::native_ecat.value()+1 );
-
- // asio did not supply decoders, so test the defaults
- BOOST_CHECK( ec.to_errno() == EOTHER );
- BOOST_CHECK( ec.message() == "API error" );
- BOOST_CHECK( ec.wmessage() == L"API error" );
+ BOOST_CHECK( ec == boost::lib3::boo_boo );
+ BOOST_CHECK( ec.value() == boost::lib3::boo_boo );
+ BOOST_CHECK( ec.category() == boost::lib3::lib3_error_category );
+
+ BOOST_CHECK( ec == boost::system::posix::io_error );
+
+ boost::system::error_code ec3( boost::lib3::boo_boo+100,
+ boost::lib3::lib3_error_category );
+ BOOST_CHECK( ec3.category() == boost::lib3::lib3_error_category );
+ BOOST_CHECK( ec3.default_error_condition().category()
+ == boost::lib3::lib3_error_category );
+
+ // Library 4 tests:
+
+ ec = lib4::boo_boo;
+ std::cout << "ec.value() is " << ec.value() << '\n';
+
+ BOOST_CHECK( ec );
+ BOOST_CHECK( ec == lib4::boo_boo );
+ BOOST_CHECK( ec.value() == lib4::boo_boo.value() );
+ BOOST_CHECK( ec.category() == lib4::lib4_error_category );
+
+ BOOST_CHECK( ec == boost::system::posix::io_error );
+
+ boost::system::error_code ec4( lib4::boo_boo.value()+100,
+ lib4::lib4_error_category );
+ BOOST_CHECK( ec4.default_error_condition().category()
+ == lib4::lib4_error_category );
+
+ // Test 3
+
+ //test3::run();
+
return 0;
}
Modified: trunk/libs/system/test/system_error_test.cpp
==============================================================================
--- trunk/libs/system/test/system_error_test.cpp (original)
+++ trunk/libs/system/test/system_error_test.cpp 2007-09-09 10:59:10 EDT (Sun, 09 Sep 2007)
@@ -10,7 +10,7 @@
//----------------------------------------------------------------------------//
// VC++ 8.0 warns on usage of certain Standard Library and API functions that
-// can be cause buffer overruns or other possible security issues if misused.
+// can cause buffer overruns or other possible security issues if misused.
// See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx
// But the wording of the warning is misleading and unsettling, there are no
// portable alternative functions, and VC++ 8.0's own libraries use the
@@ -23,24 +23,23 @@
#include <iostream>
#ifdef BOOST_WINDOWS_API
-# include <winerror.h>
+#include <winerror.h>
#endif
using boost::system::system_error;
using boost::system::error_code;
-using boost::system::errno_ecat;
-using boost::system::no_message;
+using boost::system::system_category;
#define TEST(x,v,w) test(#x,x,v,w)
namespace
{
void test( const char * desc, const system_error & ex,
- error_code::value_type v, const char * str )
+ int v, const char * str )
{
std::cout << "test " << desc << "\n what() returns \"" << ex.what() << "\"\n";
BOOST_CHECK( ex.code().value() == v );
- BOOST_CHECK( ex.code().category() == errno_ecat );
+ BOOST_CHECK( ex.code().category() == system_category );
# ifdef BOOST_WINDOWS_API
BOOST_CHECK( std::string( ex.what() ) == str );
if ( std::string( ex.what() ) != str )
@@ -56,29 +55,22 @@
{
// all combinations of constructors:
- system_error se_0( error_code(0, errno_ecat) );
- system_error se_1( 1, errno_ecat );
- system_error se_0_m( error_code(0, errno_ecat), "se_0_m" );
- system_error se_1_m( 1, errno_ecat, "se_1_m" );
- system_error se_0_nm( error_code(0, errno_ecat), "" );
- system_error se_1_nm( 1, errno_ecat, "" );
- system_error se_0_m_im( error_code(0, errno_ecat), "se_0_m_im", no_message );
- system_error se_1_m_im( 1, errno_ecat, "se_1_m_im", no_message );
- system_error se_0_nm_im( error_code(0, errno_ecat), "", no_message );
- system_error se_1_nm_im( 1, errno_ecat, "", no_message );
- system_error se_1u_m( uvalue, errno_ecat, "se_1u_m" );
+ system_error se_0_m( error_code(0, system_category), "se_0_m" );
+ system_error se_1_m( 1, system_category, "se_1_m" );
+ system_error se_0_nm( error_code(0, system_category), "" );
+ system_error se_1_nm( 1, system_category, "" );
+ system_error se_0_nmx( error_code(0, system_category), "" );
+ system_error se_1_nmx( 1, system_category, "" );
+ system_error se_1u_m( uvalue, system_category, "se_1u_m" );
- TEST( se_0, 0, "" );
- TEST( se_1, 1, "Operation not permitted" );
TEST( se_0_m, 0, "se_0_m" );
- TEST( se_1_m, 1, "se_1_m: Operation not permitted" );
+ TEST( se_1_m, 1, "se_1_m: Incorrect function" );
TEST( se_0_nm, 0, "" );
- TEST( se_1_nm, 1, "Operation not permitted" );
- TEST( se_0_m_im, 0, "se_0_m_im" );
- TEST( se_1_m_im, 1, "se_1_m_im" );
- TEST( se_0_nm_im, 0, "" );
- TEST( se_1_nm_im, 1, "" );
- TEST( se_1u_m, 1, "se_1u_m: Operation not permitted" );
+ TEST( se_1_nm, 1, "Incorrect function" );
+ TEST( se_0_nmx, 0, "" );
+ TEST( se_1_nmx, 1, "Incorrect function" );
+ TEST( se_1u_m, 1, "se_1u_m: Incorrect function" );
+
return 0;
}
Boost-Commit list run by bdawes at acm.org, david.abrahams at rcn.com, gregod at cs.rpi.edu, cpdaniel at pacbell.net, john at johnmaddock.co.uk