diff options
author | Boris Kolpackov <boris@codesynthesis.com> | 2010-10-29 17:23:10 +0200 |
---|---|---|
committer | Boris Kolpackov <boris@codesynthesis.com> | 2010-10-29 17:23:10 +0200 |
commit | 7757844f03166a16310bdfa98c3d0b86e93e95e0 (patch) | |
tree | bab2b51f716416c79c24d83fa28654dd5ad1e46f /cutl/fs | |
parent | 547a401ac73d21b769939a3244f991112dd9d2f7 (diff) |
Implement support for changing current path
Diffstat (limited to 'cutl/fs')
-rw-r--r-- | cutl/fs/path.cxx | 57 | ||||
-rw-r--r-- | cutl/fs/path.hxx | 3 |
2 files changed, 57 insertions, 3 deletions
diff --git a/cutl/fs/path.cxx b/cutl/fs/path.cxx index 5aaf2a9..a593926 100644 --- a/cutl/fs/path.cxx +++ b/cutl/fs/path.cxx @@ -4,11 +4,11 @@ // license : MIT; see accompanying LICENSE file #ifdef _WIN32 -# include <direct.h> // _getcwd, _wgetcwd, _MAX_PATH +# include <direct.h> // _[w]getcwd, _[w]chdir, _MAX_PATH #else -# include <stdlib.h> // mbstowcs +# include <stdlib.h> // mbstowcs, wcstombs # include <limits.h> // PATH_MAX -# include <unistd.h> // getcwd +# include <unistd.h> // getcwd, chdir #endif #include <cutl/fs/path.hxx> @@ -23,6 +23,10 @@ namespace cutl return "invalid filesystem path"; } + // + // char + // + template <> basic_path<char> basic_path<char>:: current () @@ -41,6 +45,28 @@ namespace cutl } template <> + void basic_path<char>:: + current (basic_path const& p) + { + string_type const& s (p.string ()); + + if (p.empty ()) + throw invalid_basic_path<char> (s); + +#ifdef _WIN32 + if(_chdir(s.c_str ()) != 0) + throw invalid_basic_path<char> (s); +#else + if (chdir (s.c_str ()) != 0) + throw invalid_basic_path<char> (s); +#endif + } + + // + // wchar_t + // + + template <> basic_path<wchar_t> basic_path<wchar_t>:: current () { @@ -60,5 +86,30 @@ namespace cutl return basic_path<wchar_t> (wcwd); } + + template <> + void basic_path<wchar_t>:: + current (basic_path const& p) + { + string_type const& s (p.string ()); + + if (p.empty ()) + throw invalid_basic_path<wchar_t> (s); + +#ifdef _WIN32 + if(_wchdir(s.c_str ()) != 0) + throw invalid_basic_path<wchar_t> (s); +#else + char ns[PATH_MAX + 1]; + + if (wcstombs (ns, s.c_str (), PATH_MAX) == size_type (-1)) + throw invalid_basic_path<wchar_t> (s); + + ns[PATH_MAX] = '\0'; + + if (chdir (ns) != 0) + throw invalid_basic_path<wchar_t> (s); +#endif + } } } diff --git a/cutl/fs/path.hxx b/cutl/fs/path.hxx index 9bef17f..050830d 100644 --- a/cutl/fs/path.hxx +++ b/cutl/fs/path.hxx @@ -164,6 +164,9 @@ namespace cutl static basic_path current (); + static void + current (basic_path const&); + public: bool empty () const |