NPExtStr.st
author Claus Gittinger <cg@exept.de>
Thu, 02 Nov 1995 21:10:58 +0100
changeset 475 b57530aa1b0a
parent 437 a005e97d261e
child 477 8710aba7876b
permissions -rw-r--r--
use new FILE* wrapper macros (based on externalAddress)

"
 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.
"

UnboundedExternalStream subclass:#NonPositionableExternalStream
       instanceVariableNames:''
       classVariableNames:'StdInStream StdOutStream StdErrorStream'
       poolDictionaries:''
       category:'Streams-External'
!

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

$Header: /cvs/stx/stx/libbasic/Attic/NPExtStr.st,v 1.18 1995-11-02 20:10:55 cg Exp $
'!

!NonPositionableExternalStream 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/NPExtStr.st,v 1.18 1995-11-02 20:10:55 cg Exp $
"
!

documentation
"
    This class provides common protocol for all non-positionable,
    external streams. Concrete subclasses are terminal streams, pipe streams,
    PrinterStreams, Sockets etc.

    There are three special instances of this class, representing stdin,
    stdout and stderr of the smalltalk/X process (see Unix manuals, if you
    dont know what those are used for). These special streams are bound to
    to globals Stdin, Stdout and Stderr at early initialization time
    (see Smalltalk>>initializeStandardStreams).

    The name of this class is a historical leftover - it should be called
    'TTYStream' or similar.
"
! !

!NonPositionableExternalStream primitiveDefinitions!

%{
#include <stdio.h>
#define _STDIO_H_INCLUDED_
%}

! !

!NonPositionableExternalStream class methodsFor:'instance creation'!

forStdin
    "return a NonPositionableExternalStream object for reading from
     Unixes standard input file descriptor"

    StdInStream isNil ifTrue:[
	StdInStream := self basicNew initializeForStdin
    ].
    ^ StdInStream
!

forStdout
    "return a NonPositionableExternalStream object for writing to
     Unixes standard output file descriptor"

    StdOutStream isNil ifTrue:[
	StdOutStream := self basicNew initializeForStdout
    ].
    ^ StdOutStream
!

forStderr
    "return a NonPositionableExternalStream object for writing to
     Unixes standard error output file descriptor"

    StdErrorStream isNil ifTrue:[
	StdErrorStream := self basicNew initializeForStderr
    ].
    ^ StdErrorStream
! !

!NonPositionableExternalStream methodsFor:'printing & storing'!

printOn:aStream
    "append a printed representation of the receiver on aStream"

    self == Stdin ifTrue:[
	aStream nextPutAll:'Stdin'.
	^ self
    ].
    self == Stdout ifTrue:[
	aStream nextPutAll:'Stdout'.
	^ self
    ].
    self == Stderr ifTrue:[
	aStream nextPutAll:'Stderr'.
	^ self
    ].
    super printOn:aStream
!

storeOn:aStream
    "append a printed representation of the receiver on aStream, from
     which the receiver can be reconstructed."

    ((self == Stdin)
    or:[self == Stdout
    or:[self == Stderr]]) ifTrue:[
	^ self printOn:aStream
    ].
    super storeOn:aStream
! !

!NonPositionableExternalStream methodsFor:'error handling'!

positionError
    "notify that this stream has no concept of a position"

    self error:'stream as no concept of a position'
! !

!NonPositionableExternalStream methodsFor:'queries'!

isPositionable
    "return true, if the stream supports positioning (this one is not)"

    ^ false
! !

!NonPositionableExternalStream methodsFor:'positioning'!

position
    "catch position - there is none here"

    ^ self positionError
!

position:aPosition
    "catch position - there is none here"

    ^ self positionError
!

skip:numberToSkip
    "skip count objects, return the receiver"

    "dont know how to unread ..."
    numberToSkip < 0 ifTrue:[
	^ self error:'stream is not positionable'
    ].
    numberToSkip timesRepeat:self next
! !

!NonPositionableExternalStream methodsFor:'private'!

initializeForStdin
    "setup for reading stdin"

    mode := #readonly.
    buffered := true.
%{
    _INST(filePointer) = __MKOBJ(stdin);
%}
!

initializeForStdout
    "setup for writing to stdout"

    mode := #readwrite.
    buffered := false.
%{
    _INST(filePointer) = __MKOBJ(stdout);
%}
!

initializeForStderr
    "setup for writing to stderr"

    mode := #readwrite.
    buffered := false.
%{
    _INST(filePointer) = __MKOBJ(stderr);
%}
!

reOpen
    "reopen the stream after an image restart.
     If I am one of the standard streams, reopen is easy"

    (self == #StdInStream) ifTrue:[
	^ self initializeForStdin
    ].
    (self == #StdOutStream) ifTrue:[
	^ self initializeForStdout
    ].
    (self == #StdErrorStream) ifTrue:[
	^ self initializeForStderr
    ].
    ^ super reOpen
! !