diff --git a/src/std_utils.cc b/src/std_utils.cc index 9ba6cac..4c03ae4 100644 --- a/src/std_utils.cc +++ b/src/std_utils.cc @@ -52,7 +52,7 @@ namespace pEp { bool path_exists(const string &filename) { ifstream ifile(filename.c_str()); - return (bool)ifile; + return static_cast(ifile); } void path_delete(const string &filename) @@ -65,6 +65,27 @@ namespace pEp { } #ifndef WIN32 + std::string dir_get_cwd() + { + std::string ret; + char cwd[2048]; + char const *res = getcwd(cwd, sizeof(cwd)); + if (res == nullptr) { + throw std::runtime_error( + "failed to get current working directory: " + std::string(strerror(errno))); + } + ret = std::string(res); + return ret; + } + + void dir_set_cwd(const std::string &dir) + { + int err = chdir(dir.c_str()); + if (err != 0) { + throw std::runtime_error("dir_set_cwd(" + dir + ")" + std::string(strerror(errno))); + } + } + bool path_is_dir(const string &path) { bool ret = false; @@ -128,13 +149,7 @@ namespace pEp { { std::string ret{ path }; if (path[0] != '/') { - char cwd[2048]; - char const *res = getcwd(cwd, sizeof(cwd)); - if (res == nullptr) { - throw std::runtime_error( - "failed to get current working directory: " + std::string(strerror(errno))); - } - ret = std::string(cwd) + "/" + path; + ret = dir_get_cwd() + "/" + path; } return ret; } diff --git a/src/std_utils.hh b/src/std_utils.hh index 84709be..63f83a8 100644 --- a/src/std_utils.hh +++ b/src/std_utils.hh @@ -31,6 +31,8 @@ namespace pEp { void path_delete(const std::string &filename); #ifndef WIN32 + std::string dir_get_cwd(); + void dir_set_cwd(const std::string& dir); bool path_is_dir(const std::string &path); void path_delete_all(const std::string &path); std::string path_dirname(std::string const& f);