From: Sparky6 Date: Sun, 2 Jul 2023 17:03:51 +0000 (+0200) Subject: csharp-s6geige X-Git-Tag: mdoku_0.3-0~4 X-Git-Url: http://wagnertech.de/git?a=commitdiff_plain;h=6d53055836c5a23a239ee84f6d4d6ce0d9075db5;p=mDoc.git csharp-s6geige --- diff --git a/csharp/mutil/Configuration.cs b/csharp/mutil/Configuration.cs new file mode 100644 index 0000000..0036751 --- /dev/null +++ b/csharp/mutil/Configuration.cs @@ -0,0 +1,73 @@ +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(""); + } + } +} + +} +