Rough implementation of _O_TEXT/_O_BINARY translation by doing single

byte read/writes in _O_TEXT mode and CR/LR handling.
fput/getwc must read multibyte characters in _O_TEXT.
Added test cases for _O_TEXT/_O_BINARY file handling and fgetws.
This commit is contained in:
Uwe Bonnes 2003-02-19 22:09:33 +00:00 committed by Alexandre Julliard
parent 52a414c26d
commit f540ea642e
2 changed files with 312 additions and 66 deletions

View File

@ -950,7 +950,10 @@ int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
pmode = va_arg(ap, int); pmode = va_arg(ap, int);
va_end(ap); va_end(ap);
FIXME(": pmode 0x%04x ignored\n", pmode); if(pmode & ~(_S_IREAD | _S_IWRITE))
FIXME(": pmode 0x%04x ignored\n", pmode);
else
WARN(": pmode 0x%04x ignored\n", pmode);
if (oflags & _O_EXCL) if (oflags & _O_EXCL)
creation = CREATE_NEW; creation = CREATE_NEW;
@ -969,16 +972,14 @@ int MSVCRT__sopen( const char *path, int oflags, int shflags, ... )
if (oflags & _O_APPEND) if (oflags & _O_APPEND)
ioflag |= MSVCRT__IOAPPEND; ioflag |= MSVCRT__IOAPPEND;
if (oflags & _O_BINARY)
oflags |= _O_BINARY; /* FIXME: Default to text */ ioflag |= _O_BINARY;
else if (oflags & _O_TEXT)
if (oflags & _O_TEXT) ioflag |= _O_TEXT;
{ else if (*__p__fmode() & _O_BINARY)
/* Dont warn when writing */ ioflag |= _O_BINARY;
if (ioflag & GENERIC_READ) else
FIXME(":TEXT node not implemented\n"); ioflag |= _O_TEXT; /* default to TEXT*/
oflags &= ~_O_TEXT;
}
switch( shflags ) switch( shflags )
{ {
@ -1062,13 +1063,17 @@ int MSVCRT__wsopen( const MSVCRT_wchar_t* path, int oflags, int shflags, ... )
int _open( const char *path, int flags, ... ) int _open( const char *path, int flags, ... )
{ {
va_list ap; va_list ap;
int pmode;
va_start(ap, flags); if (flags & _O_CREAT)
pmode = va_arg(ap, int); {
va_end(ap); int pmode;
va_start(ap, flags);
return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode ); pmode = va_arg(ap, int);
va_end(ap);
return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
}
else
return MSVCRT__sopen( path, flags, _SH_DENYNO);
} }
/********************************************************************* /*********************************************************************
@ -1159,22 +1164,56 @@ int _read(int fd, void *buf, unsigned int count)
if (hand == INVALID_HANDLE_VALUE) if (hand == INVALID_HANDLE_VALUE)
return -1; return -1;
if (ReadFile(hand, buf, count, &num_read, NULL)) if (MSVCRT_flags[fd]& _O_BINARY)
{
if (num_read != count && MSVCRT_files[fd])
{ {
TRACE(":EOF\n"); if (ReadFile(hand, buf, count, &num_read, NULL))
MSVCRT_flags[fd] |= MSVCRT__IOEOF; {
/* if (num_read != count && MSVCRT_files[fd])
MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF; {
*/ TRACE(":EOF\n");
MSVCRT_flags[fd] |= MSVCRT__IOEOF;
/*
MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
*/
}
TRACE("%s\n",debugstr_an(buf,num_read));
return num_read;
}
TRACE(":failed-last error (%ld)\n",GetLastError());
if (MSVCRT_files[fd])
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
return -1;
} }
return num_read; else
} {
TRACE(":failed-last error (%ld)\n",GetLastError()); char cc, *s=(char*)buf,* buf_start=(char*)buf;
if (MSVCRT_files[fd]) unsigned int i;
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
return -1; for (i = 0 , num_read = 1; i < count && (num_read == 1);)
{
if (ReadFile(hand, &cc, 1, &num_read, NULL))
if (num_read == 1)
if ((cc != '\r') || MSVCRT_flags[fd] & _O_BINARY)
{
*s++ = (char)cc;
i++;
}
}
if (num_read != 1)
{
TRACE(":EOF\n");
if (MSVCRT_files[fd])
MSVCRT_flags[fd] |= MSVCRT__IOEOF;
/*
MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
*/
}
if (count > 4)
TRACE("%s\n",debugstr_an(buf_start, s-buf_start));
return s-buf_start;
}
return 0;
} }
/********************************************************************* /*********************************************************************
@ -1193,9 +1232,12 @@ int _getw(MSVCRT_FILE* file)
*/ */
int _setmode(int fd,int mode) int _setmode(int fd,int mode)
{ {
if (mode & _O_TEXT) int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY);
FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode); if (mode & (~(_O_TEXT|_O_BINARY)))
return 0; FIXME("fd (%d) mode (0x%08x) unknown\n",fd,mode);
MSVCRT_flags[fd] &= ~(_O_TEXT|_O_BINARY);
MSVCRT_flags[fd] |= mode & (_O_TEXT | _O_BINARY);
return ret;
} }
/********************************************************************* /*********************************************************************
@ -1437,20 +1479,58 @@ int _write(int fd, const void* buf, unsigned int count)
TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count); TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
#endif #endif
if (hand == INVALID_HANDLE_VALUE) if (hand == INVALID_HANDLE_VALUE)
return -1; {
*MSVCRT__errno() = MSVCRT_EBADF;
return -1;
}
/* If appending, go to EOF */ /* If appending, go to EOF */
if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND) if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
_lseek(fd, 0, FILE_END); _lseek(fd, 0, FILE_END);
if (WriteFile(hand, buf, count, &num_written, NULL) if (MSVCRT_flags[fd] & _O_BINARY)
&& (num_written == count)) {
return num_written; if (WriteFile(hand, buf, count, &num_written, NULL)
&& (num_written == count))
TRACE(":failed-last error (%ld)\n",GetLastError()); return num_written;
if (MSVCRT_files[fd]) TRACE(":failed-last error (%ld)\n",GetLastError());
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR; if (MSVCRT_files[fd])
{
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
*MSVCRT__errno() = MSVCRT_ENOSPC;
}
}
else
{
char *s=(char*)buf, *buf_start=(char*)buf, *p;
char crlf[]= {'\r','\n'};
unsigned int i;
DWORD num_to_write;
for (i = 0; i< count && !(MSVCRT_flags[fd] & MSVCRT__IOERR);i++, s++)
{
if (*s == '\n')
{
p = crlf;
num_to_write = 2;
}
else
{
p = s;
num_to_write = 1;
}
if ((WriteFile(hand, p, num_to_write, &num_written, NULL) == 0 ) || (num_written != num_to_write))
{
TRACE(":failed-last error (%ld) num_written %ld\n",GetLastError(),num_written);
if (MSVCRT_files[fd])
{
MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
*MSVCRT__errno() = MSVCRT_ENOSPC;
return s - buf_start;
}
}
}
return s - buf_start;
}
return -1; return -1;
} }
@ -1591,7 +1671,7 @@ char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n'; for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
cc = MSVCRT_fgetc(file)) cc = MSVCRT_fgetc(file))
if (cc != '\r') /* _read already handled the translation */
{ {
if (--size <= 0) break; if (--size <= 0) break;
*s++ = (char)cc; *s++ = (char)cc;
@ -1605,19 +1685,36 @@ char *MSVCRT_fgets(char *s, int size, MSVCRT_FILE* file)
if (--size > 0) if (--size > 0)
*s++ = '\n'; *s++ = '\n';
*s = '\0'; *s = '\0';
TRACE(":got '%s'\n", buf_start); TRACE(":got '%s'\n", debugstr_a(buf_start));
return buf_start; return buf_start;
} }
/********************************************************************* /*********************************************************************
* fgetwc (MSVCRT.@) * fgetwc (MSVCRT.@)
*
* In _O_TEXT mode, bultibyte characters are read from the file, dropping
* the CR from CR/LF combinations
*/ */
MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file) MSVCRT_wint_t MSVCRT_fgetwc(MSVCRT_FILE* file)
{ {
MSVCRT_wchar_t wc; char c;
if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
if (file->_flag & _O_BINARY)
{
MSVCRT_wchar_t wc;
if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
return MSVCRT_WEOF;
return wc;
}
c = MSVCRT_fgetc(file);
if ((*__p___mb_cur_max() > 1) && MSVCRT_isleadbyte(c))
{
FIXME("Treat Multibyte characters\n");
}
if (c == MSVCRT_EOF)
return MSVCRT_WEOF; return MSVCRT_WEOF;
return wc; else
return (MSVCRT_wint_t)c;
} }
/********************************************************************* /*********************************************************************
@ -1657,7 +1754,7 @@ MSVCRT_wchar_t *MSVCRT_fgetws(MSVCRT_wchar_t *s, int size, MSVCRT_FILE* file)
for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n'; for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
cc = MSVCRT_fgetwc(file)) cc = MSVCRT_fgetwc(file))
if (cc != L'\r') /* _read already handled the translation */
{ {
if (--size <= 0) break; if (--size <= 0) break;
*s++ = cc; *s++ = cc;
@ -2021,8 +2118,13 @@ MSVCRT_size_t MSVCRT_fwrite(const void *ptr, MSVCRT_size_t size, MSVCRT_size_t n
*/ */
int MSVCRT_fputs(const char *s, MSVCRT_FILE* file) int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
{ {
size_t len = strlen(s); size_t i, len = strlen(s);
return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF; if (file->_flag & _O_BINARY)
return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
for (i=0; i<len; i++)
if (MSVCRT_fputc(s[i], file) == MSVCRT_EOF)
return MSVCRT_EOF;
return 0;
} }
/********************************************************************* /*********************************************************************
@ -2030,8 +2132,17 @@ int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
*/ */
int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file) int MSVCRT_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
{ {
size_t len = strlenW(s); size_t i, len = strlenW(s);
return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF; if (file->_flag & _O_BINARY)
return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
for (i=0; i<len; i++)
{
if ((s[i] == L'\n') && (MSVCRT_fputc('\r', file) == MSVCRT_EOF))
return MSVCRT_WEOF;
if (MSVCRT_fputwc(s[i], file) == MSVCRT_WEOF)
return MSVCRT_WEOF;
}
return 0;
} }
/********************************************************************* /*********************************************************************

View File

@ -19,8 +19,11 @@
*/ */
#include <windef.h> #include <windef.h>
#include <winnls.h>
#include <stdlib.h>
#include <stdio.h> #include <stdio.h>
#include <fcntl.h> #include <fcntl.h>
#include <sys/stat.h>
#include <io.h> #include <io.h>
#include "wine/test.h" #include "wine/test.h"
@ -28,24 +31,156 @@
static void test_fdopen( void ) static void test_fdopen( void )
{ {
static char buffer[] = {0,1,2,3,4,5,6,7,8,9}; static char buffer[] = {0,1,2,3,4,5,6,7,8,9};
int fd; int fd;
FILE *file; FILE *file;
fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY); fd = open ("fdopen.tst", O_WRONLY | O_CREAT | O_BINARY, _S_IREAD |_S_IWRITE);
write (fd, buffer, sizeof (buffer)); write (fd, buffer, sizeof (buffer));
close (fd); close (fd);
fd = open ("fdopen.tst", O_RDONLY | O_BINARY); fd = open ("fdopen.tst", O_RDONLY | O_BINARY);
lseek (fd, 5, SEEK_SET); lseek (fd, 5, SEEK_SET);
file = fdopen (fd, "rb"); file = fdopen (fd, "rb");
ok (fread (buffer, 1, sizeof (buffer), file) == 5, "read wrong byte count"); ok (fread (buffer, 1, sizeof (buffer), file) == 5, "read wrong byte count");
ok (memcmp (buffer, buffer + 5, 5) == 0, "read wrong bytes"); ok (memcmp (buffer, buffer + 5, 5) == 0, "read wrong bytes");
fclose (file); fclose (file);
unlink ("fdopen.tst"); unlink ("fdopen.tst");
}
static WCHAR* AtoW( char* p )
{
WCHAR* buffer;
DWORD len = MultiByteToWideChar( CP_ACP, 0, p, -1, NULL, 0 );
buffer = malloc( len * sizeof(WCHAR) );
MultiByteToWideChar( CP_ACP, 0, p, -1, buffer, len );
return buffer;
}
static void test_fgetwc( void )
{
#define LLEN 512
char* tempf;
FILE *tempfh;
const char mytext[]= "This is test_fgetwc\n";
WCHAR wtextW[LLEN+1];
WCHAR *mytextW = NULL, *aptr, *wptr;
BOOL diff_found = FALSE;
int i;
tempf=_tempnam(".","wne");
tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
fputs(mytext,tempfh);
fclose(tempfh);
tempfh = fopen(tempf,"rt");
fgetws(wtextW,LLEN,tempfh);
mytextW = AtoW ((char*)mytext);
aptr = mytextW;
wptr = wtextW;
for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
{
diff_found |= (*aptr != *wptr);
}
ok(!(diff_found), "fgetwc difference found in TEXT mode");
if(mytextW) free (mytextW);
fclose(tempfh);
unlink(tempf);
}
static void test_file_put_get( void )
{
char* tempf;
FILE *tempfh;
const char mytext[]= "This is a test_file_put_get\n";
const char dostext[]= "This is a test_file_put_get\r\n";
char btext[LLEN];
WCHAR wtextW[LLEN+1];
WCHAR *mytextW = NULL, *aptr, *wptr;
BOOL diff_found = FALSE;
int i;
tempf=_tempnam(".","wne");
tempfh = fopen(tempf,"wt"); /* open in TEXT mode */
fputs(mytext,tempfh);
fclose(tempfh);
tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
fgets(btext,LLEN,tempfh);
ok( strlen(mytext) + 1 == strlen(btext),"TEXT/BINARY mode not handled for write");
ok( btext[strlen(mytext)-1] == '\r', "CR not written");
fclose(tempfh);
tempfh = fopen(tempf,"wb"); /* open in BINARY mode */
fputs(dostext,tempfh);
fclose(tempfh);
tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
fgets(btext,LLEN,tempfh);
ok(strcmp(btext, mytext) == 0,"_O_TEXT read doesn't strip CR");
fclose(tempfh);
tempfh = fopen(tempf,"rb"); /* open in TEXT mode */
fgets(btext,LLEN,tempfh);
ok(strcmp(btext, dostext) == 0,"_O_BINARY read doesn't preserve CR");
fclose(tempfh);
tempfh = fopen(tempf,"rt"); /* open in TEXT mode */
fgetws(wtextW,LLEN,tempfh);
mytextW = AtoW ((char*)mytext);
aptr = mytextW;
wptr = wtextW;
for (i=0; i<strlen(mytext); i++, aptr++, wptr++)
{
diff_found |= (*aptr != *wptr);
}
ok(!(diff_found), "fgetwc doesn't strip CR in TEXT mode");
if(mytextW) free (mytextW);
fclose(tempfh);
unlink(tempf);
}
static void test_file_write_read( void )
{
char* tempf;
int tempfd;
const char mytext[]= "This is test_file_write_read\nsecond line\n";
const char dostext[]= "This is test_file_write_read\r\nsecond line\r\n";
char btext[LLEN];
tempf=_tempnam(".","wne");
ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_TEXT|_O_RDWR,_S_IREAD | _S_IWRITE)) != -1,"Can't open"); /* open in TEXT mode */
ok(_write(tempfd,mytext,strlen(mytext)) == strlen(mytext), "_write _O_TEXT bad return value");
_close(tempfd);
tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
ok(_read(tempfd,btext,LLEN) == strlen(dostext), "_read _O_BINARY got bad length");
ok( memcmp(dostext,btext,strlen(dostext)) == 0,"problems with _O_TEXT _write and _O_BINARY _write");
ok( btext[strlen(dostext)-2] == '\r', "CR not written");
_close(tempfd);
tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
ok(_read(tempfd,btext,LLEN) == strlen(mytext), "_read _O_TEXT got bad length");
ok( memcmp(mytext,btext,strlen(mytext)) == 0,"problems with _O_TEXT _write / _write");
_close(tempfd);
ok(unlink(tempf) !=-1 ,"Can't unlink");
tempf=_tempnam(".","wne");
ok((tempfd = _open(tempf,_O_CREAT|_O_TRUNC|_O_BINARY|_O_RDWR,0)) != -1,"Can't open %s",tempf); /* open in BINARY mode */
ok(_write(tempfd,dostext,strlen(dostext)) == strlen(dostext), "_write _O_TEXT bad return value");
_close(tempfd);
tempfd = _open(tempf,_O_RDONLY|_O_BINARY,0); /* open in BINARY mode */
ok(_read(tempfd,btext,LLEN) == strlen(dostext), "_read _O_BINARY got bad length");
ok( memcmp(dostext,btext,strlen(dostext)) == 0,"problems with _O_TEXT _write and _O_BINARY _write");
ok( btext[strlen(dostext)-2] == '\r', "CR not written");
_close(tempfd);
tempfd = _open(tempf,_O_RDONLY|_O_TEXT); /* open in TEXT mode */
ok(_read(tempfd,btext,LLEN) == strlen(mytext), "_read _O_TEXT got bad length");
ok( memcmp(mytext,btext,strlen(mytext)) == 0,"problems with _O_TEXT _write / _write");
_close(tempfd);
unlink(tempf);
} }
START_TEST(file) START_TEST(file)
{ {
test_fdopen(); test_fdopen();
test_fgetwc();
test_file_put_get();
test_file_write_read();
} }