/* * Copyright (C) 2023 Christopher J. Howard * * This file is part of Antkeeper source code. * * Antkeeper source code is free software: you can redistribute it and/or modify * it under the terms of the GNU General Public License as published by * the Free Software Foundation, either version 3 of the License, or * (at your option) any later version. * * Antkeeper source code is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the * GNU General Public License for more details. * * You should have received a copy of the GNU General Public License * along with Antkeeper source code. If not, see . */ #include #include #include #include #if defined(_WIN32) #include #include #else #include #include #include #include #endif std::filesystem::path get_executable_path() { std::filesystem::path executable_path; #if defined(_WIN32) // Get executable path on Windows std::wstring path(MAX_PATH, L'\0'); GetModuleFileNameW(GetModuleHandleW(nullptr), path.data(), MAX_PATH); path.erase(std::find(path.begin(), path.end(), L'\0'), path.end()); executable_path = path; #else // Get executable path on Linux char path[PATH_MAX]; ssize_t length = ::readlink("/proc/self/exe", path, sizeof(path) - 1); if (length != -1) { path[length] = '\0'; executable_path = path; } #endif return executable_path; } std::filesystem::path get_executable_data_path() { #if defined(_WIN32) return get_executable_path().parent_path(); #else return get_executable_path().parent_path().parent_path() / "share"; #endif } std::filesystem::path get_local_config_path() { std::filesystem::path local_config_path; #if defined(_WIN32) std::wstring path(MAX_PATH, L'\0'); if (SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, path.data()) == S_OK) { path.erase(std::find(path.begin(), path.end(), L'\0'), path.end()); local_config_path = path; } // Windows Vista+ // wchar_t* path_buffer = nullptr; // if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, nullptr, &path_buffer) == S_OK) // { // local_config_path = std::filesystem::path(path_buffer); // CoTaskMemFree(static_cast(path_buffer)); // } #else // Determine home path std::filesystem::path home_path = getpwuid(getuid())->pw_dir; // Determine config path char* xdg_config_home = std::getenv("XDG_CONFIG_HOME"); if (!xdg_config_home) { // Default to $HOME/.config/ as per: // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables local_config_path = home_path / ".config/"; } else { local_config_path = xdg_config_home; } #endif return local_config_path; } std::filesystem::path get_shared_config_path() { #if defined(_WIN32) std::filesystem::path shared_config_path; std::wstring path(MAX_PATH, L'\0'); if (SHGetFolderPathW(nullptr, CSIDL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, path.data()) == S_OK) { path.erase(std::find(path.begin(), path.end(), L'\0'), path.end()); shared_config_path = path; } // Windows Vista+ // wchar_t* path_buffer = nullptr; // if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &path_buffer) == S_OK) // { // shared_config_path = path_buffer; // CoTaskMemFree(static_cast(path_buffer)); // } return shared_config_path; #else return get_local_config_path(); #endif }