wasCSharpSQLite – Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 #undef DEBUG
2 /*
3 * StdChannel.java --
4 *
5 * Copyright (c) 1997 Sun Microsystems, Inc.
6 *
7 * See the file "license.terms" for information on usage and
8 * redistribution of this file, and for a DISCLAIMER OF ALL
9 * WARRANTIES.
10 *
11 * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
12 *
13 * RCS @(#) $Id: StdChannel.java,v 1.19 2003/03/08 03:42:44 mdejong Exp $
14 *
15 */
16 using System;
17 using System.IO;
18 namespace tcl.lang
19 {
20  
21 /// <summary> Subclass of the abstract class Channel. It implements all of the
22 /// methods to perform read, write, open, close, etc on system stdio channels.
23 /// </summary>
24  
25 public class StdChannel : Channel
26 {
27 public override string ChanType
28 {
29 get
30 {
31 return "tty";
32 }
33  
34 }
35 override protected internal Stream InputStream
36 {
37 get
38 {
39 return null;
40 // return System.Console.In;
41 }
42  
43 }
44 override protected internal Stream OutputStream
45 {
46 get
47 {
48 throw new System.SystemException( "should never be called" );
49 }
50  
51 }
52  
53 /// <summary> stdType store which type, of the three below, this StdChannel is.</summary>
54  
55 private int stdType = -1;
56  
57 /// <summary> Flags indicating the type of this StdChannel.</summary>
58  
59 public const int STDIN = 0;
60 public const int STDOUT = 1;
61 public const int STDERR = 2;
62  
63 /// <summary> Constructor that does nothing. Open() must be called before
64 /// any of the subsequent read, write, etc calls can be made.
65 /// </summary>
66  
67 internal StdChannel()
68 {
69 }
70  
71 /// <summary> Constructor that will automatically call open.
72 ///
73 /// </summary>
74 /// <param name="stdName">name of the stdio channel; stdin, stderr or stdout.
75 /// </param>
76  
77 internal StdChannel( string stdName )
78 {
79 if ( stdName.Equals( "stdin" ) )
80 {
81 open( STDIN );
82 }
83 else if ( stdName.Equals( "stdout" ) )
84 {
85 open( STDOUT );
86 }
87 else if ( stdName.Equals( "stderr" ) )
88 {
89 open( STDERR );
90 }
91 else
92 {
93 throw new TclRuntimeError( "Error: unexpected type for StdChannel" );
94 }
95 }
96  
97  
98 internal StdChannel( int type )
99 {
100 open( type );
101 }
102  
103  
104 /// <summary> Set the channel type to one of the three stdio types. Throw a
105 /// tclRuntimeEerror if the stdName is not one of the three types. If
106 /// it is a stdin channel, initialize the "in" data member. Since "in"
107 /// is static it may have already be initialized, test for this case
108 /// first. Set the names to fileX, this will be the key in the chanTable
109 /// hashtable to access this object. Note: it is not put into the hash
110 /// table in this function. The calling function is responsible for that.
111 ///
112 /// </summary>
113 /// <param name="stdName">String that equals stdin, stdout, stderr
114 /// </param>
115 /// <returns> The name of the channelId
116 /// </returns>
117  
118 internal string open( int type )
119 {
120  
121 switch ( type )
122 {
123  
124 case STDIN:
125 mode = TclIO.RDONLY;
126 Buffering = TclIO.BUFF_LINE;
127 ChanName = "stdin";
128 break;
129  
130 case STDOUT:
131 mode = TclIO.WRONLY;
132 Buffering = TclIO.BUFF_LINE;
133 ChanName = "stdout";
134 break;
135  
136 case STDERR:
137 mode = TclIO.WRONLY;
138 Buffering = TclIO.BUFF_NONE;
139 ChanName = "stderr";
140 break;
141  
142 default:
143 throw new System.SystemException( "type does not match one of STDIN, STDOUT, or STDERR" );
144  
145 }
146  
147 stdType = type;
148  
149 return ChanName;
150 }
151  
152 /// <summary> Write to stdout or stderr. If the stdType is not set to
153 /// STDOUT or STDERR this is an error; either the stdType wasnt
154 /// correctly initialized, or this was called on a STDIN channel.
155 ///
156 /// </summary>
157 /// <param name="interp">the current interpreter.
158 /// </param>
159 /// <param name="s">the string to write
160 /// </param>
161  
162 public override void write( Interp interp, TclObject outData )
163 {
164  
165 checkWrite( interp );
166  
167 if ( stdType == STDERR )
168 {
169  
170 System.Console.Error.Write( outData.ToString() );
171 }
172 else
173 {
174  
175 string s = outData.ToString();
176 System.Console.Out.Write( s );
177 if ( buffering == TclIO.BUFF_NONE || ( buffering == TclIO.BUFF_LINE && s.EndsWith( "\n" ) ) )
178 {
179 System.Console.Out.Flush();
180 }
181 }
182 }
183  
184 /// <summary> Check for any output that might still need to be flushed
185 /// when the channel is closed.
186 /// </summary>
187  
188 internal override void close()
189 {
190 if ( stdType == STDOUT )
191 System.Console.Out.Flush();
192 base.close();
193 }
194 }
195 }