466671953145e018e7bc457a0f9ee008971bd209
2 * COPYRIGHT: GNU GPL, see COPYING in the top level directory
3 * PROJECT: ReactOS crt library
4 * FILE: lib/sdk/crt/printf/swprintf.c
5 * PURPOSE: Implementation of swprintf
6 * PROGRAMMER: Timo Kreuzer
14 #include <internal/safecrt.h>
18 #define _tstreamout wstreamout
20 #define _tstreamout streamout
23 #define min(a,b) (((a) < (b)) ? (a) : (b))
25 int __cdecl
_tstreamout(FILE *stream
, const TCHAR
*format
, va_list argptr
);
28 #if defined(USER32_WSPRINTF) && defined(_M_IX86)
49 const size_t count
= INT_MAX
;
52 const size_t sizeOfBuffer
= count
;
60 /* Check trivial case */
61 if ((buffer
== NULL
) && (count
== 0) && (sizeOfBuffer
== 0))
67 /* Validate parameters */
68 if (MSVCRT_CHECK_PMT(((buffer
== NULL
) || (format
== NULL
) || (sizeOfBuffer
<= 0))))
74 /* Limit output to count + 1 characters */
76 sizeOfBuffer
= min(sizeOfBuffer
, count
+ 1);
79 /* Setup the FILE structure */
80 stream
._base
= (char*)buffer
;
81 stream
._ptr
= stream
._base
;
83 stream
._cnt
= (int)(sizeOfBuffer
* sizeof(TCHAR
));
85 stream
._flag
= _IOSTRG
| _IOWRT
;
89 va_start(argptr
, format
);
91 result
= _tstreamout(&stream
, format
, argptr
);
97 /* Check for failure or unterminated string */
98 if ((result
< 0) || (result
== sizeOfBuffer
))
100 /* Null-terminate the buffer at the end */
101 buffer
[sizeOfBuffer
-1] = _T('\0');
103 /* Check if we can truncate */
104 if (count
!= _TRUNCATE
)
106 /* We can't, invoke invalid parameter handler */
107 MSVCRT_INVALID_PMT("Buffer is too small", ERANGE
);
109 /* If we came back, set the buffer to an empty string */
117 /* Null-terminate the buffer after the string */
118 buffer
[result
] = _T('\0');
120 /* Only zero terminate if there is enough space left */
121 if (stream
._cnt
>= sizeof(TCHAR
)) *(TCHAR
*)stream
._ptr
= _T('\0');