001: package murlen.util.fscript;
002:
003: import java.io.*;
004: import java.util.*;
005:
006: /**
007: * <p>BasicIO - an simple IO Subclass of FScript</p>
008: * <p>
009: * <I>Copyright (C) 2000 murlen.</I></p>
010: * <p>
011: * This library is free software; you can redistribute it and/or
012: * modify it under the terms of the GNU Library General Public
013: * License as published by the Free Software Foundation; either
014: * version 2 of the License, or (at your option) any later version.</p>
015: * <p>
016: * This library is distributed in the hope that it will be useful,
017: * but WITHOUT ANY WARRANTY; without even the implied warranty of
018: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
019: * Library General Public License for more details.</p>
020: *
021: * <p>You should have received a copy of the GNU Library General Public
022: * License along with this library; if not, write to the Free
023: * Software Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA </p>
024: * @author murlen\
025: *CVSTEST
026:
027: */
028: public class BasicIO extends FScript {
029:
030: private Object files[];
031:
032: /**Constructor*/
033: public BasicIO() {
034: super ();
035: files = new Object[25];
036: }
037:
038: /**
039: *<p> Overridden from FScript implements the following FScript functions </p>
040: *
041: * <p> note that this only provides very basic IO facilities,
042: * line by line read/write
043: * to files, and stdio read write. There is a maximum of 25 open files</p>
044: * <p> <b>(void) println(param...)</b> - write to stdout -
045: * takes variable parameter list </p>
046: * <p> <b>string readln() </b> - reads a string from stdin </p>
047: * <p> <b>int open(string filename,string mode) </b> -
048: * opens a file 'filename' for
049: * reading (mode="r") or writing (mode="w") returns an integer which is
050: * used in future calls. Returns -1 on >25 files opened </p>
051: * <p> <b>string read(fp) </b> - reads one line from previously openened file
052: * </p>
053: * <p> <b>void write(fp,param...) - writes concatination of all params to one
054: * line of file </p>
055: */
056: public Object callFunction(String name, ArrayList param)
057: throws FSException {
058:
059: //(void) println(param.....)
060: if (name.equals("println")) {
061: int n;
062: String s = "";
063: for (n = 0; n < param.size(); n++) {
064: s = s + param.get(n);
065: }
066: System.out.println(s);
067: }
068: //string readln()
069: else if (name.equals("readln")) {
070: try {
071: return new BufferedReader(new InputStreamReader(
072: System.in)).readLine();
073:
074: } catch (IOException e) {
075: throw new FSException(e.getMessage());
076: }
077: }
078: //int open(string file,string mode)
079: else if (name.equals("open")) {
080: int n;
081:
082: try {
083: for (n = 0; n < 25; n++) {
084: if (files[n] == null) {
085: if (((String) param.get(1)).equals("r")) {
086: files[n] = new BufferedReader(
087: new FileReader((String) param
088: .get(0)));
089: break;
090: } else if (((String) param.get(1)).equals("w")) {
091: files[n] = new BufferedWriter(
092: new FileWriter((String) param
093: .get(0)));
094: break;
095: } else {
096: throw new FSException(
097: "open expects 'r' or 'w' for modes");
098: }
099: }
100: }
101: } catch (IOException e) {
102: throw new FSException(e.getMessage());
103: }
104: if (n < 25)
105: return new Integer(n);
106: else
107: return new Integer(-1);
108: }
109: //(void)close(int fp)
110: else if (name.equals("close")) {
111: int n;
112: n = ((Integer) param.get(0)).intValue();
113: if (files[n] == null) {
114: throw new FSException(
115: "Invalid file number passed to close");
116: }
117: try {
118: if (files[n] instanceof BufferedWriter) {
119: ((BufferedWriter) files[n]).close();
120: } else {
121: ((BufferedReader) files[n]).close();
122: }
123: files[n] = null;
124: } catch (IOException e) {
125: throw new FSException(e.getMessage());
126: }
127: }
128: //(void) write(params....)
129: else if (name.equals("write")) {
130: int n;
131: String s = "";
132: for (n = 1; n < param.size(); n++) {
133: s = s + param.get(n);
134: }
135: n = ((Integer) param.get(0)).intValue();
136: if (files[n] == null) {
137: throw new FSException(
138: "Invalid file number passed to write");
139: }
140: if (!(files[n] instanceof BufferedWriter)) {
141: throw new FSException("Invalid file mode for write");
142: }
143: try {
144: ((BufferedWriter) files[n]).write(s, 0, s.length());
145: ((BufferedWriter) files[n]).newLine();
146: } catch (IOException e) {
147: throw new FSException(e.getMessage());
148: }
149: }
150: //string read(int fp)
151: else if (name.equals("read")) {
152: int n;
153: String s;
154: n = ((Integer) param.get(0)).intValue();
155: if (files[n] == null) {
156: throw new FSException(
157: "Invalid file number passed to read");
158: }
159: if (!(files[n] instanceof BufferedReader)) {
160: throw new FSException("Invalid file mode for read");
161: }
162: try {
163: s = ((BufferedReader) files[n]).readLine();
164: //dodge eof problems
165: if (s == null)
166: s = "";
167: return s;
168: } catch (IOException e) {
169: throw new FSException(e.getMessage());
170: }
171: }
172: //int eof(fp)
173: else if (name.equals("eof")) {
174: int n;
175: n = ((Integer) param.get(0)).intValue();
176: if (files[n] == null) {
177: throw new FSException(
178: "Invalid file number passed to eof");
179: }
180: BufferedReader br = (BufferedReader) files[n];
181: try {
182: br.mark(1024);
183: if (br.readLine() == null) {
184: return new Integer(1);
185: } else {
186: br.reset();
187: return new Integer(0);
188: }
189: } catch (IOException e) {
190: throw new FSException(e.getMessage());
191: }
192: } else {
193: super .callFunction(name, param);
194: }
195: return new Integer(0);
196: }
197: }
|