diff options
author | Sascha Hauer <s.hauer@pengutronix.de> | 2021-02-02 21:11:56 +0100 |
---|---|---|
committer | Sascha Hauer <s.hauer@pengutronix.de> | 2021-03-16 11:31:22 +0100 |
commit | cc9ea0ae10f46f8fdec14ae26c19ed490fd3b71c (patch) | |
tree | 8b3a71abaae7d9109454dd9a2746aac13efb43e3 /lib/stringlist.c | |
parent | 186838e09de277ac766352b0f37ba8172ebc5f0e (diff) | |
download | barebox-cc9ea0ae10f46f8fdec14ae26c19ed490fd3b71c.tar.gz barebox-cc9ea0ae10f46f8fdec14ae26c19ed490fd3b71c.tar.xz |
stringlist: Add adding sort uniq
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>
Diffstat (limited to 'lib/stringlist.c')
-rw-r--r-- | lib/stringlist.c | 23 |
1 files changed, 23 insertions, 0 deletions
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; |