From 248ca73856ae0f65c8bf2ce8d53b05f5ba40caea Mon Sep 17 00:00:00 2001 From: dennisppaul Date: Tue, 1 Oct 2024 18:14:08 +0200 Subject: [PATCH] added file functions --- include/UmgebungFunctions.h | 6 +++- src/UmgebungFunctions.cpp | 66 ++++++++++++++++++++++++++++++++++++- 2 files changed, 70 insertions(+), 2 deletions(-) diff --git a/include/UmgebungFunctions.h b/include/UmgebungFunctions.h index ba6972c..9578e30 100644 --- a/include/UmgebungFunctions.h +++ b/include/UmgebungFunctions.h @@ -19,7 +19,6 @@ #pragma once -#include #include #include #include @@ -112,4 +111,9 @@ namespace umgebung { (oss << ... << args); return oss.str(); } + + std::string get_executable_location(); + std::string find_file_in_paths(const std::vector& paths, const std::string& filename); + std::string find_in_environment_path(const std::string& filename); + } // namespace umgebung diff --git a/src/UmgebungFunctions.cpp b/src/UmgebungFunctions.cpp index 1071966..e97e87d 100644 --- a/src/UmgebungFunctions.cpp +++ b/src/UmgebungFunctions.cpp @@ -25,7 +25,9 @@ #include #include -#if defined(SYSTEM_WIN32) +#if defined(__APPLE__) || defined(__linux__) +#include +#elif defined(_WIN32) #include #endif @@ -181,4 +183,66 @@ namespace umgebung { std::string sketchPath() { return sketchPath_impl(); } + + + std::string find_file_in_paths(const std::vector& paths, const std::string& filename) { + for (const auto& path: paths) { + std::filesystem::path full_path = std::filesystem::path(path) / filename; + if (std::filesystem::exists(full_path)) { + return full_path.string(); + } + } + return ""; + } + + std::string find_in_environment_path(const std::string& filename) { + std::string path; + char* env = getenv("PATH"); + if (env != nullptr) { + std::string path_env(env); + // On Windows, PATH entries are separated by ';', on Linux/macOS by ':' +#if defined(_WIN32) + char path_separator = ';'; +#else + char path_separator = ':'; +#endif + + std::istringstream ss(path_env); + std::string token; + + while (std::getline(ss, token, path_separator)) { + std::filesystem::path candidate = std::filesystem::path(token) / filename; + if (std::filesystem::exists(candidate)) { + path = candidate.string(); + break; + } + } + } + return path; + } + + std::string get_executable_location() { +#if defined(__APPLE__) || defined(__linux__) + Dl_info info; + // Get the address of a function within the library (can be any function) + if (dladdr((void*) &get_executable_location, &info)) { + std::filesystem::path lib_path(info.dli_fname); // Full path to the library + return lib_path.parent_path().string() + std::filesystem::path::preferred_separator; // Return the directory without the library name + } else { + std::cerr << "Could not retrieve library location (dladdr)" << std::endl; + return ""; + } +#elif defined(_WIN32) + HMODULE hModule = nullptr; // Handle to the DLL + char path[MAX_PATH]; + + if (GetModuleFileNameA(hModule, path, MAX_PATH) != 0) { + std::filesystem::path lib_path(path); // Full path to the DLL + return lib_path.parent_path().string() + std::filesystem::path::preferred_separator; // Add the separator + } else { + std::cerr << "Could not retrieve library location (GetModuleFileName)" << std::endl; + return ""; + } +#endif + } } // namespace umgebung \ No newline at end of file