summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThomas Haller <thaller@redhat.com>2017-02-04 17:49:30 +0100
committerThomas Haller <thaller@redhat.com>2017-02-04 17:54:49 +0100
commita1659075543b1d3fc090b59397ef80024cd3a6d9 (patch)
tree3ef5338787e18d449912f9d6b415f17afed5bbe4
parent6404c79e4d6c62e85662162232fb624469693bcd (diff)
downloadNetworkManager-a1659075543b1d3fc090b59397ef80024cd3a6d9.tar.gz
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).
-rw-r--r--shared/nm-utils/nm-shared-utils.c48
-rw-r--r--shared/nm-utils/nm-shared-utils.h4
2 files changed, 52 insertions, 0 deletions
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,