u_string.h

Go to the documentation of this file.
00001 /**************************************************************************
00002  *
00003  * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
00004  * All Rights Reserved.
00005  *
00006  * Permission is hereby granted, free of charge, to any person obtaining a
00007  * copy of this software and associated documentation files (the
00008  * "Software"), to deal in the Software without restriction, including
00009  * without limitation the rights to use, copy, modify, merge, publish,
00010  * distribute, sub license, and/or sell copies of the Software, and to
00011  * permit persons to whom the Software is furnished to do so, subject to
00012  * the following conditions:
00013  *
00014  * The above copyright notice and this permission notice (including the
00015  * next paragraph) shall be included in all copies or substantial portions
00016  * of the Software.
00017  *
00018  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
00019  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
00020  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
00021  * IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
00022  * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
00023  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
00024  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
00025  *
00026  **************************************************************************/
00027 
00035 #ifndef U_STRING_H_
00036 #define U_STRING_H_
00037 
00038 #if !defined(WIN32) && !defined(XF86_LIBC_H)
00039 #include <stdio.h>
00040 #endif
00041 #include <stddef.h>
00042 #include <stdarg.h>
00043 
00044 #include "pipe/p_compiler.h"
00045 
00046 
00047 #ifdef __cplusplus
00048 extern "C" {
00049 #endif
00050 
00051 
00052 #ifdef WIN32
00053 
00054 int util_vsnprintf(char *, size_t, const char *, va_list);
00055 int util_snprintf(char *str, size_t size, const char *format, ...);
00056 
00057 static INLINE void
00058 util_vsprintf(char *str, const char *format, va_list ap)
00059 {
00060    util_vsnprintf(str, (size_t)-1, format, ap);
00061 }
00062 
00063 static INLINE void
00064 util_sprintf(char *str, const char *format, ...)
00065 {
00066    va_list ap;
00067    va_start(ap, format);
00068    util_vsnprintf(str, (size_t)-1, format, ap);
00069    va_end(ap);
00070 }
00071 
00072 static INLINE char *
00073 util_strchr(const char *s, char c)
00074 {
00075    while(*s) {
00076       if(*s == c)
00077          return (char *)s;
00078       ++s;
00079    }
00080    return NULL;
00081 }
00082 
00083 static INLINE char*
00084 util_strncat(char *dst, const char *src, size_t n)
00085 {
00086    char *p = dst + strlen(dst);
00087    const char *q = src;
00088    size_t i;
00089 
00090    for (i = 0; i < n && *q != '\0'; ++i)
00091        *p++ = *q++;
00092    *p = '\0';
00093 
00094    return dst;
00095 }
00096 
00097 static INLINE int
00098 util_strcmp(const char *s1, const char *s2)
00099 {
00100    unsigned char u1, u2;
00101 
00102    while (1) {
00103       u1 = (unsigned char) *s1++;
00104       u2 = (unsigned char) *s2++;
00105       if (u1 != u2)
00106          return u1 - u2;
00107       if (u1 == '\0')
00108          return 0;
00109    }
00110    return 0;
00111 }
00112 
00113 static INLINE int
00114 util_strncmp(const char *s1, const char *s2, size_t n)
00115 {
00116    unsigned char u1, u2;
00117 
00118    while (n-- > 0) {
00119       u1 = (unsigned char) *s1++;
00120       u2 = (unsigned char) *s2++;
00121       if (u1 != u2)
00122          return u1 - u2;
00123       if (u1 == '\0')
00124          return 0;
00125    }
00126    return 0;
00127 }
00128 
00129 static INLINE char *
00130 util_strstr(const char *haystack, const char *needle)
00131 {
00132    const char *p = haystack;
00133    int len = strlen(needle);
00134 
00135    for (; (p = util_strchr(p, *needle)) != 0; p++) {
00136       if (util_strncmp(p, needle, len) == 0) {
00137          return (char *)p;
00138       }
00139    }
00140    return NULL;
00141 }
00142 
00143 static INLINE void *
00144 util_memmove(void *dest, const void *src, size_t n)
00145 {
00146    char *p = (char *)dest;
00147    const char *q = (const char *)src;
00148    if (dest < src) {
00149       while (n--)
00150          *p++ = *q++;
00151    }
00152    else
00153    {
00154       p += n;
00155       q += n;
00156       while (n--)
00157          *--p = *--q;
00158    }
00159    return dest;
00160 }
00161 
00162 
00163 #else
00164 
00165 #define util_vsnprintf vsnprintf
00166 #define util_snprintf snprintf
00167 #define util_vsprintf vsprintf
00168 #define util_sprintf sprintf
00169 #define util_strchr strchr
00170 #define util_strcmp strcmp
00171 #define util_strncmp strncmp
00172 #define util_strncat strncat
00173 #define util_strstr strstr
00174 #define util_memmove memmove
00175 
00176 #endif
00177 
00178 
00182 struct util_strbuf
00183 {
00184    char *str;
00185    char *ptr;
00186    size_t left;
00187 };
00188 
00189 
00190 static INLINE void
00191 util_strbuf_init(struct util_strbuf *sbuf, char *str, size_t size) 
00192 {
00193    sbuf->str = str;
00194    sbuf->str[0] = 0;
00195    sbuf->ptr = sbuf->str;
00196    sbuf->left = size;
00197 }
00198 
00199 
00200 static INLINE void
00201 util_strbuf_printf(struct util_strbuf *sbuf, const char *format, ...)
00202 {
00203    if(sbuf->left > 1) {
00204       size_t written;
00205       va_list ap;
00206       va_start(ap, format);
00207       written = util_vsnprintf(sbuf->ptr, sbuf->left, format, ap);
00208       va_end(ap);
00209       sbuf->ptr += written;
00210       sbuf->left -= written;
00211    }
00212 }
00213 
00214 
00215 
00216 #ifdef __cplusplus
00217 }
00218 #endif
00219 
00220 #endif /* U_STRING_H_ */

Generated on Tue Sep 29 06:25:15 2009 for Gallium3D by  doxygen 1.5.4