-
Notifications
You must be signed in to change notification settings - Fork 249
Expand file tree
/
Copy pathIniData.cs
More file actions
197 lines (174 loc) · 5.83 KB
/
IniData.cs
File metadata and controls
197 lines (174 loc) · 5.83 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
using IniParser.Configuration;
using IniParser.Model;
namespace IniParser
{
/// <summary>
/// Represents all data from an INI file
/// </summary>
public class IniData : IDeepCloneable<IniData>
{
#region Initialization
/// <summary>
/// Initializes an empty IniData instance.
/// </summary>
public IniData()
{
Global = new PropertyCollection();
Sections = new SectionCollection();
_scheme = new IniScheme();
}
/// <summary>
/// Initializes an IniData instance with a given scheme
/// </summary>
/// <param name="scheme"></param>
public IniData(IniScheme scheme)
: this()
{
_scheme = scheme.DeepClone();
}
public IniData(IniData ori)
{
Sections = ori.Sections.DeepClone();
Global = ori.Global.DeepClone();
Configuration = ori.Configuration.DeepClone();
}
#endregion
/// <summary>
/// If set to true, it will automatically create a section when you use the indexed
/// access with a section name that does not exist.
/// If set to false, it will throw an exception if you try to access a section that
/// does not exist with the index operator.
/// </summary>
/// <remarks>
/// Defaults to false.
/// </remarks>
public bool CreateSectionsIfTheyDontExist { get; set; } = false;
/// <summary>
/// Configuration used to write an ini file with the proper
/// delimiter characters and data.
/// </summary>
/// <remarks>
/// If the <see cref="IniData"/> instance was created by a parser,
/// this instance is a copy of the <see cref="IniParserConfiguration"/> used
/// by the parser (i.e. different objects instances)
/// If this instance is created programatically without using a parser, this
/// property returns an instance of <see cref=" IniParserConfiguration"/>
/// </remarks>
public IniParserConfiguration Configuration
{
get
{
// Lazy initialization
if (_configuration == null)
{
_configuration = new IniParserConfiguration();
}
return _configuration;
}
set
{
_configuration = value.DeepClone();
}
}
public IniScheme Scheme
{
get
{
// Lazy initialization
if (_scheme == null)
{
_scheme = new IniScheme();
}
return _scheme;
}
set
{
_scheme = value.DeepClone();
}
}
/// <summary>
/// Global sections. Contains properties which are not
/// enclosed in any section (i.e. they are defined at the beginning
/// of the file, before any section.
/// </summary>
public PropertyCollection Global { get; protected set; }
/// <summary>
/// Gets the <see cref="PropertyCollection"/> instance
/// with the specified section name.
/// with the specified section name.
/// </summary>
public PropertyCollection this[string sectionName]
{
get
{
if (!Sections.Contains(sectionName))
if (CreateSectionsIfTheyDontExist)
Sections.Add(sectionName);
else
return null;
return Sections[sectionName];
}
}
/// <summary>
/// Gets or sets all the <see cref="Section"/>
/// for this IniData instance.
/// </summary>
public SectionCollection Sections { get; set; }
/// <summary>
/// Deletes all data
/// </summary>
public void Clear()
{
Global.Clear();
Sections.Clear();
}
/// <summary>
/// Deletes all comments in all sections and properties values
/// </summary>
public void ClearAllComments()
{
Global.ClearComments();
foreach (var section in Sections)
{
section.ClearComments();
section.Properties.ClearComments();
}
}
/// <summary>
/// Merges the other iniData into this one by overwriting existing values.
/// Comments get appended.
/// </summary>
/// <param name="toMergeIniData">
/// IniData instance to merge into this.
/// If it is null this operation does nothing.
/// </param>
public void Merge(IniData toMergeIniData)
{
if (toMergeIniData == null) return;
Global.Merge(toMergeIniData.Global);
Sections.Merge(toMergeIniData.Sections);
}
#region IDeelCloneable<T> 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 IniData DeepClone()
{
return new IniData(this);
}
#endregion
#region Fields
/// <summary>
/// See property <see cref="Configuration"/> for more information.
/// </summary>
private IniParserConfiguration _configuration;
/// <summary>
/// Represents all sections from an INI file
/// </summary>
protected IniScheme _scheme;
#endregion
}
}