File 0022-adutil-add-_adcli_strv_add_unique.patch of Package adcli (Revision ce46d0e7322eb68daa00b5c190e5847d)
Currently displaying revision ce46d0e7322eb68daa00b5c190e5847d , Show latest
xxxxxxxxxx
1
From 4ee9d518c66b2f229a74d55b5491f51483fb5d29 Mon Sep 17 00:00:00 2001
2
From: Sumit Bose <sbose@redhat.com>
3
Date: Fri, 16 Nov 2018 13:32:33 +0100
4
Subject: [PATCH 22/25] adutil: add _adcli_strv_add_unique
5
6
_adcli_strv_add_unique checks is the new value already exists in the
7
strv before adding it. Check can be done case-sensitive or not.
8
9
Related to https://gitlab.freedesktop.org/realmd/adcli/issues/16
10
---
11
library/adprivate.h | 5 +++++
12
library/adutil.c | 65 +++++++++++++++++++++++++++++++++++++++++++++--------
13
2 files changed, 61 insertions(+), 9 deletions(-)
14
15
diff --git a/library/adprivate.h b/library/adprivate.h
16
index bc9df6d..0806430 100644
17
--- a/library/adprivate.h
18
+++ b/library/adprivate.h
19
20
char *string,
21
int *length) GNUC_WARN_UNUSED;
22
23
+char ** _adcli_strv_add_unique (char **strv,
24
+ char *string,
25
+ int *length,
26
+ bool case_sensitive) GNUC_WARN_UNUSED;
27
+
28
void _adcli_strv_remove_unsorted (char **strv,
29
const char *string,
30
int *length);
31
diff --git a/library/adutil.c b/library/adutil.c
32
index 6334b52..08da731 100644
33
--- a/library/adutil.c
34
+++ b/library/adutil.c
35
36
return seq_push (strv, length, string);
37
}
38
39
+static int
40
+_adcli_strv_has_ex (char **strv,
41
+ const char *str,
42
+ int (* compare) (const char *match, const char*value))
43
+{
44
+ int i;
45
+
46
+ for (i = 0; strv && strv[i] != NULL; i++) {
47
+ if (compare (strv[i], str) == 0)
48
+ return 1;
49
+ }
50
+
51
+ return 0;
52
+}
53
+
54
+char **
55
+_adcli_strv_add_unique (char **strv,
56
+ char *string,
57
+ int *length,
58
+ bool case_sensitive)
59
+{
60
+ if (_adcli_strv_has_ex (strv, string, case_sensitive ? strcmp : strcasecmp) == 1) {
61
+ return strv;
62
+ }
63
+
64
+ return _adcli_strv_add (strv, string, length);
65
+}
66
+
67
#define discard_const(ptr) ((void *)((uintptr_t)(ptr)))
68
69
void
70
71
(seq_compar)strcasecmp, free);
72
}
73
74
-
75
int
76
_adcli_strv_has (char **strv,
77
const char *str)
78
{
79
- int i;
80
-
81
- for (i = 0; strv && strv[i] != NULL; i++) {
82
- if (strcmp (strv[i], str) == 0)
83
- return 1;
84
- }
85
-
86
- return 0;
87
+ return _adcli_strv_has_ex (strv, str, strcmp);
88
}
89
90
void
91
92
_adcli_strv_free (strv);
93
}
94
95
+static void
96
+test_strv_add_unique_free (void)
97
+{
98
+ char **strv = NULL;
99
+
100
+ strv = _adcli_strv_add_unique (strv, strdup ("one"), NULL, false);
101
+ strv = _adcli_strv_add_unique (strv, strdup ("one"), NULL, false);
102
+ strv = _adcli_strv_add_unique (strv, strdup ("two"), NULL, false);
103
+ strv = _adcli_strv_add_unique (strv, strdup ("two"), NULL, false);
104
+ strv = _adcli_strv_add_unique (strv, strdup ("tWo"), NULL, false);
105
+ strv = _adcli_strv_add_unique (strv, strdup ("three"), NULL, false);
106
+ strv = _adcli_strv_add_unique (strv, strdup ("three"), NULL, false);
107
+ strv = _adcli_strv_add_unique (strv, strdup ("TWO"), NULL, true);
108
+
109
+ assert_num_eq (_adcli_strv_len (strv), 4);
110
+
111
+ assert_str_eq (strv[0], "one");
112
+ assert_str_eq (strv[1], "two");
113
+ assert_str_eq (strv[2], "three");
114
+ assert_str_eq (strv[3], "TWO");
115
+ assert (strv[4] == NULL);
116
+
117
+ _adcli_strv_free (strv);
118
+}
119
+
120
+
121
static void
122
test_strv_dup (void)
123
{
124
125
char *argv[])
126
{
127
test_func (test_strv_add_free, "/util/strv_add_free");
128
+ test_func (test_strv_add_unique_free, "/util/strv_add_unique_free");
129
test_func (test_strv_dup, "/util/strv_dup");
130
test_func (test_strv_count, "/util/strv_count");
131
test_func (test_check_nt_time_string_lifetime, "/util/check_nt_time_string_lifetime");
132
--
133
2.16.4
134
135