Change a lot of typos and violations against C# defination
This commit is contained in:
+162
-185
@@ -8,191 +8,168 @@ using System.Threading.Tasks;
|
||||
|
||||
namespace BlubbFish.Utils
|
||||
{
|
||||
public class InIReader
|
||||
public class InIReader
|
||||
{
|
||||
private Dictionary<String, Dictionary<String, String>> cont;
|
||||
private FileSystemWatcher k = new FileSystemWatcher(Directory.GetCurrentDirectory(), "*.ini");
|
||||
private String filename;
|
||||
|
||||
private static Dictionary<String, InIReader> instances = new Dictionary<String, InIReader>();
|
||||
|
||||
private InIReader(String filename)
|
||||
{
|
||||
private Dictionary<string, Dictionary<string, string>> cont;
|
||||
private FileSystemWatcher k = new FileSystemWatcher(Directory.GetCurrentDirectory(), "*.ini");
|
||||
private string filename;
|
||||
|
||||
private static Dictionary<string, InIReader> instances = new Dictionary<string, InIReader>();
|
||||
|
||||
private InIReader(string filename)
|
||||
{
|
||||
this.filename = filename;
|
||||
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<string, Dictionary<string, string>>();
|
||||
StreamReader file = new StreamReader(this.filename);
|
||||
List<String> buf = new List<string>();
|
||||
string fline = "";
|
||||
while (fline != null)
|
||||
{
|
||||
fline = file.ReadLine();
|
||||
if (fline != null && fline.Length > 0 && fline.Substring(0,1) != ";")
|
||||
buf.Add(fline);
|
||||
}
|
||||
file.Close();
|
||||
Dictionary<string, string> sub = new Dictionary<string, string>();
|
||||
string cap = "";
|
||||
foreach (string line in buf)
|
||||
{
|
||||
Match match = Regex.Match(line, @"^\[[a-zA-Z0-9\-_ ]+\]\w*$", RegexOptions.IgnoreCase);
|
||||
if (match.Success)
|
||||
{
|
||||
if (sub.Count != 0 && cap != "")
|
||||
{
|
||||
this.cont.Add(cap, sub);
|
||||
}
|
||||
cap = line;
|
||||
sub = new Dictionary<string, string>();
|
||||
}
|
||||
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<String> getSections()
|
||||
{
|
||||
return cont.Keys.ToList<String>();
|
||||
}
|
||||
|
||||
public String getValue(String section, String key)
|
||||
{
|
||||
if (!section.StartsWith("["))
|
||||
{
|
||||
section = "[" + section + "]";
|
||||
}
|
||||
if (cont.Keys.Contains(section))
|
||||
{
|
||||
if (cont[section].Keys.Contains(key))
|
||||
{
|
||||
return cont[section][key];
|
||||
}
|
||||
}
|
||||
return null;
|
||||
}
|
||||
|
||||
|
||||
public void SetValue(string section, string key, string value)
|
||||
{
|
||||
if (!section.StartsWith("["))
|
||||
{
|
||||
section = "[" + section + "]";
|
||||
}
|
||||
if (cont.Keys.Contains(section))
|
||||
{
|
||||
if (cont[section].Keys.Contains(key))
|
||||
{
|
||||
cont[section][key] = value;
|
||||
}
|
||||
else
|
||||
{
|
||||
cont[section].Add(key, value);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
Dictionary<string, string> sub = new Dictionary<string, string>();
|
||||
sub.Add(key, value);
|
||||
cont.Add(section, sub);
|
||||
}
|
||||
this.changed();
|
||||
}
|
||||
|
||||
private void changed()
|
||||
{
|
||||
k.Changed -= null;
|
||||
saveSettings();
|
||||
loadFile();
|
||||
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<string, Dictionary<string, string>> cap in this.cont)
|
||||
{
|
||||
file.WriteLine(cap.Key);
|
||||
foreach (KeyValuePair<string, string> sub in cap.Value)
|
||||
{
|
||||
file.WriteLine(sub.Key + "=" + sub.Value);
|
||||
}
|
||||
file.WriteLine();
|
||||
}
|
||||
file.Flush();
|
||||
file.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fügt eine neue Sektion in der Ini-Datei ein.
|
||||
/// </summary>
|
||||
/// <param name="name">Sektionsname</param>
|
||||
/// <returns>true if added, false if error</returns>
|
||||
public bool addSection(string name)
|
||||
{
|
||||
if (!name.StartsWith("["))
|
||||
{
|
||||
name = "[" + name + "]";
|
||||
}
|
||||
if (this.cont.Keys.Contains(name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
this.cont.Add(name, new Dictionary<string, string>());
|
||||
this.changed();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Löscht eine Sektion inklusive Unterpunkte aus der Ini-Datei.
|
||||
/// </summary>
|
||||
/// <param name="name">Sektionsname</param>
|
||||
/// <returns>true if removed, false if error</returns>
|
||||
public bool removeSection(string name)
|
||||
{
|
||||
if (!name.StartsWith("["))
|
||||
{
|
||||
name = "[" + name + "]";
|
||||
}
|
||||
if (!this.cont.Keys.Contains(name))
|
||||
{
|
||||
return false;
|
||||
}
|
||||
this.cont.Remove(name);
|
||||
this.changed();
|
||||
return false;
|
||||
}
|
||||
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<String, Dictionary<String, String>>();
|
||||
StreamReader file = new StreamReader(this.filename);
|
||||
List<String> buf = new List<String>();
|
||||
String fline = "";
|
||||
while (fline != null) {
|
||||
fline = file.ReadLine();
|
||||
if (fline != null && fline.Length > 0 && fline.Substring(0, 1) != ";") {
|
||||
buf.Add(fline);
|
||||
}
|
||||
}
|
||||
file.Close();
|
||||
Dictionary<String, String> sub = new Dictionary<String, String>();
|
||||
String cap = "";
|
||||
foreach (String line in buf) {
|
||||
Match match = Regex.Match(line, @"^\[[a-zA-Z0-9\-_ ]+\]\w*$", RegexOptions.IgnoreCase);
|
||||
if (match.Success) {
|
||||
if (sub.Count != 0 && cap != "") {
|
||||
this.cont.Add(cap, sub);
|
||||
}
|
||||
cap = line;
|
||||
sub = new Dictionary<String, String>();
|
||||
} 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<String> GetSections()
|
||||
{
|
||||
return this.cont.Keys.ToList<String>();
|
||||
}
|
||||
|
||||
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<String, String> sub = new Dictionary<String, String> {
|
||||
{ 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<String, Dictionary<String, String>> cap in this.cont) {
|
||||
file.WriteLine(cap.Key);
|
||||
foreach (KeyValuePair<String, String> sub in cap.Value) {
|
||||
file.WriteLine(sub.Key + "=" + sub.Value);
|
||||
}
|
||||
file.WriteLine();
|
||||
}
|
||||
file.Flush();
|
||||
file.Close();
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Fügt eine neue Sektion in der Ini-Datei ein.
|
||||
/// </summary>
|
||||
/// <param name="name">Sektionsname</param>
|
||||
/// <returns>true if added, false if error</returns>
|
||||
public Boolean AddSection(String name)
|
||||
{
|
||||
if (!name.StartsWith("[")) {
|
||||
name = "[" + name + "]";
|
||||
}
|
||||
if (this.cont.Keys.Contains(name)) {
|
||||
return false;
|
||||
}
|
||||
this.cont.Add(name, new Dictionary<String, String>());
|
||||
this.Changed();
|
||||
return true;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Löscht eine Sektion inklusive Unterpunkte aus der Ini-Datei.
|
||||
/// </summary>
|
||||
/// <param name="name">Sektionsname</param>
|
||||
/// <returns>true if removed, false if error</returns>
|
||||
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;
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user