From c2803fe1e27803224b5d7992e615b99c17935c73 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Tue, 22 Mar 2022 09:34:26 +0100 Subject: [PATCH] sys: Fixed build errors and warnings on Unix --- lib/libimhex/include/hex/helpers/fs.hpp | 3 ++- .../include/hex/helpers/intrinsics.hpp | 2 +- lib/libimhex/source/helpers/fs.cpp | 26 ++++++++++++------- 3 files changed, 19 insertions(+), 12 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/fs.hpp b/lib/libimhex/include/hex/helpers/fs.hpp index d4f5ad669..f899f53ce 100644 --- a/lib/libimhex/include/hex/helpers/fs.hpp +++ b/lib/libimhex/include/hex/helpers/fs.hpp @@ -1,5 +1,6 @@ #pragma once +#include #include #include #include @@ -77,7 +78,7 @@ namespace hex::fs { Logs }; - std::string getExecutablePath(); + std::optional getExecutablePath(); std::vector getDefaultPaths(ImHexPath path, bool listNonExisting = false); diff --git a/lib/libimhex/include/hex/helpers/intrinsics.hpp b/lib/libimhex/include/hex/helpers/intrinsics.hpp index 7a2e10fe5..97d5f82d5 100644 --- a/lib/libimhex/include/hex/helpers/intrinsics.hpp +++ b/lib/libimhex/include/hex/helpers/intrinsics.hpp @@ -4,7 +4,7 @@ namespace hex { - [[noreturn]] void unreachable() { + [[noreturn]] inline void unreachable() { __builtin_unreachable(); } diff --git a/lib/libimhex/source/helpers/fs.cpp b/lib/libimhex/source/helpers/fs.cpp index ab16f5c02..56c9150f5 100644 --- a/lib/libimhex/source/helpers/fs.cpp +++ b/lib/libimhex/source/helpers/fs.cpp @@ -20,21 +20,23 @@ namespace hex::fs { - std::string getExecutablePath() { + std::optional getExecutablePath() { #if defined(OS_WINDOWS) std::string exePath(MAX_PATH, '\0'); - GetModuleFileName(nullptr, exePath.data(), exePath.length()); + if (GetModuleFileName(nullptr, exePath.data(), exePath.length()) == 0) + return std::nullopt; return exePath; #elif defined(OS_LINUX) std::string exePath(PATH_MAX, '\0'); - readlink("/proc/self/exe", exePath.data(), PATH_MAX); + if (readlink("/proc/self/exe", exePath.data(), PATH_MAX) < 0) + return std::nullopt; return exePath; #elif defined(OS_MACOS) return getMacExecutableDirectoryPath(); #else - return ""; + return std::nullopt; #endif } @@ -100,8 +102,6 @@ namespace hex::fs { }; #if defined(OS_WINDOWS) - const auto parentDir = std::fs::path(exePath).parent_path(); - std::fs::path appDataDir; { LPWSTR wAppDataPath = nullptr; @@ -112,7 +112,10 @@ namespace hex::fs { CoTaskMemFree(wAppDataPath); } - std::vector paths = { appDataDir / "imhex", parentDir }; + std::vector paths = { appDataDir / "imhex" }; + + if (exePath) + paths.push_back(exePath->parent_path()); switch (path) { case ImHexPath::Patterns: @@ -181,7 +184,10 @@ namespace hex::fs { // Get path to special directories const std::fs::path applicationSupportDir(getMacApplicationSupportDirectoryPath()); - std::vector paths = { applicationSupportDir, exePath }; + std::vector paths = { applicationSupportDir }; + + if (exePath) + paths.push_back(exePath->parent_path()); switch (path) { case ImHexPath::Patterns: @@ -232,8 +238,8 @@ namespace hex::fs { for (auto &dir : dataDirs) dir = dir / "imhex"; - if (!exePath.empty()) - dataDirs.emplace(dataDirs.begin(), std::fs::path(exePath.data()).parent_path()); + if (exePath && !exePath->empty()) + dataDirs.push_back(exePath->parent_path()); switch (path) { case ImHexPath::Patterns: