1
0
mirror of https://github.com/zenorogue/hyperrogue.git synced 2026-05-08 08:11:21 +00:00

config/save file default location under Linux is now according to the XDG standard

This commit is contained in:
Zeno Rogue
2026-04-17 16:02:36 +02:00
parent 12f2e37961
commit d0f53270f7
2 changed files with 35 additions and 11 deletions
+31 -7
View File
@@ -61,13 +61,37 @@ EX void initializeCLI() {
#endif
#ifdef FHS
static string sbuf, cbuf;
if(getenv("HOME")) {
sbuf = getenv("HOME"); sbuf += "/."; sbuf += scorefile;
cbuf = getenv("HOME"); cbuf += "/."; cbuf += conffile;
scorefile = sbuf;
conffile = cbuf.c_str();
}
auto try_place = [&] (string env, string suffix, string& which_file, bool only_file) {
char *res = getenv(env.c_str());
if(!res) return false;
string buf = res; buf += suffix; buf += "/hyperrogue";
if(only_file && file_exists(buf + "/" + which_file)) {
which_file = buf + "/" + which_file;
return true;
}
if(!file_exists(buf)) system(("mkdir -p " +buf).c_str());
which_file = buf + "/" + which_file;
return true;
};
auto try_dot = [&] (string& which_file) {
char *res = getenv("HOME");
string buf = res;
buf += "/."; buf += which_file;
if(!file_exists(buf)) return false;
which_file = buf;
return true;
};
if(try_dot(scorefile)) {}
else if(try_place("XDG_DATA_HOME", "", scorefile, true)) {}
else if(try_place("HOME", "/.local/share", scorefile, false)) {}
else if(try_place("XDG_DATA_HOME", "", scorefile, false)) {}
if(try_dot(conffile)) {}
else if(try_place("XDG_CONFIG_HOME", "", conffile, true)) {}
else if(try_place("HOME", "/.config", conffile, true)) {}
else if(try_place("XDG_CONFIG_HOME", "", conffile, false)) {}
#endif
}