diff options
author | Karen Arutyunov <karen@codesynthesis.com> | 2020-12-16 20:29:05 +0300 |
---|---|---|
committer | Karen Arutyunov <karen@codesynthesis.com> | 2021-02-24 16:40:04 +0300 |
commit | 8e761289a2446367267c6c0d9a26e734f0f78306 (patch) | |
tree | fb495d8c18801f271d124ee48731f10df396ca89 /libcutl/fs/path.cxx | |
parent | 4c8104756b92b9fa16b3a725e8a6aa620dfd606e (diff) |
Get rid of legacy build systems and rename cutl/ to libcutl/
Diffstat (limited to 'libcutl/fs/path.cxx')
-rw-r--r-- | libcutl/fs/path.cxx | 120 |
1 files changed, 120 insertions, 0 deletions
diff --git a/libcutl/fs/path.cxx b/libcutl/fs/path.cxx new file mode 100644 index 0000000..c163d8b --- /dev/null +++ b/libcutl/fs/path.cxx @@ -0,0 +1,120 @@ +// file : libcutl/fs/path.cxx +// license : MIT; see accompanying LICENSE file + +#ifdef _WIN32 +# include <stdlib.h> // _MAX_PATH +# include <direct.h> // _[w]getcwd, _[w]chdir +#else +# include <stdlib.h> // mbstowcs, wcstombs +# include <limits.h> // PATH_MAX +# include <unistd.h> // getcwd, chdir +#endif + +#ifndef _WIN32 +# ifndef PATH_MAX +# define PATH_MAX 4096 +# endif +#endif + +#include <libcutl/fs/path.hxx> + +namespace cutl +{ + namespace fs + { + char const* invalid_path_base:: + what () const noexcept + { + return "invalid filesystem path"; + } + + // + // char + // + + template <> + LIBCUTL_EXPORT basic_path<char> basic_path<char>:: + current () + { +#ifdef _WIN32 + char cwd[_MAX_PATH]; + if(_getcwd(cwd, _MAX_PATH) == 0) + throw invalid_basic_path<char> ("."); +#else + char cwd[PATH_MAX]; + if (getcwd (cwd, PATH_MAX) == 0) + throw invalid_basic_path<char> ("."); +#endif + + return basic_path<char> (cwd); + } + + template <> + LIBCUTL_EXPORT 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 <> + LIBCUTL_EXPORT basic_path<wchar_t> basic_path<wchar_t>:: + current () + { +#ifdef _WIN32 + wchar_t wcwd[_MAX_PATH]; + if(_wgetcwd(wcwd, _MAX_PATH) == 0) + throw invalid_basic_path<wchar_t> (L"."); +#else + char cwd[PATH_MAX]; + if (getcwd (cwd, PATH_MAX) == 0) + throw invalid_basic_path<wchar_t> (L"."); + + wchar_t wcwd[PATH_MAX]; + if (mbstowcs (wcwd, cwd, PATH_MAX) == size_type (-1)) + throw invalid_basic_path<wchar_t> (L"."); +#endif + + return basic_path<wchar_t> (wcwd); + } + + template <> + LIBCUTL_EXPORT 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 + } + } +} |