FileStr.st
changeset 159 514c749165c3
parent 92 0c73b48551ac
child 173 58e9778954bc
--- a/FileStr.st	Mon Oct 10 01:29:01 1994 +0100
+++ b/FileStr.st	Mon Oct 10 01:29:28 1994 +0100
@@ -1,6 +1,6 @@
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      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
@@ -12,16 +12,16 @@
 
 ExternalStream subclass:#FileStream
        instanceVariableNames:'pathName'
-       classVariableNames:''
+       classVariableNames:'OpenErrorSignal'
        poolDictionaries:''
        category:'Streams-External'
 !
 
 FileStream comment:'
 COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      All Rights Reserved
 
-$Header: /cvs/stx/stx/libbasic/Attic/FileStr.st,v 1.14 1994-08-05 00:54:46 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/FileStr.st,v 1.15 1994-10-10 00:26:04 claus Exp $
 '!
 
 !FileStream class methodsFor:'documentation'!
@@ -29,7 +29,7 @@
 copyright
 "
  COPYRIGHT (c) 1989 by Claus Gittinger
-              All Rights Reserved
+	      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
@@ -42,7 +42,7 @@
 
 version
 "
-$Header: /cvs/stx/stx/libbasic/Attic/FileStr.st,v 1.14 1994-08-05 00:54:46 claus Exp $
+$Header: /cvs/stx/stx/libbasic/Attic/FileStr.st,v 1.15 1994-10-10 00:26:04 claus Exp $
 "
 !
 
@@ -88,6 +88,18 @@
 
 %}
 
+!FileStream class methodsFor:'initialization'!
+
+initialize
+    OpenErrorSignal isNil ifTrue:[
+	super initialize.
+
+	OpenErrorSignal := StreamErrorSignal newSignalMayProceed:true.
+	OpenErrorSignal nameClass:self message:#openErrorSignal.
+	OpenErrorSignal notifierString:'open error'.
+    ].
+! !
+
 !FileStream class methodsFor:'instance creation'!
 
 newFileNamed:filename
@@ -179,7 +191,7 @@
 
     stream := self oldFileNamed:filename.
     stream isNil ifTrue:[
-        stream := self newFileNamed:filename
+	stream := self newFileNamed:filename
     ].
     ^ stream
 !
@@ -193,7 +205,7 @@
 
     stream := self oldFileNamed:filename in:aDirectory.
     stream isNil ifTrue:[
-        stream := self newFileNamed:filename in:aDirectory
+	stream := self newFileNamed:filename in:aDirectory
     ].
     ^ stream
 !
@@ -261,6 +273,26 @@
     ^ newStream
 ! !
 
+!FileStream methodsFor:'error handling'!
+
+errorOpen
+    "report an error, that the stream is already opened"
+
+    ^ StreamErrorSignal
+	raiseRequestWith:self
+	errorString:(self class name , ' is already open')
+!
+
+openError
+    "report an error, that file open failed"
+
+    LastErrorNumber := lastErrorNumber.
+^nil.
+    ^ OpenErrorSignal
+	raiseRequestWith:self
+	errorString:('error on open: ' , self lastErrorString)
+! !
+
 !FileStream methodsFor:'accessing'!
 
 store:something
@@ -278,8 +310,8 @@
     lastIndex := 0.
     index := path indexOf:$/.
     [index ~~ 0] whileTrue:[
-        lastIndex := index.
-        index := path indexOf:$/ startingAt:(index + 1)
+	lastIndex := index.
+	index := path indexOf:$/ startingAt:(index + 1)
     ].
     (lastIndex == 0) ifTrue:[^ '.'].
     (lastIndex == 1) ifTrue:[^ '/'].
