From 57636255efd1b5bf5f18b2053227dc48898ba5dd Mon Sep 17 00:00:00 2001 From: Andrew Nguyen Date: Wed, 28 Apr 2010 04:20:48 -0500 Subject: [PATCH] kernel32: Merge the insertion and non-insertion codepaths in the format_message helper together to share common functionality. --- dlls/kernel32/format_msg.c | 162 +++++++++++++++------------------------ dlls/kernel32/tests/format_msg.c | 106 +++++++++++++++++++++++++ 2 files changed, 169 insertions(+), 99 deletions(-) diff --git a/dlls/kernel32/format_msg.c b/dlls/kernel32/format_msg.c index c93514f31eb..302a78d928c 100644 --- a/dlls/kernel32/format_msg.c +++ b/dlls/kernel32/format_msg.c @@ -285,103 +285,78 @@ static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr } while (0) f = fmtstr; - if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) { - while (*f && !eos) { - if (*f=='%') { - f++; - switch (*f) - { - case '0': - eos = TRUE; - f++; - break; - case 'n': - ADD_TO_T('\r'); - ADD_TO_T('\n'); + while (*f && !eos) { + if (*f=='%') { + int insertnr; + WCHAR *str,*x; + + f++; + switch (*f) { + case '1':case '2':case '3':case '4':case '5': + case '6':case '7':case '8':case '9': + if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) + goto ignore_inserts; + insertnr = *f-'0'; + switch (f[1]) { + case '0':case '1':case '2':case '3': + case '4':case '5':case '6':case '7': + case '8':case '9': f++; - break; - case 'r': - ADD_TO_T('\r'); - f++; - break; - case 't': - ADD_TO_T('\t'); + insertnr = insertnr*10 + *f-'0'; f++; break; - case '\0': - SetLastError(ERROR_INVALID_PARAMETER); - HeapFree(GetProcessHeap(), 0, target); - return NULL; default: - ADD_TO_T('%'); - ADD_TO_T(*f++); + f++; break; } - } - else - ADD_TO_T(*f++); - } - } - else { - while (*f && !eos) { - if (*f=='%') { - int insertnr; - WCHAR *str,*x; - + f = format_insert( unicode_caller, insertnr, f, dwFlags, format_args, &str ); + for (x = str; *x; x++) ADD_TO_T(*x); + HeapFree( GetProcessHeap(), 0, str ); + break; + case 'n': + ADD_TO_T('\r'); + ADD_TO_T('\n'); f++; - if (!*f) { - SetLastError(ERROR_INVALID_PARAMETER); - HeapFree(GetProcessHeap(), 0, target); - return NULL; - } - - switch (*f) { - case '1':case '2':case '3':case '4':case '5': - case '6':case '7':case '8':case '9': - insertnr = *f-'0'; - switch (f[1]) { - case '0':case '1':case '2':case '3': - case '4':case '5':case '6':case '7': - case '8':case '9': - f++; - insertnr = insertnr*10 + *f-'0'; - f++; - break; - default: - f++; - break; - } - f = format_insert( unicode_caller, insertnr, f, dwFlags, format_args, &str ); - for (x = str; *x; x++) ADD_TO_T(*x); - HeapFree( GetProcessHeap(), 0, str ); - break; - case 'n': - ADD_TO_T('\r'); - ADD_TO_T('\n'); + break; + case 'r': + ADD_TO_T('\r'); + f++; + break; + case 't': + ADD_TO_T('\t'); + f++; + break; + case '0': + eos = TRUE; + f++; + break; + case '\0': + SetLastError(ERROR_INVALID_PARAMETER); + HeapFree(GetProcessHeap(), 0, target); + return NULL; + ignore_inserts: + default: + if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) + ADD_TO_T('%'); + ADD_TO_T(*f++); + break; + } + } else { + ch = *f; + f++; + if (ch == '\r') { + if (*f == '\n') f++; - break; - case 'r': + if(width) + ADD_TO_T(' '); + else + { ADD_TO_T('\r'); - f++; - break; - case 't': - ADD_TO_T('\t'); - f++; - break; - case '0': - eos = TRUE; - f++; - break; - default: - ADD_TO_T(*f++); - break; + ADD_TO_T('\n'); } } else { - ch = *f; - f++; - if (ch == '\r') { - if (*f == '\n') - f++; + if (ch == '\n') + { if(width) ADD_TO_T(' '); else @@ -389,20 +364,9 @@ static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr ADD_TO_T('\r'); ADD_TO_T('\n'); } - } else { - if (ch == '\n') - { - if(width) - ADD_TO_T(' '); - else - { - ADD_TO_T('\r'); - ADD_TO_T('\n'); - } - } - else - ADD_TO_T(ch); } + else + ADD_TO_T(ch); } } } diff --git a/dlls/kernel32/tests/format_msg.c b/dlls/kernel32/tests/format_msg.c index 397665c42fc..16090909f77 100644 --- a/dlls/kernel32/tests/format_msg.c +++ b/dlls/kernel32/tests/format_msg.c @@ -789,6 +789,56 @@ static void test_message_ignore_inserts(void) sizeof(out)/sizeof(CHAR), NULL); ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret); ok(!strcmp("\r\n\r\t", out), "Expected output string \"\\r\\n\\r\\t\", got %s\n", out); + + /* CRLF characters are processed normally. */ + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "hi\n", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret); + ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out); + + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "hi\r\n", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret); + ok(!strcmp("hi\r\n", out), "Expected output string \"hi\\r\\n\", got %s\n", out); + + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "\r", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(ret == 2, "Expected FormatMessageA to return 2, got %d\n", ret); + ok(!strcmp("\r\n", out), "Expected output string \"\\r\\n\", got %s\n", out); + + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, "\r\r\n", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret); + ok(!strcmp("\r\n\r\n", out), "Expected output string \"\\r\\n\\r\\n\", got %s\n", out); + + /* The width parameter is handled the same also. */ + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\n", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(!strcmp("hi ", out) || + broken(!strcmp("hi\r\n", out)), /* Win9x */ + "Expected output string \"hi \", got %s\n", out); + ok(ret == 3 || + broken(ret == 4), /* Win9x */ + "Expected FormatMessageA to return 3, got %d\n", ret); + + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, "hi\r\n", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(ret == 3, "Expected FormatMessageA to return 3, got %d\n", ret); + ok(!strcmp("hi ", out), "Expected output string \"hi \", got %s\n", out); + + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(ret == 1, "Expected FormatMessageA to return 1, got %d\n", ret); + ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out); + + ret = FormatMessageA(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, "\r\r\n", 0, 0, out, + sizeof(out)/sizeof(CHAR), NULL); + ok(ret == 2, "Expected FormatMessageA to return 2, got %d\n", ret); + ok(!strcmp(" ", out), "Expected output string \" \", got %s\n", out); } static void test_message_ignore_inserts_wide(void) @@ -801,8 +851,18 @@ static void test_message_ignore_inserts_wide(void) static const WCHAR fmt_t12oos99[] = {'t','e','s','t','%','1','%','2','!','*','.','*','s','!','%','9','9',0}; static const WCHAR fmt_pctspacedot[] = {'%','%','%',' ','%','.','%','!',0}; static const WCHAR fmt_nrt[] = {'%','n','%','r','%','t',0}; + static const WCHAR fmt_hi_lf[] = {'h','i','\n',0}; + static const WCHAR fmt_hi_crlf[] = {'h','i','\r','\n',0}; + static const WCHAR fmt_cr[] = {'\r',0}; + static const WCHAR fmt_crcrlf[] = {'\r','\r','\n',0}; static const WCHAR s_nrt[] = {'\r','\n','\r','\t',0}; + static const WCHAR s_hi_crlf[] = {'h','i','\r','\n',0}; + static const WCHAR s_crlf[] = {'\r','\n',0}; + static const WCHAR s_crlfcrlf[] = {'\r','\n','\r','\n',0}; + static const WCHAR s_hi_sp[] = {'h','i',' ',0}; + static const WCHAR s_sp[] = {' ',0}; + static const WCHAR s_2sp[] = {' ',' ',0}; DWORD ret; WCHAR out[256]; @@ -847,6 +907,52 @@ static void test_message_ignore_inserts_wide(void) sizeof(out)/sizeof(WCHAR), NULL); ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret); ok(!lstrcmpW(s_nrt, out), "Expected output string \"\\r\\n\\r\\t\", got %s\n", wine_dbgstr_w(out)); + + /* CRLF characters are processed normally. */ + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_hi_lf, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret); + ok(!lstrcmpW(s_hi_crlf, out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out)); + + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_hi_crlf, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret); + ok(!lstrcmpW(s_hi_crlf, out), "Expected output string \"hi\\r\\n\", got %s\n", wine_dbgstr_w(out)); + + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_cr, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 2, "Expected FormatMessageW to return 2, got %d\n", ret); + ok(!lstrcmpW(s_crlf, out), "Expected output string \"\\r\\n\", got %s\n", wine_dbgstr_w(out)); + + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS, fmt_crcrlf, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret); + ok(!lstrcmpW(s_crlfcrlf, out), "Expected output string \"\\r\\n\\r\\n\", got %s\n", wine_dbgstr_w(out)); + + /* The width parameter is handled the same also. */ + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_lf, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 3, "Expected FormatMessageW to return 3, got %d\n", ret); + ok(!lstrcmpW(s_hi_sp, out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out)); + + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_hi_crlf, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 3, "Expected FormatMessageW to return 3, got %d\n", ret); + ok(!lstrcmpW(s_hi_sp, out), "Expected output string \"hi \", got %s\n", wine_dbgstr_w(out)); + + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_cr, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 1, "Expected FormatMessageW to return 1, got %d\n", ret); + ok(!lstrcmpW(s_sp, out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out)); + + ret = FormatMessageW(FORMAT_MESSAGE_FROM_STRING | FORMAT_MESSAGE_IGNORE_INSERTS | + FORMAT_MESSAGE_MAX_WIDTH_MASK, fmt_crcrlf, 0, 0, out, + sizeof(out)/sizeof(WCHAR), NULL); + ok(ret == 2, "Expected FormatMessageW to return 2, got %d\n", ret); + ok(!lstrcmpW(s_2sp, out), "Expected output string \" \", got %s\n", wine_dbgstr_w(out)); } static void test_message_insufficient_buffer(void) -- 2.11.4.GIT