/* Copyright (c) 2004 Joseph Gleason Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions: The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software. THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. Current versions of this and other code can be downloaded at: http://gleason.cc/ */ package cc.glsn; import java.io.File; import java.io.FileReader; import java.io.Serializable; import java.io.StreamTokenizer; import java.io.FileNotFoundException; import java.io.IOException; import java.util.HashMap; import java.util.Map; import java.util.ArrayList; import java.util.StringTokenizer; import java.math.BigInteger; import java.math.BigDecimal; //Takes a file and makes a hashtable of strings out of it //each line in the file being one hashtable entry //Then the entires can be looked up and converted //with accessor functions bellow // //Designed for reading options in from a configuration //file at runtime public class ConfigFile implements Serializable { private static final long serialVersionUID = 1L; private Map Params; private Map IntegerCache; private Map DoubleCache; private Map LongCache; private Map ShortCache; private Map BooleanCache; private Map BigDecimalCache; private Map BigIntegerCache; private Map ArrayCache; public static int InitalMapSize=131; public ConfigFile(String Filename) throws FileNotFoundException { this(new File(Filename)); } public ConfigFile(File InFile) throws FileNotFoundException { Params=new HashMap(InitalMapSize,0.5f); IntegerCache=new HashMap(InitalMapSize,0.5f); DoubleCache=new HashMap(InitalMapSize,0.5f); LongCache=new HashMap(InitalMapSize,0.5f); ShortCache=new HashMap(InitalMapSize,0.5f); BooleanCache=new HashMap(InitalMapSize,0.5f); BigDecimalCache=new HashMap(InitalMapSize,0.5f); BigIntegerCache=new HashMap(InitalMapSize,0.5f); ArrayCache=new HashMap(InitalMapSize,0.5f); FileReader FR; FR=new FileReader(InFile); StreamTokenizer ST=new StreamTokenizer(FR); ST.resetSyntax(); ST.eolIsSignificant(false); ST.wordChars(0,254); ST.ordinaryChar('#'); ST.commentChar('#'); ST.ordinaryChar(34); ST.quoteChar(34); //" ST.whitespaceChars('=','='); ST.whitespaceChars('\n','\n'); ST.whitespaceChars('\r','\r'); ST.whitespaceChars('\t','\t'); ST.whitespaceChars(' ',' '); //ST.parseNumbers(); try { int T=ST.nextToken(); while ((T != StreamTokenizer.TT_EOF) && (T!=-1)) { if (T != StreamTokenizer.TT_WORD) { System.out.println("syntax error in server config file in line " + ST.lineno()); } else { String P=new String(ST.sval); T=ST.nextToken(); if ((T==StreamTokenizer.TT_WORD) || (T==34)) { Params.put(P,new String(ST.sval)); //System.out.println("Adding: " + P + ":=:" + ST.sval); } else { System.out.print(InFile.getPath() + ": ConfigFile freaking out: " + T ); System.out.println(" Line: " + ST.lineno()); } } T=ST.nextToken(); } FR.close(); } catch (IOException e) { System.out.println("IO error in config file reading."); } } private synchronized void addCache(String Key, Object D, Map M) { synchronized(M) { M.put(Key,D); } } public boolean check(String Key) { synchronized(Params) { if (Params.containsKey(Key)) return true; return false; } } private Object get(String Key) { //String K=(String)Params.tailMap(Key).firstKey(); synchronized(Params) { if (!Params.containsKey(Key) ) { System.out.println("Warning: key not in ConfigFile: " + Key); } return Params.get(Key); } } private Object getFromMap(String Key, Map M) { synchronized(M) { return M.get(Key); } } public int getInt(String Key) { Integer I=(Integer)getFromMap(Key,IntegerCache); if (I==null) { I=new Integer((String)get(Key)); addCache(Key,I,IntegerCache); } return I.intValue(); } public long getLong(String Key) { Long L=(Long)getFromMap(Key,LongCache); if (L==null) { L=new Long((String)get(Key)); addCache(Key,L,LongCache); } return L.longValue(); } public short getShort(String Key) { Short S=(Short)getFromMap(Key,ShortCache); if (S==null) { S=new Short((String)get(Key)); addCache(Key,S,ShortCache); } return S.shortValue(); } public double getDouble(String Key) { Double D=(Double)getFromMap(Key,DoubleCache); if (D==null) { //using BigDouble for its better string parsing D=new Double(new BigDecimal((String)get(Key)).doubleValue()); addCache(Key,D,DoubleCache); } return D.doubleValue(); } public boolean getBoolean(String Key) { Boolean B=(Boolean)getFromMap(Key,BooleanCache); if (B==null) { B=new Boolean((String)get(Key)); addCache(Key,B,BooleanCache); } return B.booleanValue(); } public String getString(String Key) { String D=new String((String)get(Key)); return D; } public BigDecimal getBigDecimal(String Key) { BigDecimal B=(BigDecimal)getFromMap(Key,BigDecimalCache); if (B==null) { B=new BigDecimal((String)get(Key)); addCache(Key,B,BigDecimalCache); } return B; } public BigInteger getBigInteger(String Key) { BigInteger B=(BigInteger)getFromMap(Key,BigIntegerCache); if (B==null) { B=new BigInteger((String)get(Key)); addCache(Key,B,BigIntegerCache); } return B; } public ArrayList getList(String Key) { ArrayList A=(ArrayList)getFromMap(Key,ArrayCache); if (A==null) { String S=getString(Key); StringTokenizer ST=new StringTokenizer(S," "); int c=ST.countTokens(); A=new ArrayList(c); try { for (int i=0; i