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