diff options
author | Jiří Klimeš <jklimes@redhat.com> | 2014-09-10 09:36:59 +0200 |
---|---|---|
committer | Jiří Klimeš <jklimes@redhat.com> | 2014-09-10 15:00:49 +0200 |
commit | 112c53be32b46a38a38f58606bf7c682356932d5 (patch) | |
tree | a63b06002488f20714e9530238ffe924991c1232 | |
parent | 9197d76e96dd694a428df6c11ca060aec13d7dcf (diff) | |
download | NetworkManager-112c53be32b46a38a38f58606bf7c682356932d5.tar.gz |
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.
-rw-r--r-- | libnm-core/nm-utils-private.h | 6 | ||||
-rw-r--r-- | 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 ae78d96276..be9619dbf6 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 591b1e1d52..58e1d20e4b 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, |