directory readOps for NT
authorClaus Gittinger <cg@exept.de>
Thu, 15 Aug 1996 14:28:37 +0200
changeset 1631 0d50b800b011
parent 1630 09e6117a1457
child 1632 355409cd49d8
directory readOps for NT
DirStr.st
DirectoryStream.st
--- a/DirStr.st	Thu Aug 15 14:18:53 1996 +0200
+++ b/DirStr.st	Thu Aug 15 14:28:37 1996 +0200
@@ -20,34 +20,83 @@
 !DirectoryStream primitiveDefinitions!
 %{
 
-#include <stdio.h>
-#define _STDIO_H_INCLUDED_
+#define UNIX_LIKE
+#if defined(WIN32)
+# undef UNIX_LIKE
+#endif
 
-#include <errno.h>
-#define _ERRNO_H_INCLUDED_
+#ifdef UNIX_LIKE
+
+# include <stdio.h>
+# define _STDIO_H_INCLUDED_
+
+# include <errno.h>
+# define _ERRNO_H_INCLUDED_
 
-#ifndef transputer
-# include <sys/types.h>
-# include <sys/stat.h>
-
-# ifdef HAS_OPENDIR
+# ifndef transputer
 #  include <sys/types.h>
-#  ifdef NEXT
-#   include <sys/dir.h>
-#  else
-#   ifndef VMS
-#    include <dirent.h>
-#   endif /* not VMS */
+#  include <sys/stat.h>
+
+#  ifdef HAS_OPENDIR
+#   include <sys/types.h>
+#   ifdef NEXT
+#    include <sys/dir.h>
+#   else
+#    ifndef VMS
+#     include <dirent.h>
+#    endif /* not VMS */
+#   endif
 #  endif
 # endif
-#endif
 
 /*
  * on some systems errno is a macro ... check for it here
  */
-#ifndef errno
- extern errno;
-#endif
+# ifndef errno
+  extern errno;
+# endif
+#endif /* UNIX_LIKE */
+
+#ifdef WIN32
+
+# ifdef i386
+#  define _X86_
+# endif
+
+# undef INT
+# undef Array
+# undef Number
+# undef Method
+# undef Point
+# undef Context
+# undef Rectangle
+
+# include <h/sys/types.h> /* */
+# include <h/stdarg.h> /* */
+# include <h/windef.h> /* */
+# include <h/winbase.h> /* */
+# include <h/wingdi.h> /* */
+# include <h/winuser.h> /* */
+
+# ifdef __DEF_Array
+#  define Array __DEF_Array
+# endif
+# ifdef __DEF_Number
+#  define Number __DEF_Number
+# endif
+# ifdef __DEF_Method
+#  define Method __DEF_Method
+# endif
+# ifdef __DEF_Point
+#  define Point __DEF_Point
+# endif
+# ifdef __DEF_Context
+#  define Context __DEF_Context
+# endif
+
+# define __HANDLEVal(o)  (HANDLE)__MKCP(o)
+
+#endif /* WIN32 */
 
 %}
 ! !
@@ -353,7 +402,7 @@
     Basically, its an interface to opendir, readdir and closedir.
 
     [author:]
-        Claus Gittinger
+	Claus Gittinger
 "
 ! !
 
@@ -378,36 +427,52 @@
 %{
 #ifdef HAS_OPENDIR
     DIR *d;
-#ifdef NEXT
+# ifdef NEXT
     struct direct *dp;
-#else
+# else
     struct dirent *dp;
-#endif
+# endif
     OBJ dirP;
 
-
     if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) {
-        __INST(lastErrorNumber) = nil;
-        d = (DIR *)__FILEVal(dirP);
+	__INST(lastErrorNumber) = nil;
+	d = (DIR *)__FILEVal(dirP);
 
-        __BEGIN_INTERRUPTABLE__
-        do {
-            errno = 0;
-            dp = readdir(d);
-        } while ((dp == NULL) && (errno == EINTR));
-        __END_INTERRUPTABLE__
+	__BEGIN_INTERRUPTABLE__
+	do {
+	    errno = 0;
+	    dp = readdir(d);
+	} while ((dp == NULL) && (errno == EINTR));
+	__END_INTERRUPTABLE__
 
-        if (dp != NULL) {
-            nextEntry = __MKSTRING((char *)(dp->d_name) COMMA_CON);
-        } else {
-            if (errno) {
-                __INST(lastErrorNumber) = __MKSMALLINT(errno);
-            } else {
-               __INST(hitEOF) = true;
-            }
+	if (dp != NULL) {
+	    nextEntry = __MKSTRING((char *)(dp->d_name) COMMA_CON);
+	} else {
+	    if (errno) {
+		__INST(lastErrorNumber) = __MKSMALLINT(errno);
+	    } else {
+	       __INST(hitEOF) = true;
+	    }
        }
     }
-#endif
+#else /* no HAS_OPENDIR */
+# ifdef WIN32
+    HANDLE d;
+    WIN32_FIND_DATA data;
+    OBJ dirP;
+
+    if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) {
+	__INST(lastErrorNumber) = nil;
+	d = __HANDLEVal(dirP);
+
+	if (FindNextFile(d, &data)) {
+	    nextEntry = __MKSTRING( data.cFileName );
+	} else {
+	   __INST(hitEOF) = true;
+	}
+    }
+# endif /* WIN32 */
+#endif /* HAS_OPENDIR */
 %}.
     lastErrorNumber notNil ifTrue:[^ self ioError].
     prevEntry := readAhead.
@@ -440,6 +505,15 @@
 	__INST(dirPointer) = nil;
 	closedir( (DIR *)(__FILEVal(dp)) );
     }
+#else
+# ifdef WIN32
+    OBJ dp;
+
+    if ((dp = __INST(dirPointer)) != nil) {
+	__INST(dirPointer) = nil;
+	FindClose( __HANDLEVal(dp) );
+    }
+# endif
 #endif
 %}
 ! !
@@ -449,7 +523,7 @@
 openForReading
     "open the file for readonly"
 
-    |ok|
+    |ok entry|
 
     mode := #readonly.
 %{
@@ -476,6 +550,30 @@
 	    }
 	}
     }
+#else
+# ifdef WIN32
+    HANDLE d;
+    OBJ path, dp;
+    char pattern[512];
+    WIN32_FIND_DATA data;
+
+    ok = false;
+    if (__INST(dirPointer) == nil) {
+	path = __INST(pathName);
+	if (__isString(path)) {
+	    strcpy(pattern, __stringVal(path));
+	    strcat(pattern, "\\*");
+	    d = FindFirstFile(pattern, &data);
+	    if (d == INVALID_HANDLE_VALUE) {
+		__INST(lastErrorNumber) = __MKSMALLINT(GetLastError());
+	    } else {
+		__INST(dirPointer) = dp = __MKOBJ(d); __STORE(self, dp);
+		entry = __MKSTRING( data.cFileName );
+		ok = true;
+	    }
+	}
+    }
+# endif
 #endif
 %}.
     ok isNil ifTrue:[
@@ -487,7 +585,11 @@
 
     (ok == true) ifTrue:[
 	Lobby register:self.
-	self nextLine. "read 1st entry into readAhead buffer"
+	entry isNil ifTrue:[
+	    self nextLine. "read 1st entry into readAhead buffer"
+	] ifFalse:[
+	    readAhead := entry.
+	].
 	^ self
     ].
     dirPointer notNil ifTrue:[^ self errorOpen].
@@ -513,5 +615,5 @@
 !DirectoryStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/Attic/DirStr.st,v 1.30 1996-04-25 17:01:37 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/Attic/DirStr.st,v 1.31 1996-08-15 12:28:37 cg Exp $'
 ! !
--- a/DirectoryStream.st	Thu Aug 15 14:18:53 1996 +0200
+++ b/DirectoryStream.st	Thu Aug 15 14:28:37 1996 +0200
@@ -20,34 +20,83 @@
 !DirectoryStream primitiveDefinitions!
 %{
 
-#include <stdio.h>
-#define _STDIO_H_INCLUDED_
+#define UNIX_LIKE
+#if defined(WIN32)
+# undef UNIX_LIKE
+#endif
 
-#include <errno.h>
-#define _ERRNO_H_INCLUDED_
+#ifdef UNIX_LIKE
+
+# include <stdio.h>
+# define _STDIO_H_INCLUDED_
+
+# include <errno.h>
+# define _ERRNO_H_INCLUDED_
 
-#ifndef transputer
-# include <sys/types.h>
-# include <sys/stat.h>
-
-# ifdef HAS_OPENDIR
+# ifndef transputer
 #  include <sys/types.h>
-#  ifdef NEXT
-#   include <sys/dir.h>
-#  else
-#   ifndef VMS
-#    include <dirent.h>
-#   endif /* not VMS */
+#  include <sys/stat.h>
+
+#  ifdef HAS_OPENDIR
+#   include <sys/types.h>
+#   ifdef NEXT
+#    include <sys/dir.h>
+#   else
+#    ifndef VMS
+#     include <dirent.h>
+#    endif /* not VMS */
+#   endif
 #  endif
 # endif
-#endif
 
 /*
  * on some systems errno is a macro ... check for it here
  */
-#ifndef errno
- extern errno;
-#endif
+# ifndef errno
+  extern errno;
+# endif
+#endif /* UNIX_LIKE */
+
+#ifdef WIN32
+
+# ifdef i386
+#  define _X86_
+# endif
+
+# undef INT
+# undef Array
+# undef Number
+# undef Method
+# undef Point
+# undef Context
+# undef Rectangle
+
+# include <h/sys/types.h> /* */
+# include <h/stdarg.h> /* */
+# include <h/windef.h> /* */
+# include <h/winbase.h> /* */
+# include <h/wingdi.h> /* */
+# include <h/winuser.h> /* */
+
+# ifdef __DEF_Array
+#  define Array __DEF_Array
+# endif
+# ifdef __DEF_Number
+#  define Number __DEF_Number
+# endif
+# ifdef __DEF_Method
+#  define Method __DEF_Method
+# endif
+# ifdef __DEF_Point
+#  define Point __DEF_Point
+# endif
+# ifdef __DEF_Context
+#  define Context __DEF_Context
+# endif
+
+# define __HANDLEVal(o)  (HANDLE)__MKCP(o)
+
+#endif /* WIN32 */
 
 %}
 ! !
@@ -353,7 +402,7 @@
     Basically, its an interface to opendir, readdir and closedir.
 
     [author:]
-        Claus Gittinger
+	Claus Gittinger
 "
 ! !
 
@@ -378,36 +427,52 @@
 %{
 #ifdef HAS_OPENDIR
     DIR *d;
-#ifdef NEXT
+# ifdef NEXT
     struct direct *dp;
-#else
+# else
     struct dirent *dp;
-#endif
+# endif
     OBJ dirP;
 
-
     if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) {
-        __INST(lastErrorNumber) = nil;
-        d = (DIR *)__FILEVal(dirP);
+	__INST(lastErrorNumber) = nil;
+	d = (DIR *)__FILEVal(dirP);
 
-        __BEGIN_INTERRUPTABLE__
-        do {
-            errno = 0;
-            dp = readdir(d);
-        } while ((dp == NULL) && (errno == EINTR));
-        __END_INTERRUPTABLE__
+	__BEGIN_INTERRUPTABLE__
+	do {
+	    errno = 0;
+	    dp = readdir(d);
+	} while ((dp == NULL) && (errno == EINTR));
+	__END_INTERRUPTABLE__
 
-        if (dp != NULL) {
-            nextEntry = __MKSTRING((char *)(dp->d_name) COMMA_CON);
-        } else {
-            if (errno) {
-                __INST(lastErrorNumber) = __MKSMALLINT(errno);
-            } else {
-               __INST(hitEOF) = true;
-            }
+	if (dp != NULL) {
+	    nextEntry = __MKSTRING((char *)(dp->d_name) COMMA_CON);
+	} else {
+	    if (errno) {
+		__INST(lastErrorNumber) = __MKSMALLINT(errno);
+	    } else {
+	       __INST(hitEOF) = true;
+	    }
        }
     }
-#endif
+#else /* no HAS_OPENDIR */
+# ifdef WIN32
+    HANDLE d;
+    WIN32_FIND_DATA data;
+    OBJ dirP;
+
+    if (__INST(hitEOF) != true && (dirP = __INST(dirPointer)) != nil) {
+	__INST(lastErrorNumber) = nil;
+	d = __HANDLEVal(dirP);
+
+	if (FindNextFile(d, &data)) {
+	    nextEntry = __MKSTRING( data.cFileName );
+	} else {
+	   __INST(hitEOF) = true;
+	}
+    }
+# endif /* WIN32 */
+#endif /* HAS_OPENDIR */
 %}.
     lastErrorNumber notNil ifTrue:[^ self ioError].
     prevEntry := readAhead.
@@ -440,6 +505,15 @@
 	__INST(dirPointer) = nil;
 	closedir( (DIR *)(__FILEVal(dp)) );
     }
+#else
+# ifdef WIN32
+    OBJ dp;
+
+    if ((dp = __INST(dirPointer)) != nil) {
+	__INST(dirPointer) = nil;
+	FindClose( __HANDLEVal(dp) );
+    }
+# endif
 #endif
 %}
 ! !
@@ -449,7 +523,7 @@
 openForReading
     "open the file for readonly"
 
-    |ok|
+    |ok entry|
 
     mode := #readonly.
 %{
@@ -476,6 +550,30 @@
 	    }
 	}
     }
+#else
+# ifdef WIN32
+    HANDLE d;
+    OBJ path, dp;
+    char pattern[512];
+    WIN32_FIND_DATA data;
+
+    ok = false;
+    if (__INST(dirPointer) == nil) {
+	path = __INST(pathName);
+	if (__isString(path)) {
+	    strcpy(pattern, __stringVal(path));
+	    strcat(pattern, "\\*");
+	    d = FindFirstFile(pattern, &data);
+	    if (d == INVALID_HANDLE_VALUE) {
+		__INST(lastErrorNumber) = __MKSMALLINT(GetLastError());
+	    } else {
+		__INST(dirPointer) = dp = __MKOBJ(d); __STORE(self, dp);
+		entry = __MKSTRING( data.cFileName );
+		ok = true;
+	    }
+	}
+    }
+# endif
 #endif
 %}.
     ok isNil ifTrue:[
@@ -487,7 +585,11 @@
 
     (ok == true) ifTrue:[
 	Lobby register:self.
-	self nextLine. "read 1st entry into readAhead buffer"
+	entry isNil ifTrue:[
+	    self nextLine. "read 1st entry into readAhead buffer"
+	] ifFalse:[
+	    readAhead := entry.
+	].
 	^ self
     ].
     dirPointer notNil ifTrue:[^ self errorOpen].
@@ -513,5 +615,5 @@
 !DirectoryStream class methodsFor:'documentation'!
 
 version
-    ^ '$Header: /cvs/stx/stx/libbasic/DirectoryStream.st,v 1.30 1996-04-25 17:01:37 cg Exp $'
+    ^ '$Header: /cvs/stx/stx/libbasic/DirectoryStream.st,v 1.31 1996-08-15 12:28:37 cg Exp $'
 ! !