-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStreamIniDataParser.cs
95 lines (83 loc) · 3.16 KB
/
StreamIniDataParser.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
using System;
using System.IO;
using IniParser.Model;
using IniParser.Parser;
using IniParser.Model.Formatting;
namespace IniParser
{
/// <summary>
/// Represents an INI data parser for streams.
/// </summary>
public class StreamIniDataParser
{
/// <summary>
/// This instance will handle ini data parsing and writing
/// </summary>
public IniDataParser Parser { get; protected set; }
/// <summary>
/// Ctor
/// </summary>
public StreamIniDataParser() : this (new IniDataParser()) {}
/// <summary>
/// Ctor
/// </summary>
/// <param name="parser"></param>
public StreamIniDataParser(IniDataParser parser)
{
Parser = parser;
}
#region Public Methods
/// <summary>
/// Reads data in INI format from a stream.
/// </summary>
/// <param name="reader">Reader stream.</param>
/// <returns>
/// And <see cref="IniData"/> instance with the readed ini data parsed.
/// </returns>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="reader"/> is <c>null</c>.
/// </exception>
public IniData ReadData(StreamReader reader)
{
if (reader == null)
throw new ArgumentNullException("reader");
return Parser.Parse(reader.ReadToEnd());
}
/// <summary>
/// Writes the ini data to a stream.
/// </summary>
/// <param name="writer">A write stream where the ini data will be stored</param>
/// <param name="iniData">An <see cref="IniData"/> instance.</param>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="writer"/> is <c>null</c>.
/// </exception>
public void WriteData(StreamWriter writer, IniData iniData)
{
if (iniData == null)
throw new ArgumentNullException("iniData");
if (writer == null)
throw new ArgumentNullException("writer");
writer.Write(iniData.ToString());
}
/// <summary>
/// Writes the ini data to a stream.
/// </summary>
/// <param name="writer">A write stream where the ini data will be stored</param>
/// <param name="iniData">An <see cref="IniData"/> instance.</param>
/// <param name="formatter">Formaterr instance that controls how the ini data is transformed to a string</param>
/// <exception cref="ArgumentNullException">
/// Thrown if <paramref name="writer"/> is <c>null</c>.
/// </exception>
public void WriteData(StreamWriter writer, IniData iniData, IIniDataFormatter formatter)
{
if (formatter == null)
throw new ArgumentNullException("formatter");
if (iniData == null)
throw new ArgumentNullException("iniData");
if (writer == null)
throw new ArgumentNullException("writer");
writer.Write(iniData.ToString(formatter));
}
#endregion
}
}