msvcrt: Improve CR CR LF handling.

This commit is contained in:
Dan Kegel 2007-10-04 20:43:31 -07:00 committed by Alexandre Julliard
parent 75c13b8e5d
commit 4cd3a16f18
2 changed files with 30 additions and 5 deletions

View File

@ -1639,15 +1639,21 @@ int CDECL _rmtmp(void)
/*********************************************************************
* (internal) remove_cr
*
* Remove all \r inplace.
* Translate all \r\n to \n inplace.
* return the number of \r removed
* Corner cases required by some apps:
* \r\r\n -> \r\n
* BUG: should save state across calls somehow, so CR LF that
* straddles buffer boundary gets recognized properly?
*/
static unsigned int remove_cr(char *buf, unsigned int count)
{
unsigned int i, j;
for (i = 0; i < count; i++) if (buf[i] == '\r') break;
for (j = i + 1; j < count; j++) if (buf[j] != '\r') buf[i++] = buf[j];
for (i=0, j=0; j < count; j++)
if ((buf[j] != '\r') || ((j+1) < count && buf[j+1] != '\n'))
buf[i++] = buf[j];
return count - i;
}
@ -2210,7 +2216,8 @@ int CDECL MSVCRT_fgetc(MSVCRT_FILE* file)
j = *i;
} else
j = MSVCRT__filbuf(file);
if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT) || (j != '\r'))
if (!(MSVCRT_fdesc[file->_file].wxflag & WX_TEXT)
|| ((j != '\r') || (file->_cnt && ((char *)file->_ptr)[0] != '\n')))
return j;
} while(1);
}

View File

@ -271,6 +271,23 @@ static void test_readmode( BOOL ascii_mode )
unlink ("fdopen.tst");
}
static void test_asciimode(void)
{
FILE *fp;
char buf[64];
/* Simple test of CR CR LF handling. Test both fgets and fread code paths, they're different! */
fp = fopen("ascii.tst", "wb");
fputs("\r\r\n", fp);
fclose(fp);
fp = fopen("ascii.tst", "rt");
ok(fgets(buf, sizeof(buf), fp) != NULL, "fgets\n");
ok(0 == strcmp(buf, "\r\n"), "CR CR LF not read as CR LF\n");
rewind(fp);
ok((fread(buf, 1, sizeof(buf), fp) == 2) && (0 == strcmp(buf, "\r\n")), "CR CR LF not read as CR LF\n");
fclose(fp);
unlink("ascii.tst");
}
static WCHAR* AtoW( const char* p )
{
@ -996,6 +1013,7 @@ START_TEST(file)
test_fdopen();
test_fopen_fclose_fcloseall();
test_fileops();
test_asciimode();
test_readmode(FALSE); /* binary mode */
test_readmode(TRUE); /* ascii mode */
test_fgetc();