using System; using System.Collections.Generic; using System.IO; using System.Linq; using System.Text; using System.Text.RegularExpressions; using System.Threading.Tasks; namespace BlubbFish.Utils { public class InIReader : IDisposable { private Dictionary> cont; private FileSystemWatcher k = new FileSystemWatcher(Directory.GetCurrentDirectory(), "*.ini"); private String filename; private static Dictionary instances = new Dictionary(); private InIReader(String filename) { this.filename = filename; this.k.Changed += new FileSystemEventHandler(this.ReadAgain); LoadFile(); } public static InIReader GetInstance(String filename) { if (!instances.Keys.Contains(filename)) { instances.Add(filename, new InIReader(filename)); } return instances[filename]; } private void ReadAgain(Object sender, EventArgs e) { this.LoadFile(); } private void LoadFile() { this.cont = new Dictionary>(); StreamReader file = new StreamReader(this.filename); List buf = new List(); String fline = ""; while (fline != null) { fline = file.ReadLine(); if (fline != null && fline.Length > 0 && fline.Substring(0, 1) != ";") { buf.Add(fline); } } file.Close(); Dictionary sub = new Dictionary(); String cap = ""; foreach (String line in buf) { Match match = Regex.Match(line, @"^\[[a-zA-ZäöüÄÖÜ0-9\-_ ]+\]\w*$", RegexOptions.IgnoreCase); if (match.Success) { if (sub.Count != 0 && cap != "") { this.cont.Add(cap, sub); } cap = line; sub = new Dictionary(); } else { if (line != "" && cap != "") { String key = line.Substring(0, line.IndexOf('=')); String value = line.Substring(line.IndexOf('=') + 1); sub.Add(key, value); } } } if (sub.Count != 0 && cap != "") { this.cont.Add(cap, sub); } } public List GetSections() { return this.cont.Keys.ToList(); } public Dictionary GetSection(String section) { if(this.cont.Keys.Contains(section)) { return this.cont[section]; } if(this.cont.Keys.Contains("["+section+"]")) { return this.cont["[" + section + "]"]; } return new Dictionary(); } public String GetValue(String section, String key) { if (!section.StartsWith("[")) { section = "[" + section + "]"; } if (this.cont.Keys.Contains(section)) { if (this.cont[section].Keys.Contains(key)) { return this.cont[section][key]; } } return null; } public void SetValue(String section, String key, String value) { if (!section.StartsWith("[")) { section = "[" + section + "]"; } if (this.cont.Keys.Contains(section)) { if (this.cont[section].Keys.Contains(key)) { this.cont[section][key] = value; } else { this.cont[section].Add(key, value); } } else { Dictionary sub = new Dictionary { { key, value } }; this.cont.Add(section, sub); } this.Changed(); } private void Changed() { this.k.Changed -= null; SaveSettings(); LoadFile(); this.k.Changed += new FileSystemEventHandler(this.ReadAgain); } private void SaveSettings() { StreamWriter file = new StreamWriter(this.filename); file.BaseStream.SetLength(0); file.BaseStream.Flush(); file.BaseStream.Seek(0, SeekOrigin.Begin); foreach (KeyValuePair> cap in this.cont) { file.WriteLine(cap.Key); foreach (KeyValuePair sub in cap.Value) { file.WriteLine(sub.Key + "=" + sub.Value); } file.WriteLine(); } file.Flush(); file.Close(); } /// /// Fügt eine neue Sektion in der Ini-Datei ein. /// /// Sektionsname /// true if added, false if error public Boolean AddSection(String name) { if (!name.StartsWith("[")) { name = "[" + name + "]"; } if (this.cont.Keys.Contains(name)) { return false; } this.cont.Add(name, new Dictionary()); this.Changed(); return true; } /// /// Löscht eine Sektion inklusive Unterpunkte aus der Ini-Datei. /// /// Sektionsname /// true if removed, false if error public Boolean RemoveSection(String name) { if (!name.StartsWith("[")) { name = "[" + name + "]"; } if (!this.cont.Keys.Contains(name)) { return false; } this.cont.Remove(name); this.Changed(); return false; } protected virtual void Dispose(Boolean disposing) { if (disposing) { this.k.Dispose(); } } public void Dispose() { Dispose(true); GC.SuppressFinalize(this); } } }