| 92 | |
| 93 | |
| 94 | std::string get_this_executable_path(std::string* filename, |
| 95 | std::string* directory) { |
| 96 | auto get_path = []() -> std::string{ |
| 97 | #ifndef _WIN32 |
| 98 | char result[PATH_MAX]; |
| 99 | ssize_t num_read = readlink("/proc/self/exe", result, PATH_MAX); |
| 100 | if (num_read >= 0 && num_read <= PATH_MAX - 1) { |
| 101 | result[num_read] = '\0'; |
| 102 | return std::string{result}; |
| 103 | } |
| 104 | return std::string{}; |
| 105 | #else |
| 106 | std::vector<char> path_buff; |
| 107 | DWORD copied = 0; |
| 108 | do { |
| 109 | path_buff.resize(path_buff.size() + MAX_PATH); |
| 110 | copied = GetModuleFileNameA(0, path_buff.data(), path_buff.size()); |
| 111 | } while (copied >= path_buff.size()); |
| 112 | |
| 113 | path_buff.resize(copied); |
| 114 | |
| 115 | return std::string{path_buff.begin(), path_buff.end()}; |
| 116 | #endif |
| 117 | }; |
| 118 | |
| 119 | std::string path = get_path(); |
| 120 | if(filename) |
| 121 | *filename = path.empty() ? "" : fs::path{path}.filename().string(); |
| 122 | if(directory) |
| 123 | *directory = path.empty() ? "" : fs::path{path}.parent_path().string(); |
| 124 | return path; |
| 125 | } |
| 126 | |
| 127 | std::string join_path(const std::string& base, const std::string& extra) { |
| 128 | return (fs::path(base) / extra).string(); |
no test coverage detected