==========================
ini_parser
==========================
.. contents::
:depth: 3
ParsingException
================
.. seealso:: http://code.google.com/p/ini-parser/source/browse/trunk/%20ini-parser/src/IniFileParser/Exceptions.cs
::
using System;
namespace IniParser
{
///
/// Represents an error ococcurred while parsing data
///
public class ParsingException : Exception
{
///
/// Initializes a new instance of the class.
///
public ParsingException(){ }
///
/// Initializes a new instance of the class.
///
/// The message describing the exception cause.
public ParsingException(string msg)
: base(msg) { }
///
/// Initializes a new instance of the class.
///
/// The message describing the exception cause.
/// An inner exception.
public ParsingException(string msg, Exception innerException)
: base(msg, innerException) { }
}
}
KeyDataCollection
=================
.. seealso:: http://code.google.com/p/ini-parser/source/browse/trunk/%20ini-parser/src/IniFileParser/KeyDataCollection.cs
::
using System;
using System.Collections;
using System.Collections.Generic;
namespace IniParser
{
///
/// Represents a collection of Keydata.
///
public class KeyDataCollection : ICloneable, IEnumerable
{
#region Initialization
///
/// Initializes a new instance of the class.
///
public KeyDataCollection()
{
_keyData = new Dictionary();
}
///
/// Initializes a new instance of the class
/// from a previous instance of .
///
///
/// Data is deeply copied
///
///
/// The instance of the class
/// used to create the new instance.
public KeyDataCollection(KeyDataCollection ori)
{
_keyData = new Dictionary();
foreach ( string key in _keyData.Keys )
_keyData.Add(key, (KeyData)ori._keyData[key].Clone() );
}
#endregion
#region Properties
///
/// Gets or sets the value of a concrete key.
///
///
/// If we try to assign the value of a key which doesn't exists,
/// a new key is added with the name and the value is assigned to it.
///
/// Name of the key
///
/// The string with key's value or null
/// if the key was not found.
///
public string this[string keyName]
{
get
{
if (_keyData.ContainsKey(keyName))
return _keyData[keyName].Value;
return null;
}
set
{
if (!_keyData.ContainsKey(keyName))
return;
_keyData[keyName].Value = value;
}
}
///
/// Return the number of keys in the collection
///
/// An integer with the number of keys in the collection.
public int Count
{
get { return _keyData.Count; }
}
#endregion
#region Public Methods
///
/// Adds a new key with the specified name and empty value and comments
///
///
/// A valid key name is a string with NO blank spaces.
///
/// New key to be added.
///
/// true if a new empty key was added
/// false otherwise.
///
/// If the key name is not valid.
public bool AddKey(string keyName)
{
//Checks valid key name
//if ( !Assert.StringHasNoBlankSpaces(keyName) )
// throw new ArgumentException("Key name is not valid");
if ( !_keyData.ContainsKey(keyName) )
{
_keyData.Add(keyName, new KeyData(keyName));
return true;
}
return false;
}
///
/// Adds a new key with the specified name and value and comments
///
///
/// A valid key name is a string with NO blank spaces.
///
/// New key to be added.
/// KeyData instance.
///
/// true if a new empty key was added
/// false otherwise.
///
/// If the key name is not valid.
public bool AddKey(string keyName, KeyData keyData)
{
if (AddKey(keyName))
{
_keyData[keyName] = keyData;
return true;
}
return false;
}
///
/// Adds a new key with the specified name and value and comments
///
///
/// A valid key name is a string with NO blank spaces.
///
/// New key to be added.
/// Value associated to the kyy.
///
/// true if a new empty key was added
/// false otherwise.
///
/// If the key name is not valid.
public bool AddKey(string keyName, string keyValue)
{
if (AddKey(keyName))
{
_keyData[keyName].Value = keyValue;
return true;
}
return false;
}
///
/// Retrieves the data for a specified key given its name
///
/// Name of the key to retrieve.
///
/// A instance holding
/// the key information or null if the key wasn't found.
///
public KeyData GetKeyData(string keyName)
{
if (_keyData.ContainsKey(keyName))
return _keyData[keyName];
return null;
}
///
/// Sets the key data associated to a specified key.
///
/// The new for the key.
public void SetKeyData(KeyData data)
{
if (data != null)
{
if (_keyData.ContainsKey(data.KeyName))
RemoveKey(data.KeyName);
AddKey(data.KeyName, data);
}
}
///
/// Gets if a specifyed key name exists in the collection.
///
/// Key name to search
/// true if a key with the specified name exists in the collectoin
/// false otherwise
public bool ContainsKey(string keyName)
{
return _keyData.ContainsKey(keyName);
}
///
/// Deletes a previously existing key, including its associated data.
///
/// The key to be removed.
///
/// true if a key with the specified name was removed
/// false otherwise.
///
public bool RemoveKey(string keyName)
{
return _keyData.Remove(keyName);
}
#endregion
#region IEnumerable Members
///
/// Allows iteration througt the collection.
///
/// A strong-typed IEnumerator
public IEnumerator GetEnumerator()
{
foreach ( string key in _keyData.Keys )
yield return _keyData[key];
}
#region IEnumerable Members
///
/// Implementation needed
///
/// A weak-typed IEnumerator.
IEnumerator IEnumerable.GetEnumerator()
{
return _keyData.GetEnumerator();
}
#endregion
#endregion
#region ICloneable Members
///
/// Creates a new object that is a copy of the current instance.
///
///
/// A new object that is a copy of this instance.
///
public object Clone()
{
return new KeyDataCollection(this);
}
#endregion
#region Non-public Members
///
/// Collection of KeyData for a given section
///
private readonly Dictionary _keyData;
#endregion
}
}