From: Florian Forster Date: Wed, 10 Mar 2010 16:20:29 +0000 (+0100) Subject: src/configfile.[ch]: Implement "cf_util_get_string_buffer". X-Git-Tag: collectd-4.10.0~58 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=62b59f500c2363af7f3df302a8bca53ca4255941;p=collectd.git src/configfile.[ch]: Implement "cf_util_get_string_buffer". A function to parse a config node into a char buffer of limited size. --- diff --git a/src/configfile.c b/src/configfile.c index b2997d64..1bd02a82 100644 --- a/src/configfile.c +++ b/src/configfile.c @@ -959,6 +959,27 @@ int cf_util_get_string (const oconfig_item_t *ci, char **ret_string) /* {{{ */ return (0); } /* }}} int cf_util_get_string */ +/* Assures the config option is a string and copies it to the provided buffer. + * Assures null-termination. */ +int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, /* {{{ */ + size_t buffer_size) +{ + if ((ci == NULL) || (buffer == NULL) || (buffer_size < 1)) + return (EINVAL); + + if ((ci->values_num != 1) || (ci->values[0].type != OCONFIG_TYPE_STRING)) + { + ERROR ("cf_util_get_string_buffer: The %s option requires " + "exactly one string argument.", ci->key); + return (-1); + } + + strncpy (buffer, ci->values[0].value.string, buffer_size); + buffer[buffer_size - 1] = 0; + + return (0); +} /* }}} int cf_util_get_string_buffer */ + int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool) /* {{{ */ { if ((ci == NULL) || (ret_bool == NULL)) diff --git a/src/configfile.h b/src/configfile.h index a73def21..c1c5e293 100644 --- a/src/configfile.h +++ b/src/configfile.h @@ -91,6 +91,11 @@ const char *global_option_get (const char *option); * success. */ int cf_util_get_string (const oconfig_item_t *ci, char **ret_string); +/* Assures the config option is a string and copies it to the provided buffer. + * Assures null-termination. */ +int cf_util_get_string_buffer (const oconfig_item_t *ci, char *buffer, + size_t buffer_size); + /* Assures the config option is a boolean and assignes it to `ret_bool'. * Otherwise, `ret_bool' is not changed and non-zero is returned. */ int cf_util_get_boolean (const oconfig_item_t *ci, _Bool *ret_bool);