751ae909e1b2684017c69eee50b0e9682a9bf032
[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 #ifdef _GNU_SOURCE
54
55 #define util_strchrnul strchrnul
56
57 #else
58
59 static inline char *
60 util_strchrnul(const char *s, char c)
61 {
62 for (; *s && *s != c; ++s);
63
64 return (char *)s;
65 }
66
67 #endif
68
69 #ifdef _WIN32
70
71 static inline int
72 util_vsnprintf(char *str, size_t size, const char *format, va_list ap)
73 {
74 /* We need to use _vscprintf to calculate the length as vsnprintf returns -1
75 * if the number of characters to write is greater than count.
76 */
77 va_list ap_copy;
78 int ret;
79 va_copy(ap_copy, ap);
80 ret = _vsnprintf(str, size, format, ap);
81 if (ret < 0) {
82 ret = _vscprintf(format, ap_copy);
83 }
84 va_end(ap_copy);
85 return ret;
86 }
87
88 static inline int
89 PRINTFLIKE(3, 4)
90 util_snprintf(char *str, size_t size, const char *format, ...)
91 {
92 va_list ap;
93 int ret;
94 va_start(ap, format);
95 ret = util_vsnprintf(str, size, format, ap);
96 va_end(ap);
97 return ret;
98 }
99
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 util_vsnprintf(str, (size_t)-1, format, ap);
107 va_end(ap);
108 }
109
110 static inline int
111 util_vasprintf(char **ret, const char *format, va_list ap)
112 {
113 va_list ap_copy;
114
115 /* Compute length of output string first */
116 va_copy(ap_copy, ap);
117 int r = util_vsnprintf(NULL, 0, format, ap_copy);
118 va_end(ap_copy);
119
120 if (r < 0)
121 return -1;
122
123 *ret = (char *) malloc(r + 1);
124 if (!*ret)
125 return -1;
126
127 /* Print to buffer */
128 return util_vsnprintf(*ret, r + 1, format, ap);
129 }
130
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 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 static inline int
162 util_strncmp(const char *s1, const char *s2, size_t n)
163 {
164 unsigned char u1, u2;
165
166 while (n-- > 0) {
167 u1 = (unsigned char) *s1++;
168 u2 = (unsigned char) *s2++;
169 if (u1 != u2)
170 return u1 - u2;
171 if (u1 == '\0')
172 return 0;
173 }
174 return 0;
175 }
176
177
178
179 #define util_strcasecmp stricmp
180 #define util_strdup _strdup
181
182 #else
183
184 #define util_vsnprintf vsnprintf
185 #define util_snprintf snprintf
186 #define util_vasprintf vasprintf
187 #define util_sprintf sprintf
188 #define util_strcmp strcmp
189 #define util_strncmp strncmp
190 #define util_strncat strncat
191 #define util_strcasecmp strcasecmp
192 #define util_strdup strdup
193
194 #endif
195
196
197 #ifdef __cplusplus
198 }
199 #endif
200
201 #endif /* U_STRING_H_ */