-
Notifications
You must be signed in to change notification settings - Fork 3
Expand file tree
/
Copy pathIniSectionCollection.cs
More file actions
178 lines (107 loc) · 4.25 KB
/
IniSectionCollection.cs
File metadata and controls
178 lines (107 loc) · 4.25 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
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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
namespace Gsemac.Text.Ini {
public class IniSectionCollection :
IIniSectionCollection {
// Public members
public int Count => GetNonTransientSections().Count();
public bool IsReadOnly => sections.IsReadOnly;
public IIniSection this[string sectionName] {
get => GetOrDefault(FormatSectionName(sectionName));
}
public IniSectionCollection() :
this(StringComparer.InvariantCultureIgnoreCase) {
}
public IniSectionCollection(IEqualityComparer<string> keyComparer) {
if (keyComparer is null)
throw new ArgumentNullException(nameof(keyComparer));
sections = new Dictionary<string, IniSectionInfo>(keyComparer);
this.keyComparer = keyComparer;
}
public void Add(string name) {
Add(new IniSection(FormatSectionName(name), keyComparer));
}
public void Add(IIniSection item) {
if (item is null)
throw new ArgumentNullException(nameof(item));
// If we try to add a section that already exists, just merge the properties.
if (sections.TryGetValue(item.Name, out IniSectionInfo existingSection)) {
existingSection.Section.Merge(item);
}
else {
sections.Add(item.Name, new IniSectionInfo(item));
}
}
public bool Remove(string name) {
return sections.Remove(FormatSectionName(name));
}
public bool Remove(IIniSection item) {
if (item is null)
return false;
// If we have an identical section to the one given, remove it.
if (Contains(item))
return sections.Remove(item.Name);
return false;
}
public bool Contains(string name) {
return sections.TryGetValue(FormatSectionName(name), out IniSectionInfo info) &&
!info.IsTransient;
}
public bool Contains(IIniSection item) {
if (item is null)
return false;
return sections.TryGetValue(item.Name, out IniSectionInfo info) &&
!info.IsTransient &&
info.Section.Equals(item);
}
public void Clear() {
sections.Clear();
}
public void CopyTo(IIniSection[] array, int arrayIndex) {
this.ToList().CopyTo(array, arrayIndex);
}
public IEnumerator<IIniSection> GetEnumerator() {
return GetNonTransientSections()
.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
// Private members
private class IniSectionInfo {
// Public members
public IIniSection Section { get; }
public bool IsTransient => isTransient && !Section.Any();
public IniSectionInfo(IIniSection section) :
this(section, isTransient: false) {
}
public IniSectionInfo(IIniSection section, bool isTransient) {
Section = section;
this.isTransient = isTransient;
}
// Private members
private readonly bool isTransient;
}
private readonly IDictionary<string, IniSectionInfo> sections;
private readonly IEqualityComparer<string> keyComparer;
private string FormatSectionName(string value) {
if (value is null)
value = string.Empty;
return value;
}
private IIniSection GetOrDefault(string sectionName) {
if (sections.TryGetValue(sectionName, out IniSectionInfo info))
return info.Section;
info = new IniSectionInfo(new IniSection(sectionName), isTransient: true);
sections.Add(sectionName, info);
return info.Section;
}
private IEnumerable<IIniSection> GetNonTransientSections() {
return sections.Values
.Where(info => !info.IsTransient)
.Select(info => info.Section);
}
}
}