/* * FileArgumentScheme.java * * Copyright (C) 2003 Miika Nurminen * * This library is free software; you can redistribute it and/or * modify it under the terms of the GNU Lesser General Public * License as published by the Free Software Foundation; either * version 2.1 of the License, or (at your option) any later version. * * This library is distributed in the hope that it will be useful, * but WITHOUT ANY WARRANTY; without even the implied warranty of * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU * Lesser General Public License for more details. * * You should have received a copy of the GNU Lesser General Public * License along with this library; if not, write to the Free Software * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * */ import java.io.*; /** * Searches arguments -i and -o for input/output filenames. If filename * is not found, standard i/o stream is used instead. * * To do: correct path handling with absolute names * * @author Miika Nurminen */ public class FileArgumentScheme { InputStream in = System.in; PrintStream out = System.out; String[] args; String[] helpStrings = new String[]{"-h","-?"}; public boolean helpWanted() { int i = StringLib.arrayIndexOf(args,helpStrings,true); return (i!=-1); } /** * If no dir is given, userdir is used. File separator is appended to end * of string. */ public String determineDir(String argname) { String ret = determineFilename(argname); if (ret==null) ret = System.getProperty("user.dir"); ret = StringLib.catOnce(ret,File.separator); return ret; } public String determineFilename(String argname) { int i = StringLib.arrayIndexOf(args,argname,true); if ((i==-1) || (i==args.length-1)) return null; return args[i+1]; } /** Creates a new instance of FileArgumentScheme */ public FileArgumentScheme(String[] args) { this.args=args; } public File getAbsoluteFile(String s) throws FileNotFoundException, IOException { File f = new File(s); // System.out.println(f.getAbsolutePath()); // System.out.println(f.getCanonicalPath()); // if (!f.isAbsolute()) f = new File(System.getProperty("user.dir")+java.io.File.separator+s); return f.getCanonicalFile(); } public void openStreams() throws FileNotFoundException, IOException { String s; s = determineFilename("-i"); if (s!=null) in = new FileInputStream(getAbsoluteFile(s)); s = determineFilename("-o"); if (s!=null) out = new PrintStream(new FileOutputStream(getAbsoluteFile(s))); } public InputStream getInput() { return in; } public PrintStream getOutput() { return out; } }