X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=src%2Flibpspp%2Fstringi-set.c;h=a7ae6994711606eb0c5a2e5acec67ecb124ea71c;hb=04ef14be963b955d3b33bb85149759b75a32c34e;hp=36745742d477350ffbb4f08cd5d8b7eb6ab94d39;hpb=6b1a7420704720085826e2b5f9fedea3ca8239de;p=pspp diff --git a/src/libpspp/stringi-set.c b/src/libpspp/stringi-set.c index 36745742d4..a7ae699471 100644 --- a/src/libpspp/stringi-set.c +++ b/src/libpspp/stringi-set.c @@ -19,12 +19,13 @@ #include -#include +#include "libpspp/stringi-set.h" #include #include -#include +#include "libpspp/cast.h" +#include "libpspp/hash-functions.h" #include "gl/xalloc.h" @@ -226,6 +227,54 @@ stringi_set_subtract (struct stringi_set *a, const struct stringi_set *b) stringi_set_delete__ (a, node->string, node->hmap_node.hash); } } + +/* Allocates and returns an array that points to each of the strings in SET. + The caller must not free or modify any of the strings. Removing a string + from SET invalidates the corresponding element of the returned array. The + caller it is responsible for freeing the returned array itself (with + free()). + + The returned array is in the same order as observed by stringi_set_first() + and stringi_set_next(), that is, no particular order. */ +char ** +stringi_set_get_array (const struct stringi_set *set) +{ + const struct stringi_set_node *node; + const char *s; + char **array; + size_t i; + + array = xnmalloc (stringi_set_count (set), sizeof *array); + + i = 0; + STRINGI_SET_FOR_EACH (s, node, set) + array[i++] = CONST_CAST (char *, s); + + return array; +} + +static int +compare_strings (const void *a_, const void *b_) +{ + const char *const *a = a_; + const char *const *b = b_; + return strcasecmp (*a, *b); +} + +/* Allocates and returns an array that points to each of the strings in SET. + The caller must not free or modify any of the strings. Removing a string + from SET invalidates the corresponding element of the returned array. The + caller it is responsible for freeing the returned array itself (with + free()). + + The returned array is ordered according to strcasecmp(). */ +char ** +stringi_set_get_sorted_array (const struct stringi_set *set) +{ + char **array = stringi_set_get_array (set); + qsort (array, stringi_set_count (set), sizeof *array, compare_strings); + return array; +} /* Internal functions. */