//read.c
if (_osfile(fh) & FTEXT) {
/* now must translate CR-LFs to LFs in the buffer */
/* For ANSI & UTF8, we read byte by byte.
For UTF16, we need to read 2 bytes (wchar_t's) at a time */
if(tmode != __IOINFO_TM_UTF16LE) {
/* set CRLF flag to indicate LF at beginning of buffer */
if ( (os_read != 0) && (*(char *)buf == LF) )
_osfile(fh) |= FCRLF;
else
_osfile(fh) &= ~FCRLF;
/* convert chars in the buffer: p is src, q is dest */
p = q = buf;
while (p < (char *)buf + bytes_read) {
if (*p == CTRLZ) {
/* if fh is not a device, set ctrl-z flag */
if ( !(_osfile(fh) & FDEV) )
_osfile(fh) |= FEOFLAG;
else
*q++ = *p++;
break; /* stop translating */
}
else if (*p != CR)
*q++ = *p++;
else {
/* *p is CR, so must check next char for LF */
if (p < (char *)buf + bytes_read - 1) {
if (*(p+1) == LF) {
p += 2;
*q++ = LF; /* convert CR-LF to LF */
}
else
*q++ = *p++; /* store char normally */
}
else {
/* This is the hard part. We found a CR at end of
buffer. We must peek ahead to see if next char
is an LF. */
++p;
dosretval = 0;
if ( !ReadFile( (HANDLE)_osfhnd(fh), &peekchr, 1,
(LPDWORD)&os_read, NULL ) )
dosretval = GetLastError();
if (dosretval != 0 || os_read == 0) {
/* couldn't read ahead, store CR */
*q++ = CR;
}
else {
/*
* peekchr now has the extra character -- we now
* have several possibilities:
*
* 1. disk file and char is not LF; just seek
* back and copy CR
* 2. disk file and char is LF; seek back and
* discard CR
* 3. disk file, char is LF but this is a
* one-byte read: store LF, don't seek back
* 4. pipe/device and char is LF; store LF.
* 5. pipe/device and char isn't LF, store CR
* and put char in pipe lookahead buffer.
*/
if (_osfile(fh) & (FDEV|FPIPE)) {
/* non-seekable device */
if (peekchr == LF)
*q++ = LF;
else {
*q++ = CR;
_pipech(fh) = peekchr;
}
}
else {
/* disk file */
if (q == buf && peekchr == LF) {
/* nothing read yet; must make some
progress */
*q++ = LF;
}
else {
/* seek back */
filepos = _lseeki64_nolock(fh, -1i64, FILE_CURRENT);
if (peekchr != LF)
*q++ = CR;
}
}
}
}
}
}
部分代码,0x0a0d会被转换为0x0a, 0x1a(CTRLZ)只能在与设备关联的文件中出现
1. 文本写入的时候,过程与之相反
2. 由此可见,文本读取比较慢