Allow schema files that are missing checksums on the !!SCHEMAMATIC line.
[versaplex.git] / wvdotnet / wvini.cs
blob6ec3d7d53691ea60ff13a618f08f3d898f6416e0
1 /*
2 * Versaplex:
3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
5 */
6 using System;
7 using System.IO;
8 using System.Collections.Generic;
10 namespace Wv
12 /// Well, it's not exactly UniConf, but...
13 public class WvIni
15 string filename;
16 Dictionary<string,IDictionary<string,string>> sections
17 = new Dictionary<string,IDictionary<string,string>>
18 (StringComparer.OrdinalIgnoreCase);
20 public WvIni(string filename)
22 this.filename = filename;
24 StreamReader r;
25 try
27 r = File.OpenText(filename);
29 catch (IOException)
31 return; // I guess there's no file!
34 using (r)
36 string section = "";
37 string s;
38 while ((s = r.ReadLine()) != null)
40 s = s.Trim();
41 if (s.Length == 0) continue; // blank line
42 if (s[0] == '#') continue; // comment
43 if (s[0] == '[' && s[s.Length-1]==']') // section
45 section = s.Substring(1, s.Length-2);
47 else if (s.IndexOf('=') >= 0)
49 string[] a = s.Split(new char[] {'='}, 2);
50 this[section][a[0].Trim()] = a[1].Trim();
52 else
53 continue; // whatever
58 public IDictionary<string,string> this[string sectname]
60 get
62 if (!sections.ContainsKey(sectname))
63 sections.Add(sectname, new Dictionary<string,string>
64 (StringComparer.OrdinalIgnoreCase));
65 return sections[sectname];
69 public string get(string section, string key, string defval)
71 string v;
72 if (!this[section].TryGetValue(key, out v))
73 return defval;
74 return v;
77 public string get(string section, string key)
79 return get(section, key, null);
82 public void set(string section, string key, string val)
84 this[section][key] = val;
87 public void maybeset(string section, string key, string val)
89 if (get(section, key) == null)
90 set(section, key, val);
93 public void save(string filename)
95 using (StreamWriter w = File.CreateText(filename))
97 foreach (string section in sections.Keys)
99 w.Write(wv.fmt("\n[{0}]\n", section));
100 foreach (string ent in sections[section].Keys)
101 w.Write(wv.fmt("{0} = {1}\n",
102 ent, sections[section][ent]));
107 public void save()
109 save(filename);