wasCSharpSQLite – Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * TclIO.java --
3 *
4 * Copyright (c) 1997 Sun Microsystems, Inc.
5 *
6 * See the file "license.terms" for information on usage and
7 * redistribution of this file, and for a DISCLAIMER OF ALL
8 * WARRANTIES.
9 *
10 * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
11 *
12 * RCS @(#) $Id: TclIO.java,v 1.9 2003/03/06 22:53:07 mdejong Exp $
13 *
14 */
15 using System;
16 using System.Collections;
17 using System.IO;
18  
19 namespace tcl.lang
20 {
21  
22 public class TclIO
23 {
24  
25 public const int READ_ALL = 1;
26 public const int READ_LINE = 2;
27 public const int READ_N_BYTES = 3;
28  
29 public const int SEEK_SET = 1;
30 public const int SEEK_CUR = 2;
31 public const int SEEK_END = 3;
32  
33 public const int RDONLY = 1;
34 public const int WRONLY = 2;
35 public const int RDWR = 4;
36 public const int APPEND = 8;
37 public const int CREAT = 16;
38 public const int EXCL = 32;
39 public const int TRUNC = 64;
40  
41 public const int BUFF_FULL = 0;
42 public const int BUFF_LINE = 1;
43 public const int BUFF_NONE = 2;
44  
45 public const int TRANS_AUTO = 0;
46 public const int TRANS_BINARY = 1;
47 public const int TRANS_LF = 2;
48 public const int TRANS_CR = 3;
49 public const int TRANS_CRLF = 4;
50  
51 public static int TRANS_PLATFORM;
52  
53 /// <summary> Table of channels currently registered for all interps. The
54 /// interpChanTable has "" references into this table that
55 /// stores the registered channels for the individual interp.
56 /// </summary>
57  
58 private static StdChannel stdinChan = null;
59 private static StdChannel stdoutChan = null;
60 private static StdChannel stderrChan = null;
61  
62 public static Channel getChannel( Interp interp, string chanName )
63 {
64 return ( (Channel)getInterpChanTable( interp )[chanName] );
65 }
66  
67  
68 internal static void registerChannel( Interp interp, Channel chan )
69 {
70  
71 if ( interp != null )
72 {
73 Hashtable chanTable = getInterpChanTable( interp );
74 SupportClass.PutElement( chanTable, chan.ChanName, chan );
75 chan.refCount++;
76 }
77 }
78  
79  
80 internal static void unregisterChannel( Interp interp, Channel chan )
81 {
82  
83 Hashtable chanTable = getInterpChanTable( interp );
84 SupportClass.HashtableRemove( chanTable, chan.ChanName );
85  
86 if ( --chan.refCount <= 0 )
87 {
88 try
89 {
90 chan.close();
91 }
92 catch ( IOException e )
93 {
94 throw new TclRuntimeError( "TclIO.unregisterChannel() Error: IOException when closing " + chan.ChanName + ": " + e.Message, e );
95 }
96 }
97 }
98  
99  
100 public static Hashtable getInterpChanTable( Interp interp )
101 {
102 Channel chan;
103  
104 if ( interp.interpChanTable == null )
105 {
106  
107 interp.interpChanTable = new Hashtable();
108  
109 chan = getStdChannel( StdChannel.STDIN );
110 registerChannel( interp, chan );
111  
112 chan = getStdChannel( StdChannel.STDOUT );
113 registerChannel( interp, chan );
114  
115 chan = getStdChannel( StdChannel.STDERR );
116 registerChannel( interp, chan );
117 }
118  
119 return interp.interpChanTable;
120 }
121  
122  
123 public static Channel getStdChannel( int type )
124 {
125 Channel chan = null;
126  
127 switch ( type )
128 {
129  
130 case StdChannel.STDIN:
131 if ( stdinChan == null )
132 {
133 stdinChan = new StdChannel( StdChannel.STDIN );
134 }
135 chan = stdinChan;
136 break;
137  
138 case StdChannel.STDOUT:
139 if ( stdoutChan == null )
140 {
141 stdoutChan = new StdChannel( StdChannel.STDOUT );
142 }
143 chan = stdoutChan;
144 break;
145  
146 case StdChannel.STDERR:
147 if ( stderrChan == null )
148 {
149 stderrChan = new StdChannel( StdChannel.STDERR );
150 }
151 chan = stderrChan;
152 break;
153  
154 default:
155 throw new TclRuntimeError( "Invalid type for StdChannel" );
156  
157 }
158  
159 return ( chan );
160 }
161  
162 /// <summary> Really ugly function that attempts to get the next available
163 /// channelId name. In C the FD returned in the native open call
164 /// returns this value, but we don't have that so we need to do
165 /// this funky iteration over the Hashtable.
166 ///
167 /// </summary>
168 /// <param name="interp">currrent interpreter.
169 /// </param>
170 /// <returns> the next integer to use in the channelId name.
171 /// </returns>
172  
173 internal static string getNextDescriptor( Interp interp, string prefix )
174 {
175 int i;
176 Hashtable htbl = getInterpChanTable( interp );
177  
178 // The first available file identifier in Tcl is "file3"
179 if ( prefix.Equals( "file" ) )
180 i = 3;
181 else
182 i = 0;
183  
184 for ( ; ( htbl[prefix + i] ) != null; i++ )
185 {
186 // Do nothing...
187 }
188 return prefix + i;
189 }
190  
191 /*
192 * Return a string description for a translation id defined above.
193 */
194  
195 internal static string getTranslationString( int translation )
196 {
197 switch ( translation )
198 {
199  
200 case TRANS_AUTO:
201 return "auto";
202  
203 case TRANS_CR:
204 return "cr";
205  
206 case TRANS_CRLF:
207 return "crlf";
208  
209 case TRANS_LF:
210 return "lf";
211  
212 case TRANS_BINARY:
213 return "lf";
214  
215 default:
216 throw new TclRuntimeError( "bad translation id" );
217  
218 }
219 }
220  
221 /*
222 * Return a numerical identifier for the given -translation string.
223 */
224  
225 internal static int getTranslationID( string translation )
226 {
227 if ( translation.Equals( "auto" ) )
228 return TRANS_AUTO;
229 else if ( translation.Equals( "cr" ) )
230 return TRANS_CR;
231 else if ( translation.Equals( "crlf" ) )
232 return TRANS_CRLF;
233 else if ( translation.Equals( "lf" ) )
234 return TRANS_LF;
235 else if ( translation.Equals( "binary" ) )
236 return TRANS_LF;
237 else if ( translation.Equals( "platform" ) )
238 return TRANS_PLATFORM;
239 else
240 return -1;
241 }
242  
243 /*
244 * Return a string description for a -buffering id defined above.
245 */
246  
247 internal static string getBufferingString( int buffering )
248 {
249 switch ( buffering )
250 {
251  
252 case BUFF_FULL:
253 return "full";
254  
255 case BUFF_LINE:
256 return "line";
257  
258 case BUFF_NONE:
259 return "none";
260  
261 default:
262 throw new TclRuntimeError( "bad buffering id" );
263  
264 }
265 }
266  
267 /*
268 * Return a numerical identifier for the given -buffering string.
269 */
270  
271 internal static int getBufferingID( string buffering )
272 {
273 if ( buffering.Equals( "full" ) )
274 return BUFF_FULL;
275 else if ( buffering.Equals( "line" ) )
276 return BUFF_LINE;
277 else if ( buffering.Equals( "none" ) )
278 return BUFF_NONE;
279 else
280 return -1;
281 }
282 static TclIO()
283 {
284 {
285 if ( Util.Windows )
286 TRANS_PLATFORM = TRANS_CRLF;
287 else if ( Util.Mac )
288 TRANS_PLATFORM = TRANS_CR;
289 else
290 TRANS_PLATFORM = TRANS_LF;
291 }
292 }
293 }
294 }