@@ -293,11 +325,11 @@
 
     lastIndex := 1.
     [true] whileTrue:[
-        index := pathName indexOf:$/ startingAt:lastIndex.
-        (index == 0) ifTrue:[
-            ^ pathName copyFrom:lastIndex
-        ].
-        lastIndex := index + 1
+	index := pathName indexOf:$/ startingAt:lastIndex.
+	(index == 0) ifTrue:[
+	    ^ pathName copyFrom:lastIndex
+	].
+	lastIndex := index + 1
     ]
 !
 
@@ -319,14 +351,14 @@
     "set the pathname starting at aDirectory, a FileDirectory"
 
     ((filename at:1) == $/) ifTrue:[
-        "filename may not start with a '/'"
-        pathName := nil
+	"filename may not start with a '/'"
+	pathName := nil
     ] ifFalse:[
-        pathName := aDirectory pathName.
-        (pathName endsWith:'/') ifFalse:[
-            pathName := pathName , '/'
-        ].
-        pathName := pathName , filename
+	pathName := aDirectory pathName.
+	(pathName endsWith:'/') ifFalse:[
+	    pathName := pathName , '/'
+	].
+	pathName := pathName , filename
     ]
 !
 
@@ -335,12 +367,12 @@
 
     pathName isNil ifTrue:[^nil].
     (mode == #readonly) ifTrue: [
-        didWrite := false.
-        ^ self openWithMode:'r'
+	didWrite := false.
+	^ self openWithMode:'r'
     ].
     (mode == #writeonly) ifTrue: [
-        didWrite := true.
-        ^ self openWithMode:'w'
+	didWrite := true.
+	^ self openWithMode:'w'
     ].
     ^ self openWithMode:'r+'
 !
@@ -349,44 +381,44 @@
     "open the file; openmode is the string defining the way to open"
 
     |retVal|
+
+    filePointer notNil ifTrue:[^ self errorOpen].
 %{
     FILE *f;
     OBJ path;
-    extern OBJ Filename;
     extern errno;
 
     if (_INST(filePointer) == nil) {
-        path = _INST(pathName);
-        if (path != nil 
-         && ((_qClass(path)==String) || (_qClass(path) == Filename))) {
-            do {
+	path = _INST(pathName);
+	if (_isNonNilObject(path) && (_qClass(path)==String)) {
+	    do {
 #ifdef LINUX
-                /* LINUX returns a non-NULL f even when interrupted */
-                errno = 0;
-                f = (FILE *) fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
-                if (errno == EINTR)
-                    f = NULL;
+		/* LINUX returns a non-NULL f even when interrupted */
+		errno = 0;
+		f = (FILE *) fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
+		if (errno == EINTR)
+		    f = NULL;
 #else
 
-                f = (FILE *) fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
+		f = (FILE *) fopen((char *) _stringVal(path), (char *) _stringVal(openmode));
 #endif
-            } while ((f == NULL) && (errno == EINTR));
-            if (f == NULL) {
-                ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
-                _INST(position) = nil;
-            } else {
-                _INST(filePointer) = MKOBJ((int)f);
-                _INST(position) = _MKSMALLINT(1);
-                retVal = self;
-            }
-        }
+	    } while ((f == NULL) && (errno == EINTR));
+	    if (f == NULL) {
+		_INST(lastErrorNumber) = _MKSMALLINT(errno);
+		_INST(position) = nil;
+	    } else {
+		_INST(filePointer) = MKOBJ((int)f);
+		_INST(position) = _MKSMALLINT(1);
+		retVal = self;
+	    }
+	}
     }
-%}
-.
+%}.
     retVal notNil ifTrue:[
-        buffered := true.       "default is buffered"
-        Lobby register:self
+	buffered := true.       "default is buffered"
+	Lobby register:self
     ].
+    lastErrorNumber notNil ifTrue:[^ self openError].
     ^ retVal
 !
 
