File: IniConfigSource.cs

package info (click to toggle)
nini 1.1.0%2Bdfsg.3-2
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 880 kB
  • sloc: cs: 7,649; xml: 2,945; makefile: 28; ansic: 7
file content (329 lines) | stat: -rw-r--r-- 8,650 bytes parent folder | download | duplicates (7)
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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
#region Copyright
//
// Nini Configuration Project.
// Copyright (C) 2006 Brent R. Matzelle.  All rights reserved.
//
// This software is published under the terms of the MIT X11 license, a copy of 
// which has been included with this distribution in the LICENSE.txt file.
// 
#endregion

using System;
using System.IO;
using System.Collections;
using Nini.Ini;

namespace Nini.Config
{
	/// <include file='IniConfigSource.xml' path='//Class[@name="IniConfigSource"]/docs/*' />
	public class IniConfigSource : ConfigSourceBase
	{
		#region Private variables
		IniDocument iniDocument = null;
		string savePath = null;
		bool caseSensitive = true;
		#endregion
		
		#region Public properties
		#endregion

		#region Constructors
		/// <include file='IniConfigSource.xml' path='//Constructor[@name="Constructor"]/docs/*' />
		public IniConfigSource ()
		{
			iniDocument = new IniDocument ();
		}

		/// <include file='IniConfigSource.xml' path='//Constructor[@name="ConstructorPath"]/docs/*' />
		public IniConfigSource (string filePath)
		{
			Load (filePath);
		}
		
		/// <include file='IniConfigSource.xml' path='//Constructor[@name="ConstructorTextReader"]/docs/*' />
		public IniConfigSource (TextReader reader)
		{
			Load (reader);
		}

		/// <include file='IniConfigSource.xml' path='//Constructor[@name="ConstructorIniDocument"]/docs/*' />
		public IniConfigSource (IniDocument document)
		{
			Load (document);
		}
		
		/// <include file='IniConfigSource.xml' path='//Constructor[@name="ConstructorStream"]/docs/*' />
		public IniConfigSource (Stream stream)
		{
			Load (stream);
		}
		#endregion
		
		#region Public properties
		/// <include file='IniConfigSource.xml' path='//Property[@name="CaseSensitive"]/docs/*' />
		public bool CaseSensitive
		{
			get { return caseSensitive; }
			set { caseSensitive = value; }
		}

		/// <include file='IniConfigSource.xml' path='//Property[@name="SavePath"]/docs/*' />
		public string SavePath
		{
			get { return savePath; }
		}
		#endregion
		
		#region Public methods
		/// <include file='IniConfigSource.xml' path='//Method[@name="LoadPath"]/docs/*' />
		public void Load (string filePath)
		{
			Load (new StreamReader (filePath));
			this.savePath = filePath;
		}
		
		/// <include file='IniConfigSource.xml' path='//Method[@name="LoadTextReader"]/docs/*' />
		public void Load (TextReader reader)
		{
			Load (new IniDocument (reader));
		}

		/// <include file='IniConfigSource.xml' path='//Method[@name="LoadIniDocument"]/docs/*' />
		public void Load (IniDocument document)
		{
			this.Configs.Clear ();

			this.Merge (this); // required for SaveAll
			iniDocument = document;
			Load ();
		}
		
		/// <include file='IniConfigSource.xml' path='//Method[@name="LoadStream"]/docs/*' />
		public void Load (Stream stream)
		{
			Load (new StreamReader (stream));
		}

		/// <include file='IniConfigSource.xml' path='//Method[@name="Save"]/docs/*' />
		public override void Save ()
		{
			if (!IsSavable ()) {
				throw new ArgumentException ("Source cannot be saved in this state");
			}

			MergeConfigsIntoDocument ();
			
			iniDocument.Save (this.savePath);
			base.Save ();
		}
		
		/// <include file='IniConfigSource.xml' path='//Method[@name="SavePath"]/docs/*' />
		public void Save (string path)
		{
			this.savePath = path;
			this.Save ();
		}
		
		/// <include file='IniConfigSource.xml' path='//Method[@name="SaveTextWriter"]/docs/*' />
		public void Save (TextWriter writer)
		{
			MergeConfigsIntoDocument ();
			iniDocument.Save (writer);
			savePath = null;
			OnSaved (new EventArgs ());
		}

		/// <include file='IniConfigSource.xml' path='//Method[@name="SaveStream"]/docs/*' />
		public void Save (Stream stream)
		{
			MergeConfigsIntoDocument ();
			iniDocument.Save (stream);
			savePath = null;
			OnSaved (new EventArgs ());
		}

