diff options
author | Max Kellermann <max@duempel.org> | 2013-08-04 12:03:56 +0200 |
---|---|---|
committer | Max Kellermann <max@duempel.org> | 2013-08-04 12:03:56 +0200 |
commit | 83f4c48c8aaa46bbede4447c95939af310ea28ad (patch) | |
tree | 7eb8c9f826d8508dba93875a1d91531d69e2fee1 | |
parent | acd742d22593a52d2e5f5bcdb2c81175d61c9b11 (diff) | |
download | mpd-83f4c48c8aaa46bbede4447c95939af310ea28ad.tar.gz mpd-83f4c48c8aaa46bbede4447c95939af310ea28ad.tar.xz mpd-83f4c48c8aaa46bbede4447c95939af310ea28ad.zip |
ConfigData: move code to block_param, config_param methods
-rw-r--r-- | src/ConfigData.cxx | 128 | ||||
-rw-r--r-- | src/ConfigData.hxx | 27 |
2 files changed, 113 insertions, 42 deletions
diff --git a/src/ConfigData.cxx b/src/ConfigData.cxx index dd102a19a..27a72eb8b 100644 --- a/src/ConfigData.cxx +++ b/src/ConfigData.cxx @@ -28,6 +28,32 @@ #include <string.h> #include <stdlib.h> +unsigned +block_param::GetUnsignedValue() const +{ + char *endptr; + long value2 = strtol(value.c_str(), &endptr, 0); + if (*endptr != 0) + MPD_ERROR("Not a valid number in line %i", line); + + if (value2 < 0) + MPD_ERROR("Not a positive number in line %i", line); + + return (unsigned)value2; +} + +bool +block_param::GetBoolValue() const +{ + bool value2; + if (!get_bool(value.c_str(), &value2)) + MPD_ERROR("%s is not a boolean value (yes, true, 1) or " + "(no, false, 0) on line %i\n", + name.c_str(), line); + + return value2; +} + config_param::config_param(const char *_value, int _line) :next(nullptr), value(g_strdup(_value)), line(_line) {} @@ -51,17 +77,69 @@ config_param::GetBlockParam(const char *name) const } const char * -config_get_block_string(const struct config_param *param, const char *name, - const char *default_value) +config_param::GetBlockValue(const char *name, const char *default_value) const { - if (param == nullptr) + const block_param *bp = GetBlockParam(name); + if (bp == nullptr) + return default_value; + + return bp->value.c_str(); +} + +char * +config_param::DupBlockString(const char *name, const char *default_value) const +{ + return g_strdup(GetBlockValue(name, default_value)); +} + +char * +config_param::DupBlockPath(const char *name, GError **error_r) const +{ + assert(error_r != nullptr); + assert(*error_r == nullptr); + + const block_param *bp = GetBlockParam(name); + if (bp == nullptr) + return nullptr; + + char *path = parsePath(bp->value.c_str(), error_r); + if (G_UNLIKELY(path == nullptr)) + g_prefix_error(error_r, + "Invalid path in \"%s\" at line %i: ", + name, bp->line); + + return path; +} + +unsigned +config_param::GetBlockValue(const char *name, unsigned default_value) const +{ + const block_param *bp = GetBlockParam(name); + if (bp == nullptr) return default_value; - const block_param *bp = param->GetBlockParam(name); + return bp->GetUnsignedValue(); +} + +gcc_pure +bool +config_param::GetBlockValue(const char *name, bool default_value) const +{ + const block_param *bp = GetBlockParam(name); if (bp == NULL) return default_value; - return bp->value.c_str(); + return bp->GetBoolValue(); +} + +const char * +config_get_block_string(const struct config_param *param, const char *name, + const char *default_value) +{ + if (param == nullptr) + return default_value; + + return param->GetBlockValue(name, default_value); } char * @@ -81,17 +159,7 @@ config_dup_block_path(const struct config_param *param, const char *name, if (param == nullptr) return nullptr; - const block_param *bp = param->GetBlockParam(name); - if (bp == NULL) - return NULL; - - char *path = parsePath(bp->value.c_str(), error_r); - if (G_UNLIKELY(path == NULL)) - g_prefix_error(error_r, - "Invalid path in \"%s\" at line %i: ", - name, bp->line); - - return path; + return param->DupBlockPath(name, error_r); } unsigned @@ -101,19 +169,7 @@ config_get_block_unsigned(const struct config_param *param, const char *name, if (param == nullptr) return default_value; - const block_param *bp = param->GetBlockParam(name); - if (bp == NULL) - return default_value; - - char *endptr; - long value = strtol(bp->value.c_str(), &endptr, 0); - if (*endptr != 0) - MPD_ERROR("Not a valid number in line %i", bp->line); - - if (value < 0) - MPD_ERROR("Not a positive number in line %i", bp->line); - - return (unsigned)value; + return param->GetBlockValue(name, default_value); } bool @@ -123,17 +179,5 @@ config_get_block_bool(const struct config_param *param, const char *name, if (param == nullptr) return default_value; - const block_param *bp = param->GetBlockParam(name); - bool success, value; - - if (bp == NULL) - return default_value; - - success = get_bool(bp->value.c_str(), &value); - if (!success) - MPD_ERROR("%s is not a boolean value (yes, true, 1) or " - "(no, false, 0) on line %i\n", - name, bp->line); - - return value; + return param->GetBlockValue(name, default_value); } diff --git a/src/ConfigData.hxx b/src/ConfigData.hxx index a70426f11..5e13d073c 100644 --- a/src/ConfigData.hxx +++ b/src/ConfigData.hxx @@ -42,6 +42,12 @@ struct block_param { gcc_nonnull_all block_param(const char *_name, const char *_value, int _line=-1) :name(_name), value(_value), line(_line), used(false) {} + + gcc_pure + unsigned GetUnsignedValue() const; + + gcc_pure + bool GetBoolValue() const; }; struct config_param { @@ -82,6 +88,27 @@ struct config_param { gcc_nonnull_all gcc_pure const block_param *GetBlockParam(const char *_name) const; + + gcc_pure + const char *GetBlockValue(const char *name, + const char *default_value=nullptr) const; + + gcc_malloc + char *DupBlockString(const char *name, + const char *default_value=nullptr) const; + + /** + * Same as config_dup_path(), but looks up the setting in the + * specified block. + */ + gcc_malloc + char *DupBlockPath(const char *name, GError **error_r) const; + + gcc_pure + unsigned GetBlockValue(const char *name, unsigned default_value) const; + + gcc_pure + bool GetBlockValue(const char *name, bool default_value) const; }; struct ConfigData { |