[sword-cvs] swordreader/src/Dll1/winceSword/src dirent.cpp,1.1.1.1,1.2 swordce.cpp,1.1.1.1,1.2 unistd.cpp,1.1.1.1,1.2

sword@www.crosswire.org sword@www.crosswire.org
Fri, 16 Jan 2004 19:57:44 -0700


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

Modified Files:
	dirent.cpp swordce.cpp unistd.cpp 
Log Message:
wince fixes


Index: dirent.cpp
===================================================================
RCS file: /cvs/core/swordreader/src/Dll1/winceSword/src/dirent.cpp,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- dirent.cpp	16 Jan 2004 17:34:27 -0000	1.1.1.1
+++ dirent.cpp	17 Jan 2004 02:57:42 -0000	1.2
@@ -1,98 +1,44 @@
 #include <dirent.h>
-
 #include <io.h>
-
-
-
-#ifdef _WIN32_WCE
-
 #include <swordce.h>
-
 #include <stdio.h>
+#include <swbuf.h>
 
-#endif
-
-
-
-DIR* opendir( const char *pSpec )
-
-{
+using sword::SWBuf;
 
+DIR* opendir(const char *pSpec) {
+	SWBuf dirPath = windizePath(pSpec);
+	dirPath += "\\*";
 	DIR *pDir = (DIR*) malloc(sizeof(DIR));
-
-#ifdef _WIN32_WCE
-
-	pDir->hFind = FindFirstFile(strtowstr(strcmp(pSpec, ".") ? pSpec : "*"), &pDir->wfd);
-
-#else
-
-	pDir->hFind = FindFirstFile((strcmp(pSpec, ".") ? pSpec : "*"), &pDir->wfd);
-
-#endif
-
+	pDir->hFind = FindFirstFile(strtowstr(dirPath), &pDir->wfd);
 	return pDir;
-
 }
 
 
-
-void closedir(DIR * pDir)
-
-{
-
+void closedir(DIR * pDir) {
 	FindClose( pDir->hFind );
-
     free(pDir);
-
 }
 
 
-
-struct dirent* readdir(DIR *pDir)
-
-{
-
-	if (pDir->hFind)
-
-	{
-
-#ifdef _WIN32_WCE
-
+struct dirent* readdir(DIR *pDir) {
+	if (pDir->hFind) {
 		strcpy(pDir->de.d_name, wstrtostr(pDir->wfd.cFileName));
-
-#else
-
-		strcpy(pDir->de.d_name, pDir->wfd.cFileName);
-
-#endif
-
-		
-
 		if ( !FindNextFile(pDir->hFind, &pDir->wfd) )
-
 			pDir->hFind = NULL;
 
-
-
 		return &pDir->de;
-
 	}
 
 	return NULL;
-
 }
 
 
-
-void rewinddir( DIR* dir )
-
-{
-
+void rewinddir(DIR* dir) {
 	lseek( (int) dir->hFind,0,SEEK_SET);
-
 }
 
 
 
-#define fclose(f)  { if (f!=NULL) fclose(f); }
+//#define fclose(f)  { if (f!=NULL) fclose(f); }
 

Index: swordce.cpp
===================================================================
RCS file: /cvs/core/swordreader/src/Dll1/winceSword/src/swordce.cpp,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- swordce.cpp	16 Jan 2004 17:34:27 -0000	1.1.1.1
+++ swordce.cpp	17 Jan 2004 02:57:42 -0000	1.2
@@ -1,4 +1,14 @@
 #include <swordce.h>
+#include <swbuf.h>
+
+using sword::SWBuf;
+// TODO: from the DLL entry point, figure out the path
+// to the dll file and use that for cwd
+const char *getWorkingDirectory() {
+	static const char *cwd = "\\Program Files\\sword";
+	return cwd;
+}
+
 
 wchar_t *strtowstr(const char *str) {
   char * str2 = (char*)str;
@@ -23,6 +33,29 @@
   *c = 0;
   return buffer;
 }
+
+
+const char *windizePath(const char *path) {
+	static SWBuf retVal = "";
+	if ((*path != '/') && (*path != '\\'))
+		retVal = getWorkingDirectory();
+	retVal += "/";
+	SWBuf tmp = path;
+	while ((tmp[0] == '/') || (tmp[0] == '\\') || (tmp[0] == '.'))
+		tmp << 1;
+	retVal += tmp;
+
+	for (int pos = 0; retVal[pos]; pos++) {
+		switch (retVal[pos]) {
+		case '/':
+			retVal[pos] = '\\';
+			break;
+		}
+	}
+
+	return retVal.c_str();
+}
+
 
 int stricmp(const char *s1, const char *s2) {
 	char * t1 = new char;

Index: unistd.cpp
===================================================================
RCS file: /cvs/core/swordreader/src/Dll1/winceSword/src/unistd.cpp,v
retrieving revision 1.1.1.1
retrieving revision 1.2
diff -u -d -r1.1.1.1 -r1.2
--- unistd.cpp	16 Jan 2004 17:34:27 -0000	1.1.1.1
+++ unistd.cpp	17 Jan 2004 02:57:42 -0000	1.2
@@ -2,12 +2,16 @@
 #include <fcntl.h>
 #include <stdio.h>
 #include <windows.h>
+#include <swordce.h>
+
 
 int close (int fd) {
   return fclose((FILE*)fd);
 }
 
+
 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) {
@@ -27,27 +31,32 @@
   if (mode & O_BINARY) {
     cmode[1] ? (cmode[2] = 'b') : (cmode[1] = 'b');
   }
-  int result=(int)fopen(path, cmode);
+  int result=(int)fopen(winPath, cmode);
+
   if (!result) {
 	  WCHAR buf[100];
 	  mbstowcs(buf,"file: ",6);
-	  mbstowcs(&buf[6],path,100);
+	  mbstowcs(&buf[6],winPath,100);
 	  wcscpy(&buf[wcslen(buf)],L" error: ");
 	  DWORD errorNr=GetLastError();
 	  _itow(errorNr,&buf[wcslen(buf)],10);
 	  MessageBox(0,buf,L"Unable to open:",MB_OK|MB_ICONERROR);
   }
+
   return result;
 }
 
+
 int open(const char *path, int access, unsigned mode) {
 	return open(path, access | mode);
 }
 
+
 int read(int fd, void* buf, size_t count) {
 	return fread(buf, 1, count, (FILE*)fd);
 }
 
+
 int write(int fd, const void * buf, size_t count) {
 	return fwrite(buf, 1, count, (FILE*)fd);
 }
@@ -68,8 +77,21 @@
 }
 
 int access(const char* path, int mode) {
-	//We cannot tell wheather this file exists, just try opening it anyway
-	return 0;
+	int retVal = 0;
+
+	DWORD attribs = GetFileAttributes(strtowstr(windizePath(path)));
+
+	if (attribs == 0xFFFFFFFF)
+		retVal = 1;
+	else if (((mode & O_RDWR) || (mode & O_WRONLY)) && (attribs & FILE_ATTRIBUTE_READONLY))
+		retVal = 1;
+/*
+	int fd = open(path, mode);
+	if (fd > 0)
+		close(fd);
+	retVal = (fd < 1);
+*/
+	return retVal;
 }
 
 // TODO: create a directory