		/// <include file='IConfigSource.xml' path='//Method[@name="Reload"]/docs/*' />
		public override void Reload ()
		{
			if (savePath == null) {
				throw new ArgumentException ("Error reloading: You must have "
							+ "the loaded the source from a file");
			}

			iniDocument = new IniDocument (savePath);
			MergeDocumentIntoConfigs ();
			base.Reload ();
		}

		/// <include file='IniConfigSource.xml' path='//Method[@name="ToString"]/docs/*' />
		public override string ToString ()
		{
			MergeConfigsIntoDocument ();
			StringWriter writer = new StringWriter ();
			iniDocument.Save (writer);

			return writer.ToString ();
		}
		#endregion
		
		#region Private methods
		/// <summary>
		/// Merges all of the configs from the config collection into the 
		/// IniDocument before it is saved.  
		/// </summary>
		private void MergeConfigsIntoDocument ()
		{
			RemoveSections ();
			foreach (IConfig config in this.Configs)
			{
				string[] keys = config.GetKeys ();

				// Create a new section if one doesn't exist
				if (iniDocument.Sections[config.Name] == null) {
					IniSection section = new IniSection (config.Name);
					iniDocument.Sections.Add (section);
				}
				RemoveKeys (config.Name);

				for (int i = 0; i < keys.Length; i++)
				{
					iniDocument.Sections[config.Name].Set (keys[i], config.Get (keys[i]));
				}
			}
		}

		/// <summary>
		/// Removes all INI sections that were removed as configs.
		/// </summary>
		private void RemoveSections ()
		{
			IniSection section = null;
			for (int i = 0; i < iniDocument.Sections.Count; i++)
			{
				section = iniDocument.Sections[i];
				if (this.Configs[section.Name] == null) {
					iniDocument.Sections.Remove (section.Name);
				}
			}
		}

		/// <summary>
		/// Removes all INI keys that were removed as config keys.
		/// </summary>
		private void RemoveKeys (string sectionName)
		{
			IniSection section = iniDocument.Sections[sectionName];

			if (section != null) {
				foreach (string key in section.GetKeys ())
				{
					if (this.Configs[sectionName].Get (key) == null) {
						section.Remove (key);
					}
				}
			}
		}

		/// <summary>
		/// Loads the configuration file.
		/// </summary>
		private void Load ()
		{
			IniConfig config = null;
			IniSection section = null;
			IniItem item = null;

			for (int j = 0; j < iniDocument.Sections.Count; j++)
			{
				section = iniDocument.Sections[j];
				config = new IniConfig (section.Name, this);

				for (int i = 0; i < section.ItemCount; i++)
				{
					item = section.GetItem (i);
					
					if  (item.Type == IniType.Key) {
						config.Add (item.Name, item.Value);
					}
				}
				
				this.Configs.Add (config);
			}
		}

		/// <summary>
		/// Merges the IniDocument into the Configs when the document is 
		/// reloaded.  
		/// </summary>
		private void MergeDocumentIntoConfigs ()
		{
			// Remove all missing configs first
			RemoveConfigs ();

			IniSection section = null;
			for (int i = 0; i < iniDocument.Sections.Count; i++)
			{
				section = iniDocument.Sections[i];

				IConfig config = this.Configs[section.Name];
				if (config == null) {
					// The section is new so add it
					config = new ConfigBase (section.Name, this);
					this.Configs.Add (config);
				}				
				RemoveConfigKeys (config);
			}
		}

		/// <summary>
		/// Removes all configs that are not in the newly loaded INI doc.  
		/// </summary>
		private void RemoveConfigs ()
		{
			IConfig config = null;
			for (int i = this.Configs.Count - 1; i > -1; i--)
			{
				config = this.Configs[i];
				// If the section is not present in the INI doc
				if (iniDocument.Sections[config.Name] == null) {
					this.Configs.Remove (config);
				}
			}
		}

		/// <summary>
		/// Removes all INI keys that were removed as config keys.
		/// </summary>
		private void RemoveConfigKeys (IConfig config)
		{
			IniSection section = iniDocument.Sections[config.Name];

			// Remove old keys
			string[] configKeys = config.GetKeys ();
			foreach (string configKey in configKeys)
			{
				if (!section.Contains (configKey)) {
					// Key doesn't exist, remove
					config.Remove (configKey);
				}
			}

			// Add or set all new keys
			string[] keys = section.GetKeys ();
			for (int i = 0; i < keys.Length; i++)
			{
				string key = keys[i];
				config.Set (key, section.GetItem (i).Value);
			}
		}

		/// <summary>
		/// Returns true if this instance is savable.
		/// </summary>
		private bool IsSavable ()
		{
			return (this.savePath != null);
		}
		#endregion
	}
}