gallium: Don't assume snprintf are always available.

This commit is contained in:
José Fonseca
2008-04-14 23:55:36 +09:00
parent 21ae3d2721
commit 5b8fa51847
9 changed files with 88 additions and 28 deletions

View File

@@ -30,6 +30,7 @@
#include "pipe/p_debug.h" #include "pipe/p_debug.h"
#include "pipe/p_util.h" #include "pipe/p_util.h"
#include "pipe/p_shader_tokens.h" #include "pipe/p_shader_tokens.h"
#include "util/u_string.h"
#include "tgsi_dump.h" #include "tgsi_dump.h"
#include "tgsi_parse.h" #include "tgsi_parse.h"
#include "tgsi_build.h" #include "tgsi_build.h"
@@ -147,7 +148,7 @@ gen_dump_uix(
{ {
char str[36]; char str[36];
sprintf( str, "0x%x", ui ); util_snprintf( str, sizeof(str), "0x%x", ui );
gen_dump_str( dump, str ); gen_dump_str( dump, str );
} }
@@ -158,7 +159,7 @@ gen_dump_uid(
{ {
char str[16]; char str[16];
sprintf( str, "%u", ui ); util_snprintf( str, sizeof(str), "%u", ui );
gen_dump_str( dump, str ); gen_dump_str( dump, str );
} }
@@ -169,7 +170,7 @@ gen_dump_sid(
{ {
char str[16]; char str[16];
sprintf( str, "%d", si ); util_snprintf( str, sizeof(str), "%d", si );
gen_dump_str( dump, str ); gen_dump_str( dump, str );
} }
@@ -180,7 +181,7 @@ gen_dump_flt(
{ {
char str[48]; char str[48];
sprintf( str, "%10.4f", flt ); util_snprintf( str, sizeof(str), "%10.4f", flt );
gen_dump_str( dump, str ); gen_dump_str( dump, str );
} }

View File

@@ -39,6 +39,7 @@
#include "pipe/p_compiler.h" #include "pipe/p_compiler.h"
#include "pipe/p_util.h" #include "pipe/p_util.h"
#include "pipe/p_debug.h" #include "pipe/p_debug.h"
#include "util/u_string.h"
#ifdef WIN32 #ifdef WIN32
@@ -60,7 +61,7 @@ void _debug_vprintf(const char *format, va_list ap)
/* EngDebugPrint does not handle float point arguments, so we need to use /* EngDebugPrint does not handle float point arguments, so we need to use
* our own vsnprintf implementation */ * our own vsnprintf implementation */
char buf[512 + 1]; char buf[512 + 1];
vsnprintf(buf, sizeof(buf), format, ap); util_vsnprintf(buf, sizeof(buf), format, ap);
_EngDebugPrint("%s", buf); _EngDebugPrint("%s", buf);
#else #else
/* TODO: Implement debug print for WINCE */ /* TODO: Implement debug print for WINCE */
@@ -311,7 +312,7 @@ debug_dump_enum(const struct debug_named_value *names,
++names; ++names;
} }
snprintf(rest, sizeof(rest), "0x%08lx", value); util_snprintf(rest, sizeof(rest), "0x%08lx", value);
return rest; return rest;
} }
@@ -344,7 +345,7 @@ debug_dump_flags(const struct debug_named_value *names,
else else
first = 0; first = 0;
snprintf(rest, sizeof(rest), "0x%08lx", value); util_snprintf(rest, sizeof(rest), "0x%08lx", value);
strncat(output, rest, sizeof(output)); strncat(output, rest, sizeof(output));
} }

View File

