[SHLWAPI] Sync with Wine Staging 3.3. CORE-14434
[reactos.git] / dll / win32 / shlwapi / wsprintf.c
index 26bca5d..6a36b6a 100644 (file)
@@ -43,6 +43,8 @@ WINE_DEFAULT_DEBUG_CHANNEL(string);
 #define WPRINTF_SHORT       0x0010  /* Short arg ('h' prefix) */
 #define WPRINTF_UPPER_HEX   0x0020  /* Upper-case hex ('X' specifier) */
 #define WPRINTF_WIDE        0x0040  /* Wide arg ('w' prefix) */
+#define WPRINTF_INTPTR      0x0080  /* Pointer-size arg ('I' prefix) */
+#define WPRINTF_I64         0x0100  /* 64-bit arg ('I64' prefix) */
 
 typedef enum
 {
@@ -65,11 +67,11 @@ typedef struct
 } WPRINTF_FORMAT;
 
 typedef union {
-    WCHAR   wchar_view;
-    CHAR    char_view;
-    LPCSTR  lpcstr_view;
-    LPCWSTR lpcwstr_view;
-    INT     int_view;
+    WCHAR    wchar_view;
+    CHAR     char_view;
+    LPCSTR   lpcstr_view;
+    LPCWSTR  lpcwstr_view;
+    LONGLONG int_view;
 } WPRINTF_DATA;
 
 static const CHAR null_stringA[] = "(null)";
@@ -111,6 +113,12 @@ static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
     if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
     else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
     else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
