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)";
86 memdup(const void *mem, size_t nmemb, size_t size)
88 void *p = malloc(nmemb * size);
90 memcpy(p, mem, nmemb * size);
94 #define MIN(a, b) ((a) < (b) ? (a) : (b))
95 #define MIN3(a, b, c) MIN(MIN((a), (b)), (c))
96 #define MAX(a, b) ((a) > (b) ? (a) : (b))
97 #define MAX3(a, b, c) MAX(MAX((a), (b)), (c))
99 /* Compiler Attributes */
101 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
102 # define XKB_EXPORT __attribute__((visibility("default")))
103 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
104 # define XKB_EXPORT __global
105 #else /* not gcc >= 4 and not Sun Studio >= 8 */
109 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
110 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
111 #else /* not gcc >= 2.3 */
112 # define ATTR_PRINTF(x,y)
115 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
116 || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
117 # define ATTR_NORETURN __attribute__((__noreturn__))
119 # define ATTR_NORETURN
122 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
123 #define ATTR_MALLOC __attribute__((__malloc__))
128 #if defined(__GNUC__) && (__GNUC__ >= 4)
129 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
131 # define ATTR_NULL_SENTINEL
132 #endif /* GNUC >= 4 */