util: include stdlib.h in u_string.h to silence MinGW warning
[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
45 #include "util/macros.h" // PRINTFLIKE
46
47
48 #ifdef __cplusplus
49 extern "C" {
50 #endif
51
52 #ifdef _GNU_SOURCE
53
54 #define util_strchrnul strchrnul
55
56 #else
57
58 static inline char *
59 util_strchrnul(const char *s, char c)
60 {
61 for (; *s && *s != c; ++s);
62
63 return (char *)s;
64 }
65
66 #endif
67
68 #ifdef _WIN32
69
70 static inline int
71 util_vsnprintf(char *str, size_t size, const char *format, va_list ap)
72 {
73 /* We need to use _vscprintf to calculate the length as vsnprintf returns -1
74 * if the number of characters to write is greater than count.
75 */
76 va_list ap_copy;
77 int ret;
78 va_copy(ap_copy, ap);
79 ret = _vsnprintf(str, size, format, ap);
80 if (ret < 0) {
81 ret = _vscprintf(format, ap_copy);
82 }
83 return ret;
84 }
85
86 static inline int
87 PRINTFLIKE(3, 4)
88 util_snprintf(char *str, size_t size, const char *format, ...)
89 {
90 va_list ap;
91 int ret;
92 va_start(ap, format);
93 ret = util_vsnprintf(str, size, format, ap);
94 va_end(ap);
95 return ret;
96 }
97
98 static inline void
99 util_vsprintf(char *str, const char *format, va_list ap)
100 {
101 util_vsnprintf(str, (size_t)-1, format, ap);
102 }
103
104 static inline void
105 PRINTFLIKE(2, 3)
106 util_sprintf(char *str, const char *format, ...)
107 {
108 va_list ap;
109 va_start(ap, format);
110 util_vsnprintf(str, (size_t)-1, format, ap);
111 va_end(ap);
112 }
113
114 static inline int
115 util_vasprintf(char **ret, const char *format, va_list ap)
116 {
117 va_list ap_copy;
118
119 /* Compute length of output string first */
120 va_copy(ap_copy, ap);
121 int r = util_vsnprintf(NULL, 0, format, ap);
122 va_end(ap_copy);
123
124 if (r < 0)
125 return -1;
126
127 *ret = (char *) malloc(r + 1);
128 if (!ret)
129 return -1;
130
131 /* Print to buffer */
132 return util_vsnprintf(*ret, r + 1, format, ap);
133 }
134
135 static inline char *
136 util_strchr(const char *s, char c)
137 {
138 char *p = util_strchrnul(s, c);
139
140 return *p ? p : NULL;
141 }
142
143 static inline char*
144 util_strncat(char *dst, const char *src, size_t n)
145 {
146 char *p = dst + strlen(dst);
147 const char *q = src;
148 size_t i;
149
150 for (i = 0; i < n && *q != '\0'; ++i)
151 *p++ = *q++;
152 *p = '\0';
153
154 return dst;
155 }
156
157 static inline int
158 util_strcmp(const char *s1, const char *s2)
159 {
160 unsigned char u1, u2;
161
162 while (1) {
163 u1 = (unsigned char) *s1++;
164 u2 = (unsigned char) *s2++;
165 if (u1 != u2)
166 return u1 - u2;
167 if (u1 == '\0')
168 return 0;
169 }
170 return 0;
171 }
172
173 static inline int
174 util_strncmp(const char *s1, const char *s2, size_t n)
175 {
176 unsigned char u1, u2;
177
178 while (n-- > 0) {
179 u1 = (unsigned char) *s1++;
180 u2 = (unsigned char) *s2++;
181 if (u1 != u2)
182 return u1 - u2;
183 if (u1 == '\0')
184 return 0;
185 }
186 return 0;
187 }
188
189 static inline char *
190 util_strstr(const char *haystack, const char *needle)
191 {
192 const char *p = haystack;
193 size_t len = strlen(needle);
194
195 for (; (p = util_strchr(p, *needle)) != 0; p++) {
196 if (util_strncmp(p, needle, len) == 0) {
197 return (char *)p;
198 }
199 }
200 return NULL;
201 }
202
203
204 #define util_strcasecmp stricmp
205
206 #else
207
208 #define util_vsnprintf vsnprintf
209 #define util_snprintf snprintf
210 #define util_vsprintf vsprintf
211 #define util_vasprintf vasprintf
212 #define util_sprintf sprintf
213 #define util_strchr strchr
214 #define util_strcmp strcmp
215 #define util_strncmp strncmp
216 #define util_strncat strncat
217 #define util_strstr strstr
218 #define util_strcasecmp strcasecmp
219
220 #endif
221
222
223 #ifdef __cplusplus
224 }
225 #endif
226
227 #endif /* U_STRING_H_ */