Rough implementation of _O_TEXT/_O_BINARY Handling

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



Changelog:
	dlls/msvcrt/file.c:
	Rough implementation of _O_TEXT/_O_BINARY translation by doing
	single byte read/writes in _O_TEXT mode an CR/LR handling
	fput/getwc must read multibyte characters in _O_TEXT
	dll/msvcrt/tests/file.c:
	Test cases for _O_TEXT/_O_BINARY file handling and fgetws
-- 
Uwe Bonnes                bon@elektron.ikp.physik.tu-darmstadt.de

Institut fuer Kernphysik  Schlossgartenstrasse 9  64289 Darmstadt
--------- Tel. 06151 162516 -------- Fax. 06151 164321 ----------
Index: wine/dlls/msvcrt/tests/file.c
===================================================================
RCS file: /home/wine/wine/dlls/msvcrt/tests/file.c,v
retrieving revision 1.1
diff -u -r1.1 file.c
--- wine/dlls/msvcrt/tests/file.c	17 Dec 2002 04:18:07 -0000	1.1
+++ wine/dlls/msvcrt/tests/file.c	17 Feb 2003 21:56:39 -0000
@@ -19,8 +19,11 @@
  */
 
 #include <windef.h>
+#include <winnls.h>
+#include <stdlib.h>
 #include <stdio.h>
 #include <fcntl.h>
+#include <sys/stat.h>
 #include <io.h>
 
 #include "wine/test.h"
@@ -31,7 +34,7 @@
 	int fd;
 	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));
 	close (fd);
 
@@ -44,8 +47,140 @@
 	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)
 {
     test_fdopen();
+    test_fgetwc();
+    test_file_put_get();
+    test_file_write_read();
 }
Index: wine/dlls/msvcrt/file.c
===================================================================
RCS file: /home/wine/wine/dlls/msvcrt/file.c,v
retrieving revision 1.46
diff -u -r1.46 file.c
--- wine/dlls/msvcrt/file.c	11 Jan 2003 21:01:04 -0000	1.46
+++ wine/dlls/msvcrt/file.c	17 Feb 2003 21:57:08 -0000
@@ -950,7 +950,10 @@
       pmode = va_arg(ap, int);
     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)
       creation = CREATE_NEW;
@@ -969,17 +972,15 @@
   if (oflags & _O_APPEND)
     ioflag |= MSVCRT__IOAPPEND;
 
