File: IniReader.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 (652 lines) | stat: -rw-r--r-- 15,521 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
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
#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.Text;
using System.Collections;

namespace Nini.Ini
{
	#region IniReadState enumeration
	/// <include file='IniReader.xml' path='//Enum[@name="IniReadState"]/docs/*' />
	public enum IniReadState : int
	{
		/// <include file='IniReader.xml' path='//Enum[@name="IniReadState"]/Value[@name="Closed"]/docs/*' />
		Closed,
		/// <include file='IniReader.xml' path='//Enum[@name="IniReadState"]/Value[@name="EndOfFile"]/docs/*' />
		EndOfFile,
		/// <include file='IniReader.xml' path='//Enum[@name="IniReadState"]/Value[@name="Error"]/docs/*' />
		Error,
		/// <include file='IniReader.xml' path='//Enum[@name="IniReadState"]/Value[@name="Initial"]/docs/*' />
		Initial,
		/// <include file='IniReader.xml' path='//Enum[@name="IniReadState"]/Value[@name="Interactive"]/docs/*' />
		Interactive
	};
	#endregion

	#region IniType enumeration
	/// <include file='IniReader.xml' path='//Enum[@name="IniType"]/docs/*' />
	public enum IniType : int
	{
		/// <include file='IniReader.xml' path='//Enum[@name="IniType"]/Value[@name="Section"]/docs/*' />
		Section,
		/// <include file='IniReader.xml' path='//Enum[@name="IniType"]/Value[@name="Key"]/docs/*' />
		Key,
		/// <include file='IniReader.xml' path='//Enum[@name="IniType"]/Value[@name="Empty"]/docs/*' />
		Empty
	}
	#endregion

	/// <include file='IniReader.xml' path='//Class[@name="IniReader"]/docs/*' />
	public class IniReader : IDisposable
	{
		#region Private variables
		int lineNumber = 1;
		int column = 1;
		IniType iniType = IniType.Empty;
		TextReader textReader = null;
		bool ignoreComments = false;
		StringBuilder name = new StringBuilder ();
		StringBuilder value = new StringBuilder ();
		StringBuilder comment = new StringBuilder ();
		IniReadState readState = IniReadState.Initial;
		bool hasComment = false;
		bool disposed = false;
		bool lineContinuation = false;
		bool acceptCommentAfterKey = true;
		bool acceptNoAssignmentOperator = false;
		bool consumeAllKeyText = false;
		char[] commentDelimiters = new char[] { ';' };
		char[] assignDelimiters = new char[] { '=' };
		#endregion

