2 using System.Collections.Generic;
7 public class Configuration {
8 private Dictionary<string,string> configs = new Dictionary<string,string>();
9 private string filename;
10 private Configuration(string filename) {
11 this.filename = filename;
12 if (File.Exists(filename)) {
13 XmlExtractor xmle = new XmlExtractor();
14 xmle.openInput(filename);
15 xmle.requireChild("configuration");
18 Dictionary<string,string> attrs;
20 int ctl = xmle.extractElement(XmlExtractor.EC_BEG, out elem, out value, out attrs);
21 while (ctl != XmlExtractor.EC_END) {
22 if (elem != "entry") throw new ApplicationException("entry tag expected.");
23 configs.Add(attrs["key"], value);
24 ctl = xmle.extractElement(ctl, out elem, out value, out attrs);
28 static private Configuration theInstance = null;
29 static public Configuration getInstance(string filename) {
30 if (theInstance == null) theInstance = new Configuration(filename);
33 static public Configuration getInstance() {
34 if (theInstance == null) throw new System.Exception("parameter filename missing.");
38 public void Add(string key, string value) {
40 if (this.configs.TryGetValue(key, out dummy)) this.configs[key] = value;
41 else this.configs.Add(key, value);
44 public string Require(string key) {
45 return this.configs[key];
48 public string Get(string key) {
50 this.configs.TryGetValue(key, out value);
54 public string Get(string key, string defaut) {
56 if (! this.configs.TryGetValue(key, out value)) return defaut;
62 using ( outfile = new StreamWriter (this.filename)) {
63 outfile.WriteLine("<configuration>");
64 foreach(var config in this.configs) {
65 outfile.WriteLine("<entry key=\"{0}\">{1}</entry>", config.Key, config.Value);
67 outfile.WriteLine("</configuration>");