-
Notifications
You must be signed in to change notification settings - Fork 6
/
IniFile.cs
149 lines (142 loc) · 4 KB
/
IniFile.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
147
148
149
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.IO;
namespace Noxico
{
public static class IniFile
{
private static Dictionary<string, Dictionary<string, string>> settings = new Dictionary<string, Dictionary<string, string>>();
private static string lastFileName;
public static void Load(string fileName)
{
settings.Clear();
var thisSection = string.Empty;
var lines = File.ReadAllLines(fileName);
foreach (var line in lines)
{
var l = line;
if (l.Contains(';'))
l = l.Remove(l.IndexOf(';'));
if (l.IsBlank())
continue;
if (l.StartsWith('[') && l.EndsWith(']'))
{
var key = l.Trim('[', ']');
settings.Add(key, new Dictionary<string, string>());
thisSection = key;
}
else if (l.Contains('=') && !string.IsNullOrEmpty(thisSection))
{
var sep = l.IndexOf('=');
var key = l.Substring(0, sep).Trim();
var val = l.Substring(sep + 1).Trim();
if (settings[thisSection].ContainsKey(key))
{
throw new Exception(string.Format("There's an error in the INI file: the key \"{0}\" in section \"{1}\" has already been used in that section.", key, thisSection));
//settings[thisSection][key] = val;
}
else
settings[thisSection].Add(key, val);
}
}
lastFileName = fileName;
}
public static void Save(string fileName)
{
if (fileName.IsBlank())
fileName = lastFileName;
if (!File.Exists(fileName))
{
var sb = new StringBuilder(string.Empty);
foreach (var section in settings)
{
sb.AppendFormat("[{0}]", section.Key);
foreach (var entry in section.Value)
{
sb.AppendLine();
sb.AppendFormat("{0}={1}", entry.Key, entry.Value);
}
sb.AppendLine();
sb.AppendLine();
}
File.WriteAllText(fileName, sb.ToString());
}
else
{
var lines = File.ReadAllLines(fileName).Select(l => l.Trim()).ToArray();
foreach (var section in settings)
{
for (var i = 0; i < lines.Length; i++)
{
if (lines[i].StartsWith('[' + section.Key + ']'))
{
var sStart = i + 1;
var sEnd = lines.Length;
for (i = sStart; i < lines.Length; i++)
{
if (lines[i].StartsWith('['))
{
sEnd = i;
break;
}
}
foreach (var entry in section.Value)
{
for (i = sStart; i < sEnd; i++)
{
if (lines[i].Contains('=') && lines[i].StartsWith(entry.Key))
{
var comment = string.Empty;
if (lines[i].Contains(';'))
comment = ' ' + lines[i].Substring(lines[i].IndexOf(';'));
lines[i] = entry.Key + "=" + entry.Value + comment;
break;
}
}
}
break;
}
}
}
File.WriteAllLines(fileName, lines);
}
}
public static string GetValue(string section, string key, string def)
{
if (settings.ContainsKey(section) && settings[section].ContainsKey(key))
return settings[section][key];
return def;
}
public static int GetValue(string section, string key, int def)
{
if (settings.ContainsKey(section) && settings[section].ContainsKey(key))
{
int i = 0;
if (int.TryParse(settings[section][key], out i))
return i;
}
return def;
}
public static bool GetValue(string section, string key, bool def)
{
if (settings.ContainsKey(section) && settings[section].ContainsKey(key))
{
bool i = false;
if (bool.TryParse(settings[section][key].Titlecase(), out i))
return i;
}
return def;
}
public static void SetValue(string section, string key, object value)
{
if (!settings.ContainsKey(section))
settings.Add(section, new Dictionary<string, string>());
if (!settings[section].ContainsKey(key))
settings[section].Add(key, value.ToString());
else
settings[section][key] = value.ToString();
}
}
}