merge the formfield patch from ooo-build
[ooovba.git] / sal / systools / win32 / uwinapi / sntprintf.c
blobfa0d96e641515b789ac98956e2eaf9005c198a82
1 #define _SNPRINTF_DLLIMPORT __declspec( dllexport )
3 #include <stdarg.h>
4 #include <stdio.h>
6 #include <tchar.h>
7 #include <systools/win32/snprintf.h>
9 #if defined(_MSC_VER) && (_MSC_VER >= 1400)
10 #pragma warning(disable:4273) // inconsistent dll linkage
11 #endif
13 #if _MSC_VER < 1300
15 /* The non-debug versions of _vscprintf/_scprintf are just calls
16 to _vsprintf/_sprintf with string buffer pointer set to NULL,
17 requires MSVCRT version 7.0 */
18 #ifdef __MINGW32__
19 static int __cdecl _vsctprintf( const TCHAR *format, va_list ap )
20 #else
21 static int __cdecl _vsctprintf( const _TXCHAR *format, va_list ap )
22 #endif
24 FILE *fp = _tfopen( _T("NUL"), _T("wb") );
26 if ( fp )
28 int retval = _vftprintf( fp, format, ap );
29 fclose( fp );
31 return retval;
34 return -1;
36 #endif
38 /* This function retrieves the pointer to the last character of a buffer.
39 That is the pointer to the last character of the buffer that fits
40 completly into that buffer or the position of the terminating zero.
42 buffer Pointer to a _TXCHAR buffer to be examined
43 count size of the buffer to be examined
45 return The pointer to the last character that fits into the buffer or
46 NULL if count is zero or count is one and the first byte was a
47 leading DBCS character
50 static _TCHAR *GetLastBufferChar( _TCHAR *buffer, size_t count )
52 _TCHAR *last = NULL;
53 _TCHAR *cur = buffer;
55 while ( (size_t)(cur - buffer) < count )
57 last = cur;
59 if ( !*last )
60 break;
62 cur = _tcsinc(last);
65 return last;
68 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */
70 _SNPRINTF_DLLIMPORT int __cdecl vsntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, va_list list )
72 int retval;
74 /* First of all call the existing non POSIX standard function assuming
75 the buffer size will be large enough */
77 retval = _vsntprintf( buffer, count, format, list );
79 if ( retval < 0 )
81 /* If the buffer wasn't large enough ensure that the buffer will be
82 zero terminated */
84 _TCHAR *last = GetLastBufferChar( buffer, count );
85 if (last )
86 *last = 0;
88 /* Retrieve the count of characters that would have been written
89 if the buffer were large enough */
91 retval = _vsctprintf( format, list );
93 else if ( (size_t)retval == count && count )
95 /* If the buffer was large enough but not large enough for the trailing
96 zero make the buffer zero terminated */
98 _TCHAR *last = GetLastBufferChar( buffer, count );
99 if (last )
100 *last = 0;
103 return retval;
106 /* Implementation of snprintf following the ISO/IEC 9899:1999 (ISO C99) standard */
108 _SNPRINTF_DLLIMPORT int __cdecl sntprintf( _TCHAR *buffer, size_t count, const _TCHAR *format, ... )
110 va_list list;
111 int retval;
113 va_start( list, format );
114 retval = vsntprintf( buffer, count, format, list );
115 va_end( list );
117 return retval;