import com.PostHorizon.renderMan.*; import com.PostHorizon.renderMan.rib.*; import java.io.*; import java.util.zip.*; public class Translate { public static int ERROR_NONE = 0; public static int ERROR_USAGE = 1; public static int ERROR_IO = 2; public static int ERROR_RM = 2; public boolean printUsageOnly; public boolean gzipInput, gzipOutput, outputBinary, doublePrecision; public int indentLevel; public String inFilename, outFilename; public Translate() { printUsageOnly = false; gzipOutput = false; gzipInput = false; outputBinary = false; doublePrecision = false; indentLevel = 1; inFilename = null; outFilename = null; } public void usage() { System.err.println("Usage: (java) com.PostHorizon.renderMan.rib.Translate [options]"); System.err.println("Valid options:"); System.err.println(" -i : Specify input file ('-' indicates standard input)"); System.err.println(" Default: standard input"); System.err.println(" -I : Specify gzipped input file"); System.err.println(" -i and -I are mutually exclusive"); System.err.println(" -o : Specify output file ('-' indicates standard output)"); System.err.println(" Default: standard output"); System.err.println(" -O : Specify gzipped output file"); System.err.println(" -o and -O are mutually exclusive"); System.err.println(" -a : Produce ASCII RIB (default)"); System.err.println(" -b : Produce binary RIB"); System.err.println(" -d : Double precision (only meaningful for binary RIB)"); System.err.println(" -s : Indentation spacing (only meaningful for ASCII RIB)"); System.err.println(" : Set to 0 to disable auto-indentation"); System.err.println(" -h : Display this help message"); System.err.println(""); } public int parseArgs(String argv[]) { int i, j, tempInt; String tempString; char currOption; for (i = 0; i < argv.length; i++) { if (argv[i].charAt(0) != '-') { usage(); return(ERROR_USAGE); } for (j = 1; j < argv[i].length(); j++) { switch (currOption = argv[i].charAt(j)) { case 'I': case 'i': gzipInput = (currOption == 'I'); if (j < (argv[i].length()-1)) { inFilename = argv[i].substring(j+1); j = argv[i].length(); } else { i++; inFilename = argv[i]; j = argv[i].length(); } break; case 'O': case 'o': gzipOutput = (currOption == 'O'); if (j < (argv[i].length()-1)) { outFilename = argv[i].substring(j+1); j = argv[i].length(); } else { i++; outFilename = argv[i]; j = argv[i].length(); } break; case 'a': outputBinary = false; break; case 'b': outputBinary = true; break; case 'd': doublePrecision = true; break; case 's': if (j < (argv[i].length()-1)) { tempString = argv[i].substring(j+1); j = argv[i].length(); } else { i++; tempString = argv[i]; j = argv[i].length(); } try { tempInt = Integer.parseInt(tempString); } catch (NumberFormatException e) { System.err.println("Invalid spacing interval"); usage(); return(ERROR_USAGE); } if (tempInt < 0) { System.err.println("Invalid spacing interval"); usage(); return(ERROR_USAGE); } indentLevel = tempInt; break; case 'h': printUsageOnly = true; break; default: System.err.println("Unknown option (\'" + currOption + "\')"); usage(); return(ERROR_USAGE); } } } return(ERROR_NONE); } public int execute() { File tempFilespec; InputStream inFile; OutputStream outFile; RIBOut ribOut; RIBIn ribIn; boolean done; int status; if (printUsageOnly) { usage(); return(ERROR_NONE); } // ---- Input file ----- if ((inFilename == null) || (inFilename.equals("-"))) { inFile = System.in; } else { tempFilespec = new File(inFilename); if (!tempFilespec.exists()) { System.err.println("Input file does not exist (" + inFilename + ")"); return(ERROR_IO); } else if (!tempFilespec.isFile()) { System.err.println("Input file is not a regular file (" + inFilename + ")"); return(ERROR_IO); } else if (!tempFilespec.canRead()) { System.err.println("Cannot read input file (" + inFilename + ")"); return(ERROR_IO); } try { inFile = new FileInputStream(tempFilespec); } catch (FileNotFoundException e) { System.err.println("Input file not found (" + inFilename + ")"); return(ERROR_IO); } } if (gzipInput) { try { inFile = new GZIPInputStream(inFile); } catch (IOException e) { System.err.println("Unable to read GZipped input"); return(ERROR_IO); } } // ---- Output file ----- if ((outFilename == null) || (outFilename.equals("-"))) { outFile = System.out; } else { tempFilespec = new File(outFilename); if (tempFilespec.exists() && !tempFilespec.isFile()) { try { inFile.close(); } catch (IOException e) {} System.err.println("Output file is not a regular file (" + outFilename + ")"); return(ERROR_IO); } else if (tempFilespec.exists() && !tempFilespec.canWrite()) { try { inFile.close(); } catch (IOException e) {} System.err.println("Cannot write output file (" + outFilename + ")"); return(ERROR_IO); } try { outFile = new FileOutputStream(tempFilespec); } catch (IOException e) { try { inFile.close(); } catch (IOException e2) {} System.err.println("Cannot write to output file (" + outFilename + ")"); return(ERROR_IO); } } if (gzipOutput) { try { outFile = new GZIPOutputStream(outFile); } catch (IOException e) { try { inFile.close(); outFile.close(); } catch (IOException e2) {} System.err.println("Unable to write GZipped output"); return(ERROR_IO); } } // ----- RIB Setup ----- if (outputBinary) { ribOut = new RIBOutBinary(outFile, doublePrecision); } else { ribOut = new RIBOutASCII(outFile, (indentLevel > 0), indentLevel); } ribIn = new RIBIn(new PushbackInputStream(inFile), ribOut); // ----- Execute ----- status = ERROR_NONE; try { ribOut.begin(); ribOut.errorHandler(RenderMan.errorPrint); do { done = false; try { ribIn.parse(); done = true; } catch (RIBParseException e) { System.err.println(e.toString()); } } while (!done); ribOut.end(); } catch (RMException e) { status = ERROR_RM; } // ----- Done ----- try { inFile.close(); outFile.close(); } catch (IOException e) { System.err.println("IO Error while closing files"); return(ERROR_IO); } return(status); } static public void main(String argv[]) { Translate translate = new Translate(); int status; if ((status = translate.parseArgs(argv)) != ERROR_NONE) System.exit(status); if ((status = translate.execute()) != ERROR_NONE) System.exit(status); } }