01: /*
02: * LinsertCmd.java
03: *
04: * Copyright (c) 1997 Cornell University.
05: * Copyright (c) 1997 Sun Microsystems, Inc.
06: *
07: * See the file "license.terms" for information on usage and
08: * redistribution of this file, and for a DISCLAIMER OF ALL
09: * WARRANTIES.
10: *
11: * RCS: @(#) $Id: LinsertCmd.java,v 1.3 2003/01/09 02:15:39 mdejong Exp $
12: *
13: */
14:
15: package tcl.lang;
16:
17: /**
18: * This class implements the built-in "linsert" command in Tcl.
19: */
20:
21: class LinsertCmd implements Command {
22: /**
23: * See Tcl user documentation for details.
24: * @exception TclException If incorrect number of arguments.
25: */
26:
27: public void cmdProc(Interp interp, TclObject argv[])
28: throws TclException {
29: if (argv.length < 4) {
30: throw new TclNumArgsException(interp, 1, argv,
31: "list index element ?element ...?");
32: }
33:
34: int size = TclList.getLength(interp, argv[1]);
35: int index = Util.getIntForIndex(interp, argv[2], size);
36: TclObject list = argv[1];
37: boolean isDuplicate = false;
38:
39: // If the list object is unshared we can modify it directly. Otherwise
40: // we create a copy to modify: this is "copy on write".
41:
42: if (list.isShared()) {
43: list = list.duplicate();
44: isDuplicate = true;
45: }
46:
47: try {
48: TclList.insert(interp, list, index, argv, 3,
49: argv.length - 1);
50: interp.setResult(list);
51: } catch (TclException e) {
52: if (isDuplicate) {
53: list.release();
54: }
55: throw e;
56: }
57: }
58: }
|