rules: move the matcher result handling to the caller
[platform/upstream/libxkbcommon.git] / src / utils.h
1 /*
2  * Copyright © 2012 Ran Benita <ran234@gmail.com>
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining a
5  * copy of this software and associated documentation files (the "Software"),
6  * to deal in the Software without restriction, including without limitation
7  * the rights to use, copy, modify, merge, publish, distribute, sublicense,
8  * and/or sell copies of the Software, and to permit persons to whom the
9  * Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice (including the next
12  * paragraph) shall be included in all copies or substantial portions of the
13  * Software.
14  *
15  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
16  * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
17  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
18  * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
19  * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
20  * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
21  * DEALINGS IN THE SOFTWARE.
22  */
23
24 #ifndef UTILS_H
25 #define UTILS_H 1
26
27 #include <errno.h>
28 #include <inttypes.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <string.h>
32
33 #include "darray.h"
34
35 #define STATIC_ASSERT(expr, message) do { \
36     switch (0) { case 0: case (expr): ; } \
37 } while (0)
38
39 char
40 to_lower(char c);
41
42 int
43 istrcmp(const char *a, const char *b);
44
45 int
46 istrncmp(const char *a, const char *b, size_t n);
47
48 static inline bool
49 streq(const char *s1, const char *s2)
50 {
51     return strcmp(s1, s2) == 0;
52 }
53
54 static inline bool
55 streq_not_null(const char *s1, const char *s2)
56 {
57     if (!s1 || !s2)
58         return false;
59     return streq(s1, s2);
60 }
61
62 static inline bool
63 istreq(const char *s1, const char *s2)
64 {
65     return istrcmp(s1, s2) == 0;
66 }
67
68 static inline bool
69 istreq_prefix(const char *s1, const char *s2)
70 {
71     return istrncmp(s1, s2, strlen(s1)) == 0;
72 }
73
74 static inline char *
75 strdup_safe(const char *s)
76 {
77     return s ? strdup(s) : NULL;
78 }
79
80 static inline size_t
81 strlen_safe(const char *s)
82 {
83     return s ? strlen(s) : 0;
84 }
85
86 static inline bool
87 isempty(const char *s)
88 {
89     return s == NULL || s[0] == '\0';
90 }
91
92 static inline const char *
93 strnull(const char *s)
94 {
95     return s ? s : "(null)";
96 }
97
98 static inline const char *
99 strempty(const char *s)
100 {
101     return s ? s : "";
102 }
103
104 static inline void *
105 memdup(const void *mem, size_t nmemb, size_t size)
106 {
107     void *p = calloc(nmemb, size);
108     if (p)
109         memcpy(p, mem, nmemb * size);
110     return p;
111 }
112
113 static inline int
114 min(int misc, int other)
115 {
116     return (misc < other) ? misc : other;
117 }
118
119 static inline int
120 max(int misc, int other)
121 {
122     return (misc > other) ? misc : other;
123 }
124
125 /* ctype.h is locale-dependent and has other oddities. */
126 static inline bool
127 is_space(char ch)
128 {
129     return ch == ' ' || (ch >= '\t' && ch <= '\r');
130 }
131
132 static inline bool
133 is_alpha(char ch)
134 {
135     return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
136 }
137
138 static inline bool
139 is_digit(char ch)
140 {
141     return ch >= '0' && ch <= '9';
142 }
143
144 static inline bool
145 is_alnum(char ch)
146 {
147     return is_alpha(ch) || is_digit(ch);
148 }
149
150 static inline bool
151 is_xdigit(char ch)
152 {
153     return
154         (ch >= '0' && ch <= '9') ||
155         (ch >= 'a' && ch <= 'f') ||
156         (ch >= 'A' && ch <= 'F');
157 }
158
159 static inline bool
160 is_graph(char ch)
161 {
162     /* See table in ascii(7). */
163     return ch >= '!' && ch <= '~';
164 }
165
166 /*
167  * Return the bit position of the most significant bit.
168  * Note: this is 1-based! It's more useful this way, and returns 0 when
169  * mask is all 0s.
170  */
171 static inline unsigned
172 msb_pos(uint32_t mask)
173 {
174     unsigned pos = 0;
175     while (mask) {
176         pos++;
177         mask >>= 1u;
178     }
179     return pos;
180 }
181
182 static inline int
183 one_bit_set(uint32_t x)
184 {
185     return x && (x & (x - 1)) == 0;
186 }
187
188 bool
189 map_file(FILE *file, char **string_out, size_t *size_out);
190
191 void
192 unmap_file(char *string, size_t size);
193
194 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
195
196 #define MIN(a, b) ((a) < (b) ? (a) : (b))
197 #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
198 #define MAX(a, b) ((a) > (b) ? (a) : (b))
199 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
200
201 /* Round up @a so it's divisible by @b. */
202 #define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b))
203
204 #if defined(HAVE_SECURE_GETENV)
205 # define secure_getenv secure_getenv
206 #elif defined(HAVE___SECURE_GETENV)
207 # define secure_getenv __secure_getenv
208 #else
209 # define secure_getenv getenv
210 #endif
211
212 #if defined(HAVE___BUILTIN_EXPECT)
213 # define likely(x)   __builtin_expect(!!(x), 1)
214 # define unlikely(x) __builtin_expect(!!(x), 0)
215 #else
216 # define likely(x)   (x)
217 # define unlikely(x) (x)
218 #endif
219
220 /* Compiler Attributes */
221
222 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
223 # define XKB_EXPORT      __attribute__((visibility("default")))
224 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
225 # define XKB_EXPORT      __global
226 #else /* not gcc >= 4 and not Sun Studio >= 8 */
227 # define XKB_EXPORT
228 #endif
229
230 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
231 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
232 #else /* not gcc >= 2.3 */
233 # define ATTR_PRINTF(x,y)
234 #endif
235
236 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
237     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
238 # define ATTR_NORETURN __attribute__((__noreturn__))
239 #else
240 # define ATTR_NORETURN
241 #endif /* GNUC  */
242
243 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
244 #define ATTR_MALLOC  __attribute__((__malloc__))
245 #else
246 #define ATTR_MALLOC
247 #endif
248
249 #if defined(__GNUC__) && (__GNUC__ >= 4)
250 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
251 #else
252 # define ATTR_NULL_SENTINEL
253 #endif /* GNUC >= 4 */
254
255 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 295)
256 #define ATTR_PACKED  __attribute__((__packed__))
257 #else
258 #define ATTR_PACKED
259 #endif
260
261 #endif /* UTILS_H */