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