You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
Zero/ZeroLevel/Services/Config/Implementation/IniFileReader.cs

126 lines
4.5 KiB

This file contains ambiguous Unicode characters!

This file contains ambiguous Unicode characters that may be confused with others in your current locale. If your use case is intentional and legitimate, you can safely ignore this warning. Use the Escape button to highlight these characters.

using System;
using System.Globalization;
using System.IO;
namespace ZeroLevel.Services.Config.Implementation
{
/// <summary>
/// Чтение конфигурации из ini файла
/// </summary>
internal sealed class IniFileReader
: IConfigurationReader
{
private readonly string _iniPath;
internal IniFileReader(string configPath)
{
if (String.IsNullOrWhiteSpace(configPath))
throw new ArgumentNullException("configPath", "Не указан путь к конфигурационному файлу");
if (!File.Exists(configPath))
{
configPath = Path.Combine(Configuration.BaseDirectory, configPath);
if (!File.Exists(configPath))
{
throw new FileNotFoundException("Не существует конфигурационный файл: " + configPath);
}
}
_iniPath = configPath;
}
public IConfiguration ReadConfiguration()
{
var result = Configuration.Create();
string sectionName = null;
foreach (var line in File.ReadAllLines(_iniPath))
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
int index = line.IndexOf('=');
string key;
string originalKey;
string value;
if (index >= 0)
{
originalKey = line.Substring(0, index).Trim();
key = originalKey.ToLower(CultureInfo.InvariantCulture);
value = line.Substring(index + 1, line.Length - index - 1).Trim();
}
else
{
originalKey = line.Trim();
key = originalKey.ToLower(CultureInfo.InvariantCulture);
value = string.Empty;
}
if (key[0].Equals(';') || key[0].Equals('#'))
continue;
if (string.IsNullOrEmpty(value) && key[0].Equals('[') && key[key.Length - 1].Equals(']'))
{
sectionName = originalKey.Trim('[', ']');
}
else
{
if (!string.IsNullOrEmpty(sectionName))
{
result.Append(string.Format("{0}.{1}", sectionName, key), value);
}
else
{
result.Append(key, value);
}
}
}
return result;
}
public IConfigurationSet ReadConfigurationSet()
{
var result = Configuration.CreateSet();
string sectionName = null;
foreach (var line in File.ReadAllLines(_iniPath))
{
if (string.IsNullOrWhiteSpace(line))
{
continue;
}
int index = line.IndexOf('=');
string key;
string originalKey;
string value;
if (index >= 0)
{
originalKey = line.Substring(0, index).Trim();
key = originalKey.ToLower(CultureInfo.InvariantCulture);
value = line.Substring(index + 1, line.Length - index - 1).Trim();
}
else
{
originalKey = line.Trim();
key = originalKey.ToLower(CultureInfo.InvariantCulture);
value = string.Empty;
}
if (key[0].Equals(';') || key[0].Equals('#'))
continue;
if (string.IsNullOrEmpty(value) && key[0].Equals('[') && key[key.Length - 1].Equals(']'))
{
sectionName = originalKey.Trim('[', ']');
}
else
{
if (!string.IsNullOrEmpty(sectionName))
{
var currentSection = (false == result.ContainsSection(sectionName)) ? result.CreateSection(sectionName) : result.GetSection(sectionName);
currentSection.Append(key, value);
}
else
{
result.Default.Append(key, value);
}
}
}
return result;
}
}
}

Powered by TurnKey Linux.