using System; using System.Collections.Generic; using System.IO; namespace mutil { public class Configuration { private Dictionary configs = new Dictionary(); private string filename; private Configuration(string filename) { this.filename = filename; if (File.Exists(filename)) { XmlExtractor xmle = new XmlExtractor(); xmle.openInput(filename); xmle.requireChild("configuration"); string elem; string value; Dictionary attrs; int ctl = xmle.extractElement(XmlExtractor.EC_BEG, out elem, out value, out attrs); while (ctl != XmlExtractor.EC_END) { if (elem != "entry") throw new ApplicationException("entry tag expected."); configs.Add(attrs["key"], value); ctl = xmle.extractElement(ctl, out elem, out value, out attrs); } } } static private Configuration theInstance = null; static public Configuration getInstance(string filename) { if (theInstance == null) theInstance = new Configuration(filename); return theInstance; } static public Configuration getInstance() { if (theInstance == null) throw new System.Exception("parameter filename missing."); return theInstance; } public void Add(string key, string value) { string dummy; if (this.configs.TryGetValue(key, out dummy)) this.configs[key] = value; else this.configs.Add(key, value); } public string Require(string key) { return this.configs[key]; } public string Get(string key) { string value; this.configs.TryGetValue(key, out value); return value; } public string Get(string key, string defaut) { string value; if (! this.configs.TryGetValue(key, out value)) return defaut; return value; } ~Configuration() { StreamWriter outfile; using ( outfile = new StreamWriter (this.filename)) { outfile.WriteLine(""); foreach(var config in this.configs) { outfile.WriteLine("{1}", config.Key, config.Value); } outfile.WriteLine(""); } } } }