using System.Text;
using System.Diagnostics;
namespace MadMilkman.Ini
{
///
/// Represents a class that defines INI file's format, stores properties used for both reading and writing a file.
///
///
/// After an instance of this class is passed to an constructor, further changes on that instance's properties will have no effect.
///
///
/// Property
/// Default Value
///
/// -
/// CommentStarter
///
///
/// -
/// Compression
///
///
/// -
/// EncryptionPassword
///
///
/// -
/// Encoding
/// Encoding.ASCII
///
/// -
/// KeyDelimiter
///
///
/// -
/// KeyDuplicate
///
///
/// -
/// KeyNameCaseSensitive
///
///
/// -
/// KeySpaceAroundDelimiter
///
///
/// -
/// SectionDuplicate
///
///
/// -
/// SectionNameCaseSensitive
///
///
/// -
/// SectionWrapper
///
///
///
///
public sealed class IniOptions
{
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private Encoding encoding;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
private IniSectionWrapper sectionWrapper;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal char sectionWrapperStart;
[DebuggerBrowsable(DebuggerBrowsableState.Never)]
internal char sectionWrapperEnd;
///
/// Gets or sets encoding for reading and writing an INI file.
///
///
/// Value should not be , if it is then a default Encoding.ASCII value will be used.
///
public Encoding Encoding
{
get { return this.encoding; }
set
{
if (value == null)
this.encoding = Encoding.ASCII;
this.encoding = value;
}
}
///
/// Gets or sets comments starting character.
///
public IniCommentStarter CommentStarter { get; set; }
///
/// Gets or sets a value indicating if file's size is reduced.
/// If file is decompressed on Load and compressed on Save.
///
public bool Compression { get; set; }
///
/// Gets or sets an INI file's protection password.
/// File is decrypted on Load and encrypted on Save if a password is not or .
///
public string EncryptionPassword { get; set; }
///
/// Gets or sets keys name and value delimiter character.
///
public IniKeyDelimiter KeyDelimiter { get; set; }
///
/// Gets or sets a value indicating whether keys with same name are allowed, disallowed or ignored.
///
public IniDuplication KeyDuplicate { get; set; }
///
/// Gets or sets a value indicating whether keys name are case sensitive.
///
public bool KeyNameCaseSensitive { get; set; }
///
/// Gets or sets a value indicating whether space is written around the keys delimiter.
///
public bool KeySpaceAroundDelimiter { get; set; }
///
/// Gets or sets a value indicating whether sections with same name are allowed, disallowed or ignored.
///
public IniDuplication SectionDuplicate { get; set; }
///
/// Gets or sets a value indicating whether sections name are case sensitive.
///
public bool SectionNameCaseSensitive { get; set; }
///
/// Gets or sets wrapper characters of sections name.
///
public IniSectionWrapper SectionWrapper
{
get { return this.sectionWrapper; }
set
{
this.sectionWrapper = value;
this.SectionWrapperToCharacters(out this.sectionWrapperStart, out this.sectionWrapperEnd);
}
}
///
/// Initializes a new instance of the class.
///
public IniOptions()
{
this.encoding = Encoding.ASCII;
this.CommentStarter = IniCommentStarter.Semicolon;
this.Compression = false;
this.EncryptionPassword = null;
this.KeyDelimiter = IniKeyDelimiter.Equal;
this.KeyDuplicate = IniDuplication.Allowed;
this.KeyNameCaseSensitive = false;
this.KeySpaceAroundDelimiter = false;
this.SectionDuplicate = IniDuplication.Allowed;
this.SectionNameCaseSensitive = false;
this.SectionWrapper = IniSectionWrapper.SquareBrackets;
}
// Deep copy constructor.
internal IniOptions(IniOptions options)
{
this.encoding = options.encoding;
this.CommentStarter = options.CommentStarter;
this.Compression = options.Compression;
this.EncryptionPassword = options.EncryptionPassword;
this.KeyDelimiter = options.KeyDelimiter;
this.KeyDuplicate = options.KeyDuplicate;
this.KeyNameCaseSensitive = options.KeyNameCaseSensitive;
this.KeySpaceAroundDelimiter = options.KeySpaceAroundDelimiter;
this.SectionDuplicate = options.SectionDuplicate;
this.SectionNameCaseSensitive = options.SectionNameCaseSensitive;
this.SectionWrapper = options.SectionWrapper;
}
/* REMARKS: ToChar(bool) extension method on IniSectionWrapper would be nice, but in order to define
* an extension method in .NET 2.0 we need to declare ExtensionAttribute our self.
*
* Changing SectionWrapperToCharacters method's return type to Tuple would be nice,
* but writing our own Tuple implementation for .NET 2.0 is an unnecessary overhead. */
private void SectionWrapperToCharacters(out char startCharacter, out char endCharacte)
{
switch (this.sectionWrapper)
{
case IniSectionWrapper.AngleBrackets:
startCharacter = '<';
endCharacte = '>';
break;
case IniSectionWrapper.CurlyBrackets:
startCharacter = '{';
endCharacte = '}';
break;
case IniSectionWrapper.Parentheses:
startCharacter = '(';
endCharacte = ')';
break;
default:
startCharacter = '[';
endCharacte = ']';
break;
}
}
}
}