wasCSharpSQLite – Blame information for rev 1
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * ReadCmd.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: ReadCmd.java,v 1.8 2003/03/08 03:42:44 mdejong Exp $ |
||
13 | * |
||
14 | */ |
||
15 | using System.Text; |
||
16 | using System.IO; |
||
17 | namespace tcl.lang |
||
18 | { |
||
19 | |||
20 | /// <summary> This class implements the built-in "read" command in Tcl.</summary> |
||
21 | |||
22 | class ReadCmd : Command |
||
23 | { |
||
24 | |||
25 | /// <summary> This procedure is invoked to process the "read" Tcl command. |
||
26 | /// See the user documentation for details on what it does. |
||
27 | /// |
||
28 | /// </summary> |
||
29 | /// <param name="interp">the current interpreter. |
||
30 | /// </param> |
||
31 | /// <param name="argv">command arguments. |
||
32 | /// </param> |
||
33 | |||
34 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv ) |
||
35 | { |
||
36 | |||
37 | Channel chan; // The channel being operated on this |
||
38 | // method |
||
39 | int i = 1; // Index to the next arg in argv |
||
40 | int toRead = 0; // Number of bytes or chars to read from channel |
||
41 | int charactersRead; // Number of bytes or chars read from channel |
||
42 | bool readAll = true; // If true read-all else toRead |
||
43 | bool noNewline = false; // If true, strip the newline if there |
||
44 | TclObject result; |
||
45 | |||
46 | |||
47 | if ( ( argv.Length != 2 ) && ( argv.Length != 3 ) ) |
||
48 | { |
||
49 | |||
50 | errorWrongNumArgs( interp, argv[0].ToString() ); |
||
51 | } |
||
52 | |||
53 | |||
54 | if ( argv[i].ToString().Equals( "-nonewline" ) ) |
||
55 | { |
||
56 | noNewline = true; |
||
57 | i++; |
||
58 | } |
||
59 | |||
60 | if ( i == argv.Length ) |
||
61 | { |
||
62 | |||
63 | errorWrongNumArgs( interp, argv[0].ToString() ); |
||
64 | } |
||
65 | |||
66 | |||
67 | chan = TclIO.getChannel( interp, argv[i].ToString() ); |
||
68 | if ( chan == null ) |
||
69 | { |
||
70 | |||
71 | throw new TclException( interp, "can not find channel named \"" + argv[i].ToString() + "\"" ); |
||
72 | } |
||
73 | |||
74 | // Consumed channel name. |
||
75 | |||
76 | i++; |
||
77 | |||
78 | // Compute how many bytes or chars to read, and see whether the final |
||
79 | // noNewline should be dropped. |
||
80 | |||
81 | if ( i < argv.Length ) |
||
82 | { |
||
83 | |||
84 | string arg = argv[i].ToString(); |
||
85 | |||
86 | if ( System.Char.IsDigit( arg[0] ) ) |
||
87 | { |
||
88 | toRead = TclInteger.get( interp, argv[i] ); |
||
89 | readAll = false; |
||
90 | } |
||
91 | else if ( arg.Equals( "nonewline" ) ) |
||
92 | { |
||
93 | noNewline = true; |
||
94 | } |
||
95 | else |
||
96 | { |
||
97 | throw new TclException( interp, "bad argument \"" + arg + "\": should be \"nonewline\"" ); |
||
98 | } |
||
99 | } |
||
100 | |||
101 | try |
||
102 | { |
||
103 | if ( (System.Object)chan.Encoding == null ) |
||
104 | { |
||
105 | result = TclByteArray.newInstance(); |
||
106 | } |
||
107 | else |
||
108 | { |
||
109 | result = TclString.newInstance( new StringBuilder( 64 ) ); |
||
110 | } |
||
111 | if ( readAll ) |
||
112 | { |
||
113 | charactersRead = chan.read( interp, result, TclIO.READ_ALL, 0 ); |
||
114 | |||
115 | // If -nonewline was specified, and we have not hit EOF |
||
116 | // and the last char is a "\n", then remove it and return. |
||
117 | |||
118 | if ( noNewline ) |
||
119 | { |
||
120 | |||
121 | string inStr = result.ToString(); |
||
122 | if ( ( charactersRead > 0 ) && ( inStr[charactersRead - 1] == '\n' ) ) |
||
123 | { |
||
124 | interp.setResult( inStr.Substring( 0, ( ( charactersRead - 1 ) ) - ( 0 ) ) ); |
||
125 | return TCL.CompletionCode.RETURN; |
||
126 | } |
||
127 | } |
||
128 | } |
||
129 | else |
||
130 | { |
||
131 | // FIXME: Bug here, the -nonewline flag must be respected |
||
132 | // when reading a set number of bytes |
||
133 | charactersRead = chan.read( interp, result, TclIO.READ_N_BYTES, toRead ); |
||
134 | } |
||
135 | |||
136 | /* |
||
137 | // FIXME: Port this -nonewline logic from the C code. |
||
138 | if (charactersRead < 0) { |
||
139 | Tcl_ResetResult(interp); |
||
140 | Tcl_AppendResult(interp, "error reading \"", name, "\": ", |
||
141 | Tcl_PosixError(interp), (char *) NULL); |
||
142 | Tcl_DecrRefCount(resultPtr); |
||
143 | return TCL_ERROR; |
||
144 | } |
||
145 | |||
146 | // If requested, remove the last newline in the channel if at EOF. |
||
147 | |||
148 | if ((charactersRead > 0) && (newline != 0)) { |
||
149 | char *result; |
||
150 | int length; |
||
151 | |||
152 | result = Tcl_GetStringFromObj(resultPtr, length); |
||
153 | if (result[length - 1] == '\n') { |
||
154 | Tcl_SetObjLength(resultPtr, length - 1); |
||
155 | } |
||
156 | } |
||
157 | |||
158 | */ |
||
159 | |||
160 | interp.setResult( result ); |
||
161 | } |
||
162 | catch ( IOException e ) |
||
163 | { |
||
164 | throw new TclRuntimeError( "ReadCmd.cmdProc() Error: IOException when reading " + chan.ChanName ); |
||
165 | } |
||
166 | return TCL.CompletionCode.RETURN; |
||
167 | } |
||
168 | |||
169 | /// <summary> A unique error msg is printed for read, therefore dont call this |
||
170 | /// instead of the standard TclNumArgsException(). |
||
171 | /// |
||
172 | /// </summary> |
||
173 | /// <param name="interp">the current interpreter. |
||
174 | /// </param> |
||
175 | /// <param name="cmd">the name of the command (extracted form argv[0] of cmdProc) |
||
176 | /// </param> |
||
177 | |||
178 | private void errorWrongNumArgs( Interp interp, string cmd ) |
||
179 | { |
||
180 | throw new TclException( interp, "wrong # args: should be \"" + "read channelId ?numChars?\" " + "or \"read ?-nonewline? channelId\"" ); |
||
181 | } |
||
182 | } |
||
183 | } |