Author: cwittich
Date: Fri Oct 16 23:43:28 2009
New Revision: 43522
URL:
http://svn.reactos.org/svn/reactos?rev=43522&view=rev
Log:
fix all msvcrt file winetests
Modified:
trunk/reactos/lib/sdk/crt/stdio/file.c
Modified: trunk/reactos/lib/sdk/crt/stdio/file.c
URL:
http://svn.reactos.org/svn/reactos/trunk/reactos/lib/sdk/crt/stdio/file.c?r…
==============================================================================
--- trunk/reactos/lib/sdk/crt/stdio/file.c [iso-8859-1] (original)
+++ trunk/reactos/lib/sdk/crt/stdio/file.c [iso-8859-1] Fri Oct 16 23:43:28 2009
@@ -902,10 +902,18 @@
{
/* Flush output if needed */
if(file->_flag & _IOWRT)
- flush_buffer(file);
+ flush_buffer(file);
if(whence == SEEK_CUR && file->_flag & _IOREAD ) {
- offset -= file->_cnt;
+ offset -= file->_cnt;
+ if (fdesc[file->_file].wxflag & WX_TEXT) {
+ /* Black magic correction for CR removal */
+ int i;
+ for (i=0; i<file->_cnt; i++) {
+ if (file->_ptr[i] == '\n')
+ offset--;
+ }
+ }
}
/* Discard buffered input */
file->_cnt = 0;
@@ -1557,27 +1565,6 @@
}
/*********************************************************************
- * (internal) remove_cr
- *
- * 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, j=0; j < count; j++)
- if ((buf[j] != '\r') || ((j+1) < count && buf[j+1] !=
'\n'))
- buf[i++] = buf[j];
-
- return count - i;
-}
-
-/*********************************************************************
* (internal) read_i
*/
static int read_i(int fd, void *buf, unsigned int count)
@@ -1604,18 +1591,25 @@
{
if (fdesc[fd].wxflag & WX_TEXT)
{
- int i;
- /* in text mode, a ctrl-z signals EOF */
- for (i=0; i<num_read; i++)
+ DWORD i, j;
+ for (i=0, j=0; i<num_read; i++)
{
+ /* in text mode, a ctrl-z signals EOF */
if (bufstart[i] == 0x1a)
{
- num_read = i;
fdesc[fd].wxflag |= (WX_ATEOF|WX_READEOF);
TRACE(":^Z EOF %s\n",debugstr_an(buf,num_read));
break;
}
+ /* in text mode, strip \r if followed by \n.
+ * BUG: should save state across calls somehow, so CR LF that
+ * straddles buffer boundary gets recognized properly?
+ */
+ if ((bufstart[i] != '\r')
+ || ((i+1) < num_read && bufstart[i+1] !=
'\n'))
+ bufstart[j++] = bufstart[i];
}
+ num_read = j;
}
if (count != 0 && num_read == 0)
{
@@ -1650,10 +1644,6 @@
{
int num_read;
num_read = read_i(fd, buf, count);
- if (num_read>0 && fdesc[fd].wxflag & WX_TEXT)
- {
- num_read -= remove_cr(buf,num_read);
- }
return num_read;
}
@@ -1955,17 +1945,13 @@
{
unsigned char *i;
unsigned int j;
- do {
- if (file->_cnt>0) {
- file->_cnt--;
- i = (unsigned char *)file->_ptr++;
- j = *i;
- } else
- j = _filbuf(file);
- if (!(fdesc[file->_file].wxflag & WX_TEXT)
- || ((j != '\r') || (file->_cnt && file->_ptr[0] !=
'\n')))
- return j;
- } while(1);
+ if (file->_cnt>0) {
+ file->_cnt--;
+ i = (unsigned char *)file->_ptr++;
+ j = *i;
+ } else
+ j = _filbuf(file);
+ return j;
}
/*********************************************************************
@@ -2300,11 +2286,13 @@
}
if(file->_bufsiz) {
int res=flush_buffer(file);
- return res?res : fputc(c, file);
+ return res?res : fputc(c, file);
} else {
- unsigned char cc=c;
+ unsigned char cc=c;
int len;
- len = _write(file->_file, &cc, 1);
+ /* set _cnt to 0 for unbuffered FILEs */
+ file->_cnt = 0;
+ len = _write(file->_file, &cc, 1);
if (len == 1) return c & 0xff;
file->_flag |= _IOERR;
return EOF;
@@ -2336,8 +2324,6 @@
memcpy(ptr, file->_ptr, pcnt);
file->_cnt -= pcnt;
file->_ptr += pcnt;
- if (fdesc[file->_file].wxflag & WX_TEXT)
- pcnt -= remove_cr(ptr,pcnt);
read += pcnt ;
rcnt -= pcnt ;
ptr = (char*)ptr + pcnt;
@@ -2493,17 +2479,26 @@
*/
LONG CDECL ftell(FILE* file)
{
+ /* TODO: just call fgetpos and return lower half of result */
int off=0;
long pos;
+ pos = _tell(file->_file);
+ if(pos == -1) return -1;
if(file->_bufsiz) {
- if( file->_flag & _IOWRT ) {
- off = file->_ptr - file->_base;
- } else {
- off = -file->_cnt;
- }
- }
- pos = _tell(file->_file);
- if(pos == -1) return pos;
+ if( file->_flag & _IOWRT ) {
+ off = file->_ptr - file->_base;
+ } else {
+ off = -file->_cnt;
+ if (fdesc[file->_file].wxflag & WX_TEXT) {
+ /* Black magic correction for CR removal */
+ int i;
+ for (i=0; i<file->_cnt; i++) {
+ if (file->_ptr[i] == '\n')
+ off--;
+ }
+ }
+ }
+ }
return off + pos;
}
@@ -2512,22 +2507,25 @@
*/
int CDECL fgetpos(FILE* file, fpos_t *pos)
{
- /* This code has been lifted form the ftell function */
int off=0;
-
*pos = _lseeki64(file->_file,0,SEEK_CUR);
-
- if (*pos == -1) return -1;
-
+ if(*pos == -1) return -1;
if(file->_bufsiz) {
- if( file->_flag & _IOWRT ) {
- off = file->_ptr - file->_base;
- } else {
- off = -file->_cnt;
- }
+ if( file->_flag & _IOWRT ) {
+ off = file->_ptr - file->_base;
+ } else {
+ off = -file->_cnt;
+ if (fdesc[file->_file].wxflag & WX_TEXT) {
+ /* Black magic correction for CR removal */
+ int i;
+ for (i=0; i<file->_cnt; i++) {
+ if (file->_ptr[i] == '\n')
+ off--;
+ }
+ }
+ }
}
*pos += off;
-
return 0;
}