CommandLine: refactor config search, use standard directory API
This commit is contained in:
parent
e42637226f
commit
ca69ad8beb
@ -34,6 +34,7 @@
|
|||||||
#include "fs/AllocatedPath.hxx"
|
#include "fs/AllocatedPath.hxx"
|
||||||
#include "fs/Traits.hxx"
|
#include "fs/Traits.hxx"
|
||||||
#include "fs/FileSystem.hxx"
|
#include "fs/FileSystem.hxx"
|
||||||
|
#include "fs/StandardDirectory.hxx"
|
||||||
#include "util/Error.hxx"
|
#include "util/Error.hxx"
|
||||||
#include "util/Domain.hxx"
|
#include "util/Domain.hxx"
|
||||||
#include "util/OptionDef.hxx"
|
#include "util/OptionDef.hxx"
|
||||||
@ -55,7 +56,8 @@
|
|||||||
#include <stdlib.h>
|
#include <stdlib.h>
|
||||||
|
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
#define CONFIG_FILE_LOCATION "\\mpd\\mpd.conf"
|
#define CONFIG_FILE_LOCATION "mpd\\mpd.conf"
|
||||||
|
#define APP_CONFIG_FILE_LOCATION "conf\\mpd.conf"
|
||||||
#else
|
#else
|
||||||
#define USER_CONFIG_FILE_LOCATION1 ".mpdconf"
|
#define USER_CONFIG_FILE_LOCATION1 ".mpdconf"
|
||||||
#define USER_CONFIG_FILE_LOCATION2 ".mpd/mpd.conf"
|
#define USER_CONFIG_FILE_LOCATION2 ".mpd/mpd.conf"
|
||||||
@ -186,14 +188,36 @@ static void help(void)
|
|||||||
exit(EXIT_SUCCESS);
|
exit(EXIT_SUCCESS);
|
||||||
}
|
}
|
||||||
|
|
||||||
gcc_pure
|
class ConfigLoader
|
||||||
static AllocatedPath
|
|
||||||
PathBuildChecked(const AllocatedPath &a, PathTraitsFS::const_pointer b)
|
|
||||||
{
|
{
|
||||||
if (a.IsNull())
|
Error &error;
|
||||||
return AllocatedPath::Null();
|
bool result;
|
||||||
|
public:
|
||||||
|
ConfigLoader(Error &_error) : error(_error), result(false) { }
|
||||||
|
|
||||||
return AllocatedPath::Build(a, b);
|
bool GetResult() const { return result; }
|
||||||
|
|
||||||
|
bool TryFile(const Path path);
|
||||||
|
bool TryFile(const AllocatedPath &base_path,
|
||||||
|
PathTraitsFS::const_pointer path);
|
||||||
|
};
|
||||||
|
|
||||||
|
bool ConfigLoader::TryFile(Path path)
|
||||||
|
{
|
||||||
|
if (FileExists(path)) {
|
||||||
|
result = ReadConfigFile(path, error);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
bool ConfigLoader::TryFile(const AllocatedPath &base_path,
|
||||||
|
PathTraitsFS::const_pointer path)
|
||||||
|
{
|
||||||
|
if (base_path.IsNull())
|
||||||
|
return false;
|
||||||
|
auto full_path = AllocatedPath::Build(base_path, path);
|
||||||
|
return TryFile(full_path);
|
||||||
}
|
}
|
||||||
|
|
||||||
bool
|
bool
|
||||||
@ -270,41 +294,25 @@ parse_cmdline(int argc, char **argv, struct options *options,
|
|||||||
}
|
}
|
||||||
|
|
||||||
/* use default configuration file path */
|
/* use default configuration file path */
|
||||||
|
|
||||||
|
ConfigLoader loader(error);
|
||||||
|
|
||||||
|
bool found =
|
||||||
#ifdef WIN32
|
#ifdef WIN32
|
||||||
AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()),
|
loader.TryFile(GetUserConfigDir(), CONFIG_FILE_LOCATION) ||
|
||||||
CONFIG_FILE_LOCATION);
|
loader.TryFile(GetSystemConfigDir(), CONFIG_FILE_LOCATION) ||
|
||||||
if (!path.IsNull() && FileExists(path))
|
loader.TryFile(GetAppBaseDir(), APP_CONFIG_FILE_LOCATION);
|
||||||
return ReadConfigFile(path, error);
|
|
||||||
|
|
||||||
const char *const*system_config_dirs =
|
|
||||||
g_get_system_config_dirs();
|
|
||||||
|
|
||||||
for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) {
|
|
||||||
path = PathBuildChecked(AllocatedPath::FromUTF8(system_config_dirs[i]),
|
|
||||||
CONFIG_FILE_LOCATION);
|
|
||||||
if (!path.IsNull() && FileExists(path))
|
|
||||||
return ReadConfigFile(path, error);
|
|
||||||
}
|
|
||||||
#else
|
#else
|
||||||
AllocatedPath path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_user_config_dir()),
|
loader.TryFile(GetUserConfigDir(),
|
||||||
USER_CONFIG_FILE_LOCATION_XDG);
|
USER_CONFIG_FILE_LOCATION_XDG) ||
|
||||||
if (!path.IsNull() && FileExists(path))
|
loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION1) ||
|
||||||
return ReadConfigFile(path, error);
|
loader.TryFile(GetHomeDir(), USER_CONFIG_FILE_LOCATION2) ||
|
||||||
|
loader.TryFile(Path::FromFS(SYSTEM_CONFIG_FILE_LOCATION));
|
||||||
path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()),
|
|
||||||
USER_CONFIG_FILE_LOCATION1);
|
|
||||||
if (!path.IsNull() && FileExists(path))
|
|
||||||
return ReadConfigFile(path, error);
|
|
||||||
|
|
||||||
path = PathBuildChecked(AllocatedPath::FromUTF8(g_get_home_dir()),
|
|
||||||
USER_CONFIG_FILE_LOCATION2);
|
|
||||||
if (!path.IsNull() && FileExists(path))
|
|
||||||
return ReadConfigFile(path, error);
|
|
||||||
|
|
||||||
path = AllocatedPath::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION);
|
|
||||||
if (!path.IsNull() && FileExists(path))
|
|
||||||
return ReadConfigFile(path, error);
|
|
||||||
#endif
|
#endif
|
||||||
error.Set(cmdline_domain, "No configuration file found");
|
if (!found) {
|
||||||
return false;
|
error.Set(cmdline_domain, "No configuration file found");
|
||||||
|
return false;
|
||||||
|
}
|
||||||
|
|
||||||
|
return loader.GetResult();
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user