From ace1743f7f78bb13f99553d6e97ad1beecf1ba99 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Mon, 13 Apr 2015 15:50:17 +0200 Subject: Add separate type to represent directory paths --- build/path.cxx | 51 +++++++++++++++++++++++---------------------------- 1 file changed, 23 insertions(+), 28 deletions(-) (limited to 'build/path.cxx') diff --git a/build/path.cxx b/build/path.cxx index efd0532..899dc8f 100644 --- a/build/path.cxx +++ b/build/path.cxx @@ -8,11 +8,16 @@ # include // _MAX_PATH # include // _[w]getcwd, _[w]chdir #else +# include // EINVAL # include // mbstowcs, wcstombs # include // PATH_MAX # include // getcwd, chdir #endif +#include + +using namespace std; + namespace build { char const* invalid_path_base:: @@ -26,7 +31,7 @@ namespace build // template <> - basic_path basic_path:: + path_traits::string_type path_traits:: current () { // @@ throw system_error (and in the other current() versions). @@ -34,31 +39,26 @@ namespace build #ifdef _WIN32 char cwd[_MAX_PATH]; if(_getcwd(cwd, _MAX_PATH) == 0) - throw invalid_basic_path ("."); + throw system_error (errno, system_category ()); #else char cwd[PATH_MAX]; if (getcwd (cwd, PATH_MAX) == 0) - throw invalid_basic_path ("."); + throw system_error (errno, system_category ()); #endif - return basic_path (cwd); + return string_type (cwd); } template <> - void basic_path:: - current (basic_path const& p) + void path_traits:: + current (string_type const& s) { - string_type const& s (p.string ()); - - if (p.empty ()) - throw invalid_basic_path (s); - #ifdef _WIN32 if(_chdir(s.c_str ()) != 0) - throw invalid_basic_path (s); + throw system_error (errno, system_category ()); #else if (chdir (s.c_str ()) != 0) - throw invalid_basic_path (s); + throw system_error (errno, system_category ()); #endif } @@ -67,48 +67,43 @@ namespace build // template <> - basic_path basic_path:: + path_traits::string_type path_traits:: current () { #ifdef _WIN32 wchar_t wcwd[_MAX_PATH]; if(_wgetcwd(wcwd, _MAX_PATH) == 0) - throw invalid_basic_path (L"."); + throw system_error (errno, system_category ()); #else char cwd[PATH_MAX]; if (getcwd (cwd, PATH_MAX) == 0) - throw invalid_basic_path (L"."); + throw system_error (errno, system_category ()); wchar_t wcwd[PATH_MAX]; if (mbstowcs (wcwd, cwd, PATH_MAX) == size_type (-1)) - throw invalid_basic_path (L"."); + throw system_error (EINVAL, system_category ()); #endif - return basic_path (wcwd); + return string_type (wcwd); } template <> - void basic_path:: - current (basic_path const& p) + void path_traits:: + current (string_type const& s) { - string_type const& s (p.string ()); - - if (p.empty ()) - throw invalid_basic_path (s); - #ifdef _WIN32 if(_wchdir(s.c_str ()) != 0) - throw invalid_basic_path (s); + throw system_error (errno, system_category ()); #else char ns[PATH_MAX + 1]; if (wcstombs (ns, s.c_str (), PATH_MAX) == size_type (-1)) - throw invalid_basic_path (s); + throw system_error (EINVAL, system_category ()); ns[PATH_MAX] = '\0'; if (chdir (ns) != 0) - throw invalid_basic_path (s); + throw system_error (errno, system_category ()); #endif } } -- cgit v1.1