-
Notifications
You must be signed in to change notification settings - Fork 3
/
Copy pathIniPropertyCollection.cs
137 lines (80 loc) · 3.05 KB
/
IniPropertyCollection.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
using System;
using System.Collections;
using System.Collections.Generic;
namespace Gsemac.Text.Ini {
public class IniPropertyCollection :
IIniPropertyCollection {
// Public members
public int Count => properties.Count;
public bool IsReadOnly => properties.IsReadOnly;
public IIniProperty this[string propertyName] {
get => Get(FormatPropertyName(propertyName));
}
public IniPropertyCollection() :
this(StringComparer.InvariantCultureIgnoreCase) {
}
public IniPropertyCollection(IEqualityComparer<string> keyComparer) {
if (keyComparer is null)
throw new ArgumentNullException(nameof(keyComparer));
properties = new Dictionary<string, IIniProperty>(keyComparer);
}
public void Add(string name, string value) {
Add(new IniProperty(FormatPropertyName(name), value));
}
public void Add(IIniProperty item) {
if (item is null)
throw new ArgumentNullException(nameof(item));
if (properties.TryGetValue(item.Name, out IIniProperty existingProperty)) {
// Update the existing property.
existingProperty.Value = item.Value;
}
else {
properties.Add(item.Name, item);
}
}
public bool Remove(string name) {
return properties.Remove(name);
}
public bool Remove(IIniProperty item) {
if (item is null)
return false;
// If we have an identical property to the one given, remove it.
if (Contains(item))
return properties.Remove(item.Name);
return false;
}
public bool Contains(IIniProperty item) {
if (item is null)
return false;
return properties.TryGetValue(item.Name, out IIniProperty property) &&
property.Equals(item);
}
public bool ContainsKey(string propertyName) {
return properties.ContainsKey(FormatPropertyName(propertyName));
}
public void Clear() {
properties.Clear();
}
public void CopyTo(IIniProperty[] array, int arrayIndex) {
properties.Values.CopyTo(array, arrayIndex);
}
public IEnumerator<IIniProperty> GetEnumerator() {
return properties.Values.GetEnumerator();
}
IEnumerator IEnumerable.GetEnumerator() {
return GetEnumerator();
}
// Private members
private readonly IDictionary<string, IIniProperty> properties;
private string FormatPropertyName(string value) {
if (value is null)
value = string.Empty;
return value;
}
private IIniProperty Get(string name) {
if (name is object && properties.TryGetValue(name, out IIniProperty property))
return property;
return null;
}
}
}