001: /*
002: * LsortCmd.java
003: *
004: * The file implements the Tcl "lsort" command.
005: *
006: * Copyright (c) 1997 Sun Microsystems, Inc.
007: *
008: * See the file "license.terms" for information on usage and
009: * redistribution of this file, and for a DISCLAIMER OF ALL
010: * WARRANTIES.
011: *
012: * RCS: @(#) $Id: LsortCmd.java,v 1.3 2003/01/09 02:15:39 mdejong Exp $
013: */
014:
015: package tcl.lang;
016:
017: /*
018: * This LsortCmd class implements the Command interface for specifying a new
019: * Tcl command. The Lsort command implements the built-in Tcl command "lsort"
020: * which is used to sort Tcl lists. See user documentation for more details.
021: */
022:
023: class LsortCmd implements Command {
024:
025: /*
026: * List of switches that are legal in the lsort command.
027: */
028:
029: static final private String validOpts[] = { "-ascii", "-command",
030: "-decreasing", "-dictionary", "-increasing", "-index",
031: "-integer", "-real" };
032:
033: /*
034: *----------------------------------------------------------------------
035: *
036: * cmdProc --
037: *
038: * This procedure is invoked as part of the Command interface to
039: * process the "lsort" Tcl command. See the user documentation for
040: * details on what it does.
041: *
042: * Results:
043: * A standard Tcl result.
044: *
045: * Side effects:
046: * See the user documentation.
047: *
048: *----------------------------------------------------------------------
049: */
050:
051: public void cmdProc(Interp interp, /* Current interpreter. */
052: TclObject argv[]) /* Argument list. */
053: throws TclException /* A standard Tcl exception. */
054: {
055: if (argv.length < 2) {
056: throw new TclNumArgsException(interp, 1, argv,
057: "?options? list");
058: }
059:
060: String command = null;
061: int sortMode = QSort.ASCII;
062: int sortIndex = -1;
063: boolean sortIncreasing = true;
064:
065: for (int i = 1; i < argv.length - 1; i++) {
066: int index = TclIndex.get(interp, argv[i], validOpts,
067: "option", 0);
068:
069: switch (index) {
070: case 0: /* -ascii */
071: sortMode = QSort.ASCII;
072: break;
073:
074: case 1: /* -command */
075: if (i == argv.length - 2) {
076: throw new TclException(interp,
077: "\"-command\" option must be"
078: + " followed by comparison command");
079: }
080: sortMode = QSort.COMMAND;
081: command = argv[i + 1].toString();
082: i++;
083: break;
084:
085: case 2: /* -decreasing */
086: sortIncreasing = false;
087: break;
088:
089: case 3: /* -dictionary */
090: sortMode = QSort.DICTIONARY;
091: break;
092:
093: case 4: /* -increasing */
094: sortIncreasing = true;
095: break;
096:
097: case 5: /* -index */
098: if (i == argv.length - 2) {
099: throw new TclException(interp,
100: "\"-index\" option must be followed by list index");
101: }
102: sortIndex = Util
103: .getIntForIndex(interp, argv[i + 1], -2);
104: command = argv[i + 1].toString();
105: i++;
106: break;
107:
108: case 6: /* -integer */
109: sortMode = QSort.INTEGER;
110: break;
111:
112: case 7: /* -real */
113: sortMode = QSort.REAL;
114: break;
115: }
116: }
117:
118: TclObject list = argv[argv.length - 1];
119: boolean isDuplicate = false;
120:
121: // If the list object is unshared we can modify it directly. Otherwise
122: // we create a copy to modify: this is "copy on write".
123:
124: if (list.isShared()) {
125: list = list.duplicate();
126: isDuplicate = true;
127: }
128:
129: try {
130: TclList.sort(interp, list, sortMode, sortIndex,
131: sortIncreasing, command);
132: interp.setResult(list);
133: } catch (TclException e) {
134: if (isDuplicate) {
135: list.release();
136: }
137: throw e;
138: }
139: }
140:
141: } // LsortCmd
|