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
This commit is contained in:
Érico Rolim 2021-01-07 20:57:22 -03:00
parent 472337a477
commit 1e42b5527e
3 changed files with 16 additions and 0 deletions

View File

@ -39,6 +39,7 @@ int main()
struct str_array entries = { 0 }; struct str_array entries = { 0 };
read_entries_from_stream(&entries, delim, stdin); read_entries_from_stream(&entries, delim, stdin);
sort_entries(&entries);
filter_entries(&entries, NULL); filter_entries(&entries, NULL);
int tmp_fd; int tmp_fd;

View File

@ -32,6 +32,9 @@ void filter_entries(struct str_array *a, const struct str_array *m)
{ {
if (!m) { if (!m) {
a->ms = a->n; a->ms = a->n;
for (size_t i = 0; i < a->n; i++) {
a->m[i] = true;
}
return; 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) void print_entries(const struct str_array *a)
{ {
for (size_t i = 0; i < a->n; i++) { for (size_t i = 0; i < a->n; i++) {

View File

@ -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 *); void add_entry(struct str_array *, char *);
char *pop_entry(struct str_array *); char *pop_entry(struct str_array *);
void filter_entries(struct str_array *, const 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 *); void print_entries(const struct str_array *);
#endif #endif