1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2010 Free Software Foundation, Inc.
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.
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.
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/>. */
17 #ifndef LIBPSPP_STRING_ARRAY_H
18 #define LIBPSPP_STRING_ARRAY_H
22 #include "libpspp/str.h"
24 /* An unordered array of strings.
26 Not opaque by any means. */
34 /* Suitable for use as the initializer for a string_array named ARRAY. Typical
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 }
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 *);
45 static inline size_t string_array_count (const struct string_array *);
46 static inline bool string_array_is_empty (const struct string_array *);
48 bool string_array_contains (const struct string_array *, const char *);
49 size_t string_array_find (const struct string_array *, const char *);
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);
58 void string_array_clear (struct string_array *);
60 void string_array_terminate_null (struct string_array *);
61 void string_array_shrink (struct string_array *);
63 void string_array_sort (struct string_array *);
64 void string_array_uniq (struct string_array *);
66 bool string_array_equal (const struct string_array *,
67 const struct string_array *);
68 bool string_array_equal_case (const struct string_array *,
69 const struct string_array *);
71 void string_array_parse (struct string_array *, struct substring string,
72 struct substring delimiters);
73 char *string_array_join (const struct string_array *, const char *separator);
75 /* Macros for conveniently iterating through a string_array, e.g. to print all
76 of the strings in "my_array":
81 STRING_ARRAY_FOR_EACH (string, idx, &my_array)
84 #define STRING_ARRAY_FOR_EACH(STRING, IDX, ARRAY) \
87 ? ((STRING) = (ARRAY)->strings[IDX], true) \
91 /* Returns the number of strings currently in ARRAY. */
93 string_array_count (const struct string_array *array)
98 /* Returns true if ARRAY currently contains no strings, false otherwise. */
100 string_array_is_empty (const struct string_array *array)
102 return array->n == 0;
105 #endif /* libpspp/string-array.h */