@@ -166,8 +166,8 @@
#include <config.h> #include <config.h>
#else #else
#ifdef WIN32 #ifdef WIN32
#define vsnprintf rpl_vsnprintf #define vsnprintf util_vsnprintf
#define snprintf rpl_snprintf #define snprintf util_snprintf
#define HAVE_VSNPRINTF 0 #define HAVE_VSNPRINTF 0
#define HAVE_SNPRINTF 0 #define HAVE_SNPRINTF 0
#define HAVE_VASPRINTF 1 /* not needed */ #define HAVE_VASPRINTF 1 /* not needed */
@@ -445,7 +445,7 @@ static UINTMAX_T myround(LDOUBLE);
static LDOUBLE mypow10(int); static LDOUBLE mypow10(int);
int int
rpl_vsnprintf(char *str, size_t size, const char *format, va_list args) util_vsnprintf(char *str, size_t size, const char *format, va_list args)
{ {
LDOUBLE fvalue; LDOUBLE fvalue;
INTMAX_T value; INTMAX_T value;
@@ -1404,7 +1404,7 @@ mymemcpy(void *dst, void *src, size_t len)
#endif /* NEED_MYMEMCPY */ #endif /* NEED_MYMEMCPY */
int int
rpl_vasprintf(char **ret, const char *format, va_list ap) util_vasprintf(char **ret, const char *format, va_list ap)
{ {
size_t size; size_t size;
int len; int len;
@@ -1422,10 +1422,10 @@ rpl_vasprintf(char **ret, const char *format, va_list ap)
#if !HAVE_SNPRINTF #if !HAVE_SNPRINTF
#if HAVE_STDARG_H #if HAVE_STDARG_H
int int
rpl_snprintf(char *str, size_t size, const char *format, ...) util_snprintf(char *str, size_t size, const char *format, ...)
#else #else
int int
rpl_snprintf(va_alist) va_dcl util_snprintf(va_alist) va_dcl
#endif /* HAVE_STDARG_H */ #endif /* HAVE_STDARG_H */
{ {
#if !HAVE_STDARG_H #if !HAVE_STDARG_H
@@ -1449,10 +1449,10 @@ rpl_snprintf(va_alist) va_dcl
#if !HAVE_ASPRINTF #if !HAVE_ASPRINTF
#if HAVE_STDARG_H #if HAVE_STDARG_H
int int
rpl_asprintf(char **ret, const char *format, ...) util_asprintf(char **ret, const char *format, ...)
#else #else
int int
rpl_asprintf(va_alist) va_dcl util_asprintf(va_alist) va_dcl
#endif /* HAVE_STDARG_H */ #endif /* HAVE_STDARG_H */
{ {
#if !HAVE_STDARG_H #if !HAVE_STDARG_H

View File

@@ -0,0 +1,63 @@
/**************************************************************************
*
* Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
* All Rights Reserved.
*
* Permission is hereby granted, free of charge, to any person obtaining a
* copy of this software and associated documentation files (the
* "Software"), to deal in the Software without restriction, including
* without limitation the rights to use, copy, modify, merge, publish,
* distribute, sub license, and/or sell copies of the Software, and to
* permit persons to whom the Software is furnished to do so, subject to
* the following conditions:
*
* The above copyright notice and this permission notice (including the
* next paragraph) shall be included in all copies or substantial portions
* of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
* OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
* IN NO EVENT SHALL TUNGSTEN GRAPHICS AND/OR ITS SUPPLIERS BE LIABLE FOR
* ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*
**************************************************************************/
/**
* @file
* Platform independent functions for string manipulation.
*
* @author Jose Fonseca <jrfonseca@tungstengraphics.com>
*/
#ifndef U_STRING_H_
#define U_STRING_H_
#ifndef WIN32
#include <stdio.h>
#endif
#include <stddef.h>
#include <stdarg.h>
#ifdef __cplusplus
extern "C" {
#endif
#ifdef WIN32
int util_vsnprintf(char *, size_t, const char *, va_list);
int util_snprintf(char *str, size_t size, const char *format, ...);
#else
#define util_vsnprintf vsnprintf
#define util_snprintf snprintf
#endif
#ifdef __cplusplus
}
#endif
#endif /* U_STRING_H_ */

View File

@@ -33,6 +33,7 @@
#include "i915_fpc.h" #include "i915_fpc.h"
#include "pipe/p_shader_tokens.h" #include "pipe/p_shader_tokens.h"
#include "util/u_string.h"
#include "tgsi/util/tgsi_parse.h" #include "tgsi/util/tgsi_parse.h"
#include "tgsi/util/tgsi_dump.h" #include "tgsi/util/tgsi_dump.h"
@@ -122,7 +123,7 @@ i915_program_error(struct i915_fp_compile *p, const char *msg, ...)
debug_printf("i915_program_error: "); debug_printf("i915_program_error: ");
va_start( args, msg ); va_start( args, msg );
vsprintf( buffer, msg, args ); util_vsnprintf( buffer, sizeof(buffer), msg, args );
va_end( args ); va_end( args );
debug_printf(buffer); debug_printf(buffer);
debug_printf("\n"); debug_printf("\n");

View File

@@ -28,6 +28,7 @@
#include "pipe/p_util.h" #include "pipe/p_util.h"
#include "pipe/p_winsys.h" #include "pipe/p_winsys.h"
#include "util/u_string.h"
#include "i915_reg.h" #include "i915_reg.h"
#include "i915_context.h" #include "i915_context.h"
@@ -78,7 +79,7 @@ i915_get_name( struct pipe_screen *pscreen )
break; break;
} }
sprintf(buffer, "i915 (chipset: %s)", chipset); util_snprintf(buffer, sizeof(buffer), "i915 (chipset: %s)", chipset);
return buffer; return buffer;
} }

View File

@@ -28,6 +28,7 @@
#include "pipe/p_util.h" #include "pipe/p_util.h"
#include "pipe/p_winsys.h" #include "pipe/p_winsys.h"
#include "util/u_string.h"
#include "brw_context.h" #include "brw_context.h"
#include "brw_screen.h" #include "brw_screen.h"
@@ -66,7 +67,7 @@ brw_get_name( struct pipe_screen *screen )
break; break;
} }
sprintf(buffer, "i965 (chipset: %s)", chipset); util_snprintf(buffer, sizeof(buffer), "i965 (chipset: %s)", chipset);
return buffer; return buffer;
} }

View File

@@ -28,7 +28,7 @@
#ifndef PIPE_FORMAT_H #ifndef PIPE_FORMAT_H
#define PIPE_FORMAT_H #define PIPE_FORMAT_H
#include <stdio.h> /* for sprintf */ #include "util/u_string.h"
#include "p_compiler.h" #include "p_compiler.h"
#include "p_debug.h" #include "p_debug.h"
@@ -367,7 +367,7 @@ static INLINE char *pf_sprint_name( char *str, enum pipe_format format )
strcat( str, "S" ); strcat( str, "S" );
break; break;
} }
sprintf( &str[strlen( str )], "%u", size * scale ); util_snprintf( &str[strlen( str )], 32, "%u", size * scale );
} }
if (i != 0) { if (i != 0) {
strcat( str, "_" ); strcat( str, "_" );

View File

@@ -138,14 +138,6 @@ REALLOC( void *old_ptr, unsigned old_size, unsigned new_size )
#define GETENV( X ) debug_get_option( X, NULL ) #define GETENV( X ) debug_get_option( X, NULL )
#ifdef WIN32
int rpl_vsnprintf(char *, size_t, const char *, va_list);
int rpl_snprintf(char *str, size_t size, const char *format, ...);
#define vsnprintf rpl_vsnprintf
#define snprintf rpl_snprintf
#endif
/** /**
* Return memory on given byte alignment * Return memory on given byte alignment
*/ */