using System; using System.Collections.Generic; using System.IO; using System.Text; using System.Reflection; using GC_McExt.Logging; namespace GC_McExt.Configuration { public sealed class SettingsReader { private string[] configuration; public SettingsReader(string file) { using (StreamReader reader = new StreamReader(file)) { configuration = reader.ReadToEnd().Split('\n'); reader.Close(); } } public string GetSetting(string section, string key) { bool isSeekingConfiguration = false; string value = ""; foreach (string line in configuration) { //Discard commented settings on the file. if (line.StartsWith(";")) continue; if (!isSeekingConfiguration) { if (line.StartsWith(string.Format("[{0}]", section))) isSeekingConfiguration = true; } else { //Reaching another section means that the key doesn't exist. if (line.StartsWith("[")) break; string searchKey = string.Format("{0}=", key); if (line.StartsWith(searchKey)) { value = line.Remove(0, searchKey.Length).Trim(); } } } return value; } public Dictionary GetSection(string section) { bool isSeekingConfiguration = false; Dictionary dictionary = new Dictionary(); foreach (string line in configuration) { //Discard commented settings on the file. if (line.StartsWith(";")) continue; if (!isSeekingConfiguration) { if (line.StartsWith(string.Format("[{0}]", section))) isSeekingConfiguration = true; } else { //Reaching another section means that the key doesn't exist. if (line.StartsWith("[")) break; if (!string.IsNullOrEmpty(line)) { //We only read settings that have a key with atleast 1 character in the //name... if (line.IndexOf('=') > 0) { string key = line.Substring(0, line.IndexOf('=')); string value = line.Substring(line.IndexOf('=')); dictionary.Add(key,value); } } } } return dictionary; } } }