vxodbc: render timeless datetimes correctly into strings.
[versaplex.git] / wvdotnet / wvini.cs
blobf90f341ee56e3953a004b24720fa3860ff39eaeb
1 using System;
2 using System.IO;
3 using System.Collections.Generic;
5 namespace Wv
7 /// Well, it's not exactly UniConf, but...
8 public class WvIni
10 string filename;
11 Dictionary<string,IDictionary<string,string>> sections
12 = new Dictionary<string,IDictionary<string,string>>
13 (StringComparer.OrdinalIgnoreCase);
15 public WvIni(string filename)
17 this.filename = filename;
19 StreamReader r;
20 try
22 r = File.OpenText(filename);
24 catch (IOException)
26 return; // I guess there's no file!
29 using (r)
31 string section = "";
32 string s;
33 while ((s = r.ReadLine()) != null)
35 s = s.Trim();
36 if (s.Length == 0) continue; // blank line
37 if (s[0] == '#') continue; // comment
38 if (s[0] == '[' && s[s.Length-1]==']') // section
40 section = s.Substring(1, s.Length-2);
42 else if (s.IndexOf('=') >= 0)
44 string[] a = s.Split(new char[] {'='}, 2);
45 this[section][a[0].Trim()] = a[1].Trim();
47 else
48 continue; // whatever
53 public IDictionary<string,string> this[string sectname]
55 get
57 if (!sections.ContainsKey(sectname))
58 sections.Add(sectname, new Dictionary<string,string>
59 (StringComparer.OrdinalIgnoreCase));
60 return sections[sectname];
64 public string get(string section, string key, string defval)
66 string v;
67 if (!this[section].TryGetValue(key, out v))
68 return defval;
69 return v;
72 public string get(string section, string key)
74 return get(section, key, null);
77 public void set(string section, string key, string val)
79 this[section][key] = val;
82 public void save(string filename)
84 using (StreamWriter w = File.CreateText(filename))
86 foreach (string section in sections.Keys)
88 w.Write(wv.fmt("\n[{0}]\n", section));
89 foreach (string ent in sections[section].Keys)
90 w.Write(wv.fmt("{0} = {1}\n",
91 ent, sections[section][ent]));
96 public void save()
98 save(filename);