2 using System.Collections.Generic;
8 internal class ConfigEntry
11 public ISet<string> list;
12 public ConfigEntry(string v)
18 public static class Config
20 private static Dictionary<String,Dictionary<string,ConfigEntry>> Data;
21 private static bool ready = false;
22 private static string file_name;
24 // return values for Load
25 public const int LOAD_OK = 0;
26 public const int LOAD_NO_INPUT_FILE = 1;
27 public static int Load (String FileName)
29 Config.file_name = FileName;
30 Data = new Dictionary<string, Dictionary<string,ConfigEntry>>();
32 if (!System.IO.File.Exists (FileName)) {
34 return LOAD_NO_INPUT_FILE;
37 using (StreamReader File = new StreamReader (FileName)) {
39 string section = null;
40 string key = "no_key";
41 Dictionary<string,ConfigEntry> SectionDic = null;
42 bool listProcessing = false;
43 while (File.Peek() != -1) {
44 Line = File.ReadLine ();
47 listProcessing = false;
49 SectionDic [key].list.Add (Line);
50 } else if (Line.Length == 0 || Line.Substring (0, 1) == "#") {
51 ; // this is a comment line
52 } else if (Line.Substring (0, 1) == "[") {
54 if (section != null) {
56 Data [section] = SectionDic;
59 section = Line.Substring (1, Line.IndexOf (']') - 1);
60 SectionDic = new Dictionary<string, ConfigEntry> ();
62 key = Line.Substring (0, Line.IndexOf (':'));
63 String Value = Line.Substring (Line.IndexOf (':') + 1);
64 var ce = new ConfigEntry (Value);
65 if (Value == "LIST") {
66 listProcessing = true;
67 ce.list = new SortedSet<string> ();
69 SectionDic [key] = ce;
73 throw new ApplicationException ("Configuration list has no end");
74 if (SectionDic != null) Data [section] = SectionDic;
80 public static Config GetInstance()
82 if (Config.Instance == null) Config.Instance = new Config ();
83 return Config.Instance;
85 public static String GetConfig(string section, string key){
87 if (Data.ContainsKey(section)){
88 if (Data[section].ContainsKey(key)) return Data [section][key].value;
92 public static ISet<string> GetList(string section, string key){
94 if (!Data.ContainsKey (section))
96 if (!Data [section].ContainsKey (key))
98 if (Data [section] [key].value != "LIST")
99 throw new ApplicationException ("Required List-Config is not a list");
100 return Data [section] [key].list;
102 public static ISet<string> RequireList(string section, string key){
103 ISet<string> result = GetList (section, key);
105 throw new ApplicationException ("Config missing:"+ section+ key);
108 public static String RequireConfig(string section, string key){
109 string value = GetConfig (section, key);
111 throw new ApplicationException ("No Config Entry for " + section + "/"+key);
114 public static void Set(string section, string key, string value){
116 if (! Data.ContainsKey (section)) {
117 Data [section] = new Dictionary<string, ConfigEntry> ();
119 Data [section] [key] = new ConfigEntry(value);
121 public static void Set(string section, string key, ISet<string> value){
122 Set (section, key, "LIST");
123 Data [section] [key].list = value;
125 public static void Write(){
127 StreamWriter File = new StreamWriter (Config.file_name, false);
128 foreach (var section in Data) {
130 File.WriteLine ("[" + section.Key + "]");
131 foreach (var elem in section.Value) {
132 File.WriteLine (elem.Key + ":" + elem.Value.value);
133 if (elem.Value.value == "LIST") {
134 foreach (string list_ele in elem.Value.list) {
135 File.WriteLine (list_ele);
137 File.WriteLine ("LIST");
143 public static void Remove(string section)
146 Data.Remove (section);
148 private static void Check()
151 throw new ApplicationException ("Configuration data not loaded");