[sword-cvs] swordreader/src/Dll1/winceSword/src unistd.cpp,1.2,1.3

sword@www.crosswire.org sword@www.crosswire.org
Sat, 17 Jan 2004 00:00:33 -0700


Update of /cvs/core/swordreader/src/Dll1/winceSword/src
In directory www:/tmp/cvs-serv16326/src/Dll1/winceSword/src

Modified Files:
	unistd.cpp 
Log Message:
redid io calls-- not done

Index: unistd.cpp
===================================================================
RCS file: /cvs/core/swordreader/src/Dll1/winceSword/src/unistd.cpp,v
retrieving revision 1.2
retrieving revision 1.3
diff -u -d -r1.2 -r1.3
--- unistd.cpp	17 Jan 2004 02:57:42 -0000	1.2
+++ unistd.cpp	17 Jan 2004 07:00:31 -0000	1.3
@@ -3,6 +3,11 @@
 #include <stdio.h>
 #include <windows.h>
 #include <swordce.h>
+#include <unistd.h>
+
+// TODO: make list of our own file handles to make this work
+// static list<int, HANDLE> openFileHandles
+// static int nextFileHandle = 1
 
 
 int close (int fd) {
@@ -12,28 +17,36 @@
 
 int open(const char *path, int mode) {
   const char *winPath = windizePath(path);
-  char cmode[4];
-  cmode[0] = cmode[1] = cmode[2] = cmode[3] = 0;
-  if (mode & O_RDWR) {
-    cmode[1] = '+';
-	cmode[0] = 'r';
-  }
-  if (mode & O_RDONLY)
-    cmode[0] = 'r';
-  if (mode & O_WRONLY)
-    cmode[0] = 'w';
+  DWORD access = 0;
+  DWORD create = OPEN_EXISTING;
+  DWORD share = FILE_SHARE_READ;
 
-  if ((mode & O_CREAT ) && (cmode[0] == 'w')) cmode[1] = '+';
-  if (mode & O_APPEND) {
-    if (cmode[0] == 'r') cmode[1] = '+';
-    cmode[0] = 'a';
+  if (mode & O_WRONLY)
+    access |= GENERIC_WRITE;
+  else {
+    access |= GENERIC_READ;
   }
-  if (mode & O_BINARY) {
-    cmode[1] ? (cmode[2] = 'b') : (cmode[1] = 'b');
+
+  if (mode & O_RDWR) {
+    access |= GENERIC_READ | GENERIC_WRITE;
   }
-  int result=(int)fopen(winPath, cmode);
 
-  if (!result) {
+  if (mode & O_CREAT)
+    create = OPEN_ALWAYS;
+  if (mode & O_TRUNC)
+	create = TRUNCATE_EXISTING;
+
+//  if (!(access & GENERIC_WRITE))
+//	share |= FILE_SHARE_WRITE;
+
+//  if (mode & O_BINARY)
+
+  HANDLE result = CreateFile(strtowstr(winPath), access, share, NULL, create, FILE_ATTRIBUTE_NORMAL, NULL);
+
+
+//  int result=(int)fopen(winPath, cmode);
+
+  if (result == INVALID_HANDLE_VALUE) {
 	  WCHAR buf[100];
 	  mbstowcs(buf,"file: ",6);
 	  mbstowcs(&buf[6],winPath,100);
@@ -41,9 +54,13 @@
 	  DWORD errorNr=GetLastError();
 	  _itow(errorNr,&buf[wcslen(buf)],10);
 	  MessageBox(0,buf,L"Unable to open:",MB_OK|MB_ICONERROR);
+	  result = (HANDLE)-1;
+  }
+  if (((int)result > -1) && (mode & O_APPEND)) {
+	  SetFilePointer(result, 0, 0, FILE_END);
   }
 
-  return result;
+  return (int)result;
 }
 
 
@@ -52,28 +69,45 @@
 }
 
 
-int read(int fd, void* buf, size_t count) {
-	return fread(buf, 1, count, (FILE*)fd);
+int read(int fd, void *buf, size_t count) {
+	DWORD readCount;
+	ReadFile((HANDLE)fd, buf, (DWORD)count, &readCount, NULL);
+	return (int)readCount;
+
 }
 
 
-int write(int fd, const void * buf, size_t count) {
-	return fwrite(buf, 1, count, (FILE*)fd);
+int write(int fd, const void *buf, size_t count) {
+	DWORD writtenCount;
+	WriteFile((HANDLE)fd, buf, (DWORD)count, &writtenCount, NULL);
+	return writtenCount;
 }
 
 //int write(int fd, const char * buf, size_t count)
 
-typedef long off_t;
 
 off_t lseek(int fildes, off_t offset, int whence) {
-	if (!(fseek((FILE*)fildes, offset, whence))) {
-		//fgetpos((FILE*)fildes, (fpos_t*)offset);
-		return offset;
-		//return 0; //success
+
+	DWORD wWence = 0;
+	switch (whence) {
+	case SEEK_SET:
+		wWence = FILE_BEGIN;
+		break;
+	case SEEK_CUR:
+		wWence = FILE_CURRENT;
+		break;
+	case SEEK_END:
+		wWence = FILE_END;
+		break;
 	}
-	else
-	  //return (off_t)-1;
-	  return -1;
+
+
+	off_t retVal = SetFilePointer((HANDLE)fildes, (LONG)offset, 0, wWence);
+
+	if (retVal == 0xFFFFFFFF)
+		retVal = -1;
+
+	return retVal;	
 }
 
 int access(const char* path, int mode) {