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