-
Notifications
You must be signed in to change notification settings - Fork 4.6k
/
IniStreamConfigurationProvider.cs
96 lines (85 loc) · 3.6 KB
/
IniStreamConfigurationProvider.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
// Licensed to the .NET Foundation under one or more agreements.
// The .NET Foundation licenses this file to you under the MIT license.
using System;
using System.Collections.Generic;
using System.IO;
namespace Microsoft.Extensions.Configuration.Ini
{
/// <summary>
/// An INI file based <see cref="StreamConfigurationProvider"/>.
/// </summary>
public class IniStreamConfigurationProvider : StreamConfigurationProvider
{
/// <summary>
/// Constructor.
/// </summary>
/// <param name="source">The <see cref="IniStreamConfigurationSource"/>.</param>
public IniStreamConfigurationProvider(IniStreamConfigurationSource source) : base(source) { }
/// <summary>
/// Read a stream of INI values into a key/value dictionary.
/// </summary>
/// <param name="stream">The stream of INI data.</param>
/// <returns>The <see cref="IDictionary{String, String}"/> which was read from the stream.</returns>
public static IDictionary<string, string?> Read(Stream stream)
{
var data = new Dictionary<string, string?>(StringComparer.OrdinalIgnoreCase);
using (var reader = new StreamReader(stream))
{
string sectionPrefix = string.Empty;
while (reader.Peek() != -1)
{
string rawLine = reader.ReadLine()!; // Since Peak didn't return -1, stream hasn't ended.
string line = rawLine.Trim();
// Ignore blank lines
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
// Ignore comments
if (line[0] is ';' or '#' or '/')
{
continue;
}
// [Section:header]
if (line[0] == '[' && line[line.Length - 1] == ']')
{
// remove the brackets
#if NET
sectionPrefix = string.Concat(line.AsSpan(1, line.Length - 2).Trim(), ConfigurationPath.KeyDelimiter);
#else
sectionPrefix = line.Substring(1, line.Length - 2).Trim() + ConfigurationPath.KeyDelimiter;
#endif
continue;
}
// key = value OR "value"
int separator = line.IndexOf('=');
if (separator < 0)
{
throw new FormatException(SR.Format(SR.Error_UnrecognizedLineFormat, rawLine));
}
string key = sectionPrefix + line.Substring(0, separator).Trim();
string value = line.Substring(separator + 1).Trim();
// Remove quotes
if (value.Length > 1 && value[0] == '"' && value[value.Length - 1] == '"')
{
value = value.Substring(1, value.Length - 2);
}
if (data.ContainsKey(key))
{
throw new FormatException(SR.Format(SR.Error_KeyIsDuplicated, key));
}
data[key] = value;
}
}
return data;
}
/// <summary>
/// Loads INI configuration key/values from a stream into a provider.
/// </summary>
/// <param name="stream">The <see cref="Stream"/> to load ini configuration data from.</param>
public override void Load(Stream stream)
{
Data = Read(stream);
}
}
}