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