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

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