wasCSharpSQLite – Blame information for rev 3
?pathlinks?
Rev | Author | Line No. | Line |
---|---|---|---|
1 | office | 1 | /* |
2 | * LsortCmd.java |
||
3 | * |
||
4 | * The file implements the Tcl "lsort" command. |
||
5 | * |
||
6 | * Copyright (c) 1997 Sun Microsystems, Inc. |
||
7 | * |
||
8 | * See the file "license.terms" for information on usage and |
||
9 | * redistribution of this file, and for a DISCLAIMER OF ALL |
||
10 | * WARRANTIES. |
||
11 | * |
||
12 | * Included in SQLite3 port to C# for use in testharness only; 2008 Noah B Hart |
||
13 | * |
||
14 | * RCS @(#) $Id: LsortCmd.java,v 1.3 2003/01/09 02:15:39 mdejong Exp $ |
||
15 | */ |
||
16 | using System; |
||
17 | namespace tcl.lang |
||
18 | { |
||
19 | |||
20 | /* |
||
21 | * This LsortCmd class implements the Command interface for specifying a new |
||
22 | * Tcl command. The Lsort command implements the built-in Tcl command "lsort" |
||
23 | * which is used to sort Tcl lists. See user documentation for more details. |
||
24 | */ |
||
25 | |||
26 | class LsortCmd : Command |
||
27 | { |
||
28 | |||
29 | /* |
||
30 | * List of switches that are legal in the lsort command. |
||
31 | */ |
||
32 | |||
33 | private static readonly string[] validOpts = new string[] { "-ascii", "-command", "-decreasing", "-dictionary", "-increasing", "-index", "-integer", "-real", "-unique" }; |
||
34 | |||
35 | /* |
||
36 | *---------------------------------------------------------------------- |
||
37 | * |
||
38 | * cmdProc -- |
||
39 | * |
||
40 | * This procedure is invoked as part of the Command interface to |
||
41 | * process the "lsort" Tcl command. See the user documentation for |
||
42 | * details on what it does. |
||
43 | * |
||
44 | * Results: |
||
45 | * A standard Tcl result. |
||
46 | * |
||
47 | * Side effects: |
||
48 | * See the user documentation. |
||
49 | * |
||
50 | *---------------------------------------------------------------------- |
||
51 | */ |
||
52 | |||
53 | public TCL.CompletionCode cmdProc( Interp interp, TclObject[] argv ) |
||
54 | { |
||
55 | if ( argv.Length < 2 ) |
||
56 | { |
||
57 | throw new TclNumArgsException( interp, 1, argv, "?options? list" ); |
||
58 | } |
||
59 | |||
60 | string command = null; |
||
61 | int sortMode = QSort.ASCII; |
||
62 | int sortIndex = -1; |
||
63 | bool sortIncreasing = true; |
||
64 | bool unique = false; |
||
65 | |||
66 | for ( int i = 1; i < argv.Length - 1; i++ ) |
||
67 | { |
||
68 | int index = TclIndex.get( interp, argv[i], validOpts, "option", 0 ); |
||
69 | |||
70 | switch ( index ) |
||
71 | { |
||
72 | |||
73 | case 0: |
||
74 | sortMode = QSort.ASCII; |
||
75 | break; |
||
76 | |||
77 | |||
78 | case 1: |
||
79 | if ( i == argv.Length - 2 ) |
||
80 | { |
||
81 | throw new TclException( interp, "\"-command\" option must be" + " followed by comparison command" ); |
||
82 | } |
||
83 | sortMode = QSort.COMMAND; |
||
84 | |||
85 | command = argv[i + 1].ToString(); |
||
86 | i++; |
||
87 | break; |
||
88 | |||
89 | |||
90 | case 2: |
||
91 | sortIncreasing = false; |
||
92 | break; |
||
93 | |||
94 | |||
95 | case 3: |
||
96 | sortMode = QSort.DICTIONARY; |
||
97 | break; |
||
98 | |||
99 | |||
100 | case 4: |
||
101 | sortIncreasing = true; |
||
102 | break; |
||
103 | |||
104 | |||
105 | case 5: |
||
106 | if ( i == argv.Length - 2 ) |
||
107 | { |
||
108 | throw new TclException( interp, "\"-index\" option must be followed by list index" ); |
||
109 | } |
||
110 | sortIndex = Util.getIntForIndex( interp, argv[i + 1], -2 ); |
||
111 | |||
112 | command = argv[i + 1].ToString(); |
||
113 | i++; |
||
114 | break; |
||
115 | |||
116 | |||
117 | case 6: |
||
118 | sortMode = QSort.INTEGER; |
||
119 | break; |
||
120 | |||
121 | |||
122 | case 7: |
||
123 | sortMode = QSort.REAL; |
||
124 | break; |
||
125 | |||
126 | case 8: /* -unique */ |
||
127 | unique = true; |
||
128 | break; |
||
129 | } |
||
130 | } |
||
131 | |||
132 | TclObject list = argv[argv.Length - 1]; |
||
133 | bool isDuplicate = false; |
||
134 | |||
135 | // If the list object is unshared we can modify it directly. Otherwise |
||
136 | // we create a copy to modify: this is "copy on write". |
||
137 | |||
138 | if ( list.Shared ) |
||
139 | { |
||
140 | list = list.duplicate(); |
||
141 | isDuplicate = true; |
||
142 | } |
||
143 | |||
144 | try |
||
145 | { |
||
146 | TclList.sort( interp, list, sortMode, sortIndex, sortIncreasing, command, unique ); |
||
147 | interp.setResult( list ); |
||
148 | } |
||
149 | catch ( TclException e ) |
||
150 | { |
||
151 | if ( isDuplicate ) |
||
152 | { |
||
153 | list.release(); |
||
154 | } |
||
155 | throw; |
||
156 | } |
||
157 | return TCL.CompletionCode.RETURN; |
||
158 | } |
||
159 | } // LsortCmd |
||
160 | } |