Blame 0001-adutil-add-_adcli_strv_add_unique.patch

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