X-Git-Url: http://wagnertech.de/git?p=mcloud.git;a=blobdiff_plain;f=util%2FConfig.cs;fp=util%2FConfig.cs;h=d9d2a43d52bbfd956b88d7e0680afcb1a8846dea;hp=0000000000000000000000000000000000000000;hb=a82796d64877d73d6fd2ee86140ee788b16d402e;hpb=fd98c53230d650456bb9f0114a93d17280460809 diff --git a/util/Config.cs b/util/Config.cs new file mode 100644 index 0000000..d9d2a43 --- /dev/null +++ b/util/Config.cs @@ -0,0 +1,155 @@ +using System; +using System.Collections.Generic; +using System.IO; + + +namespace util +{ + internal class ConfigEntry + { + public string value; + public ISet list; + public ConfigEntry(string v) + { + this.value = v; + } + } + + public static class Config + { + private static Dictionary> Data; + private static bool ready = false; + private static string file_name; + + // return values for Load + public const int LOAD_OK = 0; + public const int LOAD_NO_INPUT_FILE = 1; + public static int Load (String FileName) + { + Config.file_name = FileName; + Data = new Dictionary>(); + + if (!System.IO.File.Exists (FileName)) { + ready = true; + return LOAD_NO_INPUT_FILE; + } + + using (StreamReader File = new StreamReader (FileName)) { + String Line; + string section = null; + string key = "no_key"; + Dictionary SectionDic = null; + bool listProcessing = false; + while (File.Peek() != -1) { + Line = File.ReadLine (); + if (listProcessing) { + if (Line == "LIST") + listProcessing = false; + else + SectionDic [key].list.Add (Line); + } else if (Line.Length == 0 || Line.Substring (0, 1) == "#") { + ; // this is a comment line + } else if (Line.Substring (0, 1) == "[") { + // new section + if (section != null) { + // append old section + Data [section] = SectionDic; + } + // start new section + section = Line.Substring (1, Line.IndexOf (']') - 1); + SectionDic = new Dictionary (); + } else { + key = Line.Substring (0, Line.IndexOf (':')); + String Value = Line.Substring (Line.IndexOf (':') + 1); + var ce = new ConfigEntry (Value); + if (Value == "LIST") { + listProcessing = true; + ce.list = new SortedSet (); + } + SectionDic [key] = ce; + } + } + if (listProcessing) + throw new ApplicationException ("Configuration list has no end"); + if (SectionDic != null) Data [section] = SectionDic; + } + ready = true; + return LOAD_OK; + } + /* + public static Config GetInstance() + { + if (Config.Instance == null) Config.Instance = new Config (); + return Config.Instance; + }*/ + public static String GetConfig(string section, string key){ + Check (); + if (Data.ContainsKey(section)){ + if (Data[section].ContainsKey(key)) return Data [section][key].value; + } + return null; + } + public static ISet GetList(string section, string key){ + Check (); + if (!Data.ContainsKey (section)) + return null; + if (!Data [section].ContainsKey (key)) + return null; + if (Data [section] [key].value != "LIST") + throw new ApplicationException ("Required List-Config is not a list"); + return Data [section] [key].list; + } + public static ISet RequireList(string section, string key){ + ISet result = GetList (section, key); + if (result == null) + throw new ApplicationException ("Config missing:"+ section+ key); + return result; + } + public static String RequireConfig(string section, string key){ + string value = GetConfig (section, key); + if (value == null) + throw new ApplicationException ("No Config Entry for " + section + "/"+key); + return value; + } + public static void Set(string section, string key, string value){ + Check (); + if (! Data.ContainsKey (section)) { + Data [section] = new Dictionary (); + } + Data [section] [key] = new ConfigEntry(value); + } + public static void Set(string section, string key, ISet value){ + Set (section, key, "LIST"); + Data [section] [key].list = value; + } + public static void Write(){ + Check (); + StreamWriter File = new StreamWriter (Config.file_name, false); + foreach (var section in Data) { + File.WriteLine (); + File.WriteLine ("[" + section.Key + "]"); + foreach (var elem in section.Value) { + File.WriteLine (elem.Key + ":" + elem.Value.value); + if (elem.Value.value == "LIST") { + foreach (string list_ele in elem.Value.list) { + File.WriteLine (list_ele); + } + File.WriteLine ("LIST"); + } + } + } + File.Close(); + } + public static void Remove(string section) + { + Check (); + Data.Remove (section); + } + private static void Check() + { + if (!ready) + throw new ApplicationException ("Configuration data not loaded"); + } + } +} +