From 6d53055836c5a23a239ee84f6d4d6ce0d9075db5 Mon Sep 17 00:00:00 2001 From: Sparky6 Date: Sun, 2 Jul 2023 19:03:51 +0200 Subject: [PATCH] csharp-s6geige --- csharp/mutil/Configuration.cs | 73 +++++++++++++++++++++++++++++++++++ 1 file changed, 73 insertions(+) create mode 100644 csharp/mutil/Configuration.cs 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(""); + } + } +} + +} + -- 2.20.1