state: fix mod_names_are_active
[platform/upstream/libxkbcommon.git] / src / utils.h
1 #ifndef UTILS_H
2 #define UTILS_H 1
3
4 /*\
5  *
6  *                          COPYRIGHT 1990
7  *                    DIGITAL EQUIPMENT CORPORATION
8  *                       MAYNARD, MASSACHUSETTS
9  *                        ALL RIGHTS RESERVED.
10  *
11  * THE INFORMATION IN THIS SOFTWARE IS SUBJECT TO CHANGE WITHOUT NOTICE AND
12  * SHOULD NOT BE CONSTRUED AS A COMMITMENT BY DIGITAL EQUIPMENT CORPORATION.
13  * DIGITAL MAKES NO REPRESENTATIONS ABOUT THE SUITABILITY OF THIS SOFTWARE
14  * FOR ANY PURPOSE.  IT IS SUPPLIED "AS IS" WITHOUT EXPRESS OR IMPLIED
15  * WARRANTY.
16  *
17  * IF THE SOFTWARE IS MODIFIED IN A MANNER CREATING DERIVATIVE COPYRIGHT
18  * RIGHTS, APPROPRIATE LEGENDS MAY BE PLACED ON THE DERIVATIVE WORK IN
19  * ADDITION TO THAT SET FORTH ABOVE.
20  *
21  * Permission to use, copy, modify, and distribute this software and its
22  * documentation for any purpose and without fee is hereby granted, provided
23  * that the above copyright notice appear in all copies and that both that
24  * copyright notice and this permission notice appear in supporting
25  * documentation, and that the name of Digital Equipment Corporation not be
26  * used in advertising or publicity pertaining to distribution of the
27  * software without specific, written prior permission.
28  \*/
29
30 #include <stdbool.h>
31 #include <string.h>
32
33 /*
34  * We sometimes malloc strings and then expose them as const char*'s. This
35  * macro is used when we free these strings in order to avoid -Wcast-qual
36  * errors.
37  */
38 #define UNCONSTIFY(const_ptr)  ((void *) (uintptr_t) (const_ptr))
39
40 static inline bool
41 streq(const char *s1, const char *s2)
42 {
43     return strcmp(s1, s2) == 0;
44 }
45
46 static inline bool
47 streq_not_null(const char *s1, const char *s2)
48 {
49     if (!s1 || !s2)
50         return false;
51     return streq(s1, s2);
52 }
53
54 static inline bool
55 istreq(const char *s1, const char *s2)
56 {
57     return strcasecmp(s1, s2) == 0;
58 }
59
60 static inline bool
61 istreq_prefix(const char *s1, const char *s2)
62 {
63     return strncasecmp(s1, s2, strlen(s1)) == 0;
64 }
65
66 static inline char *
67 strdup_safe(const char *s)
68 {
69     return s ? strdup(s) : NULL;
70 }
71
72 static inline bool
73 isempty(const char *s)
74 {
75     return s == NULL || s[0] == '\0';
76 }
77
78 #define MIN(a, b) ((a) < (b) ? (a) : (b))
79 #define MAX(a, b) ((a) > (b) ? (a) : (b))
80
81 /* Compiler Attributes */
82
83 #if defined(__GNUC__) && (__GNUC__ >= 4) && !defined(__CYGWIN__)
84 # define XKB_EXPORT      __attribute__((visibility("default")))
85 #elif defined(__SUNPRO_C) && (__SUNPRO_C >= 0x550)
86 # define XKB_EXPORT      __global
87 #else /* not gcc >= 4 and not Sun Studio >= 8 */
88 # define XKB_EXPORT
89 #endif
90
91 #if defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 203)
92 # define ATTR_PRINTF(x,y) __attribute__((__format__(__printf__, x, y)))
93 #else /* not gcc >= 2.3 */
94 # define ATTR_PRINTF(x,y)
95 #endif
96
97 #if (defined(__GNUC__) && ((__GNUC__ * 100 + __GNUC_MINOR__) >= 205)) \
98     || (defined(__SUNPRO_C) && (__SUNPRO_C >= 0x590))
99 # define ATTR_NORETURN __attribute__((__noreturn__))
100 #else
101 # define ATTR_NORETURN
102 #endif /* GNUC  */
103
104 #if (defined(__GNUC__) && (__GNUC__ * 100 + __GNUC_MINOR__) >= 296)
105 #define ATTR_MALLOC  __attribute__((__malloc__))
106 #else
107 #define ATTR_MALLOC
108 #endif
109
110 #if defined(__GNUC__) && (__GNUC__ >= 4)
111 # define ATTR_NULL_SENTINEL __attribute__((__sentinel__))
112 #else
113 # define ATTR_NULL_SENTINEL
114 #endif /* GNUC >= 4 */
115
116 #endif /* UTILS_H */