From 1e42b5527e14337290a376701ac564af52ac7b04 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89rico=20Rolim?= Date: Thu, 7 Jan 2021 20:57:22 -0300 Subject: [PATCH] Add sort_entries, make filter_entries(a, 0) set a.m[i]=true. - sorting the entries is just better UI - filter_entries() is used to initialize a.ms to a.n, but it can lead to weird things if all a.m members aren't also initialized --- browser.c | 1 + string-array.c | 14 ++++++++++++++ string-array.h | 1 + 3 files changed, 16 insertions(+) diff --git a/browser.c b/browser.c index 1e27edd..e3f404a 100644 --- a/browser.c +++ b/browser.c @@ -39,6 +39,7 @@ int main() struct str_array entries = { 0 }; read_entries_from_stream(&entries, delim, stdin); + sort_entries(&entries); filter_entries(&entries, NULL); int tmp_fd; diff --git a/string-array.c b/string-array.c index 4296e65..2661853 100644 --- a/string-array.c +++ b/string-array.c @@ -32,6 +32,9 @@ void filter_entries(struct str_array *a, const struct str_array *m) { if (!m) { a->ms = a->n; + for (size_t i = 0; i < a->n; i++) { + a->m[i] = true; + } return; } @@ -65,6 +68,17 @@ void filter_entries(struct str_array *a, const struct str_array *m) } } +#define VOIDTOCHAR(x) (*(const char **)x) +static int str_a_item_cmp(const void *a, const void *b) +{ + return strcmp(VOIDTOCHAR(a), VOIDTOCHAR(b)); +} + +void sort_entries(struct str_array *a) +{ + qsort(a->v, a->n, sizeof(*a->v), str_a_item_cmp); +} + void print_entries(const struct str_array *a) { for (size_t i = 0; i < a->n; i++) { diff --git a/string-array.h b/string-array.h index 6743ade..9685fdc 100644 --- a/string-array.h +++ b/string-array.h @@ -33,6 +33,7 @@ static inline char *get_entry_if_match(const struct str_array *a, size_t i) void add_entry(struct str_array *, char *); char *pop_entry(struct str_array *); void filter_entries(struct str_array *, const struct str_array *); +void sort_entries(struct str_array *); void print_entries(const struct str_array *); #endif