mail archive of the barebox mailing list
 help / color / mirror / Atom feed
* [PATCH 1/3] stringlist: Add adding sort uniq
@ 2021-03-05 11:03 Sascha Hauer
  2021-03-05 11:03 ` [PATCH 2/3] stringlist: Initialize HEAD str member to NULL Sascha Hauer
  2021-03-05 11:03 ` [PATCH 3/3] complete: Use string_list_add_sort_uniq() Sascha Hauer
  0 siblings, 2 replies; 3+ messages in thread
From: Sascha Hauer @ 2021-03-05 11:03 UTC (permalink / raw)
  To: Barebox List

Function to add an entry sorted to a string list only when it doesn't
exist.

Signed-off-by: Sascha Hauer <s.hauer@pengutronix.de>
---
 include/stringlist.h |  1 +
 lib/stringlist.c     | 23 +++++++++++++++++++++++
 2 files changed, 24 insertions(+)

diff --git a/include/stringlist.h b/include/stringlist.h
index 5cd452ca5f..dbf159da91 100644
--- a/include/stringlist.h
+++ b/include/stringlist.h
@@ -12,6 +12,7 @@ struct string_list {
 int string_list_add(struct string_list *sl, const char *str);
 int string_list_add_asprintf(struct string_list *sl, const char *fmt, ...);
 int string_list_add_sorted(struct string_list *sl, const char *str);
+int string_list_add_sort_uniq(struct string_list *sl, const char *str);
 int string_list_contains(struct string_list *sl, const char *str);
 void string_list_print_by_column(struct string_list *sl);
 
diff --git a/lib/stringlist.c b/lib/stringlist.c
index 8e92c1b207..719fecdaa4 100644
--- a/lib/stringlist.c
+++ b/lib/stringlist.c
@@ -60,6 +60,29 @@ int string_list_add_sorted(struct string_list *sl, const char *str)
 	return 0;
 }
 
+int string_list_add_sort_uniq(struct string_list *sl, const char *str)
+{
+	struct string_list *new, *entry = sl;
+
+	string_list_for_each_entry(entry, sl) {
+		int cmp = strcmp(entry->str, str);
+
+		if (cmp < 0)
+			continue;
+		if (cmp == 0)
+			return 0;
+
+		break;
+	}
+
+	new = xmalloc(sizeof(*new));
+	new->str = xstrdup(str);
+
+	list_add_tail(&new->list, &entry->list);
+
+	return 0;
+}
+
 int string_list_contains(struct string_list *sl, const char *str)
 {
 	struct string_list *entry;
-- 
2.29.2


_______________________________________________
barebox mailing list
barebox@lists.infradead.org
http://lists.infradead.org/mailman/listinfo/barebox


^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2021-03-05 11:05 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2021-03-05 11:03 [PATCH 1/3] stringlist: Add adding sort uniq Sascha Hauer
2021-03-05 11:03 ` [PATCH 2/3] stringlist: Initialize HEAD str member to NULL Sascha Hauer
2021-03-05 11:03 ` [PATCH 3/3] complete: Use string_list_add_sort_uniq() Sascha Hauer

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox