From 885d187d23d44de0a3b8696d03a9e8fa2cc29f54 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Tue, 16 Jun 2015 14:46:17 +0200 Subject: [PATCH] libnm: add _nm_utils_strv_cleanup() function --- libnm-core/nm-core-internal.h | 5 +++++ libnm-core/nm-utils.c | 30 ++++++++++++++++++++++++++++++ 2 files changed, 35 insertions(+) diff --git a/libnm-core/nm-core-internal.h b/libnm-core/nm-core-internal.h index f9ae42ecd..d7cb47bf2 100644 --- a/libnm-core/nm-core-internal.h +++ b/libnm-core/nm-core-internal.h @@ -121,6 +121,11 @@ gboolean _nm_utils_string_in_list (const char *str, gssize _nm_utils_strv_find_first (char **list, gssize len, const char *needle); +char **_nm_utils_strv_cleanup (char **strv, + gboolean strip_whitespace, + gboolean skip_empty, + gboolean skip_repeated); + char ** _nm_utils_strsplit_set (const char *str, const char *delimiters, int max_tokens); diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index f9325e2a4..875882f13 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -483,6 +483,36 @@ _nm_utils_strv_find_first (char **list, gssize len, const char *needle) return -1; } +char ** +_nm_utils_strv_cleanup (char **strv, + gboolean strip_whitespace, + gboolean skip_empty, + gboolean skip_repeated) +{ + guint i, j; + + if (!strv || !*strv) + return strv; + + if (strip_whitespace) { + for (i = 0; strv[i]; i++) + g_strstrip (strv[i]); + } + if (!skip_empty && !skip_repeated) + return strv; + j = 0; + for (i = 0; strv[i]; i++) { + if ( (skip_empty && !*strv[i]) + || (skip_repeated && _nm_utils_strv_find_first (strv, j, strv[i]) >= 0)) + g_free (strv[i]); + else + strv[j++] = strv[i]; + } + strv[j] = NULL; + return strv; +} + + gboolean _nm_utils_string_slist_validate (GSList *list, const char **valid_values) {