Organize src/ and test/ headers
[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 <inttypes.h>
28 #include <stdbool.h>
29 #include <string.h>
30 #include <strings.h>
31
32 #include "darray.h"
33
34 /*
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
37  * errors.
38  */
39 #define UNCONSTIFY(const_ptr)  ((void *) (uintptr_t) (const_ptr))
40
41 static inline bool
42 streq(const char *s1, const char *s2)
43 {
44     return strcmp(s1, s2) == 0;
45 }
46
47 static inline bool
48 streq_not_null(const char *s1, const char *s2)
49 {
50     if (!s1 || !s2)
51         return false;
52     return streq(s1, s2);
53 }
54
55 static inline bool
56 istreq(const char *s1, const char *s2)
57 {
58     return strcasecmp(s1, s2) == 0;
59 }
60
61 static inline bool
62 istreq_prefix(const char *s1, const char *s2)
63 {
64     return strncasecmp(s1, s2, strlen(s1)) == 0;
65 }
66
67 static inline char *
68 strdup_safe(const char *s)
69 {
70     return s ? strdup(s) : NULL;
71 }
72
73 static inline bool
74 isempty(const char *s)
75 {
76     return s == NULL || s[0] == '\0';
77 }
78
79 static inline const char *
80 strnull(const char *s)
81 {
82     return s ? s : "(null)";
83 }
84
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))
89
90 /* Compiler Attributes */
91
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 */
97 # define XKB_EXPORT
98 #endif
99
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)
104 #endif
105
106 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
107     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
108 # define ATTR_NORETURN __attribute__((__noreturn__))
109 #else
110 # define ATTR_NORETURN
111 #endif /* GNUC  */
112
113 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
114 #define ATTR_MALLOC  __attribute__((__malloc__))
115 #else
116 #define ATTR_MALLOC
117 #endif
118
119 #if defined(__GNUC__) && (__GNUC__ >= 4)
120 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
121 #else
122 # define ATTR_NULL_SENTINEL
123 #endif /* GNUC >= 4 */
124
125 #endif /* UTILS_H */