From 7757844f03166a16310bdfa98c3d0b86e93e95e0 Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Fri, 29 Oct 2010 17:23:10 +0200 Subject: Implement support for changing current path --- cutl/fs/path.cxx | 57 +++++++++++++++++++++++++++++++++++++++++++++++++++++--- 1 file changed, 54 insertions(+), 3 deletions(-) (limited to 'cutl/fs/path.cxx') 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 // _getcwd, _wgetcwd, _MAX_PATH +# include // _[w]getcwd, _[w]chdir, _MAX_PATH #else -# include // mbstowcs +# include // mbstowcs, wcstombs # include // PATH_MAX -# include // getcwd +# include // getcwd, chdir #endif #include @@ -23,6 +23,10 @@ namespace cutl return "invalid filesystem path"; } + // + // char + // + template <> basic_path basic_path:: current () @@ -41,6 +45,28 @@ namespace cutl } template <> + void basic_path:: + current (basic_path const& p) + { + 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); +#else + if (chdir (s.c_str ()) != 0) + throw invalid_basic_path (s); +#endif + } + + // + // wchar_t + // + + template <> basic_path basic_path:: current () { @@ -60,5 +86,30 @@ namespace cutl return basic_path (wcwd); } + + template <> + void basic_path:: + current (basic_path const& p) + { + 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); +#else + char ns[PATH_MAX + 1]; + + if (wcstombs (ns, s.c_str (), PATH_MAX) == size_type (-1)) + throw invalid_basic_path (s); + + ns[PATH_MAX] = '\0'; + + if (chdir (ns) != 0) + throw invalid_basic_path (s); +#endif + } } } -- cgit v1.1