wasCSharpSQLite – Blame information for rev 4

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * SwitchCmd.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: SwitchCmd.java,v 1.2 1999/05/09 01:32:03 dejong Exp $
13 *
14 */
15 using System;
16 namespace tcl.lang
17 {
18  
19 /// <summary> This class implements the built-in "switch" command in Tcl.</summary>
20  
21 class SwitchCmd : Command
22 {
23  
24 private static readonly string[] validCmds = new string[] { "-exact", "-glob", "-regexp", "--" };
25 private const int EXACT = 0;
26 private const int GLOB = 1;
27 private const int REGEXP = 2;
28 private const int LAST = 3;
29 public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv )
30 {
31 int i, mode, body;
32 bool matched;
33 string inString;
34 TclObject[] switchArgv = null;
35  
36 mode = EXACT;
37 for ( i = 1; i < argv.Length; i++ )
38 {
39  
40 if ( !argv[i].ToString().StartsWith( "-" ) )
41 {
42 break;
43 }
44 int opt = TclIndex.get( interp, argv[i], validCmds, "option", 1 );
45 if ( opt == LAST )
46 {
47 i++;
48 break;
49 }
50 else if ( opt > LAST )
51 {
52 throw new TclException( interp, "SwitchCmd.cmdProc: bad option " + opt + " index to validCmds" );
53 }
54 else
55 {
56 mode = opt;
57 }
58 }
59  
60 if ( argv.Length - i < 2 )
61 {
62 throw new TclNumArgsException( interp, 1, argv, "?switches? string pattern body ... ?default body?" );
63 }
64  
65 inString = argv[i].ToString();
66 i++;
67  
68 // If all of the pattern/command pairs are lumped into a single
69 // argument, split them out again.
70  
71 if ( argv.Length - i == 1 )
72 {
73 switchArgv = TclList.getElements( interp, argv[i] );
74 i = 0;
75 }
76 else
77 {
78 switchArgv = argv;
79 }
80  
81 for ( ; i < switchArgv.Length; i += 2 )
82 {
83 if ( i == ( switchArgv.Length - 1 ) )
84 {
85 throw new TclException( interp, "extra switch pattern with no body" );
86 }
87  
88 // See if the pattern matches the string.
89  
90 matched = false;
91  
92 string pattern = switchArgv[i].ToString();
93  
94 if ( ( i == switchArgv.Length - 2 ) && pattern.Equals( "default" ) )
95 {
96 matched = true;
97 }
98 else
99 {
100 switch ( mode )
101 {
102  
103 case EXACT:
104 matched = inString.Equals( pattern );
105 break;
106  
107 case GLOB:
108 matched = Util.stringMatch( inString, pattern );
109 break;
110  
111 case REGEXP:
112 matched = Util.regExpMatch( interp, inString, switchArgv[i] );
113 break;
114 }
115 }
116 if ( !matched )
117 {
118 continue;
119 }
120  
121 // We've got a match. Find a body to execute, skipping bodies
122 // that are "-".
123  
124 for ( body = i + 1; ; body += 2 )
125 {
126 if ( body >= switchArgv.Length )
127 {
128  
129 throw new TclException( interp, "no body specified for pattern \"" + switchArgv[i] + "\"" );
130 }
131  
132 if ( !switchArgv[body].ToString().Equals( "-" ) )
133 {
134 break;
135 }
136 }
137  
138 try
139 {
140 interp.eval( switchArgv[body], 0 );
141 return TCL.CompletionCode.RETURN;
142 }
143 catch ( TclException e )
144 {
145 if ( e.getCompletionCode() == TCL.CompletionCode.ERROR )
146 {
147  
148 interp.addErrorInfo( "\n (\"" + switchArgv[i] + "\" arm line " + interp.errorLine + ")" );
149 }
150 throw;
151 }
152 }
153  
154 // Nothing matched: return nothing.
155 return TCL.CompletionCode.RETURN;
156 }
157 } // end SwitchCmd
158 }