1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144
|
#include "F3DConfigFileTools.h"
#include "F3DConfig.h"
#include "log.h"
#include <cstring>
#include <filesystem>
#include <vector>
#if defined(_WIN32)
// clang-format off
#include <windows.h>
#include <libloaderapi.h>
// clang-format on
#endif
#ifdef __APPLE__
#include <mach-o/dyld.h>
#endif
//----------------------------------------------------------------------------
std::string F3DConfigFileTools::GetUserConfigFileDirectory()
{
std::string applicationName = "f3d";
std::filesystem::path dirPath;
#if defined(_WIN32)
const char* appData = std::getenv("APPDATA");
if (!appData)
{
return std::string();
}
dirPath = std::filesystem::path(appData);
#else
// Implementing XDG specifications
const char* xdgConfigHome = std::getenv("XDG_CONFIG_HOME");
if (xdgConfigHome && strlen(xdgConfigHome) > 0)
{
dirPath = std::filesystem::path(xdgConfigHome);
}
else
{
const char* home = std::getenv("HOME");
if (!home || strlen(home) == 0)
{
return std::string();
}
dirPath = std::filesystem::path(home);
dirPath /= ".config";
}
#endif
dirPath /= applicationName;
return dirPath.string();
}
//----------------------------------------------------------------------------
std::string F3DConfigFileTools::GetBinaryConfigFileDirectory()
{
std::string execPath;
std::filesystem::path dirPath;
std::string errorMsg, programFilePath;
try
{
#if defined(_WIN32)
wchar_t wc[1024] = { 0 };
GetModuleFileNameW(NULL, wc, 1024);
std::wstring ws(wc);
std::transform(
ws.begin(), ws.end(), std::back_inserter(execPath), [](wchar_t c) { return (char)c; });
#else
#ifdef __APPLE__
uint32_t size = 1024;
char buffer[size];
if (_NSGetExecutablePath(buffer, &size) != 0)
{
f3d::log::error("Executable is too long to recover path to config file");
return std::string();
}
execPath = buffer;
#else
execPath = std::filesystem::canonical("/proc/self/exe").string();
#endif
#endif
// transform path to exe to path to install
// /install/bin/f3d -> /install
dirPath =
std::filesystem::canonical(std::filesystem::path(execPath)).parent_path().parent_path();
// Add platform specific paths
#if F3D_MACOS_BUNDLE
dirPath /= "Resources";
#endif
#ifdef __linux__
dirPath /= "share/f3d";
#endif
}
catch (const std::filesystem::filesystem_error&)
{
f3d::log::debug("Cannot recover binary config file directory: ", dirPath.string());
return std::string();
}
return dirPath.string();
}
//----------------------------------------------------------------------------
std::string F3DConfigFileTools::GetConfigFilePath()
{
std::string fileName = "config.json";
std::filesystem::path filePath;
std::vector<std::string> dirsToCheck;
try
{
dirsToCheck.emplace_back(F3DConfigFileTools::GetUserConfigFileDirectory());
#ifdef __APPLE__
dirsToCheck.emplace_back("/usr/local/etc/f3d");
#endif
#ifdef __linux__
dirsToCheck.emplace_back("/etc/f3d");
dirsToCheck.emplace_back("/usr/share/f3d");
#endif
dirsToCheck.emplace_back(F3DConfigFileTools::GetBinaryConfigFileDirectory());
for (std::string dir : dirsToCheck)
{
if (!dir.empty())
{
filePath = std::filesystem::path(dir) / fileName;
if (std::filesystem::exists(filePath))
{
f3d::log::debug("Config file found in: ", filePath.string());
return filePath.string();
}
}
}
f3d::log::debug("No config file found");
return std::string();
}
catch (const std::filesystem::filesystem_error&)
{
f3d::log::error("Error recovering config file path: ", filePath.string());
return std::string();
}
}
|