-
-  oflags |= _O_BINARY; /* FIXME: Default to text */
-
-  if (oflags & _O_TEXT)
-  {
-    /* Dont warn when writing */
-    if (ioflag & GENERIC_READ)
-      FIXME(":TEXT node not implemented\n");
-    oflags &= ~_O_TEXT;
-  }
-
+  if (oflags & _O_BINARY)
+    ioflag |= _O_BINARY;
+  else if (oflags & _O_TEXT)
+    ioflag |= _O_TEXT;
+  else if (*__p__fmode() & _O_BINARY)
+    ioflag |= _O_BINARY;
+  else
+    ioflag |= _O_TEXT; /* default to TEXT*/
+  
   switch( shflags )
   {
     case _SH_DENYRW:
@@ -1062,13 +1063,17 @@
 int _open( const char *path, int flags, ... )
 {
   va_list ap;
-  int pmode;
-
-  va_start(ap, flags);
-  pmode = va_arg(ap, int);
-  va_end(ap);
 
-  return MSVCRT__sopen( path, flags, _SH_DENYNO, pmode );
+  if (flags & _O_CREAT)
+  {
+    int pmode;
+    va_start(ap, flags);
+    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 @@
   if (hand == INVALID_HANDLE_VALUE)
     return -1;
 
-  if (ReadFile(hand, buf, count, &num_read, NULL))
-  {
-    if (num_read != count && MSVCRT_files[fd])
+  if (MSVCRT_flags[fd]& _O_BINARY)
     {
-      TRACE(":EOF\n");
-	MSVCRT_flags[fd] |= MSVCRT__IOEOF;
-/*
-      MSVCRT_files[fd]->_flag |= MSVCRT__IOEOF;
-*/
+      if (ReadFile(hand, buf, count, &num_read, NULL))
+	{
+	  if (num_read != count && MSVCRT_files[fd])
+	    {
+	      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;
-  }
-  TRACE(":failed-last error (%ld)\n",GetLastError());
-  if (MSVCRT_files[fd])
-     MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
-  return -1;
+  else
+    {
+      char cc, *s=(char*)buf,* buf_start=(char*)buf;
+      unsigned int i;
+      
+      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 _setmode(int fd,int mode)
 {
-  if (mode & _O_TEXT)
-    FIXME("fd (%d) mode (%d) TEXT not implemented\n",fd,mode);
-  return 0;
+  int ret = MSVCRT_flags[fd] & (_O_TEXT | _O_BINARY);
+  if (mode & (~(_O_TEXT|_O_BINARY)))
+    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 @@
     TRACE(":fd (%d) handle (%d) buf (%p) len (%d)\n",fd,hand,buf,count);
 #endif
   if (hand == INVALID_HANDLE_VALUE)
-    return -1;
+    {
+      *MSVCRT__errno() = MSVCRT_EBADF;
+      return -1;
+    }
 
   /* If appending, go to EOF */
   if (MSVCRT_flags[fd] & MSVCRT__IOAPPEND)
     _lseek(fd, 0, FILE_END);
 
-  if (WriteFile(hand, buf, count, &num_written, NULL)
-      &&  (num_written == count))
-    return num_written;
-
-  TRACE(":failed-last error (%ld)\n",GetLastError());
-  if (MSVCRT_files[fd])
-     MSVCRT_files[fd]->_flag |= MSVCRT__IOERR;
-
+  if (MSVCRT_flags[fd] & _O_BINARY)
+    {
+      if (WriteFile(hand, buf, count, &num_written, NULL)
+	  &&  (num_written == count))
+	return num_written;
+      TRACE(":failed-last error (%ld)\n",GetLastError());
+      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;
 }
 
@@ -1591,7 +1671,7 @@
 
   for(cc = MSVCRT_fgetc(file); cc != MSVCRT_EOF && cc != '\n';
       cc = MSVCRT_fgetc(file))
-    if (cc != '\r')
+    /* _read already handled the translation */
     {
       if (--size <= 0) break;
       *s++ = (char)cc;
@@ -1605,19 +1685,36 @@
     if (--size > 0)
       *s++ = '\n';
   *s = '\0';
-  TRACE(":got '%s'\n", buf_start);
+  TRACE(":got '%s'\n", debugstr_a(buf_start));
   return buf_start;
 }
 
 /*********************************************************************
  *		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_wchar_t wc;
-  if (_read(file->_file, &wc, sizeof(wc)) != sizeof(wc))
+  char c;
+
+  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 wc;
+  else
+    return (MSVCRT_wint_t)c;
 }
 
 /*********************************************************************
@@ -1657,7 +1754,7 @@
 
   for(cc = MSVCRT_fgetwc(file); cc != MSVCRT_WEOF && cc != L'\n';
       cc = MSVCRT_fgetwc(file))
-    if (cc != L'\r')
+    /* _read already handled the translation */
     {
       if (--size <= 0) break;
       *s++ = cc;
@@ -2021,8 +2118,13 @@
  */
 int MSVCRT_fputs(const char *s, MSVCRT_FILE* file)
 {
-    size_t len = strlen(s);
-    return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
+    size_t i, len = strlen(s);
+    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_fputws(const MSVCRT_wchar_t *s, MSVCRT_FILE* file)
 {
-    size_t len = strlenW(s);
-    return MSVCRT_fwrite(s,sizeof(*s),len,file) == len ? 0 : MSVCRT_EOF;
+    size_t i, len = strlenW(s);
+    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;
 }
 
 /*********************************************************************


[Index of Archives]     [Gimp for Windows]     [Red Hat]     [Samba]     [Yosemite Camping]     [Graphics Cards]     [Wine Home]

  Powered by Linux