Class IniFile

Class IniFile

java.lang.Object
   |
   +----IniFile

public class IniFile
extends Object
A class for handling Windows-style INI files. The file format is as follows:
[subject] - anything beginning with [ and ending with ] is a subject
;comment - anything beginning with a ; is a comment
variable=value - anything of the format string=string is an assignment
comment - anything that doesn't match any of the above is a comment
Author:
Steve DeGroof, degroof@mindspring.com, http://www.mindspring.com/~degroof

Variable Index

 o fileName
Name of the file
 o lines
Actual text lines of the file stored in a vector.
 o saveOnChange
If true, INI file will be saved every time a value is changed.
 o subjects
A vector of all subjects
 o values
A vector of variable value vectors grouped by subject
 o variables
A vector of variable name vectors grouped by subject

Constructor Index

 o IniFile(String)
Creates an INI file object using the specified name If the named file doesn't exist, create one
 o IniFile(String, boolean)
Creates an INI file object using the specified name If the named file doesn't exist, create one

Method Index

 o addAssignment(String, String)
Adds and assignment (i.e.
 o addSubjectLine(String)
add a subject line to the end of the lines vector
 o addValue(String, String, String, boolean)
Sets a specific subject/variable combination the given value.
 o createFile()
Create a new INI file.
 o deleteSubject(String)
delete a subject and all its variables
 o deleteValue(String, String)
delete variable within a subject
 o endOfSubject(int)
find the line number which is 1 past the last assignment in a subject starting at a given line
 o finalize()
clean up
 o findAssignmentBetween(String, int, int)
find the line containing a variable within a range of lines
 o findAssignmentLine(String, String)
find the line containing a variable within a subject
 o findSubjectLine(String)
find a subject line within the lines vector
 o getLines()
get a copy of the lines vector
 o getSubjects()
get an array containing all subjects
 o getValue(String, String)
get the value of a variable within a subject
 o getVariables(String)
get a vector containing all variables in a subject
 o isanAssignment(String)
does the line represent an assignment?
 o isaSubject(String)
does the line represent a subject?
 o loadFile()
Loads and parses the INI file.
 o main(String[])
 o parseLines()
Reads lines, filling in subjects, variables and values.
 o saveFile()
save the lines vector back to the INI file
 o setLine(String, String, String)
set a line in the lines vector
 o setValue(String, String, String)
Sets a specific subject/variable combination the given value.

Variables

 o lines
  protected Vector lines
Actual text lines of the file stored in a vector.
 o subjects
  protected Vector subjects
A vector of all subjects
 o variables
  protected Vector variables
A vector of variable name vectors grouped by subject
 o values
  protected Vector values
A vector of variable value vectors grouped by subject
 o fileName
  protected String fileName
Name of the file
 o saveOnChange
  protected boolean saveOnChange
If true, INI file will be saved every time a value is changed. Defaults to false

Constructors

 o IniFile
  public IniFile(String name)
Creates an INI file object using the specified name If the named file doesn't exist, create one
Parameters:
name - the name of the file
 o IniFile
  public IniFile(String name,
                 boolean save)
Creates an INI file object using the specified name If the named file doesn't exist, create one
Parameters:
name - the name of the file
saveOnSet - save file whenever a value is set

Methods

 o loadFile
  public void loadFile()
Loads and parses the INI file. Can be used to reload from file.
 o createFile
  protected boolean createFile()
Create a new INI file.
 o parseLines
  protected void parseLines()
Reads lines, filling in subjects, variables and values.
 o addAssignment
  protected boolean addAssignment(String subject,
                                  String assignment)
Adds and assignment (i.e. "variable=value") to a subject.
 o setValue
  public boolean setValue(String subject,
                          String variable,
                          String value)
Sets a specific subject/variable combination the given value. If the subject doesn't exist, create it. If the variable doesn't exist, create it. If saveOnChange is true, save the file;
Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
value - the value of the variable (e.g. "green")
Returns:
true if successful
 o addValue
  protected boolean addValue(String subject,
                             String variable,
                             String value,
                             boolean addToLines)
Sets a specific subject/variable combination the given value. If the subject doesn't exist, create it. If the variable doesn't exist, create it.
Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
value - the value of the variable (e.g. "green")
addToLines - add the information to the lines vector
Returns:
true if successful
 o isaSubject
  protected boolean isaSubject(String line)
does the line represent a subject?
Parameters:
line - a string representing a line from an INI file
Returns:
true if line is a subject
 o setLine
  protected void setLine(String subject,
                         String variable,
                         String value)
set a line in the lines vector
Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
value - the value of the variable (e.g. "green")
 o findAssignmentLine
  protected int findAssignmentLine(String subject,
                                   String variable)
find the line containing a variable within a subject
Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
Returns:
the line number of the assignment, -1 if not found
 o findAssignmentBetween
  protected int findAssignmentBetween(String variable,
                                      int start,
                                      int end)
find the line containing a variable within a range of lines
Parameters:
variable - the variable name (e.g. "Color")
start - the start of the range (inclusive)
end - the end of the range (exclusive)
Returns:
the line number of the assignment, -1 if not found
 o addSubjectLine
  protected void addSubjectLine(String subject)
add a subject line to the end of the lines vector
Parameters:
subject - the subject heading (e.g. "Widget Settings")
 o findSubjectLine
  protected int findSubjectLine(String subject)
find a subject line within the lines vector
Parameters:
subject - the subject heading (e.g. "Widget Settings")
Returns:
the line number of the subject, -1 if not found
 o endOfSubject
  protected int endOfSubject(int start)
find the line number which is 1 past the last assignment in a subject starting at a given line
Parameters:
start - the line number at which to start looking
Returns:
the line number of the last assignment + 1
 o isanAssignment
  protected boolean isanAssignment(String line)
does the line represent an assignment?
Parameters:
line - a string representing a line from an INI file
Returns:
true if line is an assignment
 o getLines
  public Vector getLines()
get a copy of the lines vector
 o getVariables
  public String[] getVariables(String subject)
get a vector containing all variables in a subject
Parameters:
subject - the subject heading (e.g. "Widget Settings")
Returns:
a list of variables, empty vector if subject not found
 o getSubjects
  public String[] getSubjects()
get an array containing all subjects
Returns:
a list of subjects
 o getValue
  public String getValue(String subject,
                         String variable)
get the value of a variable within a subject
Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
Returns:
the value of the variable (e.g. "green"), empty string if not found
 o deleteValue
  public void deleteValue(String subject,
                          String variable)
delete variable within a subject
Parameters:
subject - the subject heading (e.g. "Widget Settings")
variable - the variable name (e.g. "Color")
 o deleteSubject
  public void deleteSubject(String subject)
delete a subject and all its variables
Parameters:
subject - the subject heading (e.g. "Widget Settings")
 o saveFile
  public void saveFile()
save the lines vector back to the INI file
 o finalize
  protected void finalize()
clean up
Overrides:
finalize in class Object
 o main
  public static void main(String args[])