From 112c53be32b46a38a38f58606bf7c682356932d5 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Ji=C5=99=C3=AD=20Klime=C5=A1?= Date: Wed, 10 Sep 2014 09:36:59 +0200 Subject: [PATCH] nm-utils: add a wrapper for g_strsplit_set() removing empty strings from array g_strsplit_set() puts empty strings ("") into the resulting string array when a delimiter character appears as the first or last character in the string or when there are adjacent delimiter characters. However, this is not what is useful in most cases. --- libnm-core/nm-utils-private.h | 6 +++++- libnm-core/nm-utils.c | 37 ++++++++++++++++++++++++++++++++++- 2 files changed, 41 insertions(+), 2 deletions(-) diff --git a/libnm-core/nm-utils-private.h b/libnm-core/nm-utils-private.h index ae78d9627..be9619dbf 100644 --- a/libnm-core/nm-utils-private.h +++ b/libnm-core/nm-utils-private.h @@ -15,7 +15,7 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2005 - 2008 Red Hat, Inc. + * Copyright 2005 - 2014 Red Hat, Inc. */ #ifndef __NM_UTILS_PRIVATE_H__ @@ -78,4 +78,8 @@ void _nm_utils_ip6_routes_from_dbus (const GValue *dbus_value, GSList * _nm_utils_strv_to_slist (char **strv); char ** _nm_utils_slist_to_strv (GSList *slist); +char ** _nm_utils_strsplit_set (const char *str, + const char *delimiters, + int max_tokens); + #endif diff --git a/libnm-core/nm-utils.c b/libnm-core/nm-utils.c index 591b1e1d5..58e1d20e4 100644 --- a/libnm-core/nm-utils.c +++ b/libnm-core/nm-utils.c @@ -16,7 +16,7 @@ * Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor, * Boston, MA 02110-1301 USA. * - * Copyright 2005 - 2013 Red Hat, Inc. + * Copyright 2005 - 2014 Red Hat, Inc. */ #include "config.h" @@ -706,6 +706,41 @@ _nm_utils_slist_to_strv (GSList *slist) return strv; } +/** + * _nm_utils_strsplit_set: + * @str: string to split + * @delimiters: string of delimiter characters + * @max_tokens: the maximum number of tokens to split string into. When it is + * less than 1, the @str is split completely. + * + * Utility function for splitting string into a string array. It is a wrapper + * for g_strsplit_set(), but it also removes empty strings from the vector as + * they are not useful in most cases. + * + * Returns: (transfer full): a newly allocated NULL-terminated array of strings. + * The caller must free the returned array with g_strfreev(). + **/ +char ** +_nm_utils_strsplit_set (const char *str, const char *delimiters, int max_tokens) +{ + char **result; + uint i; + uint j; + + result = g_strsplit_set (str, delimiters, max_tokens); + + /* remove empty strings */ + for (i = 0; result && result[i]; i++) { + if (*result[i] == '\0') { + g_free (result[i]); + for (j = i; result[j]; j++) + result[j] = result[j + 1]; + i--; + } + } + return result; +} + static gboolean device_supports_ap_ciphers (guint32 dev_caps, guint32 ap_flags,