using System.Diagnostics;
using System.Collections.Generic;
namespace MadMilkman.Ini
{
///
/// Represents a section item of the INI file with name and keys content.
///
public sealed class IniSection : IniItem
{
///
/// Represents a section name which is used to define a global section, used for storing first keys series that don't belong to any section.
///
///
///
/// If a section with this name is located as a first file's section then its name and comments are ignored.
/// If a section with this name isn't located as first file's section then it will be written with MADMILKMAN_INI_FILE_GLOBAL_SECTION name.
///
///
public const string GlobalSectionName = "MADMILKMAN_INI_FILE_GLOBAL_SECTION";
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private readonly IniKeyCollection keys;
///
/// Gets the section's key collection.
///
public IniKeyCollection Keys { get { return this.keys; } }
///
/// Gets the to which this belongs to.
///
public IniSectionCollection ParentCollection { get { return (IniSectionCollection)this.ParentCollectionCore; } }
///
/// Initializes a new instance of the class.
///
/// The owner file.
/// The section's name.
public IniSection(IniFile parentFile, string name) : this(parentFile, name, (IEnumerable)null) { }
///
/// Initializes a new instance of the class.
///
/// The owner file.
/// The section's name.
/// The section's keys.
public IniSection(IniFile parentFile, string name, params IniKey[] keys) : this(parentFile, name, (IEnumerable)keys) { }
///
/// Initializes a new instance of the class.
///
/// The owner file.
/// The section's name.
/// The section's keys data, pairs of key's name and key's value.
[System.Diagnostics.CodeAnalysis.SuppressMessage("Microsoft.Design", "CA1006:DoNotNestGenericTypesInMemberSignatures",
Justification = "I don't want to use IDictionary, there is no need for such a contract because IEnumerable> is enough.")]
public IniSection(IniFile parentFile, string name, IEnumerable> nameValuePairs)
: this(parentFile, name, GetIniKeysFromKeyValuePairs(parentFile, nameValuePairs)) { }
///
/// Initializes a new instance of the class.
///
/// The owner file.
/// The section's name.
/// The section's keys.
public IniSection(IniFile parentFile, string name, IEnumerable keys)
: base(parentFile, name)
{
this.keys = new IniKeyCollection(parentFile, this, parentFile.options.KeyDuplicate, parentFile.options.KeyNameCaseSensitive);
if (keys != null)
foreach (IniKey key in keys)
this.keys.Add(key);
}
// Constructor used by IniReader.
internal IniSection(IniFile parentFile, string name, IniComment trailingComment)
: base(parentFile, name, trailingComment)
{
this.keys = new IniKeyCollection(parentFile, this, parentFile.options.KeyDuplicate, parentFile.options.KeyNameCaseSensitive);
}
// Deep copy constructor.
internal IniSection(IniFile destinationFile, IniSection sourceSection)
: base(destinationFile, sourceSection)
{
this.keys = new IniKeyCollection(destinationFile, this, destinationFile.options.KeyDuplicate, destinationFile.options.KeyNameCaseSensitive);
foreach (var key in sourceSection.keys)
this.keys.Add(key.Copy(destinationFile));
}
///
/// Copies this instance.
///
/// Copied .
/// IniItem's Copying
public IniSection Copy() { return this.Copy(this.ParentFile); }
///
/// Copies this instance and sets copied instance's ParentFile.
///
/// Copied section's parent file.
/// Copied that belongs to a specified .
/// IniItem's Copying
public IniSection Copy(IniFile destinationFile) { return new IniSection(destinationFile, this); }
///
/// Serializes the specified object into this .
///
/// The type of serialized object.
/// The object to serialize.
/// IniSection's Object Serialization
public void Serialize(T source) where T : class, new() { IniSerializer.Serialize(source, this); }
///
/// Deserializes this into an object of specified type.
///
/// The type of deserialized object.
/// The object being deserialized.
/// IniSection's Object Serialization
public T Deserialize() where T : class, new() { return IniSerializer.Deserialize(this); }
private static IEnumerable GetIniKeysFromKeyValuePairs(IniFile parentFile, IEnumerable> nameValuePairs)
{
if (nameValuePairs != null)
foreach (var pair in nameValuePairs)
yield return new IniKey(parentFile, pair);
}
}
}