util: use standard name for vasprintf()
[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 #define sprintf util_sprintf
98 static inline void
99 PRINTFLIKE(2, 3)
100 util_sprintf(char *str, const char *format, ...)
101 {
102 va_list ap;
103 va_start(ap, format);
104 util_vsnprintf(str, (size_t)-1, format, ap);
105 va_end(ap);
106 }
107
108 #define vasprintf util_vasprintf
109 static inline int
110 util_vasprintf(char **ret, const char *format, va_list ap)
111 {
112 va_list ap_copy;
113
114 /* Compute length of output string first */
115 va_copy(ap_copy, ap);
116 int r = util_vsnprintf(NULL, 0, format, ap_copy);
117 va_end(ap_copy);
118
119 if (r < 0)
120 return -1;
121
122 *ret = (char *) malloc(r + 1);
123 if (!*ret)
124 return -1;
125
126 /* Print to buffer */
127 return util_vsnprintf(*ret, r + 1, format, ap);
128 }
129
130 #define strncat util_strncat
131 static inline char*
132 util_strncat(char *dst, const char *src, size_t n)
133 {
134 char *p = dst + strlen(dst);
135 const char *q = src;
136 size_t i;
137
138 for (i = 0; i < n && *q != '\0'; ++i)
139 *p++ = *q++;
140 *p = '\0';
141
142 return dst;
143 }
144
145 #define strcmp util_strcmp
146 static inline int
147 util_strcmp(const char *s1, const char *s2)
148 {
149 unsigned char u1, u2;
150
151 while (1) {
152 u1 = (unsigned char) *s1++;
153 u2 = (unsigned char) *s2++;
154 if (u1 != u2)
155 return u1 - u2;
156 if (u1 == '\0')
157 return 0;
158 }
159 return 0;
160 }
161
162 #define strncmp util_strncmp
163 static inline int
164 util_strncmp(const char *s1, const char *s2, size_t n)
165 {
166 unsigned char u1, u2;
167
168 while (n-- > 0) {
169 u1 = (unsigned char) *s1++;
170 u2 = (unsigned char) *s2++;
171 if (u1 != u2)
172 return u1 - u2;
173 if (u1 == '\0')
174 return 0;
175 }
176 return 0;
177 }
178
179
180
181 #define strcasecmp stricmp
182 #define strdup _strdup
183
184 #else
185
186 #define util_vsnprintf vsnprintf
187 #define util_snprintf snprintf
188
189 #endif
190
191
192 #ifdef __cplusplus
193 }
194 #endif
195
196 #endif /* U_STRING_H_ */