Antkeeper  0.0.1
paths.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2023 Christopher J. Howard
3  *
4  * This file is part of Antkeeper source code.
5  *
6  * Antkeeper source code is free software: you can redistribute it and/or modify
7  * it under the terms of the GNU General Public License as published by
8  * the Free Software Foundation, either version 3 of the License, or
9  * (at your option) any later version.
10  *
11  * Antkeeper source code is distributed in the hope that it will be useful,
12  * but WITHOUT ANY WARRANTY; without even the implied warranty of
13  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14  * GNU General Public License for more details.
15  *
16  * You should have received a copy of the GNU General Public License
17  * along with Antkeeper source code. If not, see <http://www.gnu.org/licenses/>.
18  */
19 
20 #include <engine/utility/paths.hpp>
21 #include <cstddef>
22 #include <limits.h>
23 #include <stdexcept>
24 
25 #if defined(_WIN32)
26  #include <Shlobj.h>
27  #include <windows.h>
28 #else
29  #include <pwd.h>
30  #include <sys/types.h>
31  #include <sys/stat.h>
32  #include <unistd.h>
33 #endif
34 
35 std::filesystem::path get_executable_path()
36 {
37  std::filesystem::path executable_path;
38 
39  #if defined(_WIN32)
40  // Get executable path on Windows
41  std::wstring path(MAX_PATH, L'\0');
42  GetModuleFileNameW(GetModuleHandleW(nullptr), path.data(), MAX_PATH);
43  path.erase(std::find(path.begin(), path.end(), L'\0'), path.end());
44  executable_path = path;
45  #else
46  // Get executable path on Linux
47  char path[PATH_MAX];
48  ssize_t length = ::readlink("/proc/self/exe", path, sizeof(path) - 1);
49  if (length != -1)
50  {
51  path[length] = '\0';
52  executable_path = path;
53  }
54  #endif
55 
56  return executable_path;
57 }
58 
59 std::filesystem::path get_executable_data_path()
60 {
61  #if defined(_WIN32)
62  return get_executable_path().parent_path();
63  #else
64  return get_executable_path().parent_path().parent_path() / "share";
65  #endif
66 }
67 
68 std::filesystem::path get_local_config_path()
69 {
70  std::filesystem::path local_config_path;
71 
72  #if defined(_WIN32)
73 
74  std::wstring path(MAX_PATH, L'\0');
75  if (SHGetFolderPathW(nullptr, CSIDL_LOCAL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, path.data()) == S_OK)
76  {
77  path.erase(std::find(path.begin(), path.end(), L'\0'), path.end());
78  local_config_path = path;
79  }
80 
81  // Windows Vista+
82  // wchar_t* path_buffer = nullptr;
83  // if (SHGetKnownFolderPath(FOLDERID_LocalAppData, KF_FLAG_DEFAULT, nullptr, &path_buffer) == S_OK)
84  // {
85  // local_config_path = std::filesystem::path(path_buffer);
86  // CoTaskMemFree(static_cast<void*>(path_buffer));
87  // }
88 
89  #else
90  // Determine home path
91  std::filesystem::path home_path = getpwuid(getuid())->pw_dir;
92 
93  // Determine config path
94  char* xdg_config_home = std::getenv("XDG_CONFIG_HOME");
95  if (!xdg_config_home)
96  {
97  // Default to $HOME/.config/ as per:
98  // https://specifications.freedesktop.org/basedir-spec/basedir-spec-latest.html#variables
99  local_config_path = home_path / ".config/";
100  }
101  else
102  {
103  local_config_path = xdg_config_home;
104  }
105  #endif
106 
107  return local_config_path;
108 }
109 
110 std::filesystem::path get_shared_config_path()
111 {
112  #if defined(_WIN32)
113  std::filesystem::path shared_config_path;
114 
115  std::wstring path(MAX_PATH, L'\0');
116  if (SHGetFolderPathW(nullptr, CSIDL_APPDATA, nullptr, SHGFP_TYPE_CURRENT, path.data()) == S_OK)
117  {
118  path.erase(std::find(path.begin(), path.end(), L'\0'), path.end());
119  shared_config_path = path;
120  }
121 
122  // Windows Vista+
123  // wchar_t* path_buffer = nullptr;
124  // if (SHGetKnownFolderPath(FOLDERID_RoamingAppData, KF_FLAG_DEFAULT, nullptr, &path_buffer) == S_OK)
125  // {
126  // shared_config_path = path_buffer;
127  // CoTaskMemFree(static_cast<void*>(path_buffer));
128  // }
129 
130  return shared_config_path;
131  #else
132  return get_local_config_path();
133  #endif
134 }
T length(const quaternion< T > &q)
Calculates the length of a quaternion.
Definition: quaternion.hpp:602
std::filesystem::path get_local_config_path()
Returns the absolute path to the directory containing user-specific, device-specific application data...
Definition: paths.cpp:68
std::filesystem::path get_executable_path()
Returns the absolute path to the current executable.
Definition: paths.cpp:35
std::filesystem::path get_executable_data_path()
Returns the absolute path to the directory containing application data.
Definition: paths.cpp:59
std::filesystem::path get_shared_config_path()
Returns the absolute path to the directory containing user-specific application data that may be shar...
Definition: paths.cpp:110