-
Notifications
You must be signed in to change notification settings - Fork 0
/
INIConfig.cs
146 lines (126 loc) · 4.53 KB
/
INIConfig.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
namespace LOMNTool
{
public class INIConfig
{
private Dictionary<string, INISection> Sections = new Dictionary<string, INISection>(StringComparer.InvariantCultureIgnoreCase);
public Dictionary<string, INISection> TempSections = new Dictionary<string, INISection>(StringComparer.InvariantCultureIgnoreCase);
public INISection this[string sectionName]
{
get
{
if (!TempSections.ContainsKey(sectionName) && !Sections.ContainsKey(sectionName))
Sections.Add(sectionName, new INISection(sectionName));
else if(TempSections.ContainsKey(sectionName))
return TempSections[sectionName];
return Sections[sectionName];
}
}
public INIConfig()
{
}
public INIConfig(string filename)
{
Read(filename, false);
}
public void SetTemporary(string section, string key, string value)
{
if (!TempSections.ContainsKey(section))
{
INISection tempSection = new INISection(section);
tempSection.Keys.Add(key, value);
TempSections.Add(section, tempSection);
}
else
{
TempSections[section][key] = value;
}
}
public string GetValueOrDefault(string section, string key, string defaultValue = null)
{
if (!Sections.ContainsKey(section) && !TempSections.ContainsKey(section))
return defaultValue;
INISection s;
if (TempSections.ContainsKey(section))
s = TempSections[section];
else
s = Sections[section];
if (!s.Keys.ContainsKey(key))
return defaultValue;
else
return s.Keys[key];
}
public void Read(string filename, bool clearExisting = true)
{
if (!System.IO.File.Exists(filename))
return;
if (clearExisting)
Sections.Clear();
using (System.IO.StreamReader reader = new System.IO.StreamReader(filename))
{
INISection currentSection = new INISection(""); // Default unnamed section
while (!reader.EndOfStream)
{
string line = reader.ReadLine().Trim();
if (line.Length == 0 || line.StartsWith(";"))
continue;
if (line.StartsWith("["))
{
// Read section header
Sections.Add(currentSection.Name, currentSection);
currentSection = new INISection(line.TrimStart('[').TrimEnd(']'));
}
else
{
// Read key
string[] parts = line.Split('='); // Assume no equals sign in value
currentSection.Keys.Add(parts[0], parts[1]);
}
}
Sections.Add(currentSection.Name, currentSection);
}
}
public void Write(string filename)
{
using (System.IO.StreamWriter writer = new System.IO.StreamWriter(filename, false))
{
foreach (INISection section in Sections.Values)
{
if (!String.IsNullOrEmpty(section.Name))
writer.WriteLine("\n[" + section.Name + "]");
foreach (KeyValuePair<string, string> pair in section.Keys)
{
writer.WriteLine(pair.Key + "=" + pair.Value);
}
}
}
}
}
public class INISection
{
public string Name { get; }
public Dictionary<string, string> Keys = new Dictionary<string, string>(StringComparer.InvariantCultureIgnoreCase);
public string this[string name]
{
get
{
return Keys.ContainsKey(name) ? Keys[name] : "";
}
set
{
if (Keys.ContainsKey(name))
Keys[name] = value;
else
Keys.Add(name, value);
}
}
public INISection(string name)
{
Name = name;
}
}
}