AsciiDatabase
Class RCFile

java.lang.Object
  |
  +--AsciiDatabase.RCFile

public class RCFile
extends Object

Class for reading configuration files in the following format:

    var1=value1
    var2=value2
    [header1]
    var3=value3
    var4=value4
    [header2]
    ... 

Author:
chris.studholme@utoronto.ca

Field Summary
private  File file
           
private  String lastproperty
           
private  String line
           
private  BufferedReader reader
           
private  boolean repeatLine
           
 
Constructor Summary
RCFile(File afile)
          Opens the configuration file.
 
Method Summary
 void close()
          Close the configuration file.
protected  void finalize()
          Close the configuration file.
 boolean FindHeader(String header)
          Scan through the file (from the beginning) for a particular header.
 String FindNextHeader()
          Reads lines from the file (starting at the current position) until a header is found.
 String LastProperty()
          Returns the property name (name before equal sign) of the last value read with ReadNextCharValue().
 String ReadCharValue(String field)
          Searches for a specific value (property=value pair) at the beginning of the file (before the first header is found).
 String ReadCharValue(String field, String header)
          Searches for a specific value (property=value pair) under a particular header.
private  String readline()
          Read a single line of the file.
 String ReadNextCharValue()
          Searches the file for the next value (property=value pair).
 String ReadNextCharValue(String field)
          Searches the file (starting at the current position) for a specific value (property=value pair).
 void ResetFile()
          Reset to the beginning of the file.
private  void unreadline()
          Push back the last line read so it can be read again.
 
Methods inherited from class java.lang.Object
, clone, equals, getClass, hashCode, notify, notifyAll, registerNatives, toString, wait, wait, wait
 

Field Detail

file

private File file

reader

private BufferedReader reader

line

private String line

repeatLine

private boolean repeatLine

lastproperty

private String lastproperty
Constructor Detail

RCFile

public RCFile(File afile)
       throws IOException
Opens the configuration file.
Parameters:
afile - configuration file
Throws:
IOException - if there is an error reading from the file
Method Detail

close

public void close()
Close the configuration file.

finalize

protected void finalize()
Close the configuration file. Just calls close().
Overrides:
finalize in class Object

ResetFile

public void ResetFile()
               throws IOException
Reset to the beginning of the file.
Throws:
IOException - if there is an error reading from the file

readline

private String readline()
                 throws IOException
Read a single line of the file. If the repeatLine flag is set, the already loaded line is returned.
Returns:
String object containing the line
Throws:
IOException - if there is an error reading from the file

unreadline

private void unreadline()
Push back the last line read so it can be read again.

FindNextHeader

public String FindNextHeader()
                      throws IOException
Reads lines from the file (starting at the current position) until a header is found. A header is denoted by '[header]'. There may be whitespace surrounding the name of the header, but not before the [.
Returns:
name of header found, or null if no header was found
Throws:
IOException - if there is an error reading from the file

FindHeader

public boolean FindHeader(String header)
                   throws IOException
Scan through the file (from the beginning) for a particular header.
Parameters:
header - the particular header required
Returns:
true if the required header was found, false otherwise
Throws:
IOException - if there is an error reading from the file

LastProperty

public String LastProperty()
Returns the property name (name before equal sign) of the last value read with ReadNextCharValue().
Returns:
String containing property name

ReadNextCharValue

public String ReadNextCharValue()
                         throws IOException
Searches the file for the next value (property=value pair). If found, the value is returned and the property name can be retrieved with LastProperty(). If a new header is found, null is returned and the file is left positioned on the new header.
Returns:
String value found, null otherwise
Throws:
IOException - if there is an error reading from the file

ReadNextCharValue

public String ReadNextCharValue(String field)
                         throws IOException
Searches the file (starting at the current position) for a specific value (property=value pair). The value is returned if found. null is returned if the property could not be found before the next header (or end of file) was found.
Parameters:
field - property name required
Returns:
String value found, null otherwise
Throws:
IOException - if there is an error reading from the file

ReadCharValue

public String ReadCharValue(String field,
                            String header)
                     throws IOException
Searches for a specific value (property=value pair) under a particular header. The file is searched from the beginning.
Parameters:
field - property required
header - section containing property
Returns:
String value if found, null otherwise
Throws:
IOException - if there is an error reading from the file

ReadCharValue

public String ReadCharValue(String field)
                     throws IOException
Searches for a specific value (property=value pair) at the beginning of the file (before the first header is found).
Parameters:
field - required property
Returns:
String value if found, null otherwise
Throws:
IOException - if there is an error reading from the file