forked from connamara/quickfixn
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathSettings.cs
executable file
·77 lines (68 loc) · 2.23 KB
/
Settings.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
using System.Collections.Generic;
namespace QuickFix
{
public class Settings
{
private LinkedList<QuickFix.Dictionary> sections_ = new LinkedList<QuickFix.Dictionary>();
public Settings(System.IO.TextReader conf)
{
QuickFix.Dictionary currentSection = null;
string line = null;
while ((line = conf.ReadLine()) != null)
{
line = line.Trim();
if (IsComment(line))
{
continue;
}
else if (IsSection(line))
{
currentSection = Add(new Dictionary(SplitSection(line)));
}
else if (IsKeyValue(line) && currentSection != null)
{
string[] kv = line.Split(new char[]{'='}, 2);
currentSection.SetString(kv[0].Trim(), kv[1].Trim());
}
}
}
/// <summary>
/// Strip the outer '[' and ']' from the section name, e.g. '[DEFAULT]' becomes 'DEFAULT'
/// </summary>
/// <param name="s">the section name</param>
/// <returns></returns>
public static string SplitSection(string s)
{
return s.Trim('[', ']').Trim();
}
public static bool IsComment(string s)
{
if (s.Length < 1)
return false;
return '#' == s[0];
}
public static bool IsKeyValue(string s)
{
return s.IndexOf('=') != -1;
}
public static bool IsSection(string s)
{
if (s.Length < 2)
return false;
return s[0] == '[' && s[s.Length - 1] == ']';
}
public QuickFix.Dictionary Add(QuickFix.Dictionary section)
{
sections_.AddLast(section);
return section;
}
public LinkedList<QuickFix.Dictionary> Get(string sectionName)
{
LinkedList<QuickFix.Dictionary> result = new LinkedList<Dictionary>();
foreach (QuickFix.Dictionary dict in sections_)
if (sectionName == dict.Name)
result.AddLast(dict);
return result;
}
}
}