		#region Public properties
		/// <include file='IniReader.xml' path='//Property[@name="Name"]/docs/*' />
		public string Name
		{
			get { return this.name.ToString (); }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="Value"]/docs/*' />
		public string Value
		{
			get { return this.value.ToString (); }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="Type"]/docs/*' />
		public IniType Type
		{
			get { return iniType; }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="Comment"]/docs/*' />
		public string Comment
		{
			get { return (hasComment) ? this.comment.ToString () : null; }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="LineNumber"]/docs/*' />
		public int LineNumber
		{
			get { return lineNumber; }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="LinePosition"]/docs/*' />
		public int LinePosition
		{
			get { return column; }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="IgnoreComments"]/docs/*' />
		public bool IgnoreComments
		{
			get { return ignoreComments; }
			set { ignoreComments = value; }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="ReadState"]/docs/*' />
		public IniReadState ReadState
		{
			get { return readState; }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="LineContinuation"]/docs/*' />
		public bool LineContinuation
		{
			get { return lineContinuation; }
			set { lineContinuation = value; }
		}
		
		/// <include file='IniReader.xml' path='//Property[@name="AcceptCommentAfterKey"]/docs/*' />
		public bool AcceptCommentAfterKey
		{
			get { return acceptCommentAfterKey; }
			set { acceptCommentAfterKey = value; }
		}

		/// <include file='IniReader.xml' path='//Property[@name="AcceptNoAssignmentOperator"]/docs/*' />
		public bool AcceptNoAssignmentOperator
		{
			get { return acceptNoAssignmentOperator; }
			set { acceptNoAssignmentOperator = value; }
		}

		/// <include file='IniReader.xml' path='//Property[@name="ConsumeAllKeyText"]/docs/*' />
		public bool ConsumeAllKeyText
		{
			get { return consumeAllKeyText; }
			set { consumeAllKeyText = value; }
		}
		#endregion
		
		#region Constructors
		/// <include file='IniReader.xml' path='//Constructor[@name="ConstructorPath"]/docs/*' />
		public IniReader (string filePath)
		{
			textReader = new StreamReader (filePath);
		}
		
		/// <include file='IniReader.xml' path='//Constructor[@name="ConstructorTextReader"]/docs/*' />
		public IniReader (TextReader reader)
		{
			textReader = reader;
		}
		
		/// <include file='IniReader.xml' path='//Constructor[@name="ConstructorStream"]/docs/*' />
		public IniReader (Stream stream)
			: this (new StreamReader (stream))
		{
		}
		#endregion
		
		#region Public methods
		/// <include file='IniReader.xml' path='//Method[@name="Read"]/docs/*' />
		public bool Read ()
		{
			bool result = false;
			
			if (readState != IniReadState.EndOfFile 
				|| readState != IniReadState.Closed) {
				readState = IniReadState.Interactive;
				result = ReadNext ();
			}
			
			return result;
		}
		
		/// <include file='IniReader.xml' path='//Method[@name="MoveToNextSection"]/docs/*' />
		public bool MoveToNextSection ()
		{
			bool result = false;
			
			while (true)
			{
				result = Read ();

				if (iniType == IniType.Section || !result) {
					break;
				}
			}
			
			return result;
		}
		
		/// <include file='IniReader.xml' path='//Method[@name="MoveToNextKey"]/docs/*' />
		public bool MoveToNextKey ()
		{
			bool result = false;
			
			while (true)
			{
				result = Read ();

				if (iniType == IniType.Section) {
					result = false;
					break;
				}
				if (iniType == IniType.Key || !result) {
					break;
				}
			}
			
			return result;
		}
		
		/// <include file='IniReader.xml' path='//Method[@name="Close"]/docs/*' />
		public void Close ()
		{
			Reset ();
			readState = IniReadState.Closed;
			
			if (textReader != null) {
				textReader.Close ();
			}
		}

		/// <include file='IniReader.xml' path='//Method[@name="Dispose"]/docs/*' />
		public void Dispose ()
		{
			Dispose (true);
		}
		
		/// <include file='IniReader.xml' path='//Method[@name="GetCommentDelimiters"]/docs/*' />
		public char[] GetCommentDelimiters ()
		{
			char[] result = new char[commentDelimiters.Length];
			Array.Copy (commentDelimiters, 0, result, 0, commentDelimiters.Length);

			return result;
		}
		
		/// <include file='IniReader.xml' path='//Method[@name="SetCommentDelimiters"]/docs/*' />
		public void SetCommentDelimiters (char[] delimiters)
		{
			if (delimiters.Length < 1) {
				throw new ArgumentException ("Must supply at least one delimiter");
			}
			
			commentDelimiters = delimiters;
		}
		
		/// <include file='IniReader.xml' path='//Method[@name="GetAssignDelimiters"]/docs/*' />
		public char[] GetAssignDelimiters ()
		{
			char[] result = new char[assignDelimiters.Length];
			Array.Copy (assignDelimiters, 0, result, 0, assignDelimiters.Length);

			return result;
		}
		
		/// <include file='IniReader.xml' path='//Method[@name="SetAssignDelimiters"]/docs/*' />
		public void SetAssignDelimiters (char[] delimiters)
		{
			if (delimiters.Length < 1) {
				throw new ArgumentException ("Must supply at least one delimiter");
			}
			
			assignDelimiters = delimiters;
		}
		#endregion
		
		#region Protected methods
		/// <include file='IniReader.xml' path='//Method[@name="DisposeBoolean"]/docs/*' />
		protected virtual void Dispose (bool disposing)
		{
			if (!disposed) {
				textReader.Close ();
				disposed = true;

				if (disposing) {
					GC.SuppressFinalize (this);
				}
			}
		}
		#endregion
		
		#region Private methods
		/// <summary>
		/// Destructor.
		/// </summary>
		~IniReader ()
		{
			Dispose (false);
		}

		/// <summary>
		/// Resets all of the current INI line data.
		/// </summary>
		private void Reset ()
		{
			this.name.Remove (0, this.name.Length);
			this.value.Remove (0, this.value.Length);
			this.comment.Remove (0, this.comment.Length);
			iniType = IniType.Empty;
			hasComment = false;
		}
		
		/// <summary>
		/// Reads the next INI line item.
		/// </summary>
		private bool ReadNext ()
		{
			bool result = true;
			int ch = PeekChar ();
			Reset ();
			
			if (IsComment (ch)) {
				iniType = IniType.Empty;
				ReadChar (); // consume comment character
				ReadComment ();

				return result;
			}

			switch (ch)
			{
				case ' ':
				case '\t':
				case '\r':
					SkipWhitespace ();
					ReadNext ();
					break;
				case '\n':
					ReadChar ();
					break;
				case '[':
					ReadSection ();
					break;
				case -1:
					readState = IniReadState.EndOfFile;
					result = false;
					break;
				default:
					ReadKey ();
					break;
			}
			
			return result;
		}
		
		/// <summary>
		/// Reads a comment. Must start after the comment delimiter.
		/// </summary>
		private void ReadComment  ()
		{
			int ch = -1;
			SkipWhitespace ();
			hasComment = true;

			do
			{
				ch = ReadChar ();
				this.comment.Append ((char)ch);
			} while (!EndOfLine (ch));
			
			RemoveTrailingWhitespace (this.comment);
		}
		
		/// <summary>
		/// Removes trailing whitespace from a StringBuilder.
		/// </summary>
		private void RemoveTrailingWhitespace (StringBuilder builder)
		{
			string temp = builder.ToString ();
		
			builder.Remove (0, builder.Length);
			builder.Append (temp.TrimEnd (null));
		}
		
		/// <summary>
		/// Reads a key.
		/// </summary>
		private void ReadKey ()
		{
			int ch = -1;
			iniType = IniType.Key;
			
			while (true)
			{
				ch = PeekChar ();

				if (IsAssign (ch)) {
					ReadChar ();
					break;
				}
				
				if (EndOfLine (ch)) {
					if (acceptNoAssignmentOperator) {
						break;
					}
					throw new IniException (this, 
						String.Format ("Expected assignment operator ({0})", 
										assignDelimiters[0]));
				}

				this.name.Append ((char)ReadChar ());
			}
			
			ReadKeyValue ();
			SearchForComment ();
			RemoveTrailingWhitespace (this.name);
		}
		
		/// <summary>
		/// Reads the value of a key.
		/// </summary>
		private void ReadKeyValue ()
		{
			int ch = -1;
			bool foundQuote = false;
			int characters = 0;
			SkipWhitespace ();

			while (true)
			{
				ch = PeekChar ();

				if (!IsWhitespace (ch)) {
					characters++;
				}
				
				if (!this.ConsumeAllKeyText && ch == '"') {
					ReadChar ();

					if (!foundQuote && characters == 1) {				
						foundQuote = true;
						continue;
					} else {
						break;
					}
				}
				
				if (foundQuote && EndOfLine (ch)) {
					throw new IniException (this, "Expected closing quote (\")");
				}
				
				// Handle line continuation
				if (lineContinuation && ch == '\\') 
				{
					StringBuilder buffer = new StringBuilder ();
					buffer.Append ((char)ReadChar ()); // append '\'
					
					while (PeekChar () != '\n' && IsWhitespace (PeekChar ()))
					{
						if (PeekChar () != '\r') {
							buffer.Append ((char)ReadChar ());
						} else {
							ReadChar (); // consume '\r'
						}
					}
					
					if (PeekChar () == '\n') {
						// continue reading key value on next line
						ReadChar ();
						continue;
					} else {
						// Replace consumed characters
						this.value.Append (buffer.ToString ());
					}
				}

				if (!this.ConsumeAllKeyText) {
					// If accepting comments then don't consume as key value
					if (acceptCommentAfterKey && IsComment (ch) && !foundQuote) {
						break;
					}
				}

				// Always break at end of line
				if (EndOfLine (ch)) {
					break;
				}

				this.value.Append ((char)ReadChar ());
			}
			
			if (!foundQuote) {
				RemoveTrailingWhitespace (this.value);
			}
		}
		
		/// <summary>
		/// Reads an INI section.
		/// </summary>
		private void ReadSection ()
		{
			int ch = -1;
			iniType = IniType.Section;
			ch = ReadChar (); // consume "["

			while (true)
			{
				ch = PeekChar ();
				if (ch == ']') {
					break;
				}
				if (EndOfLine (ch)) {
					throw new IniException (this, "Expected section end (])");
				}

				this.name.Append ((char)ReadChar ());
			}

			ConsumeToEnd (); // all after '[' is garbage			
			RemoveTrailingWhitespace (this.name);
		}
		
		/// <summary>
		/// Looks for a comment.
		/// </summary>
		private void SearchForComment ()
		{
			int ch = ReadChar ();
			
			while (!EndOfLine (ch))
			{
				if (IsComment (ch)) {
					if (ignoreComments) {
						ConsumeToEnd ();
					} else {
						ReadComment ();
					}
					break;
				}
				ch = ReadChar ();
			}
		}

		/// <summary>
		/// Consumes all data until the end of a line. 
		/// </summary>		
		private void ConsumeToEnd ()
		{
			int ch = -1;

			do
			{
				ch = ReadChar ();
			} while (!EndOfLine (ch));
		}
		
		/// <summary>
		/// Returns and consumes the next character from the stream.
		/// </summary>
		private int ReadChar ()
		{
			int result = textReader.Read ();
			
			if (result == '\n') {
				lineNumber++;
				column = 1;
			} else {
				column++;
			}
			
			return result;
		}
		
		/// <summary>
		/// Returns the next upcoming character from the stream.
		/// </summary>
		private int PeekChar ()
		{
			return textReader.Peek ();
		}
		
		/// <summary>
		/// Returns true if a comment character is found.
		/// </summary>
		private bool IsComment (int ch)
		{
			return HasCharacter (commentDelimiters, ch);
		}
		
		/// <summary>
		/// Returns true if character is an assign character.
		/// </summary>
		private bool IsAssign (int ch)
		{
			return HasCharacter (assignDelimiters, ch);
		}

		/// <summary>
		/// Returns true if the character is found in the given array.
		/// </summary>
		private bool HasCharacter (char[] characters, int ch)
		{
			bool result = false;
			
			for (int i = 0; i < characters.Length; i++)
			{
				if (ch == characters[i]) 
				{
					result = true;
					break;
				}
			}
			
			return result;
		}
		
		/// <summary>
		/// Returns true if a value is whitespace.
		/// </summary>
		private bool IsWhitespace (int ch)
		{
			return ch == 0x20 || ch == 0x9 || ch == 0xD || ch == 0xA;
		}
		
		/// <summary>
		/// Skips all whitespace.
		/// </summary>
		private void SkipWhitespace ()
		{
			while (IsWhitespace (PeekChar ()))
			{
				if (EndOfLine (PeekChar ())) {
					break;
				}

				ReadChar ();
			}
		}

		/// <summary>
		/// Returns true if an end of line is found.  End of line
		/// includes both an end of line or end of file.
		/// </summary>
		private bool EndOfLine (int ch)
		{
			return (ch == '\n' || ch == -1);
		}
		#endregion
	}
}