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

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