wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * SubstCmd.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: SubstCmd.java,v 1.3 2003/01/09 02:15:39 mdejong Exp $ |
||
13 | * |
||
14 | */ |
||
15 | using System.Text; |
||
16 | namespace tcl.lang |
||
17 | { |
||
18 | |||
19 | /// <summary> This class implements the built-in "subst" command in Tcl.</summary> |
||
20 | |||
21 | class SubstCmd : Command |
||
22 | { |
||
23 | private static readonly string[] validCmds = new string[] { "-nobackslashes", "-nocommands", "-novariables" }; |
||
24 | |||
25 | internal const int OPT_NOBACKSLASHES = 0; |
||
26 | internal const int OPT_NOCOMMANDS = 1; |
||
27 | internal const int OPT_NOVARS = 2; |
||
28 | |||
29 | /// <summary> This procedure is invoked to process the "subst" Tcl command. |
||
30 | /// See the user documentation for details on what it does. |
||
31 | /// |
||
32 | /// </summary> |
||
33 | /// <param name="interp">the current interpreter. |
||
34 | /// </param> |
||
35 | /// <param name="argv">command arguments. |
||
36 | /// </param> |
||
37 | /// <exception cref=""> TclException if wrong # of args or invalid argument(s). |
||
38 | /// </exception> |
||
39 | |||
40 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv ) |
||
41 | { |
||
42 | int currentObjIndex, len, i; |
||
43 | int objc = argv.Length - 1; |
||
44 | bool doBackslashes = true; |
||
45 | bool doCmds = true; |
||
46 | bool doVars = true; |
||
47 | StringBuilder result = new StringBuilder(); |
||
48 | string s; |
||
49 | char c; |
||
50 | |||
51 | for ( currentObjIndex = 1; currentObjIndex < objc; currentObjIndex++ ) |
||
52 | { |
||
53 | |||
54 | if ( !argv[currentObjIndex].ToString().StartsWith( "-" ) ) |
||
55 | { |
||
56 | break; |
||
57 | } |
||
58 | int opt = TclIndex.get( interp, argv[currentObjIndex], validCmds, "switch", 0 ); |
||
59 | switch ( opt ) |
||
60 | { |
||
61 | |||
62 | case OPT_NOBACKSLASHES: |
||
63 | doBackslashes = false; |
||
64 | break; |
||
65 | |||
66 | case OPT_NOCOMMANDS: |
||
67 | doCmds = false; |
||
68 | break; |
||
69 | |||
70 | case OPT_NOVARS: |
||
71 | doVars = false; |
||
72 | break; |
||
73 | |||
74 | default: |
||
75 | throw new TclException( interp, "SubstCmd.cmdProc: bad option " + opt + " index to cmds" ); |
||
76 | |||
77 | } |
||
78 | } |
||
79 | if ( currentObjIndex != objc ) |
||
80 | { |
||
81 | throw new TclNumArgsException( interp, currentObjIndex, argv, "?-nobackslashes? ?-nocommands? ?-novariables? string" ); |
||
82 | } |
||
83 | |||
84 | /* |
||
85 | * Scan through the string one character at a time, performing |
||
86 | * command, variable, and backslash substitutions. |
||
87 | */ |
||
88 | |||
89 | |||
90 | s = argv[currentObjIndex].ToString(); |
||
91 | len = s.Length; |
||
92 | i = 0; |
||
93 | while ( i < len ) |
||
94 | { |
||
95 | c = s[i]; |
||
96 | |||
97 | if ( ( c == '[' ) && doCmds ) |
||
98 | { |
||
99 | ParseResult res; |
||
100 | try |
||
101 | { |
||
102 | interp.evalFlags = Parser.TCL_BRACKET_TERM; |
||
103 | interp.eval( s.Substring( i + 1, ( len ) - ( i + 1 ) ) ); |
||
104 | TclObject interp_result = interp.getResult(); |
||
105 | interp_result.preserve(); |
||
106 | res = new ParseResult( interp_result, i + interp.termOffset ); |
||
107 | } |
||
108 | catch ( TclException e ) |
||
109 | { |
||
110 | i = e.errIndex + 1; |
||
111 | throw; |
||
112 | } |
||
113 | i = res.nextIndex + 2; |
||
114 | |||
115 | result.Append( res.value.ToString() ); |
||
116 | res.release(); |
||
117 | } |
||
118 | else if ( c == '\r' ) |
||
119 | { |
||
120 | /* |
||
121 | * (ToDo) may not be portable on Mac |
||
122 | */ |
||
123 | |||
124 | i++; |
||
125 | } |
||
126 | else if ( ( c == '$' ) && doVars ) |
||
127 | { |
||
128 | ParseResult vres = Parser.parseVar( interp, s.Substring( i, ( len ) - ( i ) ) ); |
||
129 | i += vres.nextIndex; |
||
130 | |||
131 | result.Append( vres.value.ToString() ); |
||
132 | vres.release(); |
||
133 | } |
||
134 | else if ( ( c == '\\' ) && doBackslashes ) |
||
135 | { |
||
136 | BackSlashResult bs = tcl.lang.Interp.backslash( s, i, len ); |
||
137 | i = bs.nextIndex; |
||
138 | if ( bs.isWordSep ) |
||
139 | { |
||
140 | break; |
||
141 | } |
||
142 | else |
||
143 | { |
||
144 | result.Append( bs.c ); |
||
145 | } |
||
146 | } |
||
147 | else |
||
148 | { |
||
149 | result.Append( c ); |
||
150 | i++; |
||
151 | } |
||
152 | } |
||
153 | |||
154 | interp.setResult( result.ToString() ); |
||
155 | return TCL.CompletionCode.RETURN; |
||
156 | } |
||
157 | } |
||
158 | } |