@@ -451,17 +483,17 @@
     "sent after snapin to reopen streams"
 
     filePointer notNil ifTrue:[
-        "it was open, when snapped-out"
-        filePointer := nil.
-        Lobby unregister:self.
-        self open.
-        filePointer isNil ifTrue:[
-            "this happens, if after a restart, the file is no longer accessable ..."
+	"it was open, when snapped-out"
+	filePointer := nil.
+	Lobby unregister:self.
+	self open.
+	filePointer isNil ifTrue:[
+	    "this happens, if after a restart, the file is no longer accessable ..."
 
-            ('could not reopen file: ', pathName) errorPrintNewline.
-        ] ifFalse:[
-            self position:position.
-        ]
+	    ('could not reopen file: ', pathName) errorPrintNewline.
+	] ifFalse:[
+	    self position:position.
+	]
     ]
 ! !
 
@@ -477,10 +509,10 @@
     int size;
 
     if (_INST(filePointer) != nil) {
-        f = (FILE *)MKFD(_INST(filePointer));
-        if ((size = filesize(fileno(f))) >= 0) {
-            RETURN ( _MKSMALLINT(size) );
-        }
+	f = (FILE *)MKFD(_INST(filePointer));
+	if ((size = filesize(fileno(f))) >= 0) {
+	    RETURN ( _MKSMALLINT(size) );
+	}
     }
 #else
     FILE *f;
@@ -490,27 +522,28 @@
     int fd;
 
     if (_INST(filePointer) != nil) {
-        f = (FILE *)MKFD(_INST(filePointer));
-        fd = fileno(f);
-        do {
-            ret = fstat(fd, &buf);
-        } while ((ret < 0) && (errno == EINTR));
-        if (ret >= 0) {
-            RETURN ( _MKSMALLINT(buf.st_size) );
-        }
-        ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
+	f = (FILE *)MKFD(_INST(filePointer));
+	fd = fileno(f);
+	do {
+	    ret = fstat(fd, &buf);
+	} while ((ret < 0) && (errno == EINTR));
+	if (ret >= 0) {
+	    RETURN ( _MKSMALLINT(buf.st_size) );
+	}
+	_INST(lastErrorNumber) = _MKSMALLINT(errno);
     }
 #endif
-%}
-.
+%}.
+
     "could add a fall-back here:
 
-        oldPosition := self position.
-        self setToEnd.
-        sz := self position.
-        self position:oldPosition.
-        ^ sz
+	oldPosition := self position.
+	self setToEnd.
+	sz := self position.
+	self position:oldPosition.
+	^ sz
     "
+    lastErrorNumber notNil ifTrue:[^ self ioError].
     filePointer isNil ifTrue:[^ self errorNotOpen].
     ^ self primitiveFailed
 !
@@ -526,24 +559,24 @@
     extern errno;
 
     if (_INST(filePointer) != nil) {
-        f = (FILE *)MKFD(_INST(filePointer));
-        do {
-            if (_INST(buffered) == true) {
-                currentPosition = (long) ftell(f);
-            } else {
-                currentPosition = (long) lseek(fileno(f), 0L, SEEK_CUR);
-            }
-        } while ((currentPosition < 0) && (errno == EINTR));
-        if (currentPosition >= 0) {
-            /*
-             * notice: Smalltalk index starts at 1
-             */
-            RETURN ( _MKSMALLINT(currentPosition + 1) );
-        }
-        ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
+	f = (FILE *)MKFD(_INST(filePointer));
+	do {
+	    if (_INST(buffered) == true) {
+		currentPosition = (long) ftell(f);
+	    } else {
+		currentPosition = (long) lseek(fileno(f), 0L, SEEK_CUR);
+	    }
+	} while ((currentPosition < 0) && (errno == EINTR));
+	if (currentPosition >= 0) {
+	    /*
+	     * notice: Smalltalk index starts at 1
+	     */
+	    RETURN ( _MKSMALLINT(currentPosition + 1) );
+	}
+	_INST(lastErrorNumber) = _MKSMALLINT(errno);
     }
