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