kernel32: Merge the insertion and non-insertion codepaths in the format_message helper together to share common functionality.

This commit is contained in:
Andrew Nguyen 2010-04-28 04:20:48 -05:00 committed by Alexandre Julliard
parent 722fd58446
commit 57636255ef
2 changed files with 171 additions and 101 deletions

View File

@ -285,103 +285,78 @@ static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr
} while (0) } while (0)
f = fmtstr; f = fmtstr;
if (dwFlags & FORMAT_MESSAGE_IGNORE_INSERTS) { while (*f && !eos) {
while (*f && !eos) { if (*f=='%') {
if (*f=='%') { int insertnr;
f++; WCHAR *str,*x;
switch (*f)
{ f++;
case '0': switch (*f) {
eos = TRUE; 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++;
insertnr = insertnr*10 + *f-'0';
f++; f++;
break; break;
case 'n':
ADD_TO_T('\r');
ADD_TO_T('\n');
f++;
break;
case 'r':
ADD_TO_T('\r');
f++;
break;
case 't':
ADD_TO_T('\t');
f++;
break;
case '\0':
SetLastError(ERROR_INVALID_PARAMETER);
HeapFree(GetProcessHeap(), 0, target);
return NULL;
default: 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');
f++;
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('%');
ADD_TO_T(*f++);
break;
}
}
else
ADD_TO_T(*f++); ADD_TO_T(*f++);
} break;
} }
else { } else {
while (*f && !eos) { ch = *f;
if (*f=='%') { f++;
int insertnr; if (ch == '\r') {
WCHAR *str,*x; if (*f == '\n')
f++;
f++; if(width)
if (!*f) { ADD_TO_T(' ');
SetLastError(ERROR_INVALID_PARAMETER); else
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('\r');
ADD_TO_T('\n'); ADD_TO_T('\n');
f++;
break;
case 'r':
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;
} }
} else { } else {
ch = *f; if (ch == '\n')
f++; {
if (ch == '\r') {
if (*f == '\n')
f++;
if(width) if(width)
ADD_TO_T(' '); ADD_TO_T(' ');
else else
@ -389,20 +364,9 @@ static LPWSTR format_message( BOOL unicode_caller, DWORD dwFlags, LPCWSTR fmtstr
ADD_TO_T('\r'); ADD_TO_T('\r');
ADD_TO_T('\n'); 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);
} }
} }
} }

View File

@ -789,6 +789,56 @@ static void test_message_ignore_inserts(void)
sizeof(out)/sizeof(CHAR), NULL); sizeof(out)/sizeof(CHAR), NULL);
ok(ret == 4, "Expected FormatMessageA to return 4, got %d\n", ret); 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); 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) 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_t12oos99[] = {'t','e','s','t','%','1','%','2','!','*','.','*','s','!','%','9','9',0};
static const WCHAR fmt_pctspacedot[] = {'%','%','%',' ','%','.','%','!',0}; static const WCHAR fmt_pctspacedot[] = {'%','%','%',' ','%','.','%','!',0};
static const WCHAR fmt_nrt[] = {'%','n','%','r','%','t',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_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; DWORD ret;
WCHAR out[256]; WCHAR out[256];
@ -847,6 +907,52 @@ static void test_message_ignore_inserts_wide(void)
sizeof(out)/sizeof(WCHAR), NULL); sizeof(out)/sizeof(WCHAR), NULL);
ok(ret == 4, "Expected FormatMessageW to return 4, got %d\n", ret); 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)); 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) static void test_message_insufficient_buffer(void)