diff options
author | Max Kellermann <max@duempel.org> | 2013-01-29 17:23:35 +0100 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-01-29 18:09:39 +0100 |
commit | feb8d5b82f3e7c67806d5efbef1e0b86170ec6dd (patch) | |
tree | 8127b30bc339eae03226f3e97ea8ac99f62587b3 /src | |
parent | eb8922f3468fe664211911c49077a4e3442b7703 (diff) | |
download | mpd-feb8d5b82f3e7c67806d5efbef1e0b86170ec6dd.tar.gz mpd-feb8d5b82f3e7c67806d5efbef1e0b86170ec6dd.tar.xz mpd-feb8d5b82f3e7c67806d5efbef1e0b86170ec6dd.zip |
ConfigFile, CommandLine: use the Path class
Diffstat (limited to 'src')
-rw-r--r-- | src/CommandLine.cxx | 88 | ||||
-rw-r--r-- | src/ConfigFile.cxx | 12 | ||||
-rw-r--r-- | src/PlaylistFile.cxx | 5 | ||||
-rw-r--r-- | src/PlaylistSave.cxx | 3 | ||||
-rw-r--r-- | src/UpdateIO.cxx | 10 | ||||
-rw-r--r-- | src/conf.h | 14 |
6 files changed, 70 insertions, 62 deletions
diff --git a/src/CommandLine.cxx b/src/CommandLine.cxx index f04bf89b8..5384fc6dd 100644 --- a/src/CommandLine.cxx +++ b/src/CommandLine.cxx @@ -31,7 +31,8 @@ #include "PlaylistRegistry.hxx" #include "PlaylistPlugin.hxx" #include "mpd_error.h" -#include "glib_compat.h" +#include "fs/Path.hxx" +#include "fs/FileSystem.hxx" #ifdef ENABLE_ENCODER #include "encoder_list.h" @@ -133,6 +134,16 @@ static void version(void) static const char *summary = "Music Player Daemon - a daemon for playing music."; +gcc_pure +static Path +PathBuildChecked(const Path &a, Path::const_pointer b) +{ + if (a.IsNull()) + return Path::Null(); + + return Path::Build(a, b); +} + bool parse_cmdline(int argc, char **argv, struct options *options, GError **error_r) @@ -191,59 +202,44 @@ parse_cmdline(int argc, char **argv, struct options *options, return true; } else if (argc <= 1) { /* default configuration file path */ - char *path1; #ifdef G_OS_WIN32 - path1 = g_build_filename(g_get_user_config_dir(), - CONFIG_FILE_LOCATION, NULL); - if (g_file_test(path1, G_FILE_TEST_IS_REGULAR)) - ret = config_read_file(path1, error_r); - else { - int i = 0; - char *system_path = NULL; - const char * const *system_config_dirs; - - system_config_dirs = g_get_system_config_dirs(); - - while(system_config_dirs[i] != NULL) { - system_path = g_build_filename(system_config_dirs[i], - CONFIG_FILE_LOCATION, - NULL); - if(g_file_test(system_path, - G_FILE_TEST_IS_REGULAR)) { - ret = config_read_file(system_path,error_r); - g_free(system_path); - break; - } else - g_free(system_path); - ++i; - } + Path path = PathBuildChecked(Path::FromUTF8(g_get_user_config_dir()), + CONFIG_FILE_LOCATION); + if (!path.IsNull() && FileExists(path)) + return ReadConfigFile(path, error_r); + + const char *const*system_config_dirs = + g_get_system_config_dirs(); + + for (unsigned i = 0; system_config_dirs[i] != nullptr; ++i) { + path = PathBuildChecked(Path::FromUTF8(system_config_dirs[i]), + CONFIG_FILE_LOCATION); + if (!path.IsNull() && FileExists(path)) + return ReadConfigFile(path, error_r); } #else /* G_OS_WIN32 */ - char *path2; - path1 = g_build_filename(g_get_home_dir(), - USER_CONFIG_FILE_LOCATION1, NULL); - path2 = g_build_filename(g_get_home_dir(), - USER_CONFIG_FILE_LOCATION2, NULL); - if (g_file_test(path1, G_FILE_TEST_IS_REGULAR)) - ret = config_read_file(path1, error_r); - else if (g_file_test(path2, G_FILE_TEST_IS_REGULAR)) - ret = config_read_file(path2, error_r); - else if (g_file_test(SYSTEM_CONFIG_FILE_LOCATION, - G_FILE_TEST_IS_REGULAR)) - ret = config_read_file(SYSTEM_CONFIG_FILE_LOCATION, - error_r); + Path path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()), + USER_CONFIG_FILE_LOCATION1); + if (!path.IsNull() && FileExists(path)) + return ReadConfigFile(path, error_r); + + path = PathBuildChecked(Path::FromUTF8(g_get_home_dir()), + USER_CONFIG_FILE_LOCATION2); + if (!path.IsNull() && FileExists(path)) + return ReadConfigFile(path, error_r); + + path = Path::FromUTF8(SYSTEM_CONFIG_FILE_LOCATION); + if (!path.IsNull() && FileExists(path)) + return ReadConfigFile(path, error_r); #endif - g_free(path1); -#ifndef G_OS_WIN32 - g_free(path2); -#endif - - return ret; + g_set_error(error_r, cmdline_quark(), 0, + "No configuration file found"); + return false; } else if (argc == 2) { /* specified configuration file */ - return config_read_file(argv[1], error_r); + return ReadConfigFile(Path::FromFS(argv[1]), error_r); } else { g_set_error(error_r, cmdline_quark(), 0, "too many arguments"); diff --git a/src/ConfigFile.cxx b/src/ConfigFile.cxx index 614bb4f91..02a705f43 100644 --- a/src/ConfigFile.cxx +++ b/src/ConfigFile.cxx @@ -27,6 +27,7 @@ extern "C" { } #include "fs/Path.hxx" +#include "fs/FileSystem.hxx" #include "mpd_error.h" #include <glib.h> @@ -347,20 +348,23 @@ config_read_block(FILE *fp, int *count, char *string, GError **error_r) } bool -config_read_file(const char *file, GError **error_r) +ReadConfigFile(const Path &path, GError **error_r) { + assert(!path.IsNull()); + const std::string path_utf8 = path.ToUTF8(); + FILE *fp; char string[MAX_STRING_SIZE + 1]; int count = 0; struct config_entry *entry; struct config_param *param; - g_debug("loading file %s", file); + g_debug("loading file %s", path_utf8.c_str()); - if (!(fp = fopen(file, "r"))) { + if (!(fp = FOpen(path, "r"))) { g_set_error(error_r, config_quark(), errno, "Failed to open %s: %s", - file, g_strerror(errno)); + path_utf8.c_str(), g_strerror(errno)); return false; } diff --git a/src/PlaylistFile.cxx b/src/PlaylistFile.cxx index daec5ca0a..278e521ba 100644 --- a/src/PlaylistFile.cxx +++ b/src/PlaylistFile.cxx @@ -31,6 +31,7 @@ #include "conf.h" #include "Idle.hxx" #include "fs/Path.hxx" +#include "fs/FileSystem.hxx" extern "C" { #include "uri.h" @@ -433,14 +434,14 @@ static bool spl_rename_internal(const Path &from_path_fs, const Path &to_path_fs, GError **error_r) { - if (!g_file_test(from_path_fs.c_str(), G_FILE_TEST_IS_REGULAR)) { + if (!FileExists(from_path_fs)) { g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_NO_SUCH_LIST, "No such playlist"); return false; } - if (g_file_test(to_path_fs.c_str(), G_FILE_TEST_EXISTS)) { + if (FileExists(to_path_fs)) { g_set_error_literal(error_r, playlist_quark(), PLAYLIST_RESULT_LIST_EXISTS, "Playlist exists already"); diff --git a/src/PlaylistSave.cxx b/src/PlaylistSave.cxx index 49e475ee5..1ff6c856b 100644 --- a/src/PlaylistSave.cxx +++ b/src/PlaylistSave.cxx @@ -25,6 +25,7 @@ #include "Mapper.hxx" #include "Idle.hxx" #include "fs/Path.hxx" +#include "fs/FileSystem.hxx" extern "C" { #include "uri.h" @@ -76,7 +77,7 @@ spl_save_queue(const char *name_utf8, const struct queue *queue) if (path_fs.IsNull()) return PLAYLIST_RESULT_BAD_NAME; - if (g_file_test(path_fs.c_str(), G_FILE_TEST_EXISTS)) + if (FileExists(path_fs)) return PLAYLIST_RESULT_LIST_EXISTS; FILE *file = fopen(path_fs.c_str(), "w"); diff --git a/src/UpdateIO.cxx b/src/UpdateIO.cxx index eca5688a1..bd5fef69c 100644 --- a/src/UpdateIO.cxx +++ b/src/UpdateIO.cxx @@ -22,7 +22,7 @@ #include "Directory.hxx" #include "Mapper.hxx" #include "fs/Path.hxx" -#include "glib_compat.h" +#include "fs/FileSystem.hxx" #include <glib.h> @@ -68,12 +68,10 @@ directory_exists(const Directory *directory) /* invalid path: cannot exist */ return false; - GFileTest test = directory->device == DEVICE_INARCHIVE || + return directory->device == DEVICE_INARCHIVE || directory->device == DEVICE_CONTAINER - ? G_FILE_TEST_IS_REGULAR - : G_FILE_TEST_IS_DIR; - - return g_file_test(path_fs.c_str(), test); + ? FileExists(path_fs) + : DirectoryExists(path_fs); } bool diff --git a/src/conf.h b/src/conf.h index d20dc78bc..ca60591d6 100644 --- a/src/conf.h +++ b/src/conf.h @@ -81,6 +81,10 @@ #define MAX_FILTER_CHAIN_LENGTH 255 +#ifdef __cplusplus +class Path; +#endif + struct block_param { char *name; char *value; @@ -118,8 +122,6 @@ config_quark(void) return g_quark_from_static_string("config"); } -G_BEGIN_DECLS - void config_global_init(void); void config_global_finish(void); @@ -129,8 +131,14 @@ void config_global_finish(void); */ void config_global_check(void); +#ifdef __cplusplus + bool -config_read_file(const char *file, GError **error_r); +ReadConfigFile(const Path &path, GError **error_r); + +#endif + +G_BEGIN_DECLS /* don't free the returned value set _last_ to NULL to get first entry */ |