using System;
using System.IO;
using System.Diagnostics;
namespace MadMilkman.Ini
{
///
/// In-memory representation of an INI file.
///
///
/// is a central class of MadMilkman.Ini component.
/// To define an INI file's format use object.
/// To load (read) an INI file from a file's path or a stream use IniFile.Load methods.
/// To save (write) an INI file to a file's path or a stream use IniFile.Save methods.
/// To view INI file's structure representation see IniFile's Content Hierarchy Diagram.
///
/// Overview
/// INI file format on Wikipedia.
public sealed class IniFile
{
internal readonly IniOptions options;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IniSectionCollection sections;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private IniValueMappings valueMappings;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private IniValueBinding valueBinding;
///
/// Initializes a new instance of class.
///
public IniFile() : this(new IniOptions()) { }
///
/// Initializes a new instance of class.
///
/// object that defines INI file's format, settings for both Load and Save methods.
public IniFile(IniOptions options)
{
if (options == null)
throw new ArgumentNullException("options");
this.options = new IniOptions(options);
this.sections = new IniSectionCollection(this, options.SectionDuplicate, options.SectionNameCaseSensitive);
}
///
/// Gets file's sections.
///
public IniSectionCollection Sections { get { return this.sections; } }
///
/// Gets the mappings of s and their results, used in methods.
///
public IniValueMappings ValueMappings
{
get
{
if (this.valueMappings == null)
this.valueMappings = new IniValueMappings();
return this.valueMappings;
}
}
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal bool HasValueMappings { get { return this.valueMappings != null; } }
///
/// Gets the object that exposes binding operations, which are executed with methods.
///
public IniValueBinding ValueBinding
{
get
{
if (this.valueBinding == null)
this.valueBinding = new IniValueBinding(this);
return this.valueBinding;
}
}
///
/// Loads a file from a path.
///
/// Path from which to load a file.
public void Load(string filePath)
{
if (filePath == null)
throw new ArgumentNullException("filePath");
using (Stream fileStream = File.Open(filePath, FileMode.Open, FileAccess.Read, FileShare.ReadWrite))
this.Load(fileStream);
}
///
/// Loads a file from a stream.
///
/// Stream from which to load a file.
public void Load(Stream fileStream)
{
if (fileStream == null)
throw new ArgumentNullException("fileStream");
this.Load(new StreamReader(fileStream, this.options.Encoding));
if (fileStream.CanSeek)
fileStream.Seek(0, SeekOrigin.Begin);
}
///
/// Loads a file from a reader.
///
/// Reader from which to load a file.
public void Load(TextReader fileReader)
{
if (fileReader == null)
throw new ArgumentNullException("fileReader");
new IniReader(this.options).Read(this, fileReader);
}
///
/// Saves a file to a path.
///
/// Path to which to save a file.
public void Save(string filePath)
{
if (filePath == null)
throw new ArgumentNullException("filePath");
using (Stream fileStream = File.Create(filePath))
this.Save(fileStream);
}
///
/// Saves a file to a stream.
///
/// Stream to which to save a file.
public void Save(Stream fileStream)
{
if (fileStream == null)
throw new ArgumentNullException("fileStream");
this.Save(new StreamWriter(fileStream, this.options.Encoding));
if (fileStream.CanSeek)
fileStream.Seek(0, SeekOrigin.Begin);
}
///
/// Saves a file to a writer.
///
/// Writer to which to save a file.
public void Save(TextWriter fileWriter)
{
if (fileWriter == null)
throw new ArgumentNullException("fileWriter");
new IniWriter(this.options).Write(this, fileWriter);
}
}
}