-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathSectionData.cs
More file actions
220 lines (181 loc) · 5.88 KB
/
SectionData.cs
File metadata and controls
220 lines (181 loc) · 5.88 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
using System;
using System.Collections.Generic;
namespace IniParser.Model
{
/// <summary>
/// Information associated to a section in a INI File
/// Includes both the value and the comments associated to the key.
/// </summary>
public class SectionData : ICloneable
{
IEqualityComparer<string> _searchComparer;
#region Initialization
public SectionData(string sectionName)
:this(sectionName, EqualityComparer<string>.Default)
{
}
/// <summary>
/// Initializes a new instance of the <see cref="SectionData"/> class.
/// </summary>
public SectionData(string sectionName, IEqualityComparer<string> searchComparer)
{
_searchComparer = searchComparer;
if (string.IsNullOrEmpty(sectionName))
throw new ArgumentException("section name can not be empty");
_leadingComments = new List<string>();
_keyDataCollection = new KeyDataCollection(_searchComparer);
SectionName = sectionName;
}
/// <summary>
/// Initializes a new instance of the <see cref="SectionData"/> class
/// from a previous instance of <see cref="SectionData"/>.
/// </summary>
/// <remarks>
/// Data is deeply copied
/// </remarks>
/// <param name="ori">
/// The instance of the <see cref="SectionData"/> class
/// used to create the new instance.
/// </param>
/// <param name="searchComparer">
/// Search comparer.
/// </param>
public SectionData(SectionData ori, IEqualityComparer<string> searchComparer = null)
{
SectionName = ori.SectionName;
_searchComparer = searchComparer;
_leadingComments = new List<string>(ori._leadingComments);
_keyDataCollection = new KeyDataCollection(ori._keyDataCollection, searchComparer ?? ori._searchComparer);
}
#endregion
#region Operations
/// <summary>
/// Deletes all comments in this section and key/value pairs
/// </summary>
public void ClearComments()
{
LeadingComments.Clear();
TrailingComments.Clear();
Keys.ClearComments();
}
/// <summary>
/// Deletes all the key-value pairs in this section.
/// </summary>
public void ClearKeyData()
{
Keys.RemoveAllKeys();
}
/// <summary>
/// Merges otherSection into this, adding new keys if they don't exists
/// or overwriting values if the key already exists.
/// Comments get appended.
/// </summary>
/// <remarks>
/// Comments are also merged but they are always added, not overwritten.
/// </remarks>
/// <param name="toMergeSection"></param>
public void Merge(SectionData toMergeSection)
{
foreach (var comment in toMergeSection.LeadingComments)
LeadingComments.Add(comment);
Keys.Merge(toMergeSection.Keys);
foreach(var comment in toMergeSection.TrailingComments)
TrailingComments.Add(comment);
}
#endregion
#region Properties
/// <summary>
/// Gets or sets the name of the section.
/// </summary>
/// <value>
/// The name of the section
/// </value>
public string SectionName
{
get
{
return _sectionName;
}
set
{
if (!string.IsNullOrEmpty(value))
_sectionName = value;
}
}
[Obsolete("Do not use this property, use property Comments instead")]
public List<string> LeadingComments
{
get
{
return _leadingComments;
}
internal set
{
_leadingComments = new List<string>(value);
}
}
/// <summary>
/// Gets or sets the comment list associated to this section.
/// </summary>
/// <value>
/// A list of strings.
/// </value>
public List<string> Comments
{
get
{
return _leadingComments;
}
}
[Obsolete("Do not use this property, use property Comments instead")]
public List<string> TrailingComments
{
get
{
return _trailingComments;
}
internal set
{
_trailingComments = new List<string>(value);
}
}
/// <summary>
/// Gets or sets the keys associated to this section.
/// </summary>
/// <value>
/// A collection of KeyData objects.
/// </value>
public KeyDataCollection Keys
{
get
{
return _keyDataCollection;
}
set
{
_keyDataCollection = value;
}
}
#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 SectionData(this);
}
#endregion
#region Non-public members
// Comments associated to this section
private List<string> _leadingComments;
private List<string> _trailingComments = new List<string>();
// Keys associated to this section
private KeyDataCollection _keyDataCollection;
private string _sectionName;
#endregion
}
}