// --------------------------------------- // Restricted Focus Viewer (RFV) Program // Version 2.1 // Language: Java 2 // October, 2000 // Copyright (C) 2000 Anthony R. Jansen // --------------------------------------- // This program is free software; you can redistribute it and/or // modify it under the terms of the GNU General Public License // as published by the Free Software Foundation; either version 2 // of the License, or (at your option) any later version. // This program 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 General Public License for more details. // You should have received a copy of the GNU General Public License // along with this program; if not, write to the Free Software // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. // --------------------------------------- // RFV_Input_File.java // // The class is for handling data in the // input file that defines the RFV // settings and the trial information. // --------------------------------------- import java.io.*; import java.awt.*; public class RFV_Input_File { // I/O file handling variables // --------------------------- private RandomAccessFile input_file; private int line_number; private String current_line = ""; // Constant // -------- public static final String EOF = "end-of-file"; // Constructor which accepts the input filename as // its argument, and opens up the file if possible // ----------------------------------------------- public RFV_Input_File(String filename) { try { input_file = new RandomAccessFile(filename, "r"); line_number = 0; } catch(FileNotFoundException e) { RFV_Error.die(57, filename); } } // Returns the current line number // ------------------------------- public int getLineNumber() { return line_number; } // Returns the current file pointer offset, line number and current // file line for a component after its first label has been read in // ---------------------------------------------------------------- public RFV_File_Index getFileIndex(String label) { long offset = 0; try { offset = input_file.getFilePointer(); } catch(IOException e) { RFV_Error.die(58); } return new RFV_File_Index(label, offset, line_number, current_line); } // Sets the file pointer offset and line number, based on a component index // ------------------------------------------------------------------------ public void setFileIndex(RFV_File_Index index) { try { line_number = index.line_number; input_file.seek(index.file_offset); current_line = index.current_file_line; } catch(IOException e) { RFV_Error.die(59); } } // Method to read in a text line from the input file // ------------------------------------------------- private String readLine() { String str = " "; line_number++; try { str = input_file.readLine(); // If end-of-file, indicate in string // ---------------------------------- if (str == null) str = EOF; } catch(IOException e) { RFV_Error.die(60, line_number); } return str; } // Method to read in the next string token // ignoring comments and also commas and semicolons // ------------------------------------------------ public String readToken() { String token = " "; boolean comment = false; boolean comma = false; boolean semicolon = false; try { token = getNextToken(); if (token.equalsIgnoreCase(",")) comma = true; if (token.equalsIgnoreCase(";")) semicolon = true; else if (token.length() >= 2) if (token.charAt(0) == '/' && token.charAt(1) == '/') comment = true; while (comment || comma || semicolon) { if (comment) ignoreLine(); comment = false; comma = false; semicolon = false; token = getNextToken(); if (token.equalsIgnoreCase(",")) comma = true; if (token.equalsIgnoreCase(";")) semicolon = true; else if (token.length() >= 2) if (token.charAt(0) == '/' && token.charAt(1) == '/') comment = true; } } catch(IndexOutOfBoundsException e) { RFV_Error.die(61); } return token; } // Method to get the next string token // ----------------------------------- private String getNextToken() { String token = " "; int index; // If no more tokens on this line, get the next one // ------------------------------------------------ while (current_line.equals("")) current_line = readLine().trim(); try { // Determine if the current token // is a semicolon, a bracket or a comma // ------------------------------------ if (current_line.charAt(0) == ';') { current_line = current_line.substring(1).trim(); return ";"; } if (current_line.charAt(0) == '(') { current_line = current_line.substring(1).trim(); return "("; } if (current_line.charAt(0) == ')') { current_line = current_line.substring(1).trim(); return ")"; } if (current_line.charAt(0) == ',') { current_line = current_line.substring(1).trim(); return ","; } // Extract a single token from the current line // -------------------------------------------- index = -1; for (int i = 1; i < current_line.length(); i++) { if (current_line.charAt(i) == ' ' || current_line.charAt(i) == '\t' || current_line.charAt(i) == ';' || current_line.charAt(i) == ',' || current_line.charAt(i) == '(' || current_line.charAt(i) == ')') { index = i; break; } } // If no spaces, current line is a single token, otherwise // take token from the front and leave the rest of the line // -------------------------------------------------------- if (index == -1) { token = new String(current_line); current_line = ""; } else { token = current_line.substring(0, index); current_line = current_line.substring(index).trim(); } } catch(IndexOutOfBoundsException e) { RFV_Error.die(62, line_number); } return token; } // Returns a label minus the double quotes // --------------------------------------- public String readLabel() { String token = " "; int index; // If no more tokens on this line, get the next one // ------------------------------------------------ while (current_line.equals("")) current_line = readLine().trim(); try { // Must have opening quote // ----------------------- if (current_line.charAt(0) != '\"') RFV_Error.die(89, line_number); current_line = current_line.substring(1); index = current_line.indexOf('\"'); // Must have closing quote // ----------------------- if (index == -1) RFV_Error.die(90, line_number); token = current_line.substring(0, index); current_line = current_line.substring(index+1).trim(); } catch(IndexOutOfBoundsException e) { RFV_Error.die(62, line_number); } return token; } // Method returns true if a label in double // quotes is next, false otherwise // ---------------------------------------- public boolean isLabel() { boolean label = false; // If no more tokens on this line, get the next one // ------------------------------------------------ while (current_line.equals("")) current_line = readLine().trim(); try { if (current_line.charAt(0) == '\"') label = true; } catch(IndexOutOfBoundsException e) { RFV_Error.die(62, line_number); } return label; } // Method to ignore the rest of an input line // For example, if a comment is present // ------------------------------------------ public void ignoreLine() { current_line = ""; } // Method to read an integer from a line in the input file // ------------------------------------------------------- public int readInteger() { String str = " "; int value = 0; try { str = readToken(); value = Integer.parseInt(str); } catch(NumberFormatException e) { RFV_Error.die(63, line_number); } return value; } // Reads an open bracket or reports an error // ----------------------------------------- public void readOpenBracket() { if (!readToken().equalsIgnoreCase("(")) RFV_Error.die(64, line_number); } // Reads an close bracket or reports an error // ------------------------------------------ public void readCloseBracket() { if (!readToken().equalsIgnoreCase(")")) RFV_Error.die(65, line_number); } // Returns true if the next token is a close bracket, or // false otherwise. Useful for variable length arguments // ----------------------------------------------------- public boolean isCloseBracket() { String token; boolean close_bracket = false; // If no more tokens on this line, get the next one // ------------------------------------------------ while (current_line.equals("")) current_line = readLine().trim(); try { if (current_line.charAt(0) == ')') close_bracket = true; } catch(IndexOutOfBoundsException e) { RFV_Error.die(62, line_number); } return close_bracket; } // Method to match the number of open brackets with same number of close // brackets. Additional open brackets read in increase the number to match // ----------------------------------------------------------------------- public void matchOpenBrackets(int open) { String token; while (open > 0) { token = readToken(); if (token.equalsIgnoreCase(EOF)) RFV_Error.die(66); else if (token.equalsIgnoreCase("(")) open++; else if (token.equalsIgnoreCase(")")) open--; } } // Method to read in and return a color based on // red, green and blue integer values in the range 0-255 // ----------------------------------------------------- public Color readColor() { int red; int green; int blue; readOpenBracket(); red = readInteger(); if (red < 0 || red > 255) RFV_Error.die(67, line_number); green = readInteger(); if (green < 0 || green > 255) RFV_Error.die(67, line_number); blue = readInteger(); if (blue < 0 || blue > 255) RFV_Error.die(67, line_number); readCloseBracket(); return new Color(red, green, blue); } // Close file before class is destroyed // ------------------------------------ public void finalize() { try { if (input_file != null) input_file.close(); } catch(IOException e) { ; } } }