//******************************************************************************************************
// IniFile.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:
// ----------------------------------------------------------------------------------------------------
// 11/12/2004 - J. Ritchie Carroll
// Initial version of source generated.
// 01/05/2006 - J. Ritchie Carroll
// 2.0 version of source code migrated from 1.1 source (GSF.Interop.Windows.IniFile).
// 01/05/2007 - J. Ritchie Carroll
// Breaking change: Renamed "IniFileName" property to "FileName".
// Updated "SectionNames" to use List(Of String) instead of ArrayList.
// 09/10/2008 - J. Ritchie Carroll
// Converted to C#.
// 04/01/2009 - J. Ritchie Carroll
// Added "GetSectionKeys" to enumerate keys of a specified section.
// 08/05/2009 - Josh L. Patterson
// Edited Comments.
// 09/14/2009 - Stephen C. Wills
// Added new header and license agreement.
// 09/21/2011 - J. Ritchie Carroll
// Excluded class from Mono deployments due to P/Invoke requirements.
// 12/14/2012 - Starlynn Danyelle Gilliam
// Modified Header.
// 08/25/2013 - J. Ritchie Carroll
// Made INI file work for both Mono and Windows based implementations.
//
//******************************************************************************************************
namespace GSF.Interop
{
///
/// Represents a Windows INI style configuration file.
///
public class IniFile
{
#region [ Members ]
// Fields
private readonly IIniFile m_iniFile;
#endregion
#region [ Constructors ]
///
/// Creates a new .
///
/// INI file name defaults to "Win.ini" - change using FileName property.
public IniFile()
: this("Win.ini")
{
}
///
/// Creates a new using the specified INI file name.
///
/// Specified INI file name to use.
public IniFile(string fileName) =>
m_iniFile = Common.IsPosixEnvironment ?
new UnixIniFile(fileName) :
new WindowsIniFile(fileName);
#endregion
#region [ Properties ]
///
/// File name of the INI file.
///
public string FileName
{
get => m_iniFile.FileName;
set => m_iniFile.FileName = value;
}
///
/// 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] =>
m_iniFile[section, 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]
{
get => this[section, entry, null];
set => m_iniFile[section, entry] = value;
}
#endregion
#region [ Methods ]
///
/// Gets the value of the specified key.
///
/// Section key exists in.
/// Name of key.
/// Value of key.
public string GetKeyValue(string section, string entry) =>
this[section, entry, null];
///
/// Gets the value of the specified key.
///
/// Section key exists in.
/// Name of key.
/// Default value of key.
/// Value of key.
public string GetKeyValue(string section, string entry, string defaultValue) =>
this[section, entry, defaultValue];
///
/// Sets the value of the specified key.
///
/// Section key exists in.
/// Name of key.
/// The new key value to store in the INI file.
public void SetKeyValue(string section, string entry, string newValue) =>
this[section, entry] = newValue;
///
/// 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) =>
m_iniFile.GetSectionKeys(section);
///
/// Gets an array of that section names that exist in the INI file.
///
/// Array of section names from the INI file.
public string[] GetSectionNames() =>
m_iniFile.GetSectionNames();
#endregion
#region [ Static ]
// Remove any comments from key value string
internal static string RemoveComments(string keyValue)
{
// Remove any trailing comments from key value
keyValue = keyValue.Trim();
int commentIndex = keyValue.IndexOf(';');
if (commentIndex > -1)
keyValue = keyValue.Substring(0, commentIndex).Trim();
return keyValue;
}
#endregion
}
}