Merge commit 'origin/gallium-master-merge'
[mesa.git] / src / gallium / auxiliary / util / u_string.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <jrfonseca@tungstengraphics.com>
33 */
34
35 #ifndef U_STRING_H_
36 #define U_STRING_H_
37
38 #if !defined(WIN32) && !defined(XF86_LIBC_H)
39 #include <stdio.h>
40 #endif
41 #include <stddef.h>
42 #include <stdarg.h>
43
44 #include "pipe/p_compiler.h"
45
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51
52 #ifdef WIN32
53
54 int util_vsnprintf(char *, size_t, const char *, va_list);
55 int util_snprintf(char *str, size_t size, const char *format, ...);
56
57 static INLINE void
58 util_vsprintf(char *str, const char *format, va_list ap)
59 {
60 util_vsnprintf(str, (size_t)-1, format, ap);
61 }
62
63 static INLINE void
64 util_sprintf(char *str, const char *format, ...)
65 {
66 va_list ap;
67 va_start(ap, format);
68 util_vsnprintf(str, (size_t)-1, format, ap);
69 va_end(ap);
70 }
71
72 static INLINE char *
73 util_strchr(const char *s, char c)
74 {
75 while(*s) {
76 if(*s == c)
77 return (char *)s;
78 ++s;
79 }
80 return NULL;
81 }
82
83 static INLINE char*
84 util_strncat(char *dst, const char *src, size_t n)
85 {
86 char *p = dst + strlen(dst);
87 const char *q = src;
88 size_t i;
89
90 for (i = 0; i < n && *q != '\0'; ++i)
91 *p++ = *q++;
92 *p = '\0';
93
94 return dst;
95 }
96
97 static INLINE int
98 util_strcmp(const char *s1, const char *s2)
99 {
100 unsigned char u1, u2;
101
102 while (1) {
103 u1 = (unsigned char) *s1++;
104 u2 = (unsigned char) *s2++;
105 if (u1 != u2)
106 return u1 - u2;
107 if (u1 == '\0')
108 return 0;
109 }
110 return 0;
111 }
112
113 static INLINE int
114 util_strncmp(const char *s1, const char *s2, size_t n)
115 {
116 unsigned char u1, u2;
117
118 while (n-- > 0) {
119 u1 = (unsigned char) *s1++;
120 u2 = (unsigned char) *s2++;
121 if (u1 != u2)
122 return u1 - u2;
123 if (u1 == '\0')
124 return 0;
125 }
126 return 0;
127 }
128
129 static INLINE char *
130 util_strstr(const char *haystack, const char *needle)
131 {
132 const char *p = haystack;
133 int len = strlen(needle);
134
135 for (; (p = util_strchr(p, *needle)) != 0; p++) {
136 if (util_strncmp(p, needle, len) == 0) {
137 return (char *)p;
138 }
139 }
140 return NULL;
141 }
142
143 static INLINE void *
144 util_memmove(void *dest, const void *src, size_t n)
145 {
146 char *p = (char *)dest;
147 const char *q = (const char *)src;
148 if (dest < src) {
149 while (n--)
150 *p++ = *q++;
151 }
152 else
153 {
154 p += n;
155 q += n;
156 while (n--)
157 *--p = *--q;
158 }
159 return dest;
160 }
161
162
163 #else
164
165 #define util_vsnprintf vsnprintf
166 #define util_snprintf snprintf
167 #define util_vsprintf vsprintf
168 #define util_sprintf sprintf
169 #define util_strchr strchr
170 #define util_strcmp strcmp
171 #define util_strncmp strncmp
172 #define util_strncat strncat
173 #define util_strstr strstr
174 #define util_memmove memmove
175
176 #endif
177
178
179 /**
180 * Printable string buffer
181 */
182 struct util_strbuf
183 {
184 char *str;
185 char *ptr;
186 size_t left;
187 };
188
189
190 static INLINE void
191 util_strbuf_init(struct util_strbuf *sbuf, char *str, size_t size)
192 {
193 sbuf->str = str;
194 sbuf->str[0] = 0;
195 sbuf->ptr = sbuf->str;
196 sbuf->left = size;
197 }
198
199
200 static INLINE void
201 util_strbuf_printf(struct util_strbuf *sbuf, const char *format, ...)
202 {
203 if(sbuf->left > 1) {
204 size_t written;
205 va_list ap;
206 va_start(ap, format);
207 written = util_vsnprintf(sbuf->ptr, sbuf->left, format, ap);
208 va_end(ap);
209 sbuf->ptr += written;
210 sbuf->left -= written;
211 }
212 }
213
214
215
216 #ifdef __cplusplus
217 }
218 #endif
219
220 #endif /* U_STRING_H_ */