wasCSharpSQLite – Blame information for rev 4
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * TclByteArray.java |
||
3 | * |
||
4 | * This class contains the implementation of the Jacl binary data object. |
||
5 | * |
||
6 | * Copyright (c) 1999 Christian Krone. |
||
7 | * Copyright (c) 1997 Sun Microsystems, Inc. |
||
8 | * |
||
9 | * See the file "license.terms" for information on usage and |
||
10 | * redistribution of this file, and for a DISCLAIMER OF ALL |
||
11 | * WARRANTIES. |
||
12 | * |
||
13 | * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart |
||
14 | * |
||
15 | * RCS @(#) $Id: TclByteArray.java,v 1.4 2003/03/08 02:05:06 mdejong Exp $ |
||
16 | * |
||
17 | */ |
||
18 | using System; |
||
19 | namespace tcl.lang |
||
20 | { |
||
21 | |||
22 | /// <summary> This class implements the binary data object type in Tcl.</summary> |
||
23 | public class TclByteArray : InternalRep |
||
24 | { |
||
25 | |||
26 | /// <summary> The number of bytes used in the byte array. |
||
27 | /// The following structure is the internal rep for a ByteArray object. |
||
28 | /// Keeps track of how much memory has been used. This can be different from |
||
29 | /// how much has been allocated for the byte array to enable growing and |
||
30 | /// shrinking of the ByteArray object with fewer allocations. |
||
31 | /// </summary> |
||
32 | private int used; |
||
33 | |||
34 | /// <summary> Internal representation of the binary data.</summary> |
||
35 | private byte[] bytes; |
||
36 | |||
37 | /// <summary> Create a new empty Tcl binary data.</summary> |
||
38 | private TclByteArray() |
||
39 | { |
||
40 | used = 0; |
||
41 | bytes = new byte[0]; |
||
42 | } |
||
43 | |||
44 | /// <summary> Create a new Tcl binary data.</summary> |
||
45 | private TclByteArray( byte[] b ) |
||
46 | { |
||
47 | used = b.Length; |
||
48 | bytes = new byte[used]; |
||
49 | Array.Copy( b, 0, bytes, 0, used ); |
||
50 | } |
||
51 | |||
52 | /// <summary> Create a new Tcl binary data.</summary> |
||
53 | private TclByteArray( byte[] b, int position, int length ) |
||
54 | { |
||
55 | used = length; |
||
56 | bytes = new byte[used]; |
||
57 | Array.Copy( b, position, bytes, 0, used ); |
||
58 | } |
||
59 | |||
60 | /// <summary> Create a new Tcl binary data.</summary> |
||
61 | private TclByteArray( char[] c ) |
||
62 | { |
||
63 | used = c.Length; |
||
64 | bytes = new byte[used]; |
||
65 | for ( int ix = 0; ix < used; ix++ ) |
||
66 | { |
||
67 | bytes[ix] = (byte)c[ix]; |
||
68 | } |
||
69 | } |
||
70 | |||
71 | /// <summary> Returns a duplicate of the current object. |
||
72 | /// |
||
73 | /// </summary> |
||
74 | /// <param name="obj">the TclObject that contains this internalRep. |
||
75 | /// </param> |
||
76 | public InternalRep duplicate() |
||
77 | { |
||
78 | return new TclByteArray( bytes, 0, used ); |
||
79 | } |
||
80 | |||
81 | /// <summary> Implement this no-op for the InternalRep interface.</summary> |
||
82 | |||
83 | public void dispose() |
||
84 | { |
||
85 | } |
||
86 | |||
87 | /// <summary> Called to query the string representation of the Tcl object. This |
||
88 | /// method is called only by TclObject.toString() when |
||
89 | /// TclObject.stringRep is null. |
||
90 | /// |
||
91 | /// </summary> |
||
92 | /// <returns> the string representation of the Tcl object. |
||
93 | /// </returns> |
||
94 | public override string ToString() |
||
95 | { |
||
96 | char[] c = new char[used]; |
||
97 | for ( int ix = 0; ix < used; ix++ ) |
||
98 | { |
||
99 | c[ix] = (char)( bytes[ix] & 0xff ); |
||
100 | } |
||
101 | return new string( c ); |
||
102 | } |
||
103 | |||
104 | /// <summary> Creates a new instance of a TclObject with a TclByteArray internal |
||
105 | /// rep. |
||
106 | /// |
||
107 | /// </summary> |
||
108 | /// <returns> the TclObject with the given byte array value. |
||
109 | /// </returns> |
||
110 | |||
111 | public static TclObject newInstance( byte[] b, int position, int length ) |
||
112 | { |
||
113 | return new TclObject( new TclByteArray( b, position, length ) ); |
||
114 | } |
||
115 | |||
116 | /// <summary> Creates a new instance of a TclObject with a TclByteArray internal |
||
117 | /// rep. |
||
118 | /// |
||
119 | /// </summary> |
||
120 | /// <returns> the TclObject with the given byte array value. |
||
121 | /// </returns> |
||
122 | |||
123 | public static TclObject newInstance( byte[] b ) |
||
124 | { |
||
125 | return new TclObject( new TclByteArray( b ) ); |
||
126 | } |
||
127 | |||
128 | /// <summary> Creates a new instance of a TclObject with an empty TclByteArray |
||
129 | /// internal rep. |
||
130 | /// |
||
131 | /// </summary> |
||
132 | /// <returns> the TclObject with the empty byte array value. |
||
133 | /// </returns> |
||
134 | |||
135 | public static TclObject newInstance() |
||
136 | { |
||
137 | return new TclObject( new TclByteArray() ); |
||
138 | } |
||
139 | |||
140 | /// <summary> Called to convert the other object's internal rep to a ByteArray. |
||
141 | /// |
||
142 | /// </summary> |
||
143 | /// <param name="interp">current interpreter. |
||
144 | /// </param> |
||
145 | /// <param name="tobj">the TclObject to convert to use the ByteArray internal rep. |
||
146 | /// </param> |
||
147 | /// <exception cref=""> TclException if the object doesn't contain a valid ByteArray. |
||
148 | /// </exception> |
||
149 | internal static void setByteArrayFromAny( Interp interp, TclObject tobj ) |
||
150 | { |
||
151 | InternalRep rep = tobj.InternalRep; |
||
152 | |||
153 | if ( !( rep is TclByteArray ) ) |
||
154 | { |
||
155 | |||
156 | char[] c = tobj.ToString().ToCharArray(); |
||
157 | tobj.InternalRep = new TclByteArray( c ); |
||
158 | } |
||
159 | } |
||
160 | |||
161 | /// <summary> |
||
162 | /// This method changes the length of the byte array for this |
||
163 | /// object. Once the caller has set the length of the array, it |
||
164 | /// is acceptable to directly modify the bytes in the array up until |
||
165 | /// Tcl_GetStringFromObj() has been called on this object. |
||
166 | /// |
||
167 | /// Results: |
||
168 | /// The new byte array of the specified length. |
||
169 | /// |
||
170 | /// Side effects: |
||
171 | /// Allocates enough memory for an array of bytes of the requested |
||
172 | /// size. When growing the array, the old array is copied to the |
||
173 | /// new array; new bytes are undefined. When shrinking, the |
||
174 | /// old array is truncated to the specified length. |
||
175 | /// </summary> |
||
176 | |||
177 | public static byte[] setLength( Interp interp, TclObject tobj, int length ) |
||
178 | { |
||
179 | if ( tobj.Shared ) |
||
180 | { |
||
181 | throw new TclRuntimeError( "TclByteArray.setLength() called with shared object" ); |
||
182 | } |
||
183 | setByteArrayFromAny( interp, tobj ); |
||
184 | TclByteArray tbyteArray = (TclByteArray)tobj.InternalRep; |
||
185 | |||
186 | if ( length > tbyteArray.bytes.Length ) |
||
187 | { |
||
188 | byte[] newBytes = new byte[length]; |
||
189 | Array.Copy( tbyteArray.bytes, 0, newBytes, 0, tbyteArray.used ); |
||
190 | tbyteArray.bytes = newBytes; |
||
191 | } |
||
192 | tobj.invalidateStringRep(); |
||
193 | tbyteArray.used = length; |
||
194 | return tbyteArray.bytes; |
||
195 | } |
||
196 | |||
197 | /// <summary> Queries the length of the byte array. If tobj is not a byte array |
||
198 | /// object, an attempt will be made to convert it to a byte array. |
||
199 | /// |
||
200 | /// </summary> |
||
201 | /// <param name="interp">current interpreter. |
||
202 | /// </param> |
||
203 | /// <param name="tobj">the TclObject to use as a byte array. |
||
204 | /// </param> |
||
205 | /// <returns> the length of the byte array. |
||
206 | /// </returns> |
||
207 | /// <exception cref=""> TclException if tobj is not a valid byte array. |
||
208 | /// </exception> |
||
209 | public static int getLength( Interp interp, TclObject tobj ) |
||
210 | { |
||
211 | setByteArrayFromAny( interp, tobj ); |
||
212 | |||
213 | TclByteArray tbyteArray = (TclByteArray)tobj.InternalRep; |
||
214 | return tbyteArray.used; |
||
215 | } |
||
216 | |||
217 | /// <summary> Returns the bytes of a ByteArray object. If tobj is not a ByteArray |
||
218 | /// object, an attempt will be made to convert it to a ByteArray. <p> |
||
219 | /// |
||
220 | /// </summary> |
||
221 | /// <param name="interp">the current interpreter. |
||
222 | /// </param> |
||
223 | /// <param name="tobj">the byte array object. |
||
224 | /// </param> |
||
225 | /// <returns> a byte array. |
||
226 | /// </returns> |
||
227 | /// <exception cref=""> TclException if tobj is not a valid ByteArray. |
||
228 | /// </exception> |
||
229 | public static byte[] getBytes( Interp interp, TclObject tobj ) |
||
230 | { |
||
231 | setByteArrayFromAny( interp, tobj ); |
||
232 | TclByteArray tbyteArray = (TclByteArray)tobj.InternalRep; |
||
233 | return tbyteArray.bytes; |
||
234 | } |
||
235 | } |
||
236 | } |