wasCSharpSQLite – Blame information for rev 1

Subversion Repositories:
Rev:
Rev Author Line No. Line
1 office 1 /*
2 * TclIndex.java
3 *
4 * This file implements objects of type "index". This object type
5 * is used to lookup a keyword in a table of valid values and cache
6 * the index of the matching entry.
7 *
8 * Copyright (c) 1997 Sun Microsystems, Inc.
9 *
10 * See the file "license.terms" for information on usage and
11 * redistribution of this file, and for a DISCLAIMER OF ALL
12 * WARRANTIES.
13 *
14 * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart
15 *
16 * RCS @(#) $Id: TclIndex.java,v 1.5 2003/01/10 01:35:58 mdejong Exp $
17 */
18 using System.Text;
19 namespace tcl.lang
20 {
21  
22 public class TclIndex : InternalRep
23 {
24  
25 /// <summary> The variable slots for this object.</summary>
26 private int index;
27  
28 /// <summary> Table of valid options.</summary>
29  
30 private string[] table;
31  
32 /// <summary> Construct a TclIndex representation with the given index & table.</summary>
33 private TclIndex( int i, string[] tab )
34 {
35 index = i;
36 table = tab;
37 }
38  
39 /// <summary> Returns a dupilcate of the current object.</summary>
40 /// <param name="obj">the TclObject that contains this internalRep.
41 /// </param>
42 public InternalRep duplicate()
43 {
44 return new TclIndex( index, table );
45 }
46  
47 /// <summary> Implement this no-op for the InternalRep interface.</summary>
48  
49 public void dispose()
50 {
51 }
52  
53 /// <summary> Called to query the string representation of the Tcl object. This
54 /// method is called only by TclObject.toString() when
55 /// TclObject.stringRep is null.
56 ///
57 /// </summary>
58 /// <returns> the string representation of the Tcl object.
59 /// </returns>
60 public override string ToString()
61 {
62 return table[index];
63 }
64  
65 /// <summary> Tcl_GetIndexFromObj -> get
66 ///
67 /// Gets the index into the table of the object. Generate an error
68 /// it it doesn't occur. This also converts the object to an index
69 /// which should catch the lookup for speed improvement.
70 ///
71 /// </summary>
72 /// <param name="interp">the interperter or null
73 /// </param>
74 /// <param name="tobj">the object to operate on.
75 /// @paran table the list of commands
76 /// @paran msg used as part of any error messages
77 /// @paran flags may be TCL.EXACT.
78 /// </param>
79  
80 public static int get( Interp interp, TclObject tobj, string[] table, string msg, int flags )
81 {
82 InternalRep rep = tobj.InternalRep;
83  
84 if ( rep is TclIndex )
85 {
86 if ( ( (TclIndex)rep ).table == table )
87 {
88 return ( (TclIndex)rep ).index;
89 }
90 }
91  
92 string str = tobj.ToString();
93 int strLen = str.Length;
94 int tableLen = table.Length;
95 int index = -1;
96 int numAbbrev = 0;
97  
98 {
99 if ( strLen > 0 )
100 {
101  
102 for ( int i = 0; i < tableLen; i++ )
103 {
104 string option = table[i];
105  
106 if ( ( ( flags & TCL.EXACT ) == TCL.EXACT ) && ( option.Length != strLen ) )
107 {
108 continue;
109 }
110 if ( option.Equals( str ) )
111 {
112 // Found an exact match already. Return it.
113  
114 index = i;
115 goto checking_brk;
116 }
117 if ( option.StartsWith( str ) )
118 {
119 numAbbrev++;
120 index = i;
121 }
122 }
123 }
124 if ( numAbbrev != 1 )
125 {
126 StringBuilder sbuf = new StringBuilder();
127 if ( numAbbrev > 1 )
128 {
129 sbuf.Append( "ambiguous " );
130 }
131 else
132 {
133 sbuf.Append( "bad " );
134 }
135 sbuf.Append( msg );
136 sbuf.Append( " \"" );
137 sbuf.Append( str );
138 sbuf.Append( "\"" );
139 sbuf.Append( ": must be " );
140 sbuf.Append( table[0] );
141 for ( int i = 1; i < tableLen; i++ )
142 {
143 if ( i == ( tableLen - 1 ) )
144 {
145 sbuf.Append( ( i > 1 ) ? ", or " : " or " );
146 }
147 else
148 {
149 sbuf.Append( ", " );
150 }
151 sbuf.Append( table[i] );
152 }
153 throw new TclException( interp, sbuf.ToString() );
154 }
155 }
156 checking_brk:
157 ;
158 // Create a new index object.
159 tobj.InternalRep = new TclIndex( index, table );
160 return index;
161 }
162 }
163 }