-
Notifications
You must be signed in to change notification settings - Fork 22
/
Copy pathIniSectionCollection.cs
184 lines (174 loc) · 6.12 KB
/
IniSectionCollection.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
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
using BytecodeApi.Extensions;
using System.Collections;
namespace BytecodeApi.IniParser;
/// <summary>
/// Represents a collection of INI file sections.
/// </summary>
public sealed class IniSectionCollection : ICollection<IniSection>
{
private readonly List<IniSection> Sections;
/// <summary>
/// Gets the <see cref="IniSection" /> at the specified index.
/// </summary>
/// <param name="index">The index at which to retrieve the <see cref="IniSection" />.</param>
public IniSection this[int index]
{
get
{
Check.IndexOutOfRange(index, Count);
return Sections[index];
}
}
/// <summary>
/// Gets the <see cref="IniSection" /> with the specified case sensitive name.
/// </summary>
/// <param name="name">A <see cref="string" /> specifying the name of the section.</param>
public IniSection this[string name]
{
get
{
Check.ArgumentNull(name);
return Sections.FirstOrDefault(s => s.Name == name) ?? throw Throw.KeyNotFound($"A section with the name '{name}' was not found.");
}
}
/// <summary>
/// Gets the number of elements contained in the <see cref="IniSectionCollection" />.
/// </summary>
public int Count => Sections.Count;
/// <summary>
/// Gets a value indicating whether the <see cref="IniSectionCollection" /> is read-only.
/// </summary>
public bool IsReadOnly => false;
/// <summary>
/// Initializes a new instance of the <see cref="IniSectionCollection" /> class.
/// </summary>
public IniSectionCollection()
{
Sections = new();
}
/// <summary>
/// Processes all duplicate sections within this collection according to the specified behavior.
/// </summary>
/// <param name="behavior">An <see cref="IniDuplicateSectionNameBehavior" /> object specifying how duplicates are processed.</param>
public void ProcessDuplicates(IniDuplicateSectionNameBehavior behavior)
{
ProcessDuplicates(behavior, false);
}
/// <summary>
/// Processes all duplicate sections within this collection according to the specified behavior.
/// </summary>
/// <param name="behavior">An <see cref="IniDuplicateSectionNameBehavior" /> object specifying how duplicates are processed.</param>
/// <param name="ignoreCase"><see langword="true" /> to ignore character casing during name comparison.</param>
public void ProcessDuplicates(IniDuplicateSectionNameBehavior behavior, bool ignoreCase)
{
List<IniSection> removedSections = new();
switch (behavior)
{
case IniDuplicateSectionNameBehavior.Abort:
if (Sections.DistinctBy(section => ignoreCase ? section.Name?.ToLower() : section.Name).Count() != Count)
{
throw Throw.Format("Duplicate section found.");
}
break;
case IniDuplicateSectionNameBehavior.Ignore:
for (int i = 1; i < Count; i++)
{
if (Sections.Take(i).Any(section => section.Name?.Equals(Sections[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == true))
{
removedSections.Add(Sections[i]);
}
}
break;
case IniDuplicateSectionNameBehavior.Merge:
for (int i = 1; i < Count; i++)
{
if (Sections.Take(i).FirstOrDefault(section => section.Name?.Equals(Sections[i].Name, ignoreCase ? StringComparison.OrdinalIgnoreCase : StringComparison.Ordinal) == true) is IniSection firstSection)
{
firstSection.Properties.AddRange(Sections[i].Properties);
removedSections.Add(Sections[i]);
}
}
break;
case IniDuplicateSectionNameBehavior.Duplicate:
break;
default:
throw Throw.InvalidEnumArgument(nameof(behavior), behavior);
}
Sections.RemoveRange(removedSections);
}
/// <summary>
/// Adds an <see cref="IniSection" /> to the end of the <see cref="IniSectionCollection" />.
/// </summary>
/// <param name="item">The <see cref="IniSection" /> to be added to the end of the <see cref="IniSectionCollection" />.</param>
public void Add(IniSection item)
{
Check.ArgumentNull(item);
Sections.Add(item);
}
/// <summary>
/// Adds an <see cref="IniSection" /> to the end of the <see cref="IniSectionCollection" />.
/// </summary>
/// <param name="name">The name of the INI section.</param>
/// <returns>
/// The newly added <see cref="IniSection" />.
/// </returns>
public IniSection Add(string name)
{
Check.ArgumentNull(name);
IniSection section = new(name);
Add(section);
return section;
}
/// <summary>
/// Removes the first occurrence of a specific <see cref="IniSection" /> from the <see cref="IniSectionCollection" />.
/// </summary>
/// <param name="item">The <see cref="IniSection" /> to remove from the <see cref="IniSectionCollection" />.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="item" /> is successfully removed;
/// otherwise, <see langword="false" />.
/// This method also returns <see langword="false" />, if <paramref name="item" /> was not found in the <see cref="IniSectionCollection" />.
/// </returns>
public bool Remove(IniSection item)
{
return Sections.Remove(item);
}
/// <summary>
/// Removes all elements from the <see cref="IniSectionCollection" />.
/// </summary>
public void Clear()
{
Sections.Clear();
}
/// <summary>
/// Determines whether an element is in the <see cref="IniSectionCollection" />.
/// </summary>
/// <param name="item">The <see cref="IniSection" /> to locate in the <see cref="IniSectionCollection" />.</param>
/// <returns>
/// <see langword="true" />, if <paramref name="item" /> is found in the <see cref="IniSectionCollection" />;
/// otherwise, <see langword="false" />.
/// </returns>
public bool Contains(IniSection item)
{
return Sections.Contains(item);
}
void ICollection<IniSection>.CopyTo(IniSection[] array, int arrayIndex)
{
Check.ArgumentNull(array);
Check.IndexOutOfRange(arrayIndex, array.Length - Count + 1);
Sections.CopyTo(array, arrayIndex);
}
/// <summary>
/// Returns an enumerator that iterates through the <see cref="IniSectionCollection" />.
/// </summary>
/// <returns>
/// An enumerator that can be used to iterate through the <see cref="IniSectionCollection" />.
/// </returns>
public IEnumerator<IniSection> GetEnumerator()
{
return Sections.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator()
{
return Sections.GetEnumerator();
}
}