3 * Copyright (C)2007-2008 Versabanq Innovations Inc. and contributors.
4 * See the included file named LICENSE for license information.
8 using System
.Collections
.Generic
;
12 /// Well, it's not exactly UniConf, but...
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
;
27 r
= File
.OpenText(filename
);
31 return; // I guess there's no file!
38 while ((s
= r
.ReadLine()) != null)
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();
58 public IDictionary
<string,string> this[string sectname
]
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
)
72 if (!this[section
].TryGetValue(key
, out 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
]));