lcsim/src/org/lcsim/recon/cluster/directedtree
diff -N ConfigReader.java
--- /dev/null 1 Jan 1970 00:00:00 -0000
+++ ConfigReader.java 4 Apr 2006 20:36:08 -0000 1.1
@@ -0,0 +1,189 @@
+package org.lcsim.recon.cluster.directedtree;
+
+import java.util.Vector;
+import java.util.Map;
+import java.util.HashMap;
+import java.io.*;
+
+/**
+ * Utility class to read the DigiSim configuration parameters from an
+ * ASCII file.
+ *
+ * An important design issue is that the format of the configuration
+ * file is the same for both the C++ and the java versions of
+ * DigiSim. The implementation is easier if java can correctly
+ * interpret the Marlin steering file.
+ *
+ * @author Guilherme Lima
+ * @version $Id: ConfigReader.java,v 1.1 2006/04/04 20:36:08 rmcintos Exp $
+ */
+class ConfigReader {
+
+ /**
+ * Convenience constructor to be used from lcsim drivers.
+ * Configuration is provided by an InputStream object, which may
+ * be taken from a released .jar file through a call to
+ * getResourceAsStream (e.g. @see DigiSimDriver).
+ * @param input InputStream object
+ */
+ ConfigReader(InputStream input) {
+ reader = new BufferedReader(new InputStreamReader(input));
+ delimiter = " ";
+ }
+
+ /** Constructor for local config files.
+ * @param filename Configuration file name
+ */
+ ConfigReader(String filename) {
+ try {
+ FileReader input = new FileReader(new File(filename));
+ reader = new BufferedReader(input);
+ delimiter = " ";
+// System.out.println("Using local configuration file: "+filename);
+ }
+ catch(FileNotFoundException e) {
+ assert false : "File not found: "+ filename;
+ }
+ }
+
+ public void close() {
+ try {
+ reader.close();
+ }
+ catch(IOException e) {
+ System.err.println("IOException closing file");
+ }
+ }
+
+ /** Read next line from configuration file. Note that the line
+ * read is stored in a field, not as the object returned.
+ * @return true if a new line was read in, false otherwise (end of file).
+ */
+ public boolean getNextLine() {
+// static boolean endOfFile = false;
+ boolean endOfFile = false;
+ if( !endOfFile ) {
+ try {
+ line = reader.readLine();
+ }
+ catch(IOException e) {
+ System.err.println("IOException in getNextLine()");
+ System.err.println("line = "+line);
+ }
+
+ if(line==null) endOfFile = true;
+ }
+ return !endOfFile;
+ }
+
+ /**
+ * Validates and cleans up lines. Comment lines are not returned.
+ * @return true for valid line found, false when end of file is
+ * reached.
+ */
+ public boolean getNextValidLine() {
+ while( getNextLine() ) {
+ // discard comments and empties
+ if(line.startsWith("#")) continue;
+ if(line.equals("")) continue;
+ // good line found
+ break;
+ }
+
+ if(line==null) return false;
+
+ // a good line, clean it up
+ line = line.trim().replaceAll("\t"," ");
+ // collapse all spaces
+ int lastlen;
+ int newlen = line.length();
+ do {
+ lastlen = newlen;
+ line = line.replaceAll(" "," ");
+ newlen = line.length();
+ } while( newlen<lastlen );
+
+ // debug
+// System.out.println(line);
+ return true;
+ }
+
+ /**
+ * Parses the configuration file
+ * @return a map containing all the sections in the configuration
+ * file, keyed by section name.
+ */
+/*
+ public DigiSimSetup digiSimParse() {
+ Map<String,Vector<String>> sections
+ = new HashMap<String,Vector<String>>();
+ for(;;) {
+ String secName = getTokenWithPattern(1,".begin");
+ if( !secName.equals("") ) {
+ sections.put(secName, getNextSection());
+ }
+ else break;
+ }
+ return new DigiSimSetup(sections);
+ }
+*/
+ /**
+ * Return a complete section of the configuration file. It is
+ * called when the first line of a section is found (.begin).
+ * @return a vector containing all the valid lines of a
+ * section.
+ */
+ public Vector<String> getNextSection() {
+ Vector<String> section = new Vector<String>();
+ // add .begin line
+ section.add(line);
+ while( getNextValidLine() ) {
+ section.add(line);
+ if( line.contains(".end") ) break;
+ }
+ return section;
+ }
+
+ /**
+ * Search for next line with a pattern, then return the i-th token
+ * in that line. Note that when pattern is not found, file is
+ * left at EndOfFile.
+ * @param tkindex index of token to be return (starting from zero)
+ * @param pattern the pattern to be searched
+ * @return token requested, or "" if pattern not found
+ */
+ public String getTokenWithPattern(int tkindex, String pattern) {
+ for(;;) {
+ if( getLineWithPattern(pattern) ) {
+ String[] tokens = line.split("[ ]");
+ if(tkindex<tokens.length) return tokens[tkindex];
+ else return "";
+ }
+ else return "";
+ }
+ }
+
+ /**
+ * Skips file lines until a line with a pattern is found. Note
+ * that when pattern is not found, file is left at EOF.
+ * @param pattern the pattern to be searched
+ * @return true if pattern was found, false otherwise
+ */
+ public boolean getLineWithPattern(String pattern) {
+ while( getNextValidLine() ) {
+ // pattern search
+ String[] tokens = line.split("[ ]");
+ for(int i=0; i<tokens.length; ++i) {
+ if(tokens[i].equals(pattern)) return true;
+ }
+ // pattern not found, try next line
+ }
+ // EndOfFile reached...
+ return false;
+ }
+
+ // Member data
+ private BufferedReader reader;
+ private String line;
+ private String delimiter;
+}