2 * Copyright © 2012 Ran Benita <ran234@gmail.com>
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:
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
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.
35 * We sometimes malloc strings and then expose them as const char*'s. This
36 * macro is used when we free these strings in order to avoid -Wcast-qual
39 #define UNCONSTIFY(const_ptr) ((void *) (uintptr_t) (const_ptr))
42 streq(const char *s1, const char *s2)
44 return strcmp(s1, s2) == 0;
48 streq_not_null(const char *s1, const char *s2)
56 istreq(const char *s1, const char *s2)
58 return strcasecmp(s1, s2) == 0;
62 istreq_prefix(const char *s1, const char *s2)
64 return strncasecmp(s1, s2, strlen(s1)) == 0;
68 strdup_safe(const char *s)
70 return s ? strdup(s) : NULL;
74 isempty(const char *s)
76 return s == NULL || s[0] == '\0';
79 static inline const char *
80 strnull(const char *s)
82 return s ? s : "(null)";
85 #define MIN(a, b) ((a) < (b) ? (a) : (b))
86 #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
87 #define MAX(a, b) ((a) > (b) ? (a) : (b))
88 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
90 /* Compiler Attributes */
92 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
93 # define XKB_EXPORT __attribute__((visibility("default")))
94 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
95 # define XKB_EXPORT __global
96 #else /* not gcc >= 4 and not Sun Studio >= 8 */
100 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
101 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
102 #else /* not gcc >= 2.3 */
103 # define ATTR_PRINTF(x,y)
106 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
107 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
108 # define ATTR_NORETURN __attribute__((__noreturn__))
110 # define ATTR_NORETURN
113 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
114 #define ATTR_MALLOC __attribute__((__malloc__))
119 #if defined(__GNUC__) && (__GNUC__ >= 4)
120 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
122 # define ATTR_NULL_SENTINEL
123 #endif /* GNUC >= 4 */