kernel32: Replace the format_message() helper macro with a static function.

This commit is contained in:
Francois Gouget 2012-09-19 20:37:24 +02:00 committed by Alexandre Julliard
parent a273cac747
commit bca6e18d1b
1 changed files with 39 additions and 34 deletions

View File

@ -261,30 +261,36 @@ static LPCWSTR format_insert( BOOL unicode_caller, int insert, LPCWSTR format,
return format; return format;
} }
struct _format_message_data
{
LPWSTR formatted;
DWORD size;
LPWSTR t;
};
static void format_add_char(struct _format_message_data *fmd, WCHAR c)
{
*fmd->t++ = c;
if ((DWORD)(fmd->t - fmd->formatted) == fmd->size) {
fmd->formatted = HeapReAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, fmd->formatted, (fmd->size * 2) * sizeof(WCHAR));
fmd->t = fmd->formatted + fmd->size;
fmd->size *= 2;
}
}
/********************************************************************** /**********************************************************************
* format_message (internal) * format_message (internal)
*/ */
static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr, static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr,
struct format_args *format_args ) struct format_args *format_args )
{ {
LPWSTR target,t; struct _format_message_data fmd;
DWORD talloced;
LPCWSTR f; LPCWSTR f;
DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK; DWORD width = dwFlags & FORMAT_MESSAGE_MAX_WIDTH_MASK;
BOOL eos = FALSE; BOOL eos = FALSE;
WCHAR ch;
target = t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, 100 * sizeof(WCHAR) ); fmd.size = 100;
talloced = 100; fmd.formatted = fmd.t = HeapAlloc( GetProcessHeap(), HEAP_ZERO_MEMORY, fmd.size * sizeof(WCHAR) );
#define ADD_TO_T(c) do {\
*t++=c;\
if ((DWORD)(t-target) == talloced) {\
target = HeapReAlloc(GetProcessHeap(),HEAP_ZERO_MEMORY,target,talloced*2*sizeof(WCHAR));\
t = target+talloced;\
talloced*=2;\
} \
} while (0)
f = fmtstr; f = fmtstr;
while (*f && !eos) { while (*f && !eos) {
@ -302,7 +308,7 @@ static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr
(!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY) && !format_args->list)) (!(dwFlags & FORMAT_MESSAGE_ARGUMENT_ARRAY) && !format_args->list))
{ {
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
HeapFree(GetProcessHeap(), 0, target); HeapFree(GetProcessHeap(), 0, fmd.formatted);
return NULL; return NULL;
} }
insertnr = *f-'0'; insertnr = *f-'0';
@ -319,20 +325,20 @@ static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr
break; break;
} }
f = format_insert( unicode_caller, insertnr, f, dwFlags, format_args, &str ); f = format_insert( unicode_caller, insertnr, f, dwFlags, format_args, &str );
for (x = str; *x; x++) ADD_TO_T(*x); for (x = str; *x; x++) format_add_char(&fmd, *x);
HeapFree( GetProcessHeap(), 0, str ); HeapFree( GetProcessHeap(), 0, str );
break; break;
case 'n': case 'n':
ADD_TO_T('\r'); format_add_char(&fmd, '\r');
ADD_TO_T('\n'); format_add_char(&fmd, '\n');
f++; f++;
break; break;
case 'r': case 'r':
ADD_TO_T('\r'); format_add_char(&fmd, '\r');
f++; f++;
break; break;
case 't': case 't':
ADD_TO_T('\t'); format_add_char(&fmd, '\t');
f++; f++;
break; break;
case '0': case '0':
@ -341,49 +347,48 @@ static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr
break; break;
case '\0': case '\0':
SetLastError(ERROR_INVALID_PARAMETER); SetLastError(ERROR_INVALID_PARAMETER);
HeapFree(GetProcessHeap(), 0, target); HeapFree(GetProcessHeap(), 0, fmd.formatted);
return NULL; return NULL;
ignore_inserts: ignore_inserts:
default: default:
if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS)
ADD_TO_T('%'); format_add_char(&fmd, '%');
ADD_TO_T(*f++); format_add_char(&fmd, *f++);
break; break;
} }
} else { } else {
ch = *f; WCHAR ch = *f;
f++; f++;
if (ch == '\r') { if (ch == '\r') {
if (*f == '\n') if (*f == '\n')
f++; f++;
if(width) if(width)
ADD_TO_T(' '); format_add_char(&fmd, ' ');
else else
{ {
ADD_TO_T('\r'); format_add_char(&fmd, '\r');
ADD_TO_T('\n'); format_add_char(&fmd, '\n');
} }
} else { } else {
if (ch == '\n') if (ch == '\n')
{ {
if(width) if(width)
ADD_TO_T(' '); format_add_char(&fmd, ' ');
else else
{ {
ADD_TO_T('\r'); format_add_char(&fmd, '\r');
ADD_TO_T('\n'); format_add_char(&fmd, '\n');
} }
} }
else else
ADD_TO_T(ch); format_add_char(&fmd, ch);
} }
} }
} }
*t = '\0'; *fmd.t = '\0';
return target; return fmd.formatted;
} }
#undef ADD_TO_T
/*********************************************************************** /***********************************************************************
* FormatMessageA (KERNEL32.@) * FormatMessageA (KERNEL32.@)