Lely core libraries  2.2.5
cmp.h
Go to the documentation of this file.
1 
22 #ifndef LELY_UTIL_CMP_H_
23 #define LELY_UTIL_CMP_H_
24 
25 #include <lely/libc/strings.h>
26 #include <lely/libc/uchar.h>
27 #include <lely/util/util.h>
28 
29 #include <stddef.h>
30 #include <stdint.h>
31 #include <string.h>
32 #include <wchar.h>
33 
34 #ifndef LELY_UTIL_CMP_INLINE
35 #define LELY_UTIL_CMP_INLINE static inline
36 #endif
37 
38 #ifdef __cplusplus
39 extern "C" {
40 #endif
41 
50 typedef int cmp_t(const void *p1, const void *p2);
51 
52 #define LELY_UTIL_DEFINE_TYPE(name, type) \
53  LELY_UTIL_CMP_INLINE int name##_cmp(const void *p1, const void *p2);
54 #include <lely/util/def/type.def>
55 LELY_UTIL_DEFINE_TYPE(ptr, )
56 LELY_UTIL_DEFINE_TYPE(str, )
57 LELY_UTIL_DEFINE_TYPE(str_case, )
58 #undef LELY_UTIL_DEFINE_TYPE
59 
60 #define LELY_UTIL_DEFINE_TYPE(name, type) \
61  inline int name##_cmp(const void *p1, const void *p2) \
62  { \
63  if (p1 == p2) \
64  return 0; \
65 \
66  if (!p1) \
67  return -1; \
68  if (!p2) \
69  return 1; \
70 \
71  type v1 = *(const type *)p1; \
72  type v2 = *(const type *)p2; \
73  return (v2 < v1) - (v1 < v2); \
74  }
75 #include <lely/util/def/type.def>
76 #undef LELY_UTIL_DEFINE_TYPE
77 
78 inline int
79 ptr_cmp(const void *p1, const void *p2)
80 {
81  uintptr_t v1 = (uintptr_t)p1;
82  uintptr_t v2 = (uintptr_t)p2;
83  return (v2 < v1) - (v1 < v2);
84 }
85 
86 inline int
87 str_cmp(const void *p1, const void *p2)
88 {
89  if (p1 == p2)
90  return 0;
91 
92  if (!p1)
93  return -1;
94  if (!p2)
95  return 1;
96 
97  return strcmp((const char *)p1, (const char *)p2);
98 }
99 
100 inline int
101 str_case_cmp(const void *p1, const void *p2)
102 {
103  if (p1 == p2)
104  return 0;
105 
106  if (!p1)
107  return -1;
108  if (!p2)
109  return 1;
110 
111  return strcasecmp((const char *)p1, (const char *)p2);
112 }
113 
114 #ifdef __cplusplus
115 }
116 #endif
117 
118 #endif // !LELY_UTIL_CMP_H_
This header file is part of the C11 and POSIX compatibility library; it includes <strings.h>, if it exists, and defines any missing functionality.
This header file is part of the C11 and POSIX compatibility library; it includes <string.h> and defines any missing functionality.
int cmp_t(const void *p1, const void *p2)
The type of a generic comparison function.
Definition: cmp.h:50
This header file is part of the C11 and POSIX compatibility library; it includes <stddef.h> and defines any missing functionality.
This header file is part of the C11 and POSIX compatibility library; it includes <uchar.h>, if it exists, and defines any missing functionality.
This header file is part of the C11 and POSIX compatibility library; it includes <stdint.h> and defines any missing functionality.
This is the public header file of the utilities library.