New "string_array" data structure for working with arrays of strings.
[pspp-builds.git] / src / libpspp / string-array.h
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2010 Free Software Foundation, Inc.
3
4    This program is free software: you can redistribute it and/or modify
5    it under the terms of the GNU General Public License as published by
6    the Free Software Foundation, either version 3 of the License, or
7    (at your option) any later version.
8
9    This program is distributed in the hope that it will be useful,
10    but WITHOUT ANY WARRANTY; without even the implied warranty of
11    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12    GNU General Public License for more details.
13
14    You should have received a copy of the GNU General Public License
15    along with this program.  If not, see <http://www.gnu.org/licenses/>. */
16
17 #ifndef LIBPSPP_STRING_ARRAY_H
18 #define LIBPSPP_STRING_ARRAY_H
19
20 #include <stdbool.h>
21 #include <stddef.h>
22
23 /* An unordered array of strings.
24
25    Not opaque by any means. */
26 struct string_array
27   {
28     char **strings;
29     size_t n;
30     size_t allocated;
31   };
32
33 /* Suitable for use as the initializer for a string_array named ARRAY.  Typical
34    usage:
35        struct string_array array = STRING_ARRAY_INITIALIZER (array);
36    STRING_ARRAY_INITIALIZER is an alternative to calling string_array_init. */
37 #define STRING_ARRAY_INITIALIZER(ARRAY) { NULL, 0, 0 }
38
39 void string_array_init (struct string_array *);
40 void string_array_clone (struct string_array *, const struct string_array *);
41 void string_array_swap (struct string_array *, struct string_array *);
42 void string_array_destroy (struct string_array *);
43
44 static inline size_t string_array_count (const struct string_array *);
45 static inline bool string_array_is_empty (const struct string_array *);
46
47 bool string_array_contains (const struct string_array *, const char *);
48 size_t string_array_find (const struct string_array *, const char *);
49
50 void string_array_append (struct string_array *, const char *);
51 void string_array_append_nocopy (struct string_array *, char *);
52 void string_array_insert (struct string_array *, const char *, size_t before);
53 void string_array_insert_nocopy (struct string_array *, char *, size_t before);
54 void string_array_delete (struct string_array *, size_t idx);
55 char *string_array_delete_nofree (struct string_array *, size_t idx);
56
57 void string_array_clear (struct string_array *);
58
59 void string_array_terminate_null (struct string_array *);
60 void string_array_shrink (struct string_array *);
61
62 void string_array_sort (struct string_array *);
63
64 char *string_array_join (const struct string_array *, const char *separator);
65
66 /* Macros for conveniently iterating through a string_array, e.g. to print all
67    of the strings in "my_array":
68
69    const char *string;
70    size_t idx;
71
72    STRING_ARRAY_FOR_EACH (string, idx, &my_array)
73      puts (string);
74 */
75 #define STRING_ARRAY_FOR_EACH(STRING, IDX, ARRAY)                  \
76   for ((IDX) = 0;                                                  \
77        ((IDX) < (ARRAY)->n                                         \
78         ? ((STRING) = (ARRAY)->strings[IDX], true)                 \
79         : false);                                                  \
80        (IDX)++)
81 \f
82 /* Returns the number of strings currently in ARRAY. */
83 static inline size_t
84 string_array_count (const struct string_array *array)
85 {
86   return array->n;
87 }
88
89 /* Returns true if ARRAY currently contains no strings, false otherwise. */
90 static inline bool
91 string_array_is_empty (const struct string_array *array)
92 {
93   return array->n == 0;
94 }
95
96 #endif /* libpspp/string-array.h */