diff options
author | Hennes Rohling <hro@openoffice.org> | 2002-12-10 11:19:13 +0000 |
---|---|---|
committer | Hennes Rohling <hro@openoffice.org> | 2002-12-10 11:19:13 +0000 |
commit | 220b095cf8e6e608b42eaf5a564506e70c3fcc72 (patch) | |
tree | 22db1a13f8730f0591efc7f00bc1db49b4e07499 /sal | |
parent | #105668# fixed dead links (diff) | |
download | core-220b095cf8e6e608b42eaf5a564506e70c3fcc72.tar.gz core-220b095cf8e6e608b42eaf5a564506e70c3fcc72.zip |
#106038# Added ISO C99 compliant implementations of snprintf and vsnprintf
Diffstat (limited to 'sal')
-rw-r--r-- | sal/inc/sal/config.h | 9 | ||||
-rw-r--r-- | sal/inc/systools/win32/snprintf.h | 77 | ||||
-rw-r--r-- | sal/systools/win32/uwinapi/makefile.mk | 8 | ||||
-rw-r--r-- | sal/systools/win32/uwinapi/snprintf.c | 10 | ||||
-rw-r--r-- | sal/systools/win32/uwinapi/sntprintf.c | 110 | ||||
-rw-r--r-- | sal/systools/win32/uwinapi/snwprintf.c | 5 |
6 files changed, 214 insertions, 5 deletions
diff --git a/sal/inc/sal/config.h b/sal/inc/sal/config.h index 23aed154ea1a..d12adbc2bbf6 100644 --- a/sal/inc/sal/config.h +++ b/sal/inc/sal/config.h @@ -2,8 +2,8 @@ * * $RCSfile: config.h,v $ * - * $Revision: 1.14 $ - * last change: $Author: hr $ $Date: 2002-08-14 17:17:07 $ + * $Revision: 1.15 $ + * last change: $Author: hro $ $Date: 2002-12-10 12:18:23 $ * * The Contents of this file are made available subject to the terms of * either of the following licenses @@ -89,6 +89,11 @@ extern "C" { #pragma warning( disable : 4786 ) #endif +/* Provide ISO C99 compatible versions of snprint and vsnprintf */ +#ifndef _SNPRINTF_H +#include <systools/win32/snprintf.h> +#endif + #endif #endif diff --git a/sal/inc/systools/win32/snprintf.h b/sal/inc/systools/win32/snprintf.h new file mode 100644 index 000000000000..a456342f9652 --- /dev/null +++ b/sal/inc/systools/win32/snprintf.h @@ -0,0 +1,77 @@ +#ifndef _SMPRINTF_H +#define _SNPRINTF_H + +#if !defined(_WIN32) +#error ERROR: Only Win32 target supported! +#endif + +/* Macros for Unicode/ANSI support like in TCHAR.H */ + +#ifdef _UNICODE +#define sntprintf snwprintf +#define vsntprintf vsnwprintf +#else +#define sntprintf snprintf +#define vsntprintf vsnprintf +#endif + +/* Define needed types if they are not yet defined */ + +#if 0 +# ifndef _INC_STDARG +# include <stdarg.h> +# endif +#else +# ifndef _VA_LIST_DEFINED + typedef char * va_list; +# define _VA_LIST_DEFINED +# endif +#endif + +#if 0 +# ifndef _INC_WCHAR +# include <wchar.h> +# endif +#else +# ifndef _WCHAR_T_DEFINED + typedef unsigned short wchar_t; +# define _WCHAR_T_DEFINED +# endif +#endif + +#ifndef _SNPRINTF_DLLIMPORT +#define _SNPRINTF_DLLIMPORT __declspec( dllimport ) +#endif + +#ifdef __cplusplus +extern "C" { +#endif + + +/* Implementations of snprintf following the ISO/IEC 9899:1999 (ISO C99) + standard. + The difference compared to _snprintf is that the buffer always is zero + terminated (unless count is zero) and the return value is the number of + characters (not including terminating zero) that would have been written + even if the buffer wasn't large + enough to hold the string. */ + + + +/* UNICODE version */ +_SNPRINTF_DLLIMPORT int __cdecl snwprintf( wchar_t *buffer, size_t count, const wchar_t *format, ... ); + +/* SBCS and MBCS version */ +_SNPRINTF_DLLIMPORT int __cdecl snprintf( char *buffer, size_t count, const char *format, ... ); + +/* UNICODE version */ +_SNPRINTF_DLLIMPORT int __cdecl vsnwprintf( wchar_t *buffer, size_t count, const wchar_t *format, va_list ap ); + +/* SBCS and MBCS version */ +_SNPRINTF_DLLIMPORT int __cdecl vsnprintf( char *buffer, size_t count, const char *format, va_list ap ); + +#ifdef __cplusplus +} +#endif + +#endif /* _SMPRINTF_H */ diff --git a/sal/systools/win32/uwinapi/makefile.mk b/sal/systools/win32/uwinapi/makefile.mk index b9a5ff1252c4..1f5099fc9c2a 100644 --- a/sal/systools/win32/uwinapi/makefile.mk +++ b/sal/systools/win32/uwinapi/makefile.mk @@ -2,9 +2,9 @@ # # $RCSfile: makefile.mk,v $ # -# $Revision: 1.4 $ +# $Revision: 1.5 $ # -# last change: $Author: hro $ $Date: 2002-08-26 13:36:09 $ +# last change: $Author: hro $ $Date: 2002-12-10 12:17:06 $ # # The Contents of this file are made available subject to the terms of # either of the following licenses @@ -94,7 +94,9 @@ SLOFILES=\ $(SLO)$/MoveFileExW.obj\ $(SLO)$/DllGetVersion.obj\ $(SLO)$/DllMain.obj\ - $(SLO)$/ResolveThunk.obj + $(SLO)$/ResolveThunk.obj\ + $(SLO)$/snprintf.obj\ + $(SLO)$/snwprintf.obj SHL1TARGET=$(TARGET) SHL1IMPLIB=$(SHL1TARGET) diff --git a/sal/systools/win32/uwinapi/snprintf.c b/sal/systools/win32/uwinapi/snprintf.c new file mode 100644 index 000000000000..b91ce5c425eb --- /dev/null +++ b/sal/systools/win32/uwinapi/snprintf.c @@ -0,0 +1,10 @@ +/* Not unicode */ +#undef _UNICODE + +/* Support MBCS and SBCS */ + +#ifndef _MBCS +#define _MBCS +#endif + +#include "sntprintf.c"
\ No newline at end of file diff --git a/sal/systools/win32/uwinapi/sntprintf.c b/sal/systools/win32/uwinapi/sntprintf.c new file mode 100644 index 000000000000..7593e5830004 --- /dev/null +++ b/sal/systools/win32/uwinapi/sntprintf.c @@ -0,0 +1,110 @@ +#define _SNPRINTF_DLLIMPORT __declspec( dllexport ) + +#include <stdarg.h> +#include <stdio.h> + +#include <tchar.h> +#include <systools/win32/snprintf.h> + +#if _MSC_VER < 1300 + +#include <limits.h> +#define MAXSTR INT_MAX + +/* The non-debug versions of _vscprintf/_scprintf are just calls + to _vsprintf/_sprintf with string buffer pointer set to NULL */ + +static int __cdecl _vsctprintf( const _TXCHAR *format, va_list ap ) +{ + FILE str = { 0 }; + + str._cnt = MAXSTR; + str._base = str._ptr = NULL; + str._flag = _IOWRT|_IOSTRG; + + return _vftprintf( &str, format, ap ); +} +#endif + +/* This function retrieves the pointer to the last character of a buffer. + That is the pointer to the last character of the buffer that fits + completly into that buffer or the position of the terminating zero. + + buffer Pointer to a _TXCHAR buffer to be examined + count size of the buffer to be examined + + return The pointer to the last character that fits into the buffer or + NULL if count is zero or count is one and the first byte was a + leading DBCS character +*/ + +static _TCHAR *GetLastBufferChar( _TCHAR *buffer, size_t count ) +{ + _TCHAR *last = NULL; + _TCHAR *cur = buffer; + + while ( (size_t)(cur - buffer) < count ) + { + last = cur; + + if ( !*last ) + break; + + cur = _tcsinc(last); + } + + return last; +} + +/* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */ + +_SNPRINTF_DLLIMPORT int __cdecl vsntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, va_list list ) +{ + int retval; + + /* First of all call the existing non POSIX standard function assuming + the buffer size will be large enough */ + + retval = _vsntprintf( buffer, count, format, list ); + + if ( retval < 0 ) + { + /* If the buffer wasn't large enough ensure that the buffer will be + zero terminated */ + + _TCHAR *last = GetLastBufferChar( buffer, count ); + if (last ) + *last = 0; + + /* Retrieve the count of characters that would have been written + if the buffer were large enough */ + + retval = _vsctprintf( format, list ); + } + else if ( (size_t)retval == count && count ) + { + /* If the buffer was large enough but not large enough for the trailing + zero make the buffer zero terminated */ + + _TCHAR *last = GetLastBufferChar( buffer, count ); + if (last ) + *last = 0; + } + + return retval; +} + +/* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */ + +_SNPRINTF_DLLIMPORT int __cdecl sntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, ... ) +{ + va_list list; + int retval; + + va_start( list, format ); + retval = vsntprintf( buffer, count, format, list ); + va_end( list ); + + return retval; +} + diff --git a/sal/systools/win32/uwinapi/snwprintf.c b/sal/systools/win32/uwinapi/snwprintf.c new file mode 100644 index 000000000000..2f8364ae556b --- /dev/null +++ b/sal/systools/win32/uwinapi/snwprintf.c @@ -0,0 +1,5 @@ +#ifndef _UNICODE +#define _UNICODE +#endif + +#include "sntprintf.c"
\ No newline at end of file |