utils: Replace DEC copyright with Ran's
[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 <stdbool.h>
28 #include <string.h>
29
30 /*
31  * We sometimes malloc strings and then expose them as const char*'s. This
32  * macro is used when we free these strings in order to avoid -Wcast-qual
33  * errors.
34  */
35 #define UNCONSTIFY(const_ptr)  ((void *) (uintptr_t) (const_ptr))
36
37 static inline bool
38 streq(const char *s1, const char *s2)
39 {
40     return strcmp(s1, s2) == 0;
41 }
42
43 static inline bool
44 streq_not_null(const char *s1, const char *s2)
45 {
46     if (!s1 || !s2)
47         return false;
48     return streq(s1, s2);
49 }
50
51 static inline bool
52 istreq(const char *s1, const char *s2)
53 {
54     return strcasecmp(s1, s2) == 0;
55 }
56
57 static inline bool
58 istreq_prefix(const char *s1, const char *s2)
59 {
60     return strncasecmp(s1, s2, strlen(s1)) == 0;
61 }
62
63 static inline char *
64 strdup_safe(const char *s)
65 {
66     return s ? strdup(s) : NULL;
67 }
68
69 static inline bool
70 isempty(const char *s)
71 {
72     return s == NULL || s[0] == '\0';
73 }
74
75 static inline const char *
76 strnull(const char *s)
77 {
78     return s ? s : "(null)";
79 }
80
81 #define MIN(a, b) ((a) < (b) ? (a) : (b))
82 #define MAX(a, b) ((a) > (b) ? (a) : (b))
83
84 /* Compiler Attributes */
85
86 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
87 # define XKB_EXPORT      __attribute__((visibility("default")))
88 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
89 # define XKB_EXPORT      __global
90 #else /* not gcc >= 4 and not Sun Studio >= 8 */
91 # define XKB_EXPORT
92 #endif
93
94 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
95 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
96 #else /* not gcc >= 2.3 */
97 # define ATTR_PRINTF(x,y)
98 #endif
99
100 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
101     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
102 # define ATTR_NORETURN __attribute__((__noreturn__))
103 #else
104 # define ATTR_NORETURN
105 #endif /* GNUC  */
106
107 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
108 #define ATTR_MALLOC  __attribute__((__malloc__))
109 #else
110 #define ATTR_MALLOC
111 #endif
112
113 #if defined(__GNUC__) && (__GNUC__ >= 4)
114 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
115 #else
116 # define ATTR_NULL_SENTINEL
117 #endif /* GNUC >= 4 */
118
119 #endif /* UTILS_H */