Skip to content

Commit

Permalink
added file functions
Browse files Browse the repository at this point in the history
  • Loading branch information
dennisppaul committed Oct 1, 2024
1 parent 2a0fc26 commit 248ca73
Show file tree
Hide file tree
Showing 2 changed files with 70 additions and 2 deletions.
6 changes: 5 additions & 1 deletion include/UmgebungFunctions.h
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

#pragma once

#include <filesystem>
#include <iostream>
#include <sstream>
#include <string>
Expand Down Expand Up @@ -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<std::string>& paths, const std::string& filename);
std::string find_in_environment_path(const std::string& filename);

} // namespace umgebung
66 changes: 65 additions & 1 deletion src/UmgebungFunctions.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,9 @@
#include <iostream>
#include <vector>

#if defined(SYSTEM_WIN32)
#if defined(__APPLE__) || defined(__linux__)
#include <dlfcn.h>
#elif defined(_WIN32)
#include <windows.h>
#endif

Expand Down Expand Up @@ -181,4 +183,66 @@ namespace umgebung {
std::string sketchPath() {
return sketchPath_impl();
}


std::string find_file_in_paths(const std::vector<std::string>& 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

0 comments on commit 248ca73

Please sign in to comment.