Repository navigation
Expand file tree
/
Copy pathConfigHelpers.cpp
More file actions
70 lines (58 loc) · 1.7 KB
/
Copy pathConfigHelpers.cpp
File metadata and controls
70 lines (58 loc) · 1.7 KB
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
#include "ConfigHelpers.h"
#include <glib.h>
std::deque<std::string> ConfigDirs()
{
std::deque<std::string> dirs;
#ifdef SNAPCRAFT_BUILD
if(const gchar* data = g_getenv("SNAP"))
dirs.push_back(std::string(data) + "/etc");
if(const gchar* common = g_getenv("SNAP_COMMON"))
dirs.push_back(common);
#else
const gchar* programName = g_get_prgname();
const gchar * const *systemConfigDirs = g_get_system_config_dirs();
while(*systemConfigDirs) {
if(programName)
dirs.push_back(std::string(*systemConfigDirs) + G_DIR_SEPARATOR + programName);
else
dirs.push_back(*systemConfigDirs);
++systemConfigDirs;
}
const gchar* configDir = g_get_user_config_dir();
if(configDir) {
if(programName)
dirs.push_back(std::string(configDir) + G_DIR_SEPARATOR + programName);
else
dirs.push_back(configDir);
}
#endif
return dirs;
}
std::optional<std::string> DataDir()
{
#ifdef SNAPCRAFT_BUILD
if(const gchar* common = g_getenv("SNAP_COMMON"))
return common;
#else
const gchar* programName = g_get_prgname();
if(const gchar* dataDir = g_get_user_data_dir()) {
if(programName)
return std::string(dataDir) + G_DIR_SEPARATOR + programName;
else
return dataDir;
}
#endif
return {};
}
std::string FullPath(const std::string& configDir, const std::string& path)
{
std::string fullPath;
if(!g_path_is_absolute(path.c_str())) {
gchar* tmpPath =
g_build_filename(configDir.c_str(), path.c_str(), NULL);
fullPath = tmpPath;
g_free(tmpPath);
} else
fullPath = path;
return fullPath;
}