msvcrt: Use callback based printf in sprintf functions family.

This commit is contained in:
Piotr Caban 2011-04-20 14:42:48 +02:00 committed by Alexandre Julliard
parent b9e456aa79
commit bfc2f5f2a5
2 changed files with 70 additions and 52 deletions

View File

@ -35,6 +35,30 @@ typedef struct FUNC_NAME(pf_flags_t)
APICHAR Format; APICHAR Format;
} FUNC_NAME(pf_flags); } FUNC_NAME(pf_flags);
struct FUNC_NAME(_str_ctx) {
MSVCRT_size_t len;
APICHAR *buf;
};
static int FUNC_NAME(puts_clbk_str)(void *ctx, int len, const APICHAR *str)
{
struct FUNC_NAME(_str_ctx) *out = ctx;
if(!out->buf)
return len;
if(out->len < len) {
memcpy(out->buf, str, out->len);
out->buf += out->len;
out->len = 0;
return -1;
}
memcpy(out->buf, str, len*sizeof(APICHAR));
out->buf += len;
return len;
}
static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val) static inline const APICHAR* FUNC_NAME(pf_parse_int)(const APICHAR *fmt, int *val)
{ {
*val = 0; *val = 0;

View File

@ -1113,41 +1113,20 @@ printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valis
return ret; return ret;
} }
/*********************************************************************
* vsnprintf_internal (INTERNAL)
*/
static inline int vsnprintf_internal( char *str, MSVCRT_size_t len, const char *format,
MSVCRT__locale_t locale, BOOL valid, __ms_va_list valist )
{
DWORD sz;
LPWSTR formatW = NULL;
pf_output out;
int r;
out.unicode = FALSE;
out.buf.A = str;
out.grow.A = NULL;
out.used = 0;
out.len = len;
sz = MultiByteToWideChar( CP_ACP, 0, format, -1, NULL, 0 );
formatW = HeapAlloc( GetProcessHeap(), 0, sz*sizeof(WCHAR) );
MultiByteToWideChar( CP_ACP, 0, format, -1, formatW, sz );
r = pf_vsnprintf( &out, formatW, locale, valid, valist );
HeapFree( GetProcessHeap(), 0, formatW );
return r;
}
/********************************************************************* /*********************************************************************
* _vsnprintf (MSVCRT.@) * _vsnprintf (MSVCRT.@)
*/ */
int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len, int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
const char *format, __ms_va_list valist ) const char *format, __ms_va_list valist )
{ {
return vsnprintf_internal(str, len, format, NULL, FALSE, valist); static const char nullbyte = '\0';
struct _str_ctx_a ctx = {len, str};
int ret;
ret = pf_printf_a(puts_clbk_str_a, &ctx, format, NULL, FALSE, FALSE,
arg_clbk_valist, NULL, valist);
puts_clbk_str_a(&ctx, 1, &nullbyte);
return ret;
} }
/********************************************************************* /*********************************************************************
@ -1156,7 +1135,14 @@ int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format, int CDECL MSVCRT_vsnprintf_l( char *str, MSVCRT_size_t len, const char *format,
MSVCRT__locale_t locale, __ms_va_list valist ) MSVCRT__locale_t locale, __ms_va_list valist )
{ {
return vsnprintf_internal(str, len, format, locale, FALSE, valist); static const char nullbyte = '\0';
struct _str_ctx_a ctx = {len, str};
int ret;
ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, FALSE,
arg_clbk_valist, NULL, valist);
puts_clbk_str_a(&ctx, 1, &nullbyte);
return ret;
} }
/********************************************************************* /*********************************************************************
@ -1166,6 +1152,8 @@ int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
MSVCRT_size_t count, const char *format, MSVCRT_size_t count, const char *format,
MSVCRT__locale_t locale, __ms_va_list valist ) MSVCRT__locale_t locale, __ms_va_list valist )
{ {
static const char nullbyte = '\0';
struct _str_ctx_a ctx;
int len, ret; int len, ret;
if(sizeOfBuffer<count+1 || count==-1) if(sizeOfBuffer<count+1 || count==-1)
@ -1173,7 +1161,11 @@ int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
else else
len = count+1; len = count+1;
ret = vsnprintf_internal(str, len, format, locale, TRUE, valist); ctx.len = len;
ctx.buf = str;
ret = pf_printf_a(puts_clbk_str_a, &ctx, format, locale, FALSE, TRUE,
arg_clbk_valist, NULL, valist);
puts_clbk_str_a(&ctx, 1, &nullbyte);
if(ret<0 || ret==len) { if(ret<0 || ret==len) {
if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) { if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
@ -1262,31 +1254,20 @@ int CDECL MSVCRT__scprintf(const char *format, ...)
return retval; return retval;
} }
/*********************************************************************
* vsnwprintf_internal (INTERNAL)
*/
static inline int vsnwprintf_internal(MSVCRT_wchar_t *str, MSVCRT_size_t len,
const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, BOOL valid,
__ms_va_list valist)
{
pf_output out;
out.unicode = TRUE;
out.buf.W = str;
out.grow.W = NULL;
out.used = 0;
out.len = len;
return pf_vsnprintf( &out, format, locale, valid, valist );
}
/********************************************************************* /*********************************************************************
* _vsnwprintf (MSVCRT.@) * _vsnwprintf (MSVCRT.@)
*/ */
int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len, int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
const MSVCRT_wchar_t *format, __ms_va_list valist) const MSVCRT_wchar_t *format, __ms_va_list valist)
{ {
return vsnwprintf_internal(str, len, format, NULL, FALSE, valist); static const MSVCRT_wchar_t nullbyte = '\0';
struct _str_ctx_w ctx = {len, str};
int ret;
ret = pf_printf_w(puts_clbk_str_w, &ctx, format, NULL, FALSE, FALSE,
arg_clbk_valist, NULL, valist);
puts_clbk_str_w(&ctx, 1, &nullbyte);
return ret;
} }
/********************************************************************* /*********************************************************************
@ -1296,7 +1277,14 @@ int CDECL MSVCRT_vsnwprintf_l(MSVCRT_wchar_t *str, MSVCRT_size_t len,
const MSVCRT_wchar_t *format, MSVCRT__locale_t locale, const MSVCRT_wchar_t *format, MSVCRT__locale_t locale,
__ms_va_list valist) __ms_va_list valist)
{ {
return vsnwprintf_internal(str, len, format, locale, FALSE, valist); static const MSVCRT_wchar_t nullbyte = '\0';
struct _str_ctx_w ctx = {len, str};
int ret;
ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, FALSE,
arg_clbk_valist, NULL, valist);
puts_clbk_str_w(&ctx, 1, &nullbyte);
return ret;
} }
/********************************************************************* /*********************************************************************
@ -1306,13 +1294,19 @@ int CDECL MSVCRT_vsnwprintf_s_l( MSVCRT_wchar_t *str, MSVCRT_size_t sizeOfBuffer
MSVCRT_size_t count, const MSVCRT_wchar_t *format, MSVCRT_size_t count, const MSVCRT_wchar_t *format,
MSVCRT__locale_t locale, __ms_va_list valist) MSVCRT__locale_t locale, __ms_va_list valist)
{ {
static const MSVCRT_wchar_t nullbyte = '\0';
struct _str_ctx_w ctx;
int len, ret; int len, ret;
len = sizeOfBuffer; len = sizeOfBuffer;
if(count!=-1 && len>count+1) if(count!=-1 && len>count+1)
len = count+1; len = count+1;
ret = vsnwprintf_internal(str, len, format, locale, TRUE, valist); ctx.len = len;
ctx.buf = str;
ret = pf_printf_w(puts_clbk_str_w, &ctx, format, locale, FALSE, TRUE,
arg_clbk_valist, NULL, valist);
puts_clbk_str_w(&ctx, 1, &nullbyte);
if(ret<0 || ret==len) { if(ret<0 || ret==len) {
if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) { if(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {