std_utils.hh - Add dir_get_cwd() / dir_set_cwd()

LIB-18
heck 2021-10-08 20:34:18 +02:00
parent 901a9fa9a7
commit e449d65a4f
2 changed files with 25 additions and 8 deletions

View File

@ -52,7 +52,7 @@ namespace pEp {
bool path_exists(const string &filename)
{
ifstream ifile(filename.c_str());
return (bool)ifile;
return static_cast<bool>(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;
}

View File

@ -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);