From 369a1031e12193abc91e110aca352b4ea7d9ad70 Mon Sep 17 00:00:00 2001 From: blahpy Date: Thu, 10 Sep 2020 22:07:23 +1200 Subject: [PATCH] Update functions --- common/util/FileUtil.cpp | 27 ++++++++++++++++++--------- common/util/FileUtil.h | 4 ++-- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/common/util/FileUtil.cpp b/common/util/FileUtil.cpp index 876f927b3a..726407177d 100644 --- a/common/util/FileUtil.cpp +++ b/common/util/FileUtil.cpp @@ -3,22 +3,31 @@ #include /* defines FILENAME_MAX */ #ifdef _WIN32 -#include -#define GetCurrentDir _getcwd +#include #else #include -#define GetCurrentDir getcwd #endif -std::string FileUtil::GetCurrentWorkingDir() { - char buff[FILENAME_MAX]; - GetCurrentDir(buff, FILENAME_MAX); - std::string current_working_dir(buff); - return current_working_dir; +std::string FileUtil::GetExecutablePath() { +#ifdef _WIN32 + char buffer[FILENAME_MAX]; + GetModuleFileNameA(NULL, buffer, FILENAME_MAX); + std::string::size_type pos = + std::string(buffer).find_last_of("/\\"); // Strip executable from file path + return std::string(buffer).substr(0, pos); +#else // do Linux stuff + char buffer[FILENAME_MAX]; + readlink("/proc/self/exe", buffer, + FILENAME_MAX); // /proc/self acts like a "virtual folder" containing information about + // the current process + std::string::size_type pos = + std::string(buffer).find_last_of("/\\"); // Strip executable from file path + return std::string(buffer).substr(0, pos); +#endif } std::string FileUtil::get_file_path(const std::vector& input) { - std::string currentPath = FileUtil::GetCurrentWorkingDir(); // std::filesystem::current_path(); + std::string currentPath = FileUtil::GetExecutablePath(); char dirSeparator; #ifdef _WIN32 diff --git a/common/util/FileUtil.h b/common/util/FileUtil.h index 7b0517503b..97e4ea97e5 100644 --- a/common/util/FileUtil.h +++ b/common/util/FileUtil.h @@ -3,6 +3,6 @@ #include namespace FileUtil { -std::string GetCurrentWorkingDir(); -std::string get_file_path(std::vector input); +std::string GetExecutablePath(); +std::string get_file_path(const std::vector& input); } // namespace FileUtil