x11: don't iterate on empty batches
[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 #include <strings.h>
33
34 #include "darray.h"
35
36 /*
37  * We sometimes malloc strings and then expose them as const char*'s. This
38  * macro is used when we free these strings in order to avoid -Wcast-qual
39  * errors.
40  */
41 #define UNCONSTIFY(const_ptr)  ((void *) (uintptr_t) (const_ptr))
42
43 static inline bool
44 streq(const char *s1, const char *s2)
45 {
46     return strcmp(s1, s2) == 0;
47 }
48
49 static inline bool
50 streq_not_null(const char *s1, const char *s2)
51 {
52     if (!s1 || !s2)
53         return false;
54     return streq(s1, s2);
55 }
56
57 static inline bool
58 istreq(const char *s1, const char *s2)
59 {
60     return strcasecmp(s1, s2) == 0;
61 }
62
63 static inline bool
64 istreq_prefix(const char *s1, const char *s2)
65 {
66     return strncasecmp(s1, s2, strlen(s1)) == 0;
67 }
68
69 static inline char *
70 strdup_safe(const char *s)
71 {
72     return s ? strdup(s) : NULL;
73 }
74
75 static inline size_t
76 strlen_safe(const char *s)
77 {
78     return s ? strlen(s) : 0;
79 }
80
81 static inline bool
82 isempty(const char *s)
83 {
84     return s == NULL || s[0] == '\0';
85 }
86
87 static inline const char *
88 strnull(const char *s)
89 {
90     return s ? s : "(null)";
91 }
92
93 static inline const char *
94 strempty(const char *s)
95 {
96     return s ? s : "";
97 }
98
99 static inline void *
100 memdup(const void *mem, size_t nmemb, size_t size)
101 {
102     void *p = calloc(nmemb, size);
103     if (p)
104         memcpy(p, mem, nmemb * size);
105     return p;
106 }
107
108 static inline int
109 min(int misc, int other)
110 {
111     return (misc < other) ? misc : other;
112 }
113
114 static inline int
115 max(int misc, int other)
116 {
117     return (misc > other) ? misc : other;
118 }
119
120 /* ctype.h is locale-dependent and has other oddities. */
121 static inline bool
122 is_space(char ch)
123 {
124     return ch == ' ' || (ch >= '\t' && ch <= '\r');
125 }
126
127 static inline bool
128 is_alpha(char ch)
129 {
130     return (ch >= 'a' && ch <= 'z') || (ch >= 'A' && ch <= 'Z');
131 }
132
133 static inline bool
134 is_digit(char ch)
135 {
136     return ch >= '0' && ch <= '9';
137 }
138
139 static inline bool
140 is_alnum(char ch)
141 {
142     return is_alpha(ch) || is_digit(ch);
143 }
144
145 static inline bool
146 is_xdigit(char ch)
147 {
148     return
149         (ch >= '0' && ch <= '9') ||
150         (ch >= 'a' && ch <= 'f') ||
151         (ch >= 'A' && ch <= 'F');
152 }
153
154 static inline bool
155 is_graph(char ch)
156 {
157     /* See table in ascii(7). */
158     return ch >= '!' && ch <= '~';
159 }
160
161 /*
162  * Return the bit position of the most significant bit.
163  * Note: this is 1-based! It's more useful this way, and returns 0 when
164  * mask is all 0s.
165  */
166 static inline unsigned
167 msb_pos(uint32_t mask)
168 {
169     unsigned pos = 0;
170     while (mask) {
171         pos++;
172         mask >>= 1u;
173     }
174     return pos;
175 }
176
177 bool
178 map_file(FILE *file, const char **string_out, size_t *size_out);
179
180 void
181 unmap_file(const char *str, size_t size);
182
183 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
184
185 #define MIN(a, b) ((a) < (b) ? (a) : (b))
186 #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
187 #define MAX(a, b) ((a) > (b) ? (a) : (b))
188 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
189
190 /* Round up @a so it's divisible by @b. */
191 #define ROUNDUP(a, b) (((a) + (b) - 1) / (b) * (b))
192
193 #if defined(HAVE_SECURE_GETENV)
194 # define secure_getenv secure_getenv
195 #elif defined(HAVE___SECURE_GETENV)
196 # define secure_getenv __secure_getenv
197 #else
198 # define secure_getenv getenv
199 #endif
200
201 #if defined(HAVE___BUILTIN_EXPECT)
202 # define likely(x)   __builtin_expect(!!(x), 1)
203 # define unlikely(x) __builtin_expect(!!(x), 0)
204 #else
205 # define likely(x)   (x)
206 # define unlikely(x) (x)
207 #endif
208
209 /* Compiler Attributes */
210
211 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
212 # define XKB_EXPORT      __attribute__((visibility("default")))
213 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
214 # define XKB_EXPORT      __global
215 #else /* not gcc >= 4 and not Sun Studio >= 8 */
216 # define XKB_EXPORT
217 #endif
218
219 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
220 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
221 #else /* not gcc >= 2.3 */
222 # define ATTR_PRINTF(x,y)
223 #endif
224
225 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
226     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
227 # define ATTR_NORETURN __attribute__((__noreturn__))
228 #else
229 # define ATTR_NORETURN
230 #endif /* GNUC  */
231
232 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
233 #define ATTR_MALLOC  __attribute__((__malloc__))
234 #else
235 #define ATTR_MALLOC
236 #endif
237
238 #if defined(__GNUC__) && (__GNUC__ >= 4)
239 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
240 #else
241 # define ATTR_NULL_SENTINEL
242 #endif /* GNUC >= 4 */
243
244 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 295)
245 #define ATTR_PACKED  __attribute__((__packed__))
246 #else
247 #define ATTR_PACKED
248 #endif
249
250 #endif /* UTILS_H */