001: /*
002: * open.java
003: *
004: * Copyright (C) 2003 Peter Graves
005: * $Id: open.java,v 1.6 2003/11/15 11:03:35 beedlem Exp $
006: *
007: * This program is free software; you can redistribute it and/or
008: * modify it under the terms of the GNU General Public License
009: * as published by the Free Software Foundation; either version 2
010: * of the License, or (at your option) any later version.
011: *
012: * This program is distributed in the hope that it will be useful,
013: * but WITHOUT ANY WARRANTY; without even the implied warranty of
014: * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
015: * GNU General Public License for more details.
016: *
017: * You should have received a copy of the GNU General Public License
018: * along with this program; if not, write to the Free Software
019: * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
020: */
021:
022: package org.armedbear.lisp;
023:
024: import java.io.File;
025: import java.io.FileInputStream;
026: import java.io.FileNotFoundException;
027: import java.io.FileOutputStream;
028:
029: public final class open extends Lisp {
030: // ### %open-output-file
031: private static final Primitive3 _OPEN_OUTPUT_FILE = new Primitive3(
032: "%open-output-file", PACKAGE_SYS, false) {
033: public LispObject execute(LispObject first, LispObject second,
034: LispObject third) throws ConditionThrowable {
035: File file = Utilities.getFile(first);
036: boolean binary = checkBinaryElementType(second);
037: LispObject ifExists = third;
038: if (ifExists == Keyword.SUPERSEDE) {
039: ;
040: } else if (ifExists == Keyword.ERROR) {
041: if (file.exists())
042: throw new ConditionThrowable(new LispError(
043: "file already exists: " + first));
044: } else if (ifExists == NIL) {
045: if (file.exists())
046: return NIL;
047: } else {
048: // FIXME
049: throw new ConditionThrowable(new LispError(String
050: .valueOf(ifExists))
051: + " is not a recognized value for :IF-EXISTS");
052: }
053: try {
054: if (binary)
055: return new BinaryOutputStream(new FileOutputStream(
056: file));
057: else
058: return new CharacterOutputStream(
059: new FileOutputStream(file));
060: } catch (FileNotFoundException e) {
061: throw new ConditionThrowable(new LispError(
062: "unable to create file: " + first));
063: }
064: }
065: };
066:
067: // ### %open-input-file
068: private static final Primitive2 _OPEN_INPUT_FILE = new Primitive2(
069: "%open-input-file", PACKAGE_SYS, false) {
070: public LispObject execute(LispObject first, LispObject second)
071: throws ConditionThrowable {
072: File file = Utilities.getFile(first);
073: boolean binary = checkBinaryElementType(second);
074: try {
075: if (binary)
076: return new BinaryInputStream(new FileInputStream(
077: file));
078: else
079: return new CharacterInputStream(
080: new FileInputStream(file));
081: } catch (FileNotFoundException e) {
082: throw new ConditionThrowable(new LispError(
083: "file not found: " + first));
084: }
085: }
086: };
087:
088: private static final boolean checkBinaryElementType(
089: LispObject elementType) throws ConditionThrowable {
090: if (elementType == Symbol.BASE_CHAR
091: || elementType == Symbol.CHARACTER)
092: return false;
093: if (elementType == Symbol.UNSIGNED_BYTE)
094: return true;
095: if (elementType instanceof Cons) {
096: if (elementType.car() == Symbol.UNSIGNED_BYTE) {
097: if (elementType.length() == 2) {
098: if (elementType.cadr() instanceof Fixnum) {
099: if (((Fixnum) elementType.cadr()).getValue() == 8)
100: return true;
101: }
102: }
103: }
104: }
105: throw new ConditionThrowable(new LispError(String
106: .valueOf(elementType)
107: + " is not a valid stream element type"));
108: }
109: }
|