-%}
-.
+%}.
+    lastErrorNumber notNil ifTrue:[^ self ioError].
     filePointer isNil ifTrue:[^ self errorNotOpen].
     ^ self primitiveFailed
 !
@@ -558,31 +591,31 @@
     extern errno;
 
     if (_INST(filePointer) != nil) {
-        if (_isSmallInteger(newPos)) {
-            f = (FILE *)MKFD(_INST(filePointer));
-            /*
-             * notice: Smalltalk index starts at 1
-             */
-            do {
-                if (_INST(buffered) == true) {
-                    ret = fseek(f, (long) (_intVal(newPos) - 1), SEEK_SET);
-                } else {
-                    ret = (long) lseek(fileno(f), (long)(_intVal(newPos) - 1), SEEK_SET);
-                }
-            } while ((ret < 0) && (errno == EINTR));
-            if (ret >= 0) {
-                _INST(position) = newPos;
-                /*
-                 * just to make certain ...
-                 */
-                _INST(hitEOF) = false;
-                RETURN ( self );
-            }
-            ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
-        }
+	if (_isSmallInteger(newPos)) {
+	    f = (FILE *)MKFD(_INST(filePointer));
+	    /*
+	     * notice: Smalltalk index starts at 1
+	     */
+	    do {
+		if (_INST(buffered) == true) {
+		    ret = fseek(f, (long) (_intVal(newPos) - 1), SEEK_SET);
+		} else {
+		    ret = (long) lseek(fileno(f), (long)(_intVal(newPos) - 1), SEEK_SET);
+		}
+	    } while ((ret < 0) && (errno == EINTR));
+	    if (ret >= 0) {
+		_INST(position) = newPos;
+		/*
+		 * just to make certain ...
+		 */
+		_INST(hitEOF) = false;
+		RETURN ( self );
+	    }
+	    _INST(lastErrorNumber) = _MKSMALLINT(errno);
+	}
     }
-%}
-.
+%}.
+    lastErrorNumber notNil ifTrue:[^ self ioError].
     filePointer isNil ifTrue:[^ self errorNotOpen].
     ^ self primitiveFailed
 !
@@ -596,22 +629,22 @@
     extern errno;
 
     if (_INST(filePointer) != nil) {
-        f = (FILE *)MKFD(_INST(filePointer));
-        _INST(position) = nil;
-        do {
-            if (_INST(buffered) == true) {
-                ret = fseek(f, 0L, SEEK_END);
-            } else {
-                ret = (long)lseek(fileno(f), 0L, SEEK_END);
-            }
-        } while ((ret < 0) && (errno == EINTR));
-        if (ret >= 0) {
-            RETURN ( self );
-        }
-        ExternalStream_LastErrorNumber = _MKSMALLINT(errno);
+	f = (FILE *)MKFD(_INST(filePointer));
+	_INST(position) = nil;
+	do {
+	    if (_INST(buffered) == true) {
+		ret = fseek(f, 0L, SEEK_END);
+	    } else {
+		ret = (long)lseek(fileno(f), 0L, SEEK_END);
+	    }
+	} while ((ret < 0) && (errno == EINTR));
+	if (ret >= 0) {
+	    RETURN ( self );
+	}
+	_INST(lastErrorNumber) = _MKSMALLINT(errno);
     }
-%}
-.
+%}.
+    lastErrorNumber notNil ifTrue:[^ self ioError].
     filePointer isNil ifTrue:[^ self errorNotOpen].
     DemoMode ifTrue:[^ self warn:'no save in Demo mode'].
     ^ self primitiveFailed
@@ -638,8 +671,8 @@
     aStream nextPutAll:'(FileStream oldFileNamed:'.
     aStream nextPutAll:pathName.
     (self position ~~ 1) ifTrue:[
-        aStream nextPutAll:'; position:'.
-        self position storeOn:aStream
+	aStream nextPutAll:'; position:'.
+	self position storeOn:aStream
     ].
     aStream nextPut:$)
 ! !