Guest User

Untitled

a guest
Dec 17th, 2015
126
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
VB.NET 13.28 KB | None | 0 0
  1. ' ***********************************************************************
  2. ' Author   : Elektro
  3. ' Modified : 17-December-2015
  4. ' ***********************************************************************
  5.  
  6. #Region " Imports "
  7.  
  8. Imports System
  9. Imports System.Collections.ObjectModel
  10. Imports System.ComponentModel
  11. Imports System.Diagnostics
  12. Imports System.Linq
  13.  
  14. #End Region
  15.  
  16. #Region " INI Section Collection "
  17.  
  18. Namespace Types
  19.  
  20.     ''' ----------------------------------------------------------------------------------------------------
  21.     ''' <summary>
  22.     ''' Represents a strongly typed list of <see cref="IniSection"/> that can be accessed by an index.
  23.     ''' </summary>
  24.     ''' ----------------------------------------------------------------------------------------------------
  25.     Public NotInheritable Class IniSectionCollection : Inherits Collection(Of IniSection)
  26.  
  27. #Region " Constructors "
  28.  
  29.         ''' ----------------------------------------------------------------------------------------------------
  30.         ''' <summary>
  31.         ''' Initializes a new instance of the <see cref="IniSectionCollection"/> class.
  32.         ''' </summary>
  33.         ''' ----------------------------------------------------------------------------------------------------
  34.         Public Sub New()
  35.         End Sub
  36.  
  37. #End Region
  38.  
  39. #Region " Indexers "
  40.  
  41.         ''' ----------------------------------------------------------------------------------------------------
  42.         ''' <summary>
  43.         ''' Gets or sets the <see cref="IniSection"/> that matches the specified section name.
  44.         ''' </summary>
  45.         ''' ----------------------------------------------------------------------------------------------------
  46.         ''' <param name="sectionName">
  47.         ''' The section name.
  48.         ''' </param>
  49.         ''' ----------------------------------------------------------------------------------------------------
  50.         ''' <value>
  51.         ''' The <see cref="IniSection"/>.
  52.         ''' </value>
  53.         ''' ----------------------------------------------------------------------------------------------------
  54.         Default Public Overloads Property Item(ByVal sectionName As String) As IniSection
  55.             <DebuggerStepThrough>
  56.             Get
  57.                 Return Me.Find(sectionName)
  58.             End Get
  59.             <DebuggerStepThrough>
  60.             Set(ByVal value As IniSection)
  61.                 Me(Me.IndexOf(sectionName)) = value
  62.             End Set
  63.         End Property
  64.  
  65. #End Region
  66.  
  67. #Region " Public Methods "
  68.  
  69.         ''' ----------------------------------------------------------------------------------------------------
  70.         ''' <summary>
  71.         ''' Adds a <see cref="IniSection"/> to the end of the <see cref="IniSectionCollection"/>.
  72.         ''' </summary>
  73.         ''' ----------------------------------------------------------------------------------------------------
  74.         ''' <param name="section">
  75.         ''' The section to add.
  76.         ''' </param>
  77.         ''' ----------------------------------------------------------------------------------------------------
  78.         ''' <exception cref="ArgumentException">
  79.         ''' Section already exists.;section
  80.         ''' </exception>
  81.         ''' ----------------------------------------------------------------------------------------------------
  82.         <DebuggerStepThrough>
  83.         Public Shadows Sub Add(ByVal section As IniSection)
  84.  
  85.             If Me.Contains(section.Name) Then
  86.                 Throw New ArgumentException(message:="Section already exists.", paramName:="section")
  87.  
  88.             Else
  89.                 MyBase.Add(section)
  90.  
  91.             End If
  92.  
  93.         End Sub
  94.  
  95.         ''' ----------------------------------------------------------------------------------------------------
  96.         ''' <summary>
  97.         ''' Adds a <see cref="IniSection"/> to the end of the <see cref="IniSectionCollection"/>.
  98.         ''' </summary>
  99.         ''' ----------------------------------------------------------------------------------------------------
  100.         ''' <param name="sectionName">
  101.         ''' The section name to add.
  102.         ''' </param>
  103.         ''' ----------------------------------------------------------------------------------------------------
  104.         ''' <exception cref="ArgumentException">
  105.         ''' Section already exists.;section
  106.         ''' </exception>
  107.         ''' ----------------------------------------------------------------------------------------------------
  108.         <DebuggerStepThrough>
  109.         Public Shadows Sub Add(ByVal sectionName As String)
  110.  
  111.             Me.Add(New IniSection(sectionName))
  112.  
  113.         End Sub
  114.  
  115.         ''' ----------------------------------------------------------------------------------------------------
  116.         ''' <summary>
  117.         ''' Adds the specified sections to the end of the <see cref="IniSectionCollection"/>.
  118.         ''' </summary>
  119.         ''' ----------------------------------------------------------------------------------------------------
  120.         ''' <param name="sections">
  121.         ''' The sections to add.
  122.         ''' </param>
  123.         ''' ----------------------------------------------------------------------------------------------------
  124.         ''' <exception cref="ArgumentException">
  125.         ''' Section already exists.;section
  126.         ''' </exception>
  127.         ''' ----------------------------------------------------------------------------------------------------
  128.         <DebuggerStepThrough>
  129.         Public Shadows Sub AddRange(ByVal sections As IniSection())
  130.  
  131.             For Each section As IniSection In sections
  132.  
  133.                 Me.Add(section)
  134.  
  135.             Next section
  136.  
  137.         End Sub
  138.  
  139.         ''' ----------------------------------------------------------------------------------------------------
  140.         ''' <summary>
  141.         ''' Adds the specified section names to the end of the <see cref="IniSectionCollection"/>.
  142.         ''' </summary>
  143.         ''' ----------------------------------------------------------------------------------------------------
  144.         ''' <param name="sectionNames">
  145.         ''' The section names to add.
  146.         ''' </param>
  147.         ''' ----------------------------------------------------------------------------------------------------
  148.         ''' <exception cref="ArgumentException">
  149.         ''' Section already exists.;section
  150.         ''' </exception>
  151.         ''' ----------------------------------------------------------------------------------------------------
  152.         <DebuggerStepThrough>
  153.         Public Shadows Sub AddRange(ByVal sectionNames As String())
  154.  
  155.             For Each sectionName As String In sectionNames
  156.  
  157.                 Me.Add(New IniSection(sectionName))
  158.  
  159.             Next sectionName
  160.  
  161.         End Sub
  162.  
  163.         ''' ----------------------------------------------------------------------------------------------------
  164.         ''' <summary>
  165.         ''' Removes a <see cref="IniSection"/> from the <see cref="IniSectionCollection"/>.
  166.         ''' </summary>
  167.         ''' ----------------------------------------------------------------------------------------------------
  168.         ''' <param name="section">
  169.         ''' The section to remove.
  170.         ''' </param>
  171.         ''' ----------------------------------------------------------------------------------------------------
  172.         ''' <exception cref="ArgumentException">
  173.         ''' Section doesn't exists.;section
  174.         ''' </exception>
  175.         ''' ----------------------------------------------------------------------------------------------------
  176.         <DebuggerStepThrough>
  177.         Public Shadows Sub Remove(ByVal section As IniSection)
  178.  
  179.             Dim indexOf As Integer = Me.IndexOf(section)
  180.  
  181.             If (indexOf = -1) Then
  182.                 Throw New ArgumentException(message:="Section doesn't exists.", paramName:="section")
  183.  
  184.             Else
  185.                 MyBase.RemoveAt(indexOf)
  186.  
  187.             End If
  188.  
  189.         End Sub
  190.  
  191.         ''' ----------------------------------------------------------------------------------------------------
  192.         ''' <summary>
  193.         ''' Removes a <see cref="IniSection"/> from the <see cref="IniSectionCollection"/>.
  194.         ''' </summary>
  195.         ''' ----------------------------------------------------------------------------------------------------
  196.         ''' <param name="sectionName">
  197.         ''' The section name to remove.
  198.         ''' </param>
  199.         ''' ----------------------------------------------------------------------------------------------------
  200.         ''' <exception cref="ArgumentException">
  201.         ''' Section doesn't exists.;section
  202.         ''' </exception>
  203.         ''' ----------------------------------------------------------------------------------------------------
  204.         <DebuggerStepThrough>
  205.         Public Shadows Sub Remove(ByVal sectionName As String)
  206.  
  207.             Dim indexOf As Integer = Me.IndexOf(sectionName)
  208.  
  209.             If (indexOf = -1) Then
  210.                 Throw New ArgumentException(message:="Section doesn't exists.", paramName:="sectionName")
  211.  
  212.             Else
  213.                 MyBase.RemoveAt(indexOf)
  214.  
  215.             End If
  216.  
  217.         End Sub
  218.  
  219.         ''' ----------------------------------------------------------------------------------------------------
  220.         ''' <summary>
  221.         ''' Determines whether the <see cref="IniSectionCollection"/> contains a <see cref="IniSection"/> that
  222.         ''' matches the specified section name.
  223.         ''' </summary>
  224.         ''' ----------------------------------------------------------------------------------------------------
  225.         ''' <param name="sectionName">
  226.         ''' The section name.
  227.         ''' </param>
  228.         ''' ----------------------------------------------------------------------------------------------------
  229.         ''' <returns>
  230.         ''' <see langword="True"/> if the <see cref="IniSectionCollection"/> contains the <see cref="IniSection"/>,
  231.         ''' <see langword="False"/> otherwise.
  232.         ''' </returns>
  233.         ''' ----------------------------------------------------------------------------------------------------
  234.         <DebuggerStepThrough>
  235.         Public Overloads Function Contains(ByVal sectionName As String) As Boolean
  236.  
  237.             Return (From section As IniSection In Me
  238.                     Where section.Name.Equals(sectionName, StringComparison.OrdinalIgnoreCase)).Any
  239.  
  240.         End Function
  241.  
  242.         ''' ----------------------------------------------------------------------------------------------------
  243.         ''' <summary>
  244.         ''' Searches for an <see cref="IniSection"/> that matches the specified section name,
  245.         ''' and returns the first occurrence within the entire <see cref="IniSectionCollection"/>.
  246.         ''' </summary>
  247.         ''' ----------------------------------------------------------------------------------------------------
  248.         ''' <param name="sectionName">
  249.         ''' The section name.
  250.         ''' </param>
  251.         ''' ----------------------------------------------------------------------------------------------------
  252.         ''' <returns>
  253.         ''' <see cref="IniSection"/>.
  254.         ''' </returns>
  255.         ''' ----------------------------------------------------------------------------------------------------
  256.         <DebuggerStepThrough>
  257.         Public Overloads Function Find(ByVal sectionName As String) As IniSection
  258.  
  259.             Return (From section As IniSection In Me
  260.                     Where section.Name.Equals(sectionName, StringComparison.OrdinalIgnoreCase)).
  261.                     DefaultIfEmpty(Nothing).
  262.                     SingleOrDefault
  263.  
  264.         End Function
  265.  
  266.         ''' ----------------------------------------------------------------------------------------------------
  267.         ''' <summary>
  268.         ''' Searches for an <see cref="IniSection"/> that matches the specified section name and
  269.         ''' returns the zero-based index of the first occurrence within the entire <see cref="IniSectionCollection"/>.
  270.         ''' </summary>
  271.         ''' ----------------------------------------------------------------------------------------------------
  272.         ''' <param name="sectionName">
  273.         ''' The section name.
  274.         ''' </param>
  275.         ''' ----------------------------------------------------------------------------------------------------
  276.         ''' <returns>
  277.         ''' The zero-based index of the first occurrence of <see cref="IniSection"/> within the entire <see cref="IniSectionCollection"/>, if found;
  278.         ''' otherwise, <c>–1</c>.
  279.         ''' </returns>
  280.         ''' ----------------------------------------------------------------------------------------------------
  281.         <DebuggerStepThrough>
  282.         Public Overloads Function IndexOf(ByVal sectionName As String) As Integer
  283.  
  284.             Dim index As Integer = 0
  285.             Dim found As Boolean = False
  286.  
  287.             For Each section As IniSection In Me
  288.  
  289.                 If section.Name.Equals(sectionName, StringComparison.OrdinalIgnoreCase) Then
  290.                     found = True
  291.                     Exit For
  292.                 End If
  293.  
  294.                 index += 1
  295.  
  296.             Next section
  297.  
  298.             If (found) Then
  299.                 Return index
  300.  
  301.             Else
  302.                 Return -1
  303.  
  304.             End If
  305.  
  306.         End Function
  307.  
  308. #End Region
  309.  
  310.     End Class
  311.  
  312. End Namespace
  313.  
  314. #End Region
Add Comment
Please, Sign In to add comment