//******************************************************************************************************
// UnixIniFile.cs - Gbtc
//
// Copyright © 2012, Grid Protection Alliance. All Rights Reserved.
//
// Licensed to the Grid Protection Alliance (GPA) under one or more contributor license agreements. See
// the NOTICE file distributed with this work for additional information regarding copyright ownership.
// The GPA licenses this file to you under the MIT License (MIT), the "License"; you may
// not use this file except in compliance with the License. You may obtain a copy of the License at:
//
// http://www.opensource.org/licenses/MIT
//
// Unless agreed to in writing, the subject software distributed under the License is distributed on an
// "AS-IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. Refer to the
// License for the specific language governing permissions and limitations.
//
// Code Modification History:
// ----------------------------------------------------------------------------------------------------
// 08/25/2013 - J. Ritchie Carroll
// Made INI file work for both Mono and Windows based implementations.
//
//******************************************************************************************************
using System;
using System.Collections.Concurrent;
using System.Collections.Generic;
using System.IO;
using System.Linq;
namespace GSF.Interop
{
///
/// Represents a Unix INI style configuration file.
///
internal class UnixIniFile : IIniFile
{
#region [ Members ]
// Fields
private string m_fileName;
private readonly ConcurrentDictionary> m_iniData;
#endregion
#region [ Constructors ]
///
/// Creates a new using the specified INI file name.
///
/// Specified INI file name to use.
public UnixIniFile(string fileName)
{
m_fileName = fileName;
m_iniData = new ConcurrentDictionary>(StringComparer.CurrentCultureIgnoreCase);
Load();
}
#endregion
#region [ Properties ]
///
/// File name of the INI file.
///
public string FileName
{
get
{
return m_fileName;
}
set
{
m_fileName = value;
Load();
}
}
///
/// Gets the value of the specified key.
///
/// Section key exists in.
/// Name of key.
/// Default value of key.
/// Value of key.
/// This is the default member of this class.
public string this[string section, string entry, string defaultValue]
{
get
{
ConcurrentDictionary iniSection = m_iniData.GetOrAdd(section, CreateNewSection);
return iniSection.GetOrAdd(entry, defaultValue);
}
}
///
/// Gets or sets the value of the specified key.
///
/// Section key exists in.
/// Name of key.
/// The new key value to store in the INI file.
/// Value of key.
/// This is the default member of this class.
public string this[string section, string entry]
{
set
{
ConcurrentDictionary iniSection = m_iniData.GetOrAdd(section, CreateNewSection);
iniSection[entry] = value;
Save();
}
}
#endregion
#region [ Methods ]
///
/// Gets an array of keys from the specified section in the INI file.
///
/// Section to retrieve keys from.
/// Array of keys from the specified section of the INI file.
public string[] GetSectionKeys(string section)
{
ConcurrentDictionary sectionEntries;
if (m_iniData.TryGetValue(section, out sectionEntries))
return sectionEntries.Keys.ToArray();
return new string[0];
}
///
/// Gets an array of that section names that exist in the INI file.
///
/// Array of section names from the INI file.
public string[] GetSectionNames()
{
return m_iniData.Keys.ToArray();
}
private void Load()
{
m_iniData.Clear();
if (!File.Exists(m_fileName))
return;
using (StreamReader reader = new StreamReader(m_fileName))
{
string line = reader.ReadLine();
ConcurrentDictionary section = null;
while ((object)line != null)
{
line = IniFile.RemoveComments(line);
if (line.Length > 0)
{
// Check for new section
int startBracketIndex = line.IndexOf('[');
if (startBracketIndex == 0)
{
int endBracketIndex = line.IndexOf(']');
if (endBracketIndex > 1)
{
string sectionName = line.Substring(startBracketIndex + 1, endBracketIndex - 1);
if (!string.IsNullOrEmpty(sectionName))
section = m_iniData.GetOrAdd(sectionName, CreateNewSection);
}
}
if ((object)section == null)
throw new InvalidOperationException("INI file did not begin with a [section]");
// Check for key/value pair
int equalsIndex = line.IndexOf("=", StringComparison.Ordinal);
if (equalsIndex > 0)
{
string key = line.Substring(0, equalsIndex).Trim();
if (!string.IsNullOrEmpty(key))
section[key] = line.Substring(equalsIndex + 1).Trim();
}
}
line = reader.ReadLine();
}
}
}
private void Save()
{
// Saving INI file will strip comments - sorry :-(
using (StreamWriter writer = new StreamWriter(m_fileName))
{
foreach (KeyValuePair> section in m_iniData)
{
writer.WriteLine("[{0}]", section.Key);
foreach (KeyValuePair entry in section.Value)
{
writer.WriteLine("{0} = {1}", entry.Key, entry.Value);
}
writer.WriteLine();
}
}
}
private ConcurrentDictionary CreateNewSection(string sectionName)
{
return new ConcurrentDictionary(StringComparer.OrdinalIgnoreCase);
}
#endregion
}
}