// file : cutl/fs/path.cxx // copyright : Copyright (c) 2009-2013 Code Synthesis Tools CC // license : MIT; see accompanying LICENSE file #ifdef _WIN32 # include // _MAX_PATH # include // _[w]getcwd, _[w]chdir #else # include // mbstowcs, wcstombs # include // PATH_MAX # include // getcwd, chdir #endif #include namespace cutl { namespace fs { char const* invalid_path_base:: what () const throw () { return "invalid filesystem path"; } // // char // template <> basic_path basic_path:: current () { #ifdef _WIN32 char cwd[_MAX_PATH]; if(_getcwd(cwd, _MAX_PATH) == 0) throw invalid_basic_path ("."); #else char cwd[PATH_MAX]; if (getcwd (cwd, PATH_MAX) == 0) throw invalid_basic_path ("."); #endif return basic_path (cwd); } 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 () { #ifdef _WIN32 wchar_t wcwd[_MAX_PATH]; if(_wgetcwd(wcwd, _MAX_PATH) == 0) throw invalid_basic_path (L"."); #else char cwd[PATH_MAX]; if (getcwd (cwd, PATH_MAX) == 0) throw invalid_basic_path (L"."); wchar_t wcwd[PATH_MAX]; if (mbstowcs (wcwd, cwd, PATH_MAX) == size_type (-1)) throw invalid_basic_path (L"."); #endif 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 } } }