wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * OpenCmd.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: OpenCmd.java,v 1.5 2003/03/08 03:42:44 mdejong Exp $ |
||
13 | * |
||
14 | */ |
||
15 | using System; |
||
16 | using System.IO; |
||
17 | namespace tcl.lang |
||
18 | { |
||
19 | |||
20 | /// <summary> This class implements the built-in "open" command in Tcl.</summary> |
||
21 | |||
22 | class OpenCmd : Command |
||
23 | { |
||
24 | /// <summary> This procedure is invoked to process the "open" Tcl command. |
||
25 | /// See the user documentation for details on what it does. |
||
26 | /// |
||
27 | /// </summary> |
||
28 | /// <param name="interp">the current interpreter. |
||
29 | /// </param> |
||
30 | /// <param name="argv">command arguments. |
||
31 | /// </param> |
||
32 | |||
33 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv ) |
||
34 | { |
||
35 | |||
36 | bool pipeline = false; /* True if opening pipeline chan */ |
||
37 | int prot = 438; /* Final rdwr permissions of file */ |
||
38 | int modeFlags = TclIO.RDONLY; /* Rdwr mode for the file. See the |
||
39 | * TclIO class for more info on the |
||
40 | * valid modes */ |
||
41 | |||
42 | if ( ( argv.Length < 2 ) || ( argv.Length > 4 ) ) |
||
43 | { |
||
44 | throw new TclNumArgsException( interp, 1, argv, "fileName ?access? ?permissions?" ); |
||
45 | } |
||
46 | |||
47 | if ( argv.Length > 2 ) |
||
48 | { |
||
49 | TclObject mode = argv[2]; |
||
50 | |||
51 | string modeStr = mode.ToString(); |
||
52 | int len = modeStr.Length; |
||
53 | |||
54 | // This "r+1" hack is just to get a test case to pass |
||
55 | if ( ( len == 0 ) || ( modeStr.StartsWith( "r+" ) && len >= 3 ) ) |
||
56 | { |
||
57 | throw new TclException( interp, "illegal access mode \"" + modeStr + "\"" ); |
||
58 | } |
||
59 | |||
60 | if ( len < 3 ) |
||
61 | { |
||
62 | switch ( modeStr[0] ) |
||
63 | { |
||
64 | |||
65 | case 'r': |
||
66 | { |
||
67 | if ( len == 1 ) |
||
68 | { |
||
69 | modeFlags = TclIO.RDONLY; |
||
70 | break; |
||
71 | } |
||
72 | else if ( modeStr[1] == '+' ) |
||
73 | { |
||
74 | modeFlags = TclIO.RDWR; |
||
75 | break; |
||
76 | } |
||
77 | } |
||
78 | goto case 'w'; |
||
79 | |||
80 | case 'w': |
||
81 | { |
||
82 | |||
83 | FileInfo f = FileUtil.getNewFileObj( interp, argv[1].ToString() ); |
||
84 | bool tmpBool; |
||
85 | if ( File.Exists( f.FullName ) ) |
||
86 | tmpBool = true; |
||
87 | else |
||
88 | tmpBool = Directory.Exists( f.FullName ); |
||
89 | if ( tmpBool ) |
||
90 | { |
||
91 | bool tmpBool2; |
||
92 | try |
||
93 | { |
||
94 | if ( File.Exists( f.FullName ) ) |
||
95 | { |
||
96 | File.SetAttributes( f.FullName, FileAttributes.Normal ); |
||
97 | File.Delete( f.FullName ); |
||
98 | tmpBool2 = true; |
||
99 | } |
||
100 | else if ( Directory.Exists( f.FullName ) ) |
||
101 | { |
||
102 | Directory.Delete( f.FullName ); |
||
103 | tmpBool2 = true; |
||
104 | } |
||
105 | else |
||
106 | { |
||
107 | tmpBool2 = false; |
||
108 | } |
||
109 | } |
||
110 | // ATK added because .NET do not allow often to delete |
||
111 | // files used by another process |
||
112 | catch ( IOException e ) |
||
113 | { |
||
114 | throw new TclException( interp, "cannot open file: " + argv[1].ToString() ); |
||
115 | } |
||
116 | bool generatedAux = tmpBool2; |
||
117 | } |
||
118 | if ( len == 1 ) |
||
119 | { |
||
120 | modeFlags = ( TclIO.WRONLY | TclIO.CREAT ); |
||
121 | break; |
||
122 | } |
||
123 | else if ( modeStr[1] == '+' ) |
||
124 | { |
||
125 | modeFlags = ( TclIO.RDWR | TclIO.CREAT ); |
||
126 | break; |
||
127 | } |
||
128 | } |
||
129 | goto case 'a'; |
||
130 | |||
131 | case 'a': |
||
132 | { |
||
133 | if ( len == 1 ) |
||
134 | { |
||
135 | modeFlags = ( TclIO.WRONLY | TclIO.APPEND ); |
||
136 | break; |
||
137 | } |
||
138 | else if ( modeStr[1] == '+' ) |
||
139 | { |
||
140 | modeFlags = ( TclIO.RDWR | TclIO.CREAT | TclIO.APPEND ); |
||
141 | break; |
||
142 | } |
||
143 | } |
||
144 | goto default; |
||
145 | |||
146 | default: |
||
147 | { |
||
148 | throw new TclException( interp, "illegal access mode \"" + modeStr + "\"" ); |
||
149 | } |
||
150 | |||
151 | } |
||
152 | } |
||
153 | else |
||
154 | { |
||
155 | modeFlags = 0; |
||
156 | bool gotRorWflag = false; |
||
157 | int mlen = TclList.getLength( interp, mode ); |
||
158 | for ( int i = 0; i < mlen; i++ ) |
||
159 | { |
||
160 | TclObject marg = TclList.index( interp, mode, i ); |
||
161 | |||
162 | if ( marg.ToString().Equals( "RDONLY" ) ) |
||
163 | { |
||
164 | modeFlags |= TclIO.RDONLY; |
||
165 | gotRorWflag = true; |
||
166 | } |
||
167 | else |
||
168 | { |
||
169 | |||
170 | if ( marg.ToString().Equals( "WRONLY" ) ) |
||
171 | { |
||
172 | modeFlags |= TclIO.WRONLY; |
||
173 | gotRorWflag = true; |
||
174 | } |
||
175 | else |
||
176 | { |
||
177 | |||
178 | if ( marg.ToString().Equals( "RDWR" ) ) |
||
179 | { |
||
180 | modeFlags |= TclIO.RDWR; |
||
181 | gotRorWflag = true; |
||
182 | } |
||
183 | else |
||
184 | { |
||
185 | |||
186 | if ( marg.ToString().Equals( "APPEND" ) ) |
||
187 | { |
||
188 | modeFlags |= TclIO.APPEND; |
||
189 | } |
||
190 | else |
||
191 | { |
||
192 | |||
193 | if ( marg.ToString().Equals( "CREAT" ) ) |
||
194 | { |
||
195 | modeFlags |= TclIO.CREAT; |
||
196 | } |
||
197 | else |
||
198 | { |
||
199 | |||
200 | if ( marg.ToString().Equals( "EXCL" ) ) |
||
201 | { |
||
202 | modeFlags |= TclIO.EXCL; |
||
203 | } |
||
204 | else |
||
205 | { |
||
206 | |||
207 | if ( marg.ToString().Equals( "TRUNC" ) ) |
||
208 | { |
||
209 | modeFlags |= TclIO.TRUNC; |
||
210 | } |
||
211 | else |
||
212 | { |
||
213 | |||
214 | throw new TclException( interp, "invalid access mode \"" + marg.ToString() + "\": must be RDONLY, WRONLY, RDWR, APPEND, " + "CREAT EXCL, NOCTTY, NONBLOCK, or TRUNC" ); |
||
215 | } |
||
216 | } |
||
217 | } |
||
218 | } |
||
219 | } |
||
220 | } |
||
221 | } |
||
222 | } |
||
223 | if ( !gotRorWflag ) |
||
224 | { |
||
225 | throw new TclException( interp, "access mode must include either RDONLY, WRONLY, or RDWR" ); |
||
226 | } |
||
227 | } |
||
228 | } |
||
229 | |||
230 | if ( argv.Length == 4 ) |
||
231 | { |
||
232 | prot = TclInteger.get( interp, argv[3] ); |
||
233 | throw new TclException( interp, "setting permissions not implemented yet" ); |
||
234 | } |
||
235 | |||
236 | if ( ( argv[1].ToString().Length > 0 ) && ( argv[1].ToString()[0] == '|' ) ) |
||
237 | { |
||
238 | pipeline = true; |
||
239 | throw new TclException( interp, "pipes not implemented yet" ); |
||
240 | } |
||
241 | |||
242 | /* |
||
243 | * Open the file or create a process pipeline. |
||
244 | */ |
||
245 | |||
246 | if ( !pipeline ) |
||
247 | { |
||
248 | try |
||
249 | { |
||
250 | FileChannel file = new FileChannel(); |
||
251 | |||
252 | file.open( interp, argv[1].ToString(), modeFlags ); |
||
253 | TclIO.registerChannel( interp, file ); |
||
254 | interp.setResult( file.ChanName ); |
||
255 | } |
||
256 | catch ( IOException e ) |
||
257 | { |
||
258 | |||
259 | throw new TclException( interp, "cannot open file: " + argv[1].ToString() ); |
||
260 | } |
||
261 | } |
||
262 | else |
||
263 | { |
||
264 | /* |
||
265 | * Pipeline code here... |
||
266 | */ |
||
267 | } |
||
268 | return TCL.CompletionCode.RETURN; |
||
269 | } |
||
270 | } |
||
271 | } |