Blame SOURCES/0001-adutil-add-_adcli_strv_add_unique.patch

59dcbd
From 85d127fd52a8469f9f3ce0d1130fe17e756fdd75 Mon Sep 17 00:00:00 2001
59dcbd
From: Sumit Bose <sbose@redhat.com>
59dcbd
Date: Fri, 16 Nov 2018 13:32:33 +0100
59dcbd
Subject: [PATCH 1/2] adutil: add _adcli_strv_add_unique
59dcbd
59dcbd
_adcli_strv_add_unique checks is the new value already exists in the
59dcbd
strv before adding it. Check can be done case-sensitive or not.
59dcbd
59dcbd
Related to https://gitlab.freedesktop.org/realmd/adcli/issues/16
59dcbd
---
59dcbd
 library/adprivate.h |  5 ++++
59dcbd
 library/adutil.c    | 65 ++++++++++++++++++++++++++++++++++++++-------
59dcbd
 2 files changed, 61 insertions(+), 9 deletions(-)
59dcbd
59dcbd
diff --git a/library/adprivate.h b/library/adprivate.h
59dcbd
index bc9df6d..0806430 100644
59dcbd
--- a/library/adprivate.h
59dcbd
+++ b/library/adprivate.h
59dcbd
@@ -111,6 +111,11 @@ char **        _adcli_strv_add               (char **strv,
59dcbd
                                               char *string,
59dcbd
                                               int *length) GNUC_WARN_UNUSED;
59dcbd
 
59dcbd
+char **        _adcli_strv_add_unique        (char **strv,
59dcbd
+                                              char *string,
59dcbd
+                                              int *length,
59dcbd
+                                              bool case_sensitive) GNUC_WARN_UNUSED;
59dcbd
+
59dcbd
 void           _adcli_strv_remove_unsorted   (char **strv,
59dcbd
                                               const char *string,
59dcbd
                                               int *length);
59dcbd
diff --git a/library/adutil.c b/library/adutil.c
59dcbd
index 17d2caa..76ea158 100644
59dcbd
--- a/library/adutil.c
59dcbd
+++ b/library/adutil.c
59dcbd
@@ -221,6 +221,34 @@ _adcli_strv_add (char **strv,
59dcbd
 	return seq_push (strv, length, string);
59dcbd
 }
59dcbd
 
59dcbd
+static int
59dcbd
+_adcli_strv_has_ex (char **strv,
59dcbd
+                    const char *str,
59dcbd
+                    int (* compare) (const char *match, const char*value))
59dcbd
+{
59dcbd
+	int i;
59dcbd
+
59dcbd
+	for (i = 0; strv && strv[i] != NULL; i++) {
59dcbd
+		if (compare (strv[i], str) == 0)
59dcbd
+			return 1;
59dcbd
+	}
59dcbd
+
59dcbd
+	return 0;
59dcbd
+}
59dcbd
+
59dcbd
+char **
59dcbd
+_adcli_strv_add_unique (char **strv,
59dcbd
+                        char *string,
59dcbd
+                        int *length,
59dcbd
+                        bool case_sensitive)
59dcbd
+{
59dcbd
+	if (_adcli_strv_has_ex (strv, string, case_sensitive ? strcmp : strcasecmp) == 1) {
59dcbd
+		return strv;
59dcbd
+	}
59dcbd
+
59dcbd
+	return _adcli_strv_add (strv, string, length);
59dcbd
+}
59dcbd
+
59dcbd
 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
59dcbd
 
59dcbd
 void
59dcbd
@@ -241,19 +269,11 @@ _adcli_strv_remove_unsorted (char **strv,
59dcbd
 	                            (seq_compar)strcasecmp, free);
59dcbd
 }
59dcbd
 
59dcbd
-
59dcbd
 int
59dcbd
 _adcli_strv_has (char **strv,
59dcbd
                  const char *str)
59dcbd
 {
59dcbd
-	int i;
59dcbd
-
59dcbd
-	for (i = 0; strv && strv[i] != NULL; i++) {
59dcbd
-		if (strcmp (strv[i], str) == 0)
59dcbd
-			return 1;
59dcbd
-	}
59dcbd
-
59dcbd
-	return 0;
59dcbd
+	return _adcli_strv_has_ex (strv, str, strcmp);
59dcbd
 }
59dcbd
 
59dcbd
 void
59dcbd
@@ -704,6 +724,32 @@ test_strv_add_free (void)
59dcbd
 	_adcli_strv_free (strv);
59dcbd
 }
59dcbd
 
59dcbd
+static void
59dcbd
+test_strv_add_unique_free (void)
59dcbd
+{
59dcbd
+	char **strv = NULL;
59dcbd
+
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("one"), NULL, false);
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("one"), NULL, false);
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("two"), NULL, false);
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("two"), NULL, false);
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("tWo"), NULL, false);
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("three"), NULL, false);
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("three"), NULL, false);
59dcbd
+	strv = _adcli_strv_add_unique (strv, strdup ("TWO"), NULL, true);
59dcbd
+
59dcbd
+	assert_num_eq (_adcli_strv_len (strv), 4);
59dcbd
+
59dcbd
+	assert_str_eq (strv[0], "one");
59dcbd
+	assert_str_eq (strv[1], "two");
59dcbd
+	assert_str_eq (strv[2], "three");
59dcbd
+	assert_str_eq (strv[3], "TWO");
59dcbd
+	assert (strv[4] == NULL);
59dcbd
+
59dcbd
+	_adcli_strv_free (strv);
59dcbd
+}
59dcbd
+
59dcbd
+
59dcbd
 static void
59dcbd
 test_strv_dup (void)
59dcbd
 {
59dcbd
@@ -856,6 +902,7 @@ main (int argc,
59dcbd
       char *argv[])
59dcbd
 {
59dcbd
 	test_func (test_strv_add_free, "/util/strv_add_free");
59dcbd
+	test_func (test_strv_add_unique_free, "/util/strv_add_unique_free");
59dcbd
 	test_func (test_strv_dup, "/util/strv_dup");
59dcbd
 	test_func (test_strv_count, "/util/strv_count");
59dcbd
 	test_func (test_check_nt_time_string_lifetime, "/util/check_nt_time_string_lifetime");
59dcbd
-- 
59dcbd
2.20.1
59dcbd