1 /**************************************************************************
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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:
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
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.
26 **************************************************************************/
30 * Platform independent functions for string manipulation.
32 * @author Jose Fonseca <jrfonseca@tungstengraphics.com>
38 #if !defined(WIN32) && !defined(XF86_LIBC_H)
44 #include "pipe/p_compiler.h"
54 int util_vsnprintf(char *, size_t, const char *, va_list);
55 int util_snprintf(char *str
, size_t size
, const char *format
, ...);
58 util_vsprintf(char *str
, const char *format
, va_list ap
)
60 util_vsnprintf(str
, (size_t)-1, format
, ap
);
64 util_sprintf(char *str
, const char *format
, ...)
68 util_vsnprintf(str
, (size_t)-1, format
, ap
);
73 util_strchr(const char *s
, char c
)
84 util_strncat(char *dst
, const char *src
, size_t n
)
86 char *p
= dst
+ strlen(dst
);
90 for (i
= 0; i
< n
&& *q
!= '\0'; ++i
)
98 util_strcmp(const char *s1
, const char *s2
)
100 unsigned char u1
, u2
;
103 u1
= (unsigned char) *s1
++;
104 u2
= (unsigned char) *s2
++;
114 util_strncmp(const char *s1
, const char *s2
, size_t n
)
116 unsigned char u1
, u2
;
119 u1
= (unsigned char) *s1
++;
120 u2
= (unsigned char) *s2
++;
130 util_strstr(const char *haystack
, const char *needle
)
132 const char *p
= haystack
;
133 size_t len
= strlen(needle
);
135 for (; (p
= util_strchr(p
, *needle
)) != 0; p
++) {
136 if (util_strncmp(p
, needle
, len
) == 0) {
144 util_memmove(void *dest
, const void *src
, size_t n
)
146 char *p
= (char *)dest
;
147 const char *q
= (const char *)src
;
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
180 * Printable string buffer
191 util_strbuf_init(struct util_strbuf
*sbuf
, char *str
, size_t size
)
195 sbuf
->ptr
= sbuf
->str
;
201 util_strbuf_printf(struct util_strbuf
*sbuf
, const char *format
, ...)
206 va_start(ap
, format
);
207 written
= util_vsnprintf(sbuf
->ptr
, sbuf
->left
, format
, ap
);
209 sbuf
->ptr
+= written
;
210 sbuf
->left
-= written
;
220 #endif /* U_STRING_H_ */