// Copyright (c) 1996-2002 Brian D. Carlstrom

package bdc.scheme.procedure;

import bdc.scheme.Scheme;
import bdc.scheme.SchemeException;
import bdc.scheme.Stack;
import bdc.scheme.exception.ParseException;
import bdc.scheme.expression.Procedure1;
import bdc.util.FileUtil;
import bdc.util.IOUtil;
import bdc.util.SystemUtil;
import java.io.File;
import java.io.FileNotFoundException;
import java.io.IOException;

/**
    (open-output-file x)
*/
public class OpenOutputFile extends Procedure1
{
    public Object apply1 (Stack stack) throws SchemeException
    {
        Object o1 = stack.array[stack.inUse-1];
        File file = new File(FileUtil.fixFileSeparators(Scheme.string(o1, this)));
        try {
            return IOUtil.printWriter(file, SystemUtil.getFileEncoding());
        }
        catch (FileNotFoundException fnfe) {
            throw new ParseException(file.getPath(),
                                     0,
                                     fnfe.toString());
        }
        catch (IOException ioe) {
            throw new ParseException(file.getPath(),
                                     0,
                                     ioe.toString());
        }
    }
}
