Blob Blame History Raw
From 85d127fd52a8469f9f3ce0d1130fe17e756fdd75 Mon Sep 17 00:00:00 2001
From: Sumit Bose <sbose@redhat.com>
Date: Fri, 16 Nov 2018 13:32:33 +0100
Subject: [PATCH 1/2] adutil: add _adcli_strv_add_unique

_adcli_strv_add_unique checks is the new value already exists in the
strv before adding it. Check can be done case-sensitive or not.

Related to https://gitlab.freedesktop.org/realmd/adcli/issues/16
---
 library/adprivate.h |  5 ++++
 library/adutil.c    | 65 ++++++++++++++++++++++++++++++++++++++-------
 2 files changed, 61 insertions(+), 9 deletions(-)

diff --git a/library/adprivate.h b/library/adprivate.h
index bc9df6d..0806430 100644
--- a/library/adprivate.h
+++ b/library/adprivate.h
@@ -111,6 +111,11 @@ char **        _adcli_strv_add               (char **strv,
                                               char *string,
                                               int *length) GNUC_WARN_UNUSED;
 
+char **        _adcli_strv_add_unique        (char **strv,
+                                              char *string,
+                                              int *length,
+                                              bool case_sensitive) GNUC_WARN_UNUSED;
+
 void           _adcli_strv_remove_unsorted   (char **strv,
                                               const char *string,
                                               int *length);
diff --git a/library/adutil.c b/library/adutil.c
index 17d2caa..76ea158 100644
--- a/library/adutil.c
+++ b/library/adutil.c
@@ -221,6 +221,34 @@ _adcli_strv_add (char **strv,
 	return seq_push (strv, length, string);
 }
 
+static int
+_adcli_strv_has_ex (char **strv,
+                    const char *str,
+                    int (* compare) (const char *match, const char*value))
+{
+	int i;
+
+	for (i = 0; strv && strv[i] != NULL; i++) {
+		if (compare (strv[i], str) == 0)
+			return 1;
+	}
+
+	return 0;
+}
+
+char **
+_adcli_strv_add_unique (char **strv,
+                        char *string,
+                        int *length,
+                        bool case_sensitive)
+{
+	if (_adcli_strv_has_ex (strv, string, case_sensitive ? strcmp : strcasecmp) == 1) {
+		return strv;
+	}
+
+	return _adcli_strv_add (strv, string, length);
+}
+
 #define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
 
 void
@@ -241,19 +269,11 @@ _adcli_strv_remove_unsorted (char **strv,
 	                            (seq_compar)strcasecmp, free);
 }
 
-
 int
 _adcli_strv_has (char **strv,
                  const char *str)
 {
-	int i;
-
-	for (i = 0; strv && strv[i] != NULL; i++) {
-		if (strcmp (strv[i], str) == 0)
-			return 1;
-	}
-
-	return 0;
+	return _adcli_strv_has_ex (strv, str, strcmp);
 }
 
 void
@@ -704,6 +724,32 @@ test_strv_add_free (void)
 	_adcli_strv_free (strv);
 }
 
+static void
+test_strv_add_unique_free (void)
+{
+	char **strv = NULL;
+
+	strv = _adcli_strv_add_unique (strv, strdup ("one"), NULL, false);
+	strv = _adcli_strv_add_unique (strv, strdup ("one"), NULL, false);
+	strv = _adcli_strv_add_unique (strv, strdup ("two"), NULL, false);
+	strv = _adcli_strv_add_unique (strv, strdup ("two"), NULL, false);
+	strv = _adcli_strv_add_unique (strv, strdup ("tWo"), NULL, false);
+	strv = _adcli_strv_add_unique (strv, strdup ("three"), NULL, false);
+	strv = _adcli_strv_add_unique (strv, strdup ("three"), NULL, false);
+	strv = _adcli_strv_add_unique (strv, strdup ("TWO"), NULL, true);
+
+	assert_num_eq (_adcli_strv_len (strv), 4);
+
+	assert_str_eq (strv[0], "one");
+	assert_str_eq (strv[1], "two");
+	assert_str_eq (strv[2], "three");
+	assert_str_eq (strv[3], "TWO");
+	assert (strv[4] == NULL);
+
+	_adcli_strv_free (strv);
+}
+
+
 static void
 test_strv_dup (void)
 {
@@ -856,6 +902,7 @@ main (int argc,
       char *argv[])
 {
 	test_func (test_strv_add_free, "/util/strv_add_free");
+	test_func (test_strv_add_unique_free, "/util/strv_add_unique_free");
 	test_func (test_strv_dup, "/util/strv_dup");
 	test_func (test_strv_count, "/util/strv_count");
 	test_func (test_check_nt_time_string_lifetime, "/util/check_nt_time_string_lifetime");
-- 
2.20.1