config/Path: move code to GetVariable()
This commit is contained in:
parent
e8a4317f31
commit
a9805db500
@ -70,6 +70,29 @@ InitPathParser(const ConfigData &config) noexcept
|
|||||||
#endif
|
#endif
|
||||||
}
|
}
|
||||||
|
|
||||||
|
#ifndef _WIN32
|
||||||
|
|
||||||
|
static AllocatedPath
|
||||||
|
GetVariable(std::string_view name)
|
||||||
|
{
|
||||||
|
if (name == "HOME"sv)
|
||||||
|
return GetConfiguredHome();
|
||||||
|
#ifdef USE_XDG
|
||||||
|
else if (name == "XDG_CONFIG_HOME"sv)
|
||||||
|
return GetUserConfigDir();
|
||||||
|
else if (name == "XDG_MUSIC_DIR"sv)
|
||||||
|
return GetUserMusicDir();
|
||||||
|
else if (name == "XDG_CACHE_HOME"sv)
|
||||||
|
return GetUserCacheDir();
|
||||||
|
else if (name == "XDG_RUNTIME_DIR"sv)
|
||||||
|
return GetUserRuntimeDir();
|
||||||
|
#endif
|
||||||
|
else
|
||||||
|
throw FmtRuntimeError("Unknown variable: {:?}", name);
|
||||||
|
}
|
||||||
|
|
||||||
|
#endif
|
||||||
|
|
||||||
AllocatedPath
|
AllocatedPath
|
||||||
ParsePath(std::string_view path)
|
ParsePath(std::string_view path)
|
||||||
{
|
{
|
||||||
@ -89,26 +112,9 @@ ParsePath(std::string_view path)
|
|||||||
} else if (path.starts_with('$')) {
|
} else if (path.starts_with('$')) {
|
||||||
path.remove_prefix(1);
|
path.remove_prefix(1);
|
||||||
|
|
||||||
const auto [env_var, rest] = Split(path, '/');
|
const auto [name, rest] = Split(path, '/');
|
||||||
|
const auto value = GetVariable(name);
|
||||||
AllocatedPath xdg_path(nullptr);
|
return value / AllocatedPath::FromUTF8Throw(rest);
|
||||||
if (env_var == "HOME"sv) {
|
|
||||||
xdg_path = GetConfiguredHome();
|
|
||||||
#ifdef USE_XDG
|
|
||||||
} else if (env_var == "XDG_CONFIG_HOME"sv) {
|
|
||||||
xdg_path = GetUserConfigDir();
|
|
||||||
} else if (env_var == "XDG_MUSIC_DIR"sv) {
|
|
||||||
xdg_path = GetUserMusicDir();
|
|
||||||
} else if (env_var == "XDG_CACHE_HOME"sv) {
|
|
||||||
xdg_path = GetUserCacheDir();
|
|
||||||
} else if (env_var == "XDG_RUNTIME_DIR"sv) {
|
|
||||||
xdg_path = GetUserRuntimeDir();
|
|
||||||
#endif
|
|
||||||
} else {
|
|
||||||
throw FmtRuntimeError("Unknown variable: {:?}", env_var);
|
|
||||||
}
|
|
||||||
|
|
||||||
return xdg_path / AllocatedPath::FromUTF8Throw(rest);
|
|
||||||
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
|
} else if (!PathTraitsUTF8::IsAbsolute(path)) {
|
||||||
throw FmtRuntimeError("not an absolute path: {:?}", path);
|
throw FmtRuntimeError("not an absolute path: {:?}", path);
|
||||||
} else {
|
} else {
|
||||||
|
Loading…
x
Reference in New Issue
Block a user