From 0fef1f5e1d38ce85c14523a4cc76da0e40139877 Mon Sep 17 00:00:00 2001 From: Pierre-Yves Ritschard Date: Fri, 3 Feb 2012 09:57:53 +0100 Subject: [PATCH] bring back prefix/postfix handling. simplify swap_chars. prefix and postfix need to be set to an empty string when they are null since they are used in format strings. swap_chars return value is never used and could never be negative, switch to a void prototype instead. --- src/write_graphite.c | 53 +++++++++++++--------------------------------------- 1 file changed, 13 insertions(+), 40 deletions(-) diff --git a/src/write_graphite.c b/src/write_graphite.c index 9e96198d..bafe6ac2 100644 --- a/src/write_graphite.c +++ b/src/write_graphite.c @@ -341,26 +341,19 @@ static int wg_format_values (char *ret, size_t ret_len, return (0); } -static int swap_chars (char *dst, const char *src, +static void swap_chars (char *dst, const char *src, const char from, const char to) { size_t i; - int reps = 0; - for (i = 0; i < strlen(src) ; i++) { if (src[i] == from) - { dst[i] = to; - ++reps; - } else dst[i] = src[i]; } dst[i] = '\0'; - - return reps; } static int wg_format_name (char *ret, int ret_len, @@ -380,6 +373,12 @@ static int wg_format_name (char *ret, int ret_len, assert (plugin != NULL); assert (type != NULL); + if (prefix == NULL) + prefix = ""; + + if (postfix == NULL) + postfix = ""; + if ((n_hostname = malloc(strlen(hostname)+1)) == NULL) { ERROR ("Unable to allocate memory for normalized hostname buffer"); @@ -398,23 +397,9 @@ static int wg_format_name (char *ret, int ret_len, return (-1); } - if (swap_chars(n_hostname, hostname, '.', dotchar) == -1) - { - ERROR ("Unable to normalize hostname"); - return (-1); - } - - if (swap_chars(n_plugin, plugin, ' ', '_') == -1) - { - ERROR ("Unable to normalize plugin"); - return (-1); - } - - if (swap_chars(n_type, type, ' ', '_') == -1) - { - ERROR ("Unable to normalize type"); - return (-1); - } + swap_chars(n_hostname, hostname, '.', dotchar); + swap_chars(n_plugin, plugin, ' ', '_'); + swap_chars(n_type, type, ' ', '_'); if (type_instance != NULL && type_instance[0] != '\0') { @@ -423,16 +408,8 @@ static int wg_format_name (char *ret, int ret_len, ERROR ("Unable to allocate memory for normalized datasource name buffer"); return (-1); } - if (swap_chars(n_type_instance, type_instance, '.', dotchar) == -1) - { - ERROR ("Unable to normalize datasource name"); - return (-1); - } - if (swap_chars(n_type_instance, type_instance, ' ', '_') == -1) - { - ERROR ("Unable to normalize datasource name"); - return (-1); - } + swap_chars(n_type_instance, type_instance, '.', dotchar); + swap_chars(n_type_instance, type_instance, ' ', '_'); } if (plugin_instance != NULL && plugin_instance[0] != '\0') @@ -442,11 +419,7 @@ static int wg_format_name (char *ret, int ret_len, ERROR ("Unable to allocate memory for normalized plugin instance buffer"); return (-1); } - if (swap_chars(n_plugin_instance, plugin_instance, ' ', '_') == -1) - { - ERROR ("Unable to normalize datasource name"); - return (-1); - } + swap_chars(n_plugin_instance, plugin_instance, ' ', '_'); } if ((n_plugin_instance == NULL) || (n_plugin_instance[0] == '\0')) -- 2.11.0