3 using System
.Collections
.Generic
;
7 /// Well, it's not exactly UniConf, but...
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
;
22 r
= File
.OpenText(filename
);
26 return; // I guess there's no file!
33 while ((s
= r
.ReadLine()) != null)
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();
53 public IDictionary
<string,string> this[string sectname
]
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
)
67 if (!this[section
].TryGetValue(key
, out 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
]));