msvcrt: Use callback based printf in sprintf functions family.
authorPiotr Caban <piotr@codeweavers.com>
Wed, 20 Apr 2011 12:42:48 +0000 (14:42 +0200)
committerAlexandre Julliard <julliard@winehq.org>
Mon, 25 Apr 2011 12:36:36 +0000 (14:36 +0200)
dlls/msvcrt/printf.h
dlls/msvcrt/wcs.c

index 90909a16ecb5faace71787d846556ac3666a8908..2db9b6cdbcd96ce4821025f4063b80a6cc7222d5 100644 (file)
@@ -35,6 +35,30 @@ typedef struct FUNC_NAME(pf_flags_t)
     APICHAR Format;
 } 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)
 {
     *val = 0;
index a7cec7cd9b18bd52070e1718af557eb5b9117040..b67fba28da0e5264be101f9a19ae48ba553d5681 100644 (file)
@@ -1113,41 +1113,20 @@ printf_arg arg_clbk_valist(void *ctx, int arg_pos, int type, __ms_va_list *valis
     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.@)
  */
 int CDECL MSVCRT_vsnprintf( char *str, MSVCRT_size_t len,
                             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,
                             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__locale_t locale, __ms_va_list valist )
 {
+    static const char nullbyte = '\0';
+    struct _str_ctx_a ctx;
     int len, ret;
 
     if(sizeOfBuffer<count+1 || count==-1)
@@ -1173,7 +1161,11 @@ int CDECL MSVCRT_vsnprintf_s_l( char *str, MSVCRT_size_t sizeOfBuffer,
     else
         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(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {
@@ -1262,31 +1254,20 @@ int CDECL MSVCRT__scprintf(const char *format, ...)
     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.@)
  */
 int CDECL MSVCRT_vsnwprintf(MSVCRT_wchar_t *str, MSVCRT_size_t len,
         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,
         __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__locale_t locale, __ms_va_list valist)
 {
+    static const MSVCRT_wchar_t nullbyte = '\0';
+    struct _str_ctx_w ctx;
     int len, ret;
 
     len = sizeOfBuffer;
     if(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(count!=MSVCRT__TRUNCATE && count>sizeOfBuffer) {