util: use standard name for strcmp()
[mesa.git] / src / util / u_string.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Platform independent functions for string manipulation.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #ifndef U_STRING_H_
36 #define U_STRING_H_
37
38 #if !defined(XF86_LIBC_H)
39 #include <stdio.h>
40 #endif
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <stdarg.h>
44 #include <string.h>
45
46 #include "util/macros.h" // PRINTFLIKE
47
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #ifndef _GNU_SOURCE
54
55 #define strchrnul util_strchrnul
56 static inline char *
57 util_strchrnul(const char *s, char c)
58 {
59 for (; *s && *s != c; ++s);
60
61 return (char *)s;
62 }
63
64 #endif
65
66 #ifdef _WIN32
67
68 static inline int
69 util_vsnprintf(char *str, size_t size, const char *format, va_list ap)
70 {
71 /* We need to use _vscprintf to calculate the length as vsnprintf returns -1
72 * if the number of characters to write is greater than count.
73 */
74 va_list ap_copy;
75 int ret;
76 va_copy(ap_copy, ap);
77 ret = _vsnprintf(str, size, format, ap);
78 if (ret < 0) {
79 ret = _vscprintf(format, ap_copy);
80 }
81 va_end(ap_copy);
82 return ret;
83 }
84
85 static inline int
86 PRINTFLIKE(3, 4)
87 util_snprintf(char *str, size_t size, const char *format, ...)
88 {
89 va_list ap;
90 int ret;
91 va_start(ap, format);
92 ret = util_vsnprintf(str, size, format, ap);
93 va_end(ap);
94 return ret;
95 }
96
97 static inline void
98 PRINTFLIKE(2, 3)
99 util_sprintf(char *str, const char *format, ...)
100 {
101 va_list ap;
102 va_start(ap, format);
103 util_vsnprintf(str, (size_t)-1, format, ap);
104 va_end(ap);
105 }
106
107 static inline int
108 util_vasprintf(char **ret, const char *format, va_list ap)
109 {
110 va_list ap_copy;
111
112 /* Compute length of output string first */
113 va_copy(ap_copy, ap);
114 int r = util_vsnprintf(NULL, 0, format, ap_copy);
115 va_end(ap_copy);
116
117 if (r < 0)
118 return -1;
119
120 *ret = (char *) malloc(r + 1);
121 if (!*ret)
122 return -1;
123
124 /* Print to buffer */
125 return util_vsnprintf(*ret, r + 1, format, ap);
126 }
127
128 #define strncat util_strncat
129 static inline char*
130 util_strncat(char *dst, const char *src, size_t n)
131 {
132 char *p = dst + strlen(dst);
133 const char *q = src;
134 size_t i;
135
136 for (i = 0; i < n && *q != '\0'; ++i)
137 *p++ = *q++;
138 *p = '\0';
139
140 return dst;
141 }
142
143 #define strcmp util_strcmp
144 static inline int
145 util_strcmp(const char *s1, const char *s2)
146 {
147 unsigned char u1, u2;
148
149 while (1) {
150 u1 = (unsigned char) *s1++;
151 u2 = (unsigned char) *s2++;
152 if (u1 != u2)
153 return u1 - u2;
154 if (u1 == '\0')
155 return 0;
156 }
157 return 0;
158 }
159
160 #define strncmp util_strncmp
161 static inline int
162 util_strncmp(const char *s1, const char *s2, size_t n)
163 {
164 unsigned char u1, u2;
165
166 while (n-- > 0) {
167 u1 = (unsigned char) *s1++;
168 u2 = (unsigned char) *s2++;
169 if (u1 != u2)
170 return u1 - u2;
171 if (u1 == '\0')
172 return 0;
173 }
174 return 0;
175 }
176
177
178
179 #define strcasecmp stricmp
180 #define strdup _strdup
181
182 #else
183
184 #define util_vsnprintf vsnprintf
185 #define util_snprintf snprintf
186 #define util_vasprintf vasprintf
187 #define util_sprintf sprintf
188
189 #endif
190
191
192 #ifdef __cplusplus
193 }
194 #endif
195
196 #endif /* U_STRING_H_ */