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 #include "libpspp/str.h"
23
24 /* An unordered array of strings.
25
26 Not opaque by any means. */
27 struct string_array
28 {
29 char **strings;
30 size_t n;
31 size_t allocated;
32 };
33
34 /* Suitable for use as the initializer for a string_array named ARRAY. Typical
35 usage:
36 struct string_array array = STRING_ARRAY_INITIALIZER;
37 STRING_ARRAY_INITIALIZER is an alternative to calling string_array_init. */
38 #define STRING_ARRAY_INITIALIZER { NULL, 0, 0 }
39
40 void string_array_init (struct string_array *);
41 void string_array_clone (struct string_array *, const struct string_array *);
42 void string_array_swap (struct string_array *, struct string_array *);
43 void string_array_destroy (struct string_array *);
44
45 static inline size_t string_array_count (const struct string_array *);
46 static inline bool string_array_is_empty (const struct string_array *);
47
48 bool string_array_contains (const struct string_array *, const char *);
49 size_t string_array_find (const struct string_array *, const char *);
50
51 void string_array_append (struct string_array *, const char *);
52 void string_array_append_nocopy (struct string_array *, char *);
53 void string_array_insert (struct string_array *, const char *, size_t before);
54 void string_array_insert_nocopy (struct string_array *, char *, size_t before);
55 void string_array_delete (struct string_array *, size_t idx);
56 char *string_array_delete_nofree (struct string_array *, size_t idx);
57
58 void string_array_clear (struct string_array *);
59
60 void string_array_terminate_null (struct string_array *);
61 void string_array_shrink (struct string_array *);
62
63 void string_array_sort (struct string_array *);
64
65 void string_array_parse (struct string_array *, struct substring string,
66 struct substring delimiters);
67 char *string_array_join (const struct string_array *, const char *separator);
68
69 /* Macros for conveniently iterating through a string_array, e.g. to print all
70 of the strings in "my_array":
71
72 const char *string;
73 size_t idx;
74
75 STRING_ARRAY_FOR_EACH (string, idx, &my_array)
76 puts (string);
77 */
78 #define STRING_ARRAY_FOR_EACH(STRING, IDX, ARRAY) \
79 for ((IDX) = 0; \
80 ((IDX) < (ARRAY)->n \
81 ? ((STRING) = (ARRAY)->strings[IDX], true) \
82 : false); \
83 (IDX)++)
84
85 /* Returns the number of strings currently in ARRAY. */
86 static inline size_t
string_array_count(const struct string_array * array)87 string_array_count (const struct string_array *array)
88 {
89 return array->n;
90 }
91
92 /* Returns true if ARRAY currently contains no strings, false otherwise. */
93 static inline bool
string_array_is_empty(const struct string_array * array)94 string_array_is_empty (const struct string_array *array)
95 {
96 return array->n == 0;
97 }
98
99 #endif /* libpspp/string-array.h */
100