01: /*
02: * SeekCmd.java --
03: *
04: * Copyright (c) 1997 Sun Microsystems, Inc.
05: *
06: * See the file "license.terms" for information on usage and
07: * redistribution of this file, and for a DISCLAIMER OF ALL
08: * WARRANTIES.
09: *
10: * RCS: @(#) $Id: SeekCmd.java,v 1.3 2003/03/08 03:42:44 mdejong Exp $
11: *
12: */
13:
14: package tcl.lang;
15:
16: import java.util.*;
17: import java.io.*;
18:
19: /**
20: * This class implements the built-in "seek" command in Tcl.
21: */
22:
23: class SeekCmd implements Command {
24:
25: static final private String validOrigins[] = { "start", "current",
26: "end" };
27:
28: static final int OPT_START = 0;
29: static final int OPT_CURRENT = 1;
30: static final int OPT_END = 2;
31:
32: /**
33: * This procedure is invoked to process the "seek" Tcl command.
34: * See the user documentation for details on what it does.
35: */
36:
37: public void cmdProc(Interp interp, TclObject argv[])
38: throws TclException {
39:
40: Channel chan; /* The channel being operated on this method */
41: int mode; /* Stores the search mode, either beg, cur or end
42: * of file. See the TclIO class for more info */
43:
44: if (argv.length != 3 && argv.length != 4) {
45: throw new TclNumArgsException(interp, 1, argv,
46: "channelId offset ?origin?");
47: }
48:
49: // default is the beginning of the file
50:
51: mode = TclIO.SEEK_SET;
52: if (argv.length == 4) {
53: int index = TclIndex.get(interp, argv[3], validOrigins,
54: "origin", 0);
55:
56: switch (index) {
57: case OPT_START: {
58: mode = TclIO.SEEK_SET;
59: break;
60: }
61: case OPT_CURRENT: {
62: mode = TclIO.SEEK_CUR;
63: break;
64: }
65: case OPT_END: {
66: mode = TclIO.SEEK_END;
67: break;
68: }
69: }
70: }
71:
72: chan = TclIO.getChannel(interp, argv[1].toString());
73: if (chan == null) {
74: throw new TclException(interp,
75: "can not find channel named \""
76: + argv[1].toString() + "\"");
77: }
78: long offset = TclInteger.get(interp, argv[2]);
79:
80: try {
81: chan.seek(interp, offset, mode);
82: } catch (IOException e) {
83: // FIXME: Need to figure out Tcl specific error conditions.
84: // Should we also wrap an IOException in a ReflectException?
85: throw new TclRuntimeError(
86: "SeekCmd.cmdProc() Error: IOException when seeking "
87: + chan.getChanName() + ":" + e.toString());
88: }
89: }
90: }
|