+    else if (*p == 'I')
+    {
+        if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
+        else if (p[1] == '3' && p[2] == '2') p += 3;
+        else { res->flags |= WPRINTF_INTPTR; p++; }
+    }
     switch(*p)
     {
     case 'c':
@@ -132,6 +140,10 @@ static INT WPRINTF_ParseFormatA( LPCSTR format, WPRINTF_FORMAT *res )
     case 'u':
         res->type = WPR_UNSIGNED;
         break;
+    case 'p':
+        res->width = 2 * sizeof(void *);
+        res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
+        /* fall through */
     case 'X':
         res->flags |= WPRINTF_UPPER_HEX;
         /* fall through */
@@ -183,7 +195,13 @@ static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
     if (*p == 'l') { res->flags |= WPRINTF_LONG; p++; }
     else if (*p == 'h') { res->flags |= WPRINTF_SHORT; p++; }
     else if (*p == 'w') { res->flags |= WPRINTF_WIDE; p++; }
-    switch((CHAR)*p)
+    else if (*p == 'I')
+    {
+        if (p[1] == '6' && p[2] == '4') { res->flags |= WPRINTF_I64; p += 3; }
+        else if (p[1] == '3' && p[2] == '2') p += 3;
+        else { res->flags |= WPRINTF_INTPTR; p++; }
+    }
+    switch(*p)
     {
     case 'c':
         res->type = (res->flags & WPRINTF_SHORT) ? WPR_CHAR : WPR_WCHAR;
@@ -204,6 +222,10 @@ static INT WPRINTF_ParseFormatW( LPCWSTR format, WPRINTF_FORMAT *res )
     case 'u':
         res->type = WPR_UNSIGNED;
         break;
+    case 'p':
+        res->width = 2 * sizeof(void *);
+        res->flags |= WPRINTF_ZEROPAD | WPRINTF_INTPTR;
+        /* fall through */
     case 'X':
         res->flags |= WPRINTF_UPPER_HEX;
         /* fall through */
@@ -247,16 +269,32 @@ static UINT WPRINTF_GetLen( WPRINTF_FORMAT *format, WPRINTF_DATA *arg,
         if (len > maxlen) len = maxlen;
         return (format->precision = len);
     case WPR_SIGNED:
-        len = sprintf( number, "%d", arg->int_view );
-        break;
     case WPR_UNSIGNED:
-        len = sprintf( number, "%u", (UINT)arg->int_view );
-        break;
     case WPR_HEXA:
-        len = sprintf( number,
-                       (format->flags & WPRINTF_UPPER_HEX) ? "%X" : "%x",
-                       (UINT)arg->int_view);
+    {
+        const char *digits = (format->flags & WPRINTF_UPPER_HEX) ? "0123456789ABCDEF" : "0123456789abcdef";
+        ULONGLONG num = arg->int_view;
+        int base = format->type == WPR_HEXA ? 16 : 10;
+        char buffer[20], *p = buffer, *dst = number;
+
+        if (format->type == WPR_SIGNED && arg->int_view < 0)
+        {
+            *dst++ = '-';
+            num = -arg->int_view;
+        }
+        if (format->flags & WPRINTF_INTPTR) num = (UINT_PTR)num;
+        else if (!(format->flags & WPRINTF_I64)) num = (UINT)num;
+
+        do
+        {
+            *p++ = digits[num % base];
+            num /= base;
+        } while (num);
+        while (p > buffer) *dst++ = *(--p);
+        *dst = 0;
+        len = dst - number;
         break;
+    }
     default:
         return 0;
     }
@@ -289,7 +327,7 @@ INT WINAPI wvnsprintfA( LPSTR buffer, INT maxlen, LPCSTR spec, __ms_va_list args
     WPRINTF_FORMAT format;
     LPSTR p = buffer;
     UINT i, len, sign;
-    CHAR number[20];
+    CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
     WPRINTF_DATA argData;
 
     TRACE("%p %u %s\n", buffer, maxlen, debugstr_a(spec));
@@ -318,7 +356,9 @@ INT WINAPI wvnsprintfA( LPSTR buffer, INT maxlen, LPCSTR spec, __ms_va_list args
         case WPR_HEXA:
         case WPR_SIGNED:
         case WPR_UNSIGNED:
-            argData.int_view = va_arg( args, INT );
+            if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
+            else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
+            else argData.int_view = va_arg(args, INT);
             break;
         default:
             argData.wchar_view = 0;
@@ -394,7 +434,7 @@ INT WINAPI wvnsprintfW( LPWSTR buffer, INT maxlen, LPCWSTR spec, __ms_va_list ar
     WPRINTF_FORMAT format;
     LPWSTR p = buffer;
     UINT i, len, sign;
-    CHAR number[20];
+    CHAR number[21]; /* 64bit number can be 18446744073709551616 which is 20 chars. and a \0 */
     WPRINTF_DATA argData;
 
     TRACE("%p %u %s\n", buffer, maxlen, debugstr_w(spec));
@@ -423,7 +463,9 @@ INT WINAPI wvnsprintfW( LPWSTR buffer, INT maxlen, LPCWSTR spec, __ms_va_list ar
         case WPR_HEXA:
         case WPR_SIGNED:
         case WPR_UNSIGNED:
-            argData.int_view = va_arg( args, INT );
+            if (format.flags & WPRINTF_INTPTR) argData.int_view = va_arg(args, INT_PTR);
+            else if (format.flags & WPRINTF_I64) argData.int_view = va_arg(args, LONGLONG);
+            else argData.int_view = va_arg(args, INT);
             break;
         default:
             argData.wchar_view = 0;
@@ -446,7 +488,7 @@ INT WINAPI wvnsprintfW( LPWSTR buffer, INT maxlen, LPCWSTR spec, __ms_va_list ar
         case WPR_STRING:
             {
                 LPCSTR ptr = argData.lpcstr_view;
-                for (i = 0; i < len; i++) *p++ = (WCHAR)*ptr++;
+                for (i = 0; i < len; i++) *p++ = (BYTE)*ptr++;
             }
             break;
         case WPR_WSTRING:
@@ -472,7 +514,7 @@ INT WINAPI wvnsprintfW( LPWSTR buffer, INT maxlen, LPCWSTR spec, __ms_va_list ar
             /* fall through */
         case WPR_UNSIGNED:
             for (i = len; i < format.precision; i++, maxlen--) *p++ = '0';
-            for (i = sign; i < len; i++) *p++ = (WCHAR)number[i];
+            for (i = sign; i < len; i++) *p++ = (BYTE)number[i];
             break;
         case WPR_UNKNOWN:
             continue;