From: Florian Forster Date: Fri, 11 Jan 2013 13:56:35 +0000 (+0100) Subject: src/common.[ch]: Implement strarray_{add,free}. X-Git-Tag: collectd-5.3.0~56^2~10 X-Git-Url: https://git.verplant.org/?a=commitdiff_plain;h=aaabdb7dcc46df1597930f9bf7fbfa464aa64d5d;p=collectd.git src/common.[ch]: Implement strarray_{add,free}. --- diff --git a/src/common.c b/src/common.c index b679bf70..82a4f017 100644 --- a/src/common.c +++ b/src/common.c @@ -1396,3 +1396,35 @@ int strtoderive (const char *string, derive_t *ret_value) /* {{{ */ *ret_value = tmp; return (0); } /* }}} int strtoderive */ + +int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str) /* {{{ */ +{ + char **array; + size_t array_len = *ret_array_len; + + if (str == NULL) + return (EINVAL); + + array = realloc (*ret_array, + (array_len + 1) * sizeof (*array)); + if (array == NULL) + return (ENOMEM); + *ret_array = array; + + array[array_len] = strdup (str); + if (array[array_len] == NULL) + return (ENOMEM); + + array_len++; + *ret_array_len = array_len; + return (0); +} /* }}} int strarray_add */ + +void strarray_free (char **array, size_t array_len) /* {{{ */ +{ + size_t i; + + for (i = 0; i < array_len; i++) + sfree (array[i]); + sfree (array); +} /* }}} void strarray_free */ diff --git a/src/common.h b/src/common.h index 8a7d9865..2c744363 100644 --- a/src/common.h +++ b/src/common.h @@ -317,4 +317,7 @@ int service_name_to_port_number (const char *service_name); * failure. If failure is returned, ret_value is not touched. */ int strtoderive (const char *string, derive_t *ret_value); +int strarray_add (char ***ret_array, size_t *ret_array_len, char const *str); +void strarray_free (char **array, size_t array_len); + #endif /* COMMON_H */