Widen keycode range to 8/255 if possible (bug #63390)
[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 bool
76 isempty(const char *s)
77 {
78     return s == NULL || s[0] == '\0';
79 }
80
81 static inline const char *
82 strnull(const char *s)
83 {
84     return s ? s : "(null)";
85 }
86
87 static inline void *
88 memdup(const void *mem, size_t nmemb, size_t size)
89 {
90     void *p = malloc(nmemb * size);
91     if (p)
92         memcpy(p, mem, nmemb * size);
93     return p;
94 }
95
96 static inline int
97 min(int misc, int other)
98 {
99     return (misc < other) ? misc : other;
100 }
101
102 static inline int
103 max(int misc, int other)
104 {
105     return (misc > other) ? misc : other;
106 }
107
108 bool
109 map_file(FILE *file, const char **string_out, size_t *size_out);
110
111 void
112 unmap_file(const char *str, size_t size);
113
114 #define ARRAY_SIZE(arr) ((sizeof(arr) / sizeof(*(arr))))
115
116 #define MIN(a, b) ((a) < (b) ? (a) : (b))
117 #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
118 #define MAX(a, b) ((a) > (b) ? (a) : (b))
119 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
120
121 /* Compiler Attributes */
122
123 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
124 # define XKB_EXPORT      __attribute__((visibility("default")))
125 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
126 # define XKB_EXPORT      __global
127 #else /* not gcc >= 4 and not Sun Studio >= 8 */
128 # define XKB_EXPORT
129 #endif
130
131 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
132 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
133 #else /* not gcc >= 2.3 */
134 # define ATTR_PRINTF(x,y)
135 #endif
136
137 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
138     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
139 # define ATTR_NORETURN __attribute__((__noreturn__))
140 #else
141 # define ATTR_NORETURN
142 #endif /* GNUC  */
143
144 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
145 #define ATTR_MALLOC  __attribute__((__malloc__))
146 #else
147 #define ATTR_MALLOC
148 #endif
149
150 #if defined(__GNUC__) && (__GNUC__ >= 4)
151 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
152 #else
153 # define ATTR_NULL_SENTINEL
154 #endif /* GNUC >= 4 */
155
156 #endif /* UTILS_H */