monodev
[mcloud.git] / util / Config.cs
1 using System;
2 using System.Collections.Generic;
3 using System.IO;
4
5
6 namespace util
7 {
8         internal class ConfigEntry
9         {
10                 public string value;
11                 public ISet<string> list;
12                 public ConfigEntry(string v)
13                 {
14                         this.value = v;
15                 }
16         }
17
18         public static class Config
19         {
20                 private static Dictionary<String,Dictionary<string,ConfigEntry>> Data;
21                 private static bool ready = false;
22                 private static string file_name;
23
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)
28                 {
29                         Config.file_name = FileName;
30                         Data = new Dictionary<string, Dictionary<string,ConfigEntry>>();
31
32                         if (!System.IO.File.Exists (FileName)) {
33                                 ready = true;
34                                 return LOAD_NO_INPUT_FILE;
35                         }
36
37                         using (StreamReader File = new StreamReader (FileName)) {
38                                 String Line;
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 ();
45                                         if (listProcessing) {
46                                                 if (Line == "LIST")
47                                                         listProcessing = false;
48                                                 else
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) == "[") {
53                                                 // new section
54                                                 if (section != null) {
55                                                         // append old section
56                                                         Data [section] = SectionDic;
57                                                 }
58                                                 // start new section
59                                                 section = Line.Substring (1, Line.IndexOf (']') - 1);
60                                                 SectionDic = new Dictionary<string, ConfigEntry> ();
61                                         } else {
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> ();
68                                                 }
69                                                 SectionDic [key] = ce;
70                                         }
71                                 }
72                                 if (listProcessing)
73                                         throw new ApplicationException ("Configuration list has no end");
74                                 if (SectionDic != null) Data [section] = SectionDic;
75                         }
76                         ready = true;
77                         return LOAD_OK;
78                 }
79                 /*
80                 public static Config GetInstance()
81                 {
82                         if (Config.Instance == null) Config.Instance = new Config ();
83                         return Config.Instance;
84                 }*/
85                 public static String GetConfig(string section, string key){
86                         Check ();
87                         if (Data.ContainsKey(section)){
88                                 if (Data[section].ContainsKey(key)) return Data [section][key].value;
89                         }
90                         return null;
91                 }
92                 public static ISet<string> GetList(string section, string key){
93                         Check ();
94                         if (!Data.ContainsKey (section))
95                                 return null;
96                         if (!Data [section].ContainsKey (key))
97                                 return null;
98                         if (Data [section] [key].value != "LIST")
99                                 throw new ApplicationException ("Required List-Config is not a list");
100                         return Data [section] [key].list;
101                 }
102                 public static ISet<string> RequireList(string section, string key){
103                         ISet<string> result = GetList (section, key);
104                         if (result == null)
105                                 throw new ApplicationException ("Config missing:"+ section+ key);
106                         return result;
107                 }
108                 public static String RequireConfig(string section, string key){
109                         string value = GetConfig (section, key);
110                         if (value == null)
111                                 throw new ApplicationException ("No Config Entry for " + section + "/"+key);
112                         return value;
113                 }
114                 public static void Set(string section, string key, string value){
115                         Check ();
116                         if (! Data.ContainsKey (section)) {
117                                 Data [section] = new Dictionary<string, ConfigEntry> ();
118                         }
119                         Data [section] [key] = new ConfigEntry(value);
120                 }
121                 public static void Set(string section, string key, ISet<string> value){
122                         Set (section, key, "LIST");
123                         Data [section] [key].list = value;
124                 }
125                 public static void Write(){
126                         Check ();
127                         StreamWriter File = new StreamWriter (Config.file_name, false);
128                         foreach (var section in Data) {
129                                 File.WriteLine ();
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);
136                                                 }
137                                                 File.WriteLine ("LIST");
138                                         }
139                                 }
140                         }
141                         File.Close();
142                 }
143                 public static void Remove(string section)
144                 {
145                         Check ();
146                         Data.Remove (section);
147                 }
148                 private static void Check()
149                 {
150                         if (!ready)
151                                 throw new ApplicationException ("Configuration data not loaded");
152                 }
153         }
154 }
155