001: /* Copyright 2006 David N. Welton
002:
003: Licensed under the Apache License, Version 2.0 (the "License");
004: you may not use this file except in compliance with the License.
005: You may obtain a copy of the License at
006:
007: http://www.apache.org/licenses/LICENSE-2.0
008:
009: Unless required by applicable law or agreed to in writing, software
010: distributed under the License is distributed on an "AS IS" BASIS,
011: WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
012: See the License for the specific language governing permissions and
013: limitations under the License.
014: */
015:
016: package org.hecl.files;
017:
018: import java.io.File;
019: import java.util.Hashtable;
020:
021: import org.hecl.HeclException;
022: import org.hecl.Interp;
023: import org.hecl.IntThing;
024: import org.hecl.ListThing;
025: import org.hecl.LongThing;
026: import org.hecl.Operator;
027: import org.hecl.RealThing;
028: import org.hecl.StringThing;
029: import org.hecl.Thing;
030:
031: public class FileCmds extends Operator {
032: public static final int CD = 1;
033: public static final int CURRENTFILE = 2;
034: public static final int READALL = 3;
035: public static final int WRITE = 4;
036: public static final int SOURCE = 5;
037: public static final int FILESIZE = 6;
038: public static final int FILETOLIST = 7;
039: public static final int LISTTOFILE = 8;
040:
041: private FileCmds(int cmdcode, int minargs, int maxargs) {
042: super (cmdcode, minargs, maxargs);
043: }
044:
045: public Thing operate(int cmd, Interp interp, Thing[] argv)
046: throws HeclException {
047: switch (cmd) {
048: case CD:
049: HeclFile.changeDir(argv[1].toString());
050: return null;
051:
052: case CURRENTFILE:
053: return new Thing(HeclFile.currentFile);
054:
055: case FILESIZE:
056: File fl = new File(argv[1].toString()).getAbsoluteFile();
057: return LongThing.create(fl.length());
058:
059: case FILETOLIST:
060: return ListThing.create(HeclFile.fileToList(argv[1]
061: .toString()));
062:
063: case LISTTOFILE:
064: return new Thing(HeclFile
065: .listToFile(ListThing.get(argv[1])));
066:
067: case READALL:
068: return new Thing(HeclFile.readFile(argv[1].toString()));
069:
070: case WRITE:
071: String fn = argv[1].toString();
072: String data = argv[2].toString();
073: HeclFile.writeFile(fn, data);
074: return IntThing.create(data.length());
075:
076: case SOURCE:
077: HeclFile.sourceFile(interp, argv[1].toString());
078: return null;
079:
080: default:
081: throw new HeclException("Unknown file command '"
082: + argv[0].toString() + "' with code '" + cmd + "'.");
083: }
084: }
085:
086: public static void load(Interp ip) throws HeclException {
087: Operator.load(ip, cmdtable);
088: }
089:
090: public static void unload(Interp ip) throws HeclException {
091: Operator.unload(ip, cmdtable);
092: }
093:
094: private static Hashtable cmdtable = new Hashtable();
095:
096: static {
097: cmdtable.put("cd", new FileCmds(CD, 1, 1));
098: cmdtable.put("currentfile", new FileCmds(CURRENTFILE, 0, 0));
099: cmdtable.put("filesize", new FileCmds(FILESIZE, 1, 1));
100: cmdtable.put("filetolist", new FileCmds(FILETOLIST, 1, 1));
101: cmdtable.put("listtofile", new FileCmds(LISTTOFILE, 1, 1));
102: cmdtable.put("readall", new FileCmds(READALL, 1, 1));
103: cmdtable.put("write", new FileCmds(WRITE, 2, 2));
104: cmdtable.put("source", new FileCmds(SOURCE, 1, 1));
105: }
106: }
|