#undef DEBUG
/*
* StdChannel.java --
*
* Copyright (c) 1997 Sun Microsystems, Inc.
*
* See the file "license.terms" for information on usage and
* redistribution of this file, and for a DISCLAIMER OF ALL
* WARRANTIES.
*
* Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
*
* RCS @(#) $Id: StdChannel.java,v 1.19 2003/03/08 03:42:44 mdejong Exp $
*
*/
using System;
using System.IO;
namespace tcl.lang
{
/// Subclass of the abstract class Channel. It implements all of the
/// methods to perform read, write, open, close, etc on system stdio channels.
///
public class StdChannel : Channel
{
public override string ChanType
{
get
{
return "tty";
}
}
override protected internal Stream InputStream
{
get
{
return null;
// return System.Console.In;
}
}
override protected internal Stream OutputStream
{
get
{
throw new System.SystemException( "should never be called" );
}
}
/// stdType store which type, of the three below, this StdChannel is.
private int stdType = -1;
/// Flags indicating the type of this StdChannel.
public const int STDIN = 0;
public const int STDOUT = 1;
public const int STDERR = 2;
/// Constructor that does nothing. Open() must be called before
/// any of the subsequent read, write, etc calls can be made.
///
internal StdChannel()
{
}
/// Constructor that will automatically call open.
///
///
/// name of the stdio channel; stdin, stderr or stdout.
///
internal StdChannel( string stdName )
{
if ( stdName.Equals( "stdin" ) )
{
open( STDIN );
}
else if ( stdName.Equals( "stdout" ) )
{
open( STDOUT );
}
else if ( stdName.Equals( "stderr" ) )
{
open( STDERR );
}
else
{
throw new TclRuntimeError( "Error: unexpected type for StdChannel" );
}
}
internal StdChannel( int type )
{
open( type );
}
/// Set the channel type to one of the three stdio types. Throw a
/// tclRuntimeEerror if the stdName is not one of the three types. If
/// it is a stdin channel, initialize the "in" data member. Since "in"
/// is static it may have already be initialized, test for this case
/// first. Set the names to fileX, this will be the key in the chanTable
/// hashtable to access this object. Note: it is not put into the hash
/// table in this function. The calling function is responsible for that.
///
///
/// String that equals stdin, stdout, stderr
///
/// The name of the channelId
///
internal string open( int type )
{
switch ( type )
{
case STDIN:
mode = TclIO.RDONLY;
Buffering = TclIO.BUFF_LINE;
ChanName = "stdin";
break;
case STDOUT:
mode = TclIO.WRONLY;
Buffering = TclIO.BUFF_LINE;
ChanName = "stdout";
break;
case STDERR:
mode = TclIO.WRONLY;
Buffering = TclIO.BUFF_NONE;
ChanName = "stderr";
break;
default:
throw new System.SystemException( "type does not match one of STDIN, STDOUT, or STDERR" );
}
stdType = type;
return ChanName;
}
/// Write to stdout or stderr. If the stdType is not set to
/// STDOUT or STDERR this is an error; either the stdType wasnt
/// correctly initialized, or this was called on a STDIN channel.
///
///
/// the current interpreter.
///
/// the string to write
///
public override void write( Interp interp, TclObject outData )
{
checkWrite( interp );
if ( stdType == STDERR )
{
System.Console.Error.Write( outData.ToString() );
}
else
{
string s = outData.ToString();
System.Console.Out.Write( s );
if ( buffering == TclIO.BUFF_NONE || ( buffering == TclIO.BUFF_LINE && s.EndsWith( "\n" ) ) )
{
System.Console.Out.Flush();
}
}
}
/// Check for any output that might still need to be flushed
/// when the channel is closed.
///
internal override void close()
{
if ( stdType == STDOUT )
System.Console.Out.Flush();
base.close();
}
}
}