/* Research Project: Graphical Database for Category Theory J. Bradbury, Dr. R. Rosebrugh, I. Rutherford Mount Allison University 2001 File: IniFile.java (written by: Ian Rutherford) Description: */ import java.io.FileOutputStream; import java.io.RandomAccessFile; import java.io.IOException; import java.awt.*; public class IniFile { protected int numnodes = 0, maxnodes = 50, cursor = 0; protected IniFileNode[] nodes = new IniFileNode[maxnodes]; public IniFile() { int i; for (i=0; i= maxnodes) { IniFileNode[] temp = new IniFileNode[maxnodes+50]; for (i=0; i= 'a' && name.charAt(i) <= 'z') || (name.charAt(i) >= 'A' && name.charAt(i) <= 'Z') || (name.charAt(i) >= '0' && name.charAt(i) <= '9') || name.charAt(i) == ' ')) i = name.length() + 1; } if (i == name.length() + 1) return false; // now add the header to the list if (numnodes == 0) { nodes[numnodes].title = name; } else { // make sure it doesn't already exist for (i=0; i= numnodes) return false; // remove the item if (pos == numnodes-1) { numnodes--; nodes[numnodes].erase(); } else { numnodes--; nodes[pos] = nodes[numnodes]; nodes[numnodes].erase(); } return true; } public void eraseAll() { int i; // erase all the headers and reset the cursor for (i=0; i= numnodes) return false; // set the cursor appripriately cursor = pos; return true; } public String getHeader() { return(nodes[cursor].title); } public boolean addItem(String name, String value) { // check for invalid input if (numnodes == 0) return false; // return the value given by the addItem() function in the IniFileNode class return (nodes[cursor].addItem(name, value)); } public boolean changeItem(String name, String value) { // same as above if (numnodes == 0) return false; //If node can not be changed add the item if (!nodes[cursor].changeItem(name, value)) return(addItem(name, value)); return(false); } public boolean changeItem(int pos, String data) { // same as above if (numnodes == 0) return false; //If node can not be changed add the item return(nodes[cursor].changeItem(pos, data)); } public String getItem(int pos) { // same as above if (numnodes == 0) return new String(); return (nodes[cursor].getItem(pos)); } public String getItem(String value) { // same as above if (numnodes == 0) return new String(); return (nodes[cursor].getItem(value)); } public boolean removeItem(String value) { // same as above if (numnodes == 0) return false; return (nodes[cursor].removeItem(value)); } public boolean removeItem(int pos) { // same as above if (numnodes == 0) return false; return (nodes[cursor].removeItem(pos)); } public boolean writeIni(String filename) { int i, j; String data = new String(); RandomAccessFile file; // delete the old file by opening it and writing a single byte to it try { FileOutputStream deletefile = new FileOutputStream(filename); deletefile.write(0); deletefile.close(); } catch (IOException e) { // if there was an error opening or writing to the file, return false return false; } // try to open the file try { file = new RandomAccessFile(filename, "rw"); } catch (IOException e) { // if we can't open the file, return false return false; } // make a large string containing all the .ini file data in printable format for (i=0; i= rhs.length()) rhs = rhs.substring(0, i); else rhs = rhs.substring(0, i) + rhs.substring(i+1); } } // now we have a valid item, so add it to the list String lhs = temp.substring(0,m+1); addItem(lhs, rhs); } } } public String getData() { // identical to the writeIni() function, but returns a string containing the // formatted .ini file data int i, j; String data = new String(); for (i=0; i