made _read() non-greedy - it now returns as soon as any amount of data has been read. It's the expected behavior for line-buffered streams (KJK::Hyperion)

svn path=/trunk/; revision=2910
This commit is contained in:
KJK::Hyperion 2002-05-05 17:18:50 +00:00
parent ee0b635648
commit 11cc443546

View file

@ -5,7 +5,10 @@
* PURPOSE: Reads a file * PURPOSE: Reads a file
* PROGRAMER: Boudewijn Dekker * PROGRAMER: Boudewijn Dekker
* UPDATE HISTORY: * UPDATE HISTORY:
* 28/12/98: Created * 28/12/1998: Created
* 03/05/2002: made _read() non-greedy - it now returns as soon as
* any amount of data has been read. It's the expected
* behavior for line-buffered streams (KJK::Hyperion)
*/ */
#include <windows.h> #include <windows.h>
#include <msvcrt/io.h> #include <msvcrt/io.h>
@ -16,38 +19,49 @@
size_t _read(int _fd, void *_buf, size_t _nbyte) size_t _read(int _fd, void *_buf, size_t _nbyte)
{ {
DWORD _rbyte = 0, nbyte = _nbyte, count; DWORD _rbyte = 0, nbyte = _nbyte;
int cr;
char *bufp = (char*)_buf; char *bufp = (char*)_buf;
HANDLE hfile;
int istext;
DPRINT("_read(fd %d, buf %x, nbyte %d)\n", _fd, _buf, _nbyte); DPRINT("_read(fd %d, buf %x, nbyte %d)\n", _fd, _buf, _nbyte);
while (nbyte) /* null read */
{ if(_nbyte == 0)
if (!ReadFile(_get_osfhandle(_fd), bufp, nbyte, &_rbyte, NULL)) return 0;
hfile = _get_osfhandle(_fd);
istext = __fileno_getmode(_fd) & O_TEXT;
/* read data */
if (!ReadFile(hfile, bufp, nbyte, &_rbyte, NULL))
{ {
/* failure */
return -1; return -1;
} }
if (_rbyte == 0)
break; /* text mode */
if (__fileno_getmode(_fd) & O_TEXT) if (_rbyte && istext)
{ {
cr = 0; int cr = 0;
count = _rbyte; DWORD count = _rbyte;
while (count)
/* repeat for all bytes in the buffer */
for(; count; bufp++, count--)
{ {
/* carriage return */
if (*bufp == '\r') if (*bufp == '\r')
cr++; cr++;
/* shift characters back, to ignore carriage returns */
else if (cr != 0) else if (cr != 0)
*(bufp - cr) = *bufp; *(bufp - cr) = *bufp;
bufp++;
count--;
} }
/* ignore the carriage returns */
_rbyte -= cr; _rbyte -= cr;
bufp -= cr;
} }
nbyte -= _rbyte;
} DPRINT("%d\n", _rbyte);
DPRINT("%d\n", _nbyte - nbyte); return _rbyte;
return _nbyte - nbyte;
} }