forked from fdorg/flashdevelop
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConfigHelper.cs
More file actions
62 lines (57 loc) · 2.16 KB
/
ConfigHelper.cs
File metadata and controls
62 lines (57 loc) · 2.16 KB
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
using System.Collections.Generic;
using System.IO;
namespace PluginCore.Helpers
{
public class SimpleIni
: Dictionary<string, Dictionary<string, string>>
{
public Dictionary<string, string> Flatten()
{
var flat = new Dictionary<string, string>();
foreach (var section in this)
{
foreach (var entry in section.Value)
flat[entry.Key] = entry.Value;
}
return flat;
}
}
public class ConfigHelper
{
public static Dictionary<string, SimpleIni> Cache = new Dictionary<string, SimpleIni>();
/// <summary>
/// Read a simple config file and returns its variables as a collection of dictionaries.
/// </summary>
public static SimpleIni Parse(string configPath, bool cache)
{
if (cache && Cache.ContainsKey(configPath)) return Cache[configPath];
SimpleIni ini = new SimpleIni();
Dictionary<string, string> config = new Dictionary<string, string>();
string currentSection = "Default";
if (File.Exists(configPath))
{
string[] lines = File.ReadAllLines(configPath);
foreach (string rawLine in lines)
{
string line = rawLine.Trim();
if (line.Length < 2 || line.StartsWith("#") || line.StartsWith(";")) continue;
if (line.StartsWith("["))
{
if (currentSection != null) ini.Add(currentSection, config);
config = new Dictionary<string, string>();
currentSection = line.Substring(1, line.Length - 2);
}
else
{
string[] entry = line.Split(new char[] { '=' }, 2);
if (entry.Length < 2) continue;
config[entry[0].Trim()] = entry[1].Trim();
}
}
}
ini.Add(currentSection, config);
if (cache) Cache[configPath] = ini;
return ini;
}
}
}