-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionDataCollection.cs
More file actions
260 lines (219 loc) · 8.39 KB
/
SectionDataCollection.cs
File metadata and controls
260 lines (219 loc) · 8.39 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
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
using System;
using System.Collections;
using System.Collections.Generic;
namespace IniParser.Model
{
/// <summary>
/// <para>Represents a collection of SectionData.</para>
/// </summary>
public class SectionDataCollection : ICloneable, IEnumerable<SectionData>
{
IEqualityComparer<string> _searchComparer;
#region Initialization
/// <summary>
/// Initializes a new instance of the <see cref="SectionDataCollection"/> class.
/// </summary>
public SectionDataCollection()
:this(EqualityComparer<string>.Default)
{}
/// <summary>
/// Initializes a new instance of the <see cref="IniParser.Model.SectionDataCollection"/> class.
/// </summary>
/// <param name="searchComparer">
/// StringComparer used when accessing section names
/// </param>
public SectionDataCollection(IEqualityComparer<string> searchComparer)
{
_searchComparer = searchComparer;
_sectionData = new Dictionary<string, SectionData>(_searchComparer);
}
/// <summary>
/// Initializes a new instance of the <see cref="SectionDataCollection"/> class
/// from a previous instance of <see cref="SectionDataCollection"/>.
/// </summary>
/// <remarks>
/// Data is deeply copied
/// </remarks>
/// <param name="ori">
/// The instance of the <see cref="SectionDataCollection"/> class
/// used to create the new instance.</param>
public SectionDataCollection(SectionDataCollection ori, IEqualityComparer<string> searchComparer)
{
_searchComparer = searchComparer ?? EqualityComparer<string>.Default;
_sectionData = new Dictionary<string, SectionData>(_searchComparer);
foreach(var sectionData in ori)
{
_sectionData.Add(sectionData.SectionName, (SectionData)sectionData.Clone());
};
}
#endregion
#region Properties
/// <summary>
/// Returns the number of SectionData elements in the collection
/// </summary>
public int Count { get { return _sectionData.Count; } }
/// <summary>
/// Gets the key data associated to a specified section name.
/// </summary>
/// <value>An instance of as <see cref="KeyDataCollection"/> class
/// holding the key data from the current parsed INI data, or a <c>null</c>
/// value if the section doesn't exist.</value>
public KeyDataCollection this[string sectionName]
{
get
{
if ( _sectionData.ContainsKey(sectionName) )
return _sectionData[sectionName].Keys;
return null;
}
}
#endregion
#region Public Members
/// <summary>
/// Creates a new section with empty data.
/// </summary>
/// <remarks>
/// <para>If a section with the same name exists, this operation has no effect.</para>
/// </remarks>
/// <param name="keyName">Name of the section to be created</param>
/// <return><c>true</c> if the a new section with the specified name was added,
/// <c>false</c> otherwise</return>
/// <exception cref="ArgumentException">If the section name is not valid.</exception>
public bool AddSection(string keyName)
{
//Checks valid section name
//if ( !Assert.StringHasNoBlankSpaces(keyName) )
// throw new ArgumentException("Section name contain whitespaces");
if ( !ContainsSection(keyName) )
{
_sectionData.Add( keyName, new SectionData(keyName, _searchComparer) );
return true;
}
return false;
}
/// <summary>
/// Adds a new SectionData instance to the collection
/// </summary>
/// <param name="data">Data.</param>
public void Add(SectionData data)
{
if (ContainsSection(data.SectionName))
{
SetSectionData(data.SectionName, new SectionData(data, _searchComparer));
}
else
{
_sectionData.Add(data.SectionName, new SectionData(data, _searchComparer));
}
}
/// <summary>
/// Removes all entries from this collection
/// </summary>
public void Clear()
{
_sectionData.Clear();
}
/// <summary>
/// Gets if a section with a specified name exists in the collection.
/// </summary>
/// <param name="keyName">Name of the section to search</param>
/// <returns>
/// <c>true</c> if a section with the specified name exists in the
/// collection <c>false</c> otherwise
/// </returns>
public bool ContainsSection(string keyName)
{
return _sectionData.ContainsKey(keyName);
}
/// <summary>
/// Returns the section data from a specify section given its name.
/// </summary>
/// <param name="sectionName">Name of the section.</param>
/// <returns>
/// An instance of a <see cref="SectionData"/> class
/// holding the section data for the currently INI data
/// </returns>
public SectionData GetSectionData(string sectionName)
{
if (_sectionData.ContainsKey(sectionName))
return _sectionData[sectionName];
return null;
}
public void Merge(SectionDataCollection sectionsToMerge)
{
foreach(var sectionDataToMerge in sectionsToMerge)
{
var sectionDataInThis = GetSectionData(sectionDataToMerge.SectionName);
if (sectionDataInThis == null)
{
AddSection(sectionDataToMerge.SectionName);
}
this[sectionDataToMerge.SectionName].Merge(sectionDataToMerge.Keys);
}
}
/// <summary>
/// Sets the section data for given a section name.
/// </summary>
/// <param name="sectionName"></param>
/// <param name="data">The new <see cref="SectionData"/>instance.</param>
public void SetSectionData(string sectionName, SectionData data)
{
if ( data != null )
_sectionData[sectionName] = data;
}
/// <summary>
///
/// </summary>
/// <param name="keyName"></param>
/// <return><c>true</c> if the section with the specified name was removed,
/// <c>false</c> otherwise</return>
public bool RemoveSection(string keyName)
{
return _sectionData.Remove(keyName);
}
#endregion
#region IEnumerable<SectionData> Members
/// <summary>
/// Returns an enumerator that iterates through the collection.
/// </summary>
/// <returns>
/// A <see cref="T:System.Collections.Generic.IEnumerator`1"/> that can be used to iterate through the collection.
/// </returns>
public IEnumerator<SectionData> GetEnumerator()
{
foreach (string sectionName in _sectionData.Keys)
yield return _sectionData[sectionName];
}
#endregion
#region IEnumerable Members
/// <summary>
/// Returns an enumerator that iterates through a collection.
/// </summary>
/// <returns>
/// An <see cref="T:System.Collections.IEnumerator"/> object that can be used to iterate through the collection.
/// </returns>
IEnumerator IEnumerable.GetEnumerator()
{
return GetEnumerator();
}
#endregion
#region ICloneable Members
/// <summary>
/// Creates a new object that is a copy of the current instance.
/// </summary>
/// <returns>
/// A new object that is a copy of this instance.
/// </returns>
public object Clone()
{
return new SectionDataCollection(this, _searchComparer);
}
#endregion
#region Non-public Members
/// <summary>
/// Data associated to this section
/// </summary>
private readonly Dictionary<string, SectionData> _sectionData;
#endregion
}
}