DirStr.st
author claus
Fri, 05 Aug 1994 02:55:07 +0200
changeset 92 0c73b48551ac
parent 88 81dacba7a63a
child 155 edd7fc34e104
permissions -rw-r--r--
*** empty log message ***

"
 COPYRIGHT (c) 1989 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"

FileStream subclass:#DirectoryStream
       instanceVariableNames:'dirPointer readAhead'
       classVariableNames:''
       poolDictionaries:''
       category:'Streams-External'
!

DirectoryStream comment:'
COPYRIGHT (c) 1989 by Claus Gittinger
              All Rights Reserved

$Header: /cvs/stx/stx/libbasic/Attic/DirStr.st,v 1.12 1994-08-05 00:54:27 claus Exp $
'!

!DirectoryStream class methodsFor:'documentation'!

copyright
"
 COPYRIGHT (c) 1989 by Claus Gittinger
              All Rights Reserved

 This software is furnished under a license and may be used
 only in accordance with the terms of that license and with the
 inclusion of the above copyright notice.   This software may not
 be provided or otherwise made available to, or used by, any
 other person.  No title to or ownership of the software is
 hereby transferred.
"
!

version
"
$Header: /cvs/stx/stx/libbasic/Attic/DirStr.st,v 1.12 1994-08-05 00:54:27 claus Exp $
"
!

documentation
"
    Instances of DirectoryStream allow reading a file-directory,
    as if it was a stream of filenames.
    Basically, its an interface to opendir, readdir and closedir.
"
! !

%{
#include <stdio.h>
#include <errno.h>

#ifndef transputer
# include <sys/types.h>
# include <sys/stat.h>
# ifdef HAS_OPENDIR
#  include <sys/types.h>
#  ifdef NEXT
#   include <sys/dir.h>
#  else
#   include <dirent.h>
#  endif
# endif
#endif
%}

!DirectoryStream methodsFor:'instance release'!

closeFile
    "a directoryStream has been collected - close it"
%{
#ifdef HAS_OPENDIR
    closedir((DIR *)MKFD(_INST(dirPointer)));
#endif
%}
! !

!DirectoryStream class methodsFor:'instance creation'!

directoryNamed:dirName
    "return a DirectoryStream for directory named dirName, aString"

    |newStream|

    newStream := (self basicNew) pathName:dirName.
    newStream openForReading isNil ifTrue:[^nil].
    ^ newStream
! !

!DirectoryStream methodsFor:'access reading'!

nextLine
    "return the next filename as a string"

    |prevEntry nextEntry|
%{
#ifdef HAS_OPENDIR
    DIR *d;
#ifdef NEXT
    struct direct *dp;
#else
    struct dirent *dp;
#endif
    OBJ dirP;
    extern errno;

    if ((dirP = _INST(dirPointer)) != nil) {
        extern int _immediateInterrupt;

        d = (DIR *)MKFD(dirP);

        _immediateInterrupt = 1;
	errno = 0;
        do {
            dp = readdir(d);
        } while ((dp == NULL) && (errno == EINTR));
	_immediateInterrupt = 0;

        if (dp != NULL) {
            nextEntry = _MKSTRING((char *)(dp->d_name) COMMA_CON);
	} else {
	    _INST(hitEOF) = true;
	    if (errno) {
                ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
	    }
        }
    }
#endif
%}
.
    prevEntry := readAhead.
    readAhead := nextEntry.
    ^ prevEntry
! !

!DirectoryStream methodsFor:'private'!

openForReading
    "open the file for readonly"

    |retVal|

    mode := #readonly.
%{
#ifdef HAS_OPENDIR
    DIR *d;
    OBJ path;
    extern errno;

    retVal = false;
    if (_INST(dirPointer) == nil) {
        path = _INST(pathName);
        if (__isString(path)) {
            extern int _immediateInterrupt;

            _immediateInterrupt = 1;
	    errno = 0;
            do {
                d = opendir((char *) _stringVal(path));
            } while ((d == NULL) && (errno == EINTR));
	    _immediateInterrupt = 0;

            if (d == NULL) {
                ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
            } else {
                _INST(dirPointer) = MKOBJ(d);
                retVal = true;
            }
        }
    }
#endif
%}
.
    retVal isNil ifTrue:[
        "opendir not avalable - use slow pipe"
        ^ PipeStream readingFrom:('cd ' , pathName , '; ls -a')
    ].
    (retVal == true) ifTrue:[
        Lobby register:self.
        self nextLine. "read 1st entry into readAhead buffer"
        ^ self
    ].
    ^ nil
!

reOpen
    "reOpen the stream after image restart"

    dirPointer := nil.
    super reOpen
! !

!DirectoryStream methodsFor:'testing'!

atEnd
    "return true, if position is at end"

    ^ readAhead == nil
! !

!DirectoryStream methodsFor:'closing'!

close
    "close the stream - tell operating system"

    dirPointer notNil ifTrue:[
        Lobby unregister:self.
        self closeFile.
        dirPointer := nil
    ]
! !