wasCSharpSQLite – Blame information for rev

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * FconfigureCmd.java --
3 *
4 * Copyright (c) 2001 Bruce A. Johnson
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: FconfigureCmd.java,v 1.11 2003/03/08 03:42:43 mdejong Exp $
14 *
15 */
16 using System;
17 using System.Text;
18  
19 namespace tcl.lang
20 {
21  
22 /// <summary> This class implements the built-in "fconfigure" command in Tcl.</summary>
23  
24 class FconfigureCmd : Command
25 {
26  
27 private static readonly string[] validCmds = new string[] { "-blocking", "-buffering", "-buffersize", "-encoding", "-eofchar", "-translation" };
28  
29 internal const int OPT_BLOCKING = 0;
30 internal const int OPT_BUFFERING = 1;
31 internal const int OPT_BUFFERSIZE = 2;
32 internal const int OPT_ENCODING = 3;
33 internal const int OPT_EOFCHAR = 4;
34 internal const int OPT_TRANSLATION = 5;
35  
36  
37 /// <summary> This procedure is invoked to process the "fconfigure" Tcl command.
38 /// See the user documentation for details on what it does.
39 ///
40 /// </summary>
41 /// <param name="interp">the current interpreter.
42 /// </param>
43 /// <param name="argv">command arguments.
44 /// </param>
45  
46 public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv )
47 {
48  
49 Channel chan; // The channel being operated on this method
50  
51 if ( ( argv.Length < 2 ) || ( ( ( argv.Length % 2 ) == 1 ) && ( argv.Length != 3 ) ) )
52 {
53 throw new TclNumArgsException( interp, 1, argv, "channelId ?optionName? ?value? ?optionName value?..." );
54 }
55  
56  
57 chan = TclIO.getChannel( interp, argv[1].ToString() );
58 if ( chan == null )
59 {
60  
61 throw new TclException( interp, "can not find channel named \"" + argv[1].ToString() + "\"" );
62 }
63  
64 if ( argv.Length == 2 )
65 {
66 // return list of all name/value pairs for this channelId
67 TclObject list = TclList.newInstance();
68  
69 TclList.append( interp, list, TclString.newInstance( "-blocking" ) );
70 TclList.append( interp, list, TclBoolean.newInstance( chan.Blocking ) );
71  
72 TclList.append( interp, list, TclString.newInstance( "-buffering" ) );
73 TclList.append( interp, list, TclString.newInstance( TclIO.getBufferingString( chan.Buffering ) ) );
74  
75 TclList.append( interp, list, TclString.newInstance( "-buffersize" ) );
76 TclList.append( interp, list, TclInteger.newInstance( chan.BufferSize ) );
77  
78 // -encoding
79  
80 TclList.append( interp, list, TclString.newInstance( "-encoding" ) );
81  
82 System.Text.Encoding javaEncoding = chan.Encoding;
83 string tclEncoding;
84 if ( (System.Object)javaEncoding == null )
85 {
86 tclEncoding = "binary";
87 }
88 else
89 {
90 tclEncoding = EncodingCmd.getTclName( javaEncoding );
91 }
92 TclList.append( interp, list, TclString.newInstance( tclEncoding ) );
93  
94 // -eofchar
95  
96 TclList.append( interp, list, TclString.newInstance( "-eofchar" ) );
97 if ( chan.ReadOnly )
98 {
99 char eofChar = chan.InputEofChar;
100 TclList.append( interp, list, ( eofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( eofChar ) );
101 }
102 else if ( chan.WriteOnly )
103 {
104 char eofChar = chan.OutputEofChar;
105 TclList.append( interp, list, ( eofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( eofChar ) );
106 }
107 else if ( chan.ReadWrite )
108 {
109 char inEofChar = chan.InputEofChar;
110 char outEofChar = chan.OutputEofChar;
111  
112 TclObject eofchar_pair = TclList.newInstance();
113  
114 TclList.append( interp, eofchar_pair, ( inEofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( inEofChar ) );
115  
116 TclList.append( interp, eofchar_pair, ( outEofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( outEofChar ) );
117  
118 TclList.append( interp, list, eofchar_pair );
119 }
120 else
121 {
122 // Not readable or writeable, do nothing
123 }
124  
125 // -translation
126  
127 TclList.append( interp, list, TclString.newInstance( "-translation" ) );
128  
129 if ( chan.ReadOnly )
130 {
131 TclList.append( interp, list, TclString.newInstance( TclIO.getTranslationString( chan.InputTranslation ) ) );
132 }
133 else if ( chan.WriteOnly )
134 {
135 TclList.append( interp, list, TclString.newInstance( TclIO.getTranslationString( chan.OutputTranslation ) ) );
136 }
137 else if ( chan.ReadWrite )
138 {
139 TclObject translation_pair = TclList.newInstance();
140  
141 TclList.append( interp, translation_pair, TclString.newInstance( TclIO.getTranslationString( chan.InputTranslation ) ) );
142 TclList.append( interp, translation_pair, TclString.newInstance( TclIO.getTranslationString( chan.OutputTranslation ) ) );
143  
144 TclList.append( interp, list, translation_pair );
145 }
146 else
147 {
148 // Not readable or writeable, do nothing
149 }
150  
151 interp.setResult( list );
152 }
153  
154 if ( argv.Length == 3 )
155 {
156 // return value for supplied name
157  
158 int index = TclIndex.get( interp, argv[2], validCmds, "option", 0 );
159  
160 switch ( index )
161 {
162  
163 case OPT_BLOCKING:
164 {
165 // -blocking
166 interp.setResult( chan.Blocking );
167 break;
168 }
169  
170 case OPT_BUFFERING:
171 {
172 // -buffering
173 interp.setResult( TclIO.getBufferingString( chan.Buffering ) );
174 break;
175 }
176  
177 case OPT_BUFFERSIZE:
178 {
179 // -buffersize
180 interp.setResult( chan.BufferSize );
181 break;
182 }
183  
184 case OPT_ENCODING:
185 {
186 // -encoding
187 System.Text.Encoding javaEncoding = chan.Encoding;
188 if ( (System.Object)javaEncoding == null )
189 {
190 interp.setResult( "binary" );
191 }
192 else
193 {
194 interp.setResult( EncodingCmd.getTclName( javaEncoding ) );
195 }
196 break;
197 }
198  
199 case OPT_EOFCHAR:
200 {
201 // -eofchar
202 if ( chan.ReadOnly )
203 {
204 char eofChar = chan.InputEofChar;
205 interp.setResult( ( eofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( eofChar ) );
206 }
207 else if ( chan.WriteOnly )
208 {
209 char eofChar = chan.OutputEofChar;
210 interp.setResult( ( eofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( eofChar ) );
211 }
212 else if ( chan.ReadWrite )
213 {
214 char inEofChar = chan.InputEofChar;
215 char outEofChar = chan.OutputEofChar;
216  
217 TclObject eofchar_pair = TclList.newInstance();
218  
219 TclList.append( interp, eofchar_pair, ( inEofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( inEofChar ) );
220  
221 TclList.append( interp, eofchar_pair, ( outEofChar == 0 ) ? TclString.newInstance( "" ) : TclString.newInstance( outEofChar ) );
222  
223 interp.setResult( eofchar_pair );
224 }
225 else
226 {
227 // Not readable or writeable, do nothing
228 }
229  
230 break;
231 }
232  
233 case OPT_TRANSLATION:
234 {
235 // -translation
236 if ( chan.ReadOnly )
237 {
238 interp.setResult( TclIO.getTranslationString( chan.InputTranslation ) );
239 }
240 else if ( chan.WriteOnly )
241 {
242 interp.setResult( TclIO.getTranslationString( chan.OutputTranslation ) );
243 }
244 else if ( chan.ReadWrite )
245 {
246 TclObject translation_pair = TclList.newInstance();
247  
248 TclList.append( interp, translation_pair, TclString.newInstance( TclIO.getTranslationString( chan.InputTranslation ) ) );
249 TclList.append( interp, translation_pair, TclString.newInstance( TclIO.getTranslationString( chan.OutputTranslation ) ) );
250  
251 interp.setResult( translation_pair );
252 }
253 else
254 {
255 // Not readable or writeable, do nothing
256 }
257  
258 break;
259 }
260  
261 default:
262 {
263 throw new TclRuntimeError( "Fconfigure.cmdProc() error: " + "incorrect index returned from TclIndex.get()" );
264 }
265  
266 }
267 }
268 for ( int i = 3; i < argv.Length; i += 2 )
269 {
270 // Iterate through the list setting the name with the
271 // corresponding value.
272  
273 int index = TclIndex.get( interp, argv[i - 1], validCmds, "option", 0 );
274  
275 switch ( index )
276 {
277  
278 case OPT_BLOCKING:
279 {
280 // -blocking
281 chan.Blocking = TclBoolean.get( interp, argv[i] );
282 break;
283 }
284  
285 case OPT_BUFFERING:
286 {
287 // -buffering
288  
289 int id = TclIO.getBufferingID( argv[i].ToString() );
290  
291 if ( id == -1 )
292 {
293 throw new TclException( interp, "bad value for -buffering: must be " + "one of full, line, or none" );
294 }
295  
296 chan.Buffering = id;
297 break;
298 }
299  
300 case OPT_BUFFERSIZE:
301 {
302 // -buffersize
303 chan.BufferSize = TclInteger.get( interp, argv[i] );
304 break;
305 }
306  
307 case OPT_ENCODING:
308 {
309 // -encoding
310  
311 string tclEncoding = argv[i].ToString();
312  
313 if ( tclEncoding.Equals( "" ) || tclEncoding.Equals( "binary" ) )
314 {
315 chan.Encoding = null;
316 }
317 else
318 {
319 System.Text.Encoding javaEncoding = EncodingCmd.getJavaName( tclEncoding );
320 if ( (System.Object)javaEncoding == null )
321 {
322 throw new TclException( interp, "unknown encoding \"" + tclEncoding + "\"" );
323 }
324 chan.Encoding = javaEncoding;
325 }
326  
327 break;
328 }
329  
330 case OPT_EOFCHAR:
331 {
332 // -eofchar
333 TclList.setListFromAny( interp, argv[i] );
334 int length = TclList.getLength( interp, argv[i] );
335  
336 if ( length > 2 )
337 {
338 throw new TclException( interp, "bad value for -eofchar: " + "should be a list of zero, one, or two elements" );
339 }
340  
341 char inputEofChar, outputEofChar;
342 string s;
343  
344 if ( length == 0 )
345 {
346 inputEofChar = outputEofChar = (char)( 0 );
347 }
348 else if ( length == 1 )
349 {
350  
351 s = TclList.index( interp, argv[i], 0 ).ToString();
352 inputEofChar = outputEofChar = s[0];
353 }
354 else
355 {
356  
357 s = TclList.index( interp, argv[i], 0 ).ToString();
358 inputEofChar = s[0];
359  
360  
361 s = TclList.index( interp, argv[i], 1 ).ToString();
362 outputEofChar = s[0];
363 }
364  
365 chan.InputEofChar = inputEofChar;
366 chan.OutputEofChar = outputEofChar;
367  
368 break;
369 }
370  
371 case OPT_TRANSLATION:
372 {
373 // -translation
374 TclList.setListFromAny( interp, argv[i] );
375 int length = TclList.getLength( interp, argv[i] );
376  
377 if ( length < 1 || length > 2 )
378 {
379 throw new TclException( interp, "bad value for -translation: " + "must be a one or two element list" );
380 }
381  
382 string inputTranslationArg, outputTranslationArg;
383 int inputTranslation, outputTranslation;
384  
385 if ( length == 2 )
386 {
387  
388 inputTranslationArg = TclList.index( interp, argv[i], 0 ).ToString();
389 inputTranslation = TclIO.getTranslationID( inputTranslationArg );
390  
391 outputTranslationArg = TclList.index( interp, argv[i], 1 ).ToString();
392 outputTranslation = TclIO.getTranslationID( outputTranslationArg );
393 }
394 else
395 {
396  
397 outputTranslationArg = inputTranslationArg = argv[i].ToString();
398 outputTranslation = inputTranslation = TclIO.getTranslationID( outputTranslationArg );
399 }
400  
401 if ( ( inputTranslation == -1 ) || ( outputTranslation == -1 ) )
402 {
403 throw new TclException( interp, "bad value for -translation: " + "must be one of auto, binary, cr, lf, " + "crlf, or platform" );
404 }
405  
406 if ( outputTranslation == TclIO.TRANS_AUTO )
407 outputTranslation = TclIO.TRANS_PLATFORM;
408  
409 if ( chan.ReadOnly )
410 {
411 chan.InputTranslation = inputTranslation;
412 if ( inputTranslationArg.Equals( "binary" ) )
413 {
414 chan.Encoding = null;
415 }
416 }
417 else if ( chan.WriteOnly )
418 {
419 chan.OutputTranslation = outputTranslation;
420 if ( outputTranslationArg.Equals( "binary" ) )
421 {
422 chan.Encoding = null;
423 }
424 }
425 else if ( chan.ReadWrite )
426 {
427 chan.InputTranslation = inputTranslation;
428 chan.OutputTranslation = outputTranslation;
429 if ( inputTranslationArg.Equals( "binary" ) || outputTranslationArg.Equals( "binary" ) )
430 {
431 chan.Encoding = null;
432 }
433 }
434 else
435 {
436 // Not readable or writeable, do nothing
437 }
438  
439 break;
440 }
441  
442 default:
443 {
444 throw new TclRuntimeError( "Fconfigure.cmdProc() error: " + "incorrect index returned from TclIndex.get()" );
445 }
446  
447 }
448 }
449 return TCL.CompletionCode.RETURN;
450 }
451 }
452 }