Skip to content

Commit e13cd7c

Browse files
committed
fs configpath()
1 parent a462570 commit e13cd7c

File tree

1 file changed

+36
-0
lines changed

1 file changed

+36
-0
lines changed

kit/fs/fs.h

Lines changed: 36 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,12 @@
77

88
namespace fs {
99

10+
#ifdef _WIN32
11+
#define SEP "\\"
12+
#else
13+
#define SEP "/"
14+
#endif
15+
1016
inline std::string homedir() {
1117
const char* homedir = getenv("HOME");
1218
if(!homedir)
@@ -15,6 +21,36 @@ namespace fs {
1521
K_ERROR(GENERAL, "Unable to locate home directory.");
1622
return homedir;
1723
}
24+
25+
inline std::string configdir(std::string apppath = std::string()) {
26+
std::string r;
27+
bool add_config=false;
28+
const char* cdir = getenv("XDG_CONFIG_PATH");
29+
if(!cdir){
30+
cdir = getenv("HOME");
31+
add_config=true;
32+
}
33+
if(!cdir){
34+
cdir = getenv("HOMEPATH");
35+
add_config=true;
36+
}
37+
if(!cdir){
38+
K_ERROR(GENERAL, "Unable to locate home directory.");
39+
}
40+
if(add_config)
41+
{
42+
#ifdef _WIN32
43+
r = std::string(cdir);
44+
#else
45+
r = std::string(cdir) + SEP + ".config";
46+
#endif
47+
}
48+
else
49+
r = cdir;
50+
if(not apppath.empty())
51+
return r + SEP + apppath;
52+
return r;
53+
}
1854
}
1955

2056
#endif

0 commit comments

Comments
 (0)