chiark / gitweb /
util: add new FOREACH_STRING() macro as syntactic sugar to iterate through a number...
[elogind.git] / src / shared / strv.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
10   systemd is free software; you can redistribute it and/or modify it
11   under the terms of the GNU Lesser General Public License as published by
12   the Free Software Foundation; either version 2.1 of the License, or
13   (at your option) any later version.
14
15   systemd is distributed in the hope that it will be useful, but
16   WITHOUT ANY WARRANTY; without even the implied warranty of
17   MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18   Lesser General Public License for more details.
19
20   You should have received a copy of the GNU Lesser General Public License
21   along with systemd; If not, see <http://www.gnu.org/licenses/>.
22 ***/
23
24 #include <stdarg.h>
25 #include <stdbool.h>
26
27 #include "util.h"
28
29 char *strv_find(char **l, const char *name) _pure_;
30 char *strv_find_prefix(char **l, const char *name) _pure_;
31
32 void strv_free(char **l);
33 DEFINE_TRIVIAL_CLEANUP_FUNC(char**, strv_free);
34 #define _cleanup_strv_free_ _cleanup_(strv_freep)
35
36 char **strv_copy(char * const *l);
37 unsigned strv_length(char * const *l) _pure_;
38
39 int strv_extend_strv(char ***a, char **b);
40 int strv_extend_strv_concat(char ***a, char **b, const char *suffix);
41 int strv_extend(char ***l, const char *value);
42 int strv_extendf(char ***l, const char *format, ...) _printf_(2,0);
43 int strv_push(char ***l, char *value);
44 int strv_consume(char ***l, char *value);
45
46 char **strv_remove(char **l, const char *s);
47 char **strv_uniq(char **l);
48
49 #define strv_contains(l, s) (!!strv_find((l), (s)))
50
51 char **strv_new(const char *x, ...) _sentinel_;
52 char **strv_new_ap(const char *x, va_list ap);
53
54 static inline const char* STRV_IFNOTNULL(const char *x) {
55         return x ? x : (const char *) -1;
56 }
57
58 static inline bool strv_isempty(char * const *l) {
59         return !l || !*l;
60 }
61
62 char **strv_split(const char *s, const char *separator);
63 char **strv_split_quoted(const char *s);
64 char **strv_split_newlines(const char *s);
65
66 char *strv_join(char **l, const char *separator);
67 char *strv_join_quoted(char **l);
68
69 char **strv_parse_nulstr(const char *s, size_t l);
70 char **strv_split_nulstr(const char *s);
71
72 bool strv_overlap(char **a, char **b) _pure_;
73
74 #define STRV_FOREACH(s, l)                      \
75         for ((s) = (l); (s) && *(s); (s)++)
76
77 #define STRV_FOREACH_BACKWARDS(s, l)            \
78         STRV_FOREACH(s, l)                      \
79                 ;                               \
80         for ((s)--; (l) && ((s) >= (l)); (s)--)
81
82 #define STRV_FOREACH_PAIR(x, y, l)               \
83         for ((x) = (l), (y) = (x+1); (x) && *(x) && *(y); (x) += 2, (y) = (x + 1))
84
85 char **strv_sort(char **l);
86 void strv_print(char **l);
87
88 #define STRV_MAKE(...) ((char**) ((const char*[]) { __VA_ARGS__, NULL }))
89
90 #define STRV_MAKE_EMPTY ((char*[1]) { NULL })
91
92 #define strv_from_stdarg_alloca(first)                          \
93         ({                                                      \
94                 char **_l;                                      \
95                                                                 \
96                 if (!first)                                     \
97                         _l = (char**) &first;                   \
98                 else {                                          \
99                         unsigned _n;                            \
100                         va_list _ap;                            \
101                                                                 \
102                         _n = 1;                                 \
103                         va_start(_ap, first);                   \
104                         while (va_arg(_ap, char*))              \
105                                 _n++;                           \
106                         va_end(_ap);                            \
107                                                                 \
108                         _l = newa(char*, _n+1);                 \
109                         _l[_n = 0] = (char*) first;             \
110                         va_start(_ap, first);                   \
111                         for (;;) {                              \
112                                 _l[++_n] = va_arg(_ap, char*);  \
113                                 if (!_l[_n])                    \
114                                         break;                  \
115                         }                                       \
116                         va_end(_ap);                            \
117                 }                                               \
118                 _l;                                             \
119         })
120
121 #define STR_IN_SET(x, ...) strv_contains(STRV_MAKE(__VA_ARGS__), x)
122
123 #define FOREACH_STRING(x, ...)                               \
124         for (char **_l = ({                                  \
125                 char **_ll = STRV_MAKE(__VA_ARGS__);         \
126                 x = _ll ? _ll[0] : NULL;                     \
127                 _ll;                                         \
128         });                                                  \
129         _l && *_l;                                           \
130         x = ({                                               \
131                 _l ++;                                       \
132                 _l[0];                                       \
133         }))