From a1659075543b1d3fc090b59397ef80024cd3a6d9 Mon Sep 17 00:00:00 2001 From: Thomas Haller Date: Sat, 4 Feb 2017 17:49:30 +0100 Subject: [PATCH] shared: add nm_utils_strv_find_first() helper Make _nm_utils_strv_find_first() accessible outside of libnm-core by copying it from "libnm-core/nm-core-internal.h" (and rename it). --- shared/nm-utils/nm-shared-utils.c | 48 +++++++++++++++++++++++++++++++ shared/nm-utils/nm-shared-utils.h | 4 +++ 2 files changed, 52 insertions(+) diff --git a/shared/nm-utils/nm-shared-utils.c b/shared/nm-utils/nm-shared-utils.c index 38bb8181f3..fc048fe893 100644 --- a/shared/nm-utils/nm-shared-utils.c +++ b/shared/nm-utils/nm-shared-utils.c @@ -158,6 +158,54 @@ _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 ma /*****************************************************************************/ +/** + * nm_utils_strv_find_first: + * @list: the strv list to search + * @len: the length of the list, or a negative value if @list is %NULL terminated. + * @needle: the value to search for. The search is done using strcmp(). + * + * Searches @list for @needle and returns the index of the first match (based + * on strcmp()). + * + * For convenience, @list has type 'char**' instead of 'const char **'. + * + * Returns: index of first occurrence or -1 if @needle is not found in @list. + */ +gssize +nm_utils_strv_find_first (char **list, gssize len, const char *needle) +{ + gssize i; + + if (len > 0) { + g_return_val_if_fail (list, -1); + + if (!needle) { + /* if we search a list with known length, %NULL is a valid @needle. */ + for (i = 0; i < len; i++) { + if (!list[i]) + return i; + } + } else { + for (i = 0; i < len; i++) { + if (list[i] && !strcmp (needle, list[i])) + return i; + } + } + } else if (len < 0) { + g_return_val_if_fail (needle, -1); + + if (list) { + for (i = 0; list[i]; i++) { + if (strcmp (needle, list[i]) == 0) + return i; + } + } + } + return -1; +} + +/*****************************************************************************/ + gint _nm_utils_ascii_str_to_bool (const char *str, gint default_value) diff --git a/shared/nm-utils/nm-shared-utils.h b/shared/nm-utils/nm-shared-utils.h index 5d8a3a86ab..f1f9f51833 100644 --- a/shared/nm-utils/nm-shared-utils.h +++ b/shared/nm-utils/nm-shared-utils.h @@ -43,6 +43,10 @@ void nm_utils_strbuf_append_str (char **buf, gsize *len, const char *str); /*****************************************************************************/ +gssize nm_utils_strv_find_first (char **list, gssize len, const char *needle); + +/*****************************************************************************/ + gint64 _nm_utils_ascii_str_to_int64 (const char *str, guint base, gint64 min, gint64 max, gint64 fallback); gint _nm_utils_ascii_str_to_bool (const char *str,