Rename IniFileVb -> IniFile

This commit is contained in:
Jakob Aarøe Dam 2015-04-28 12:34:45 +02:00
parent eebdaf9551
commit fdd99dbb7a
2 changed files with 376 additions and 376 deletions

View file

@ -1,376 +1,376 @@
' Programmer: Ludvik Jerabek ' Programmer: Ludvik Jerabek
' Date: 08\23\2010 ' Date: 08\23\2010
' Purpose: Allow INI manipulation in .NET ' Purpose: Allow INI manipulation in .NET
Imports System.IO Imports System.IO
Imports System.Text.RegularExpressions Imports System.Text.RegularExpressions
Imports System.Collections Imports System.Collections
Imports System.Diagnostics Imports System.Diagnostics
' IniFile class used to read and write ini files by loading the file into memory ' IniFile class used to read and write ini files by loading the file into memory
Public Class IniFile Public Class IniFile
' List of IniSection objects keeps track of all the sections in the INI file ' List of IniSection objects keeps track of all the sections in the INI file
Private m_sections As Hashtable Private m_sections As Hashtable
' Public constructor ' Public constructor
Public Sub New() Public Sub New()
m_sections = New Hashtable(StringComparer.InvariantCultureIgnoreCase) m_sections = New Hashtable(StringComparer.InvariantCultureIgnoreCase)
End Sub End Sub
' Loads the Reads the data in the ini file into the IniFile object ' Loads the Reads the data in the ini file into the IniFile object
Public Sub Load(ByVal sFileName As String, Optional ByVal bMerge As Boolean = False) Public Sub Load(ByVal sFileName As String, Optional ByVal bMerge As Boolean = False)
If Not bMerge Then If Not bMerge Then
RemoveAllSections() RemoveAllSections()
End If End If
' Clear the object... ' Clear the object...
Dim tempsection As IniSection = Nothing Dim tempsection As IniSection = Nothing
Dim oReader As New StreamReader(sFileName) Dim oReader As New StreamReader(sFileName)
Dim regexcomment As New Regex("^([\s]*#.*)", (RegexOptions.Singleline Or RegexOptions.IgnoreCase)) Dim regexcomment As New Regex("^([\s]*#.*)", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
' Broken but left for history ' Broken but left for history
'Dim regexsection As New Regex("\[[\s]*([^\[\s].*[^\s\]])[\s]*\]", (RegexOptions.Singleline Or RegexOptions.IgnoreCase)) 'Dim regexsection As New Regex("\[[\s]*([^\[\s].*[^\s\]])[\s]*\]", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
Dim regexsection As New Regex("^[\s]*\[[\s]*([^\[\s].*[^\s\]])[\s]*\][\s]*$", (RegexOptions.Singleline Or RegexOptions.IgnoreCase)) Dim regexsection As New Regex("^[\s]*\[[\s]*([^\[\s].*[^\s\]])[\s]*\][\s]*$", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
Dim regexkey As New Regex("^\s*([^=\s]*)[^=]*=(.*)", (RegexOptions.Singleline Or RegexOptions.IgnoreCase)) Dim regexkey As New Regex("^\s*([^=\s]*)[^=]*=(.*)", (RegexOptions.Singleline Or RegexOptions.IgnoreCase))
While Not oReader.EndOfStream While Not oReader.EndOfStream
Dim line As String = oReader.ReadLine() Dim line As String = oReader.ReadLine()
If line <> String.Empty Then If line <> String.Empty Then
Dim m As Match = Nothing Dim m As Match = Nothing
If regexcomment.Match(line).Success Then If regexcomment.Match(line).Success Then
m = regexcomment.Match(line) m = regexcomment.Match(line)
Trace.WriteLine(String.Format("Skipping Comment: {0}", m.Groups(0).Value)) Trace.WriteLine(String.Format("Skipping Comment: {0}", m.Groups(0).Value))
ElseIf regexsection.Match(line).Success Then ElseIf regexsection.Match(line).Success Then
m = regexsection.Match(line) m = regexsection.Match(line)
Trace.WriteLine(String.Format("Adding section [{0}]", m.Groups(1).Value)) Trace.WriteLine(String.Format("Adding section [{0}]", m.Groups(1).Value))
tempsection = AddSection(m.Groups(1).Value) tempsection = AddSection(m.Groups(1).Value)
ElseIf regexkey.Match(line).Success AndAlso tempsection IsNot Nothing Then ElseIf regexkey.Match(line).Success AndAlso tempsection IsNot Nothing Then
m = regexkey.Match(line) m = regexkey.Match(line)
Trace.WriteLine(String.Format("Adding Key [{0}]=[{1}]", m.Groups(1).Value, m.Groups(2).Value)) Trace.WriteLine(String.Format("Adding Key [{0}]=[{1}]", m.Groups(1).Value, m.Groups(2).Value))
tempsection.AddKey(m.Groups(1).Value).Value = m.Groups(2).Value tempsection.AddKey(m.Groups(1).Value).Value = m.Groups(2).Value
ElseIf tempsection IsNot Nothing Then ElseIf tempsection IsNot Nothing Then
' Handle Key without value ' Handle Key without value
Trace.WriteLine(String.Format("Adding Key [{0}]", line)) Trace.WriteLine(String.Format("Adding Key [{0}]", line))
tempsection.AddKey(line) tempsection.AddKey(line)
Else Else
' This should not occur unless the tempsection is not created yet... ' This should not occur unless the tempsection is not created yet...
Trace.WriteLine(String.Format("Skipping unknown type of data: {0}", line)) Trace.WriteLine(String.Format("Skipping unknown type of data: {0}", line))
End If End If
End If End If
End While End While
oReader.Close() oReader.Close()
End Sub End Sub
' Used to save the data back to the file or your choice ' Used to save the data back to the file or your choice
Public Sub Save(ByVal sFileName As String) Public Sub Save(ByVal sFileName As String)
Dim oWriter As New StreamWriter(sFileName, False) Dim oWriter As New StreamWriter(sFileName, False)
For Each s As IniSection In Sections For Each s As IniSection In Sections
Trace.WriteLine(String.Format("Writing Section: [{0}]", s.Name)) Trace.WriteLine(String.Format("Writing Section: [{0}]", s.Name))
oWriter.WriteLine(String.Format("[{0}]", s.Name)) oWriter.WriteLine(String.Format("[{0}]", s.Name))
For Each k As IniSection.IniKey In s.Keys For Each k As IniSection.IniKey In s.Keys
If k.Value <> String.Empty Then If k.Value <> String.Empty Then
Trace.WriteLine(String.Format("Writing Key: {0}={1}", k.Name, k.Value)) Trace.WriteLine(String.Format("Writing Key: {0}={1}", k.Name, k.Value))
oWriter.WriteLine(String.Format("{0}={1}", k.Name, k.Value)) oWriter.WriteLine(String.Format("{0}={1}", k.Name, k.Value))
Else Else
Trace.WriteLine(String.Format("Writing Key: {0}", k.Name)) Trace.WriteLine(String.Format("Writing Key: {0}", k.Name))
oWriter.WriteLine(String.Format("{0}", k.Name)) oWriter.WriteLine(String.Format("{0}", k.Name))
End If End If
Next Next
Next Next
oWriter.Close() oWriter.Close()
End Sub End Sub
' Gets all the sections ' Gets all the sections
Public ReadOnly Property Sections() As System.Collections.ICollection Public ReadOnly Property Sections() As System.Collections.ICollection
Get Get
Return m_sections.Values Return m_sections.Values
End Get End Get
End Property End Property
' Adds a section to the IniFile object, returns a IniSection object to the new or existing object ' Adds a section to the IniFile object, returns a IniSection object to the new or existing object
Public Function AddSection(ByVal sSection As String) As IniSection Public Function AddSection(ByVal sSection As String) As IniSection
Dim s As IniSection = Nothing Dim s As IniSection = Nothing
sSection = sSection.Trim() sSection = sSection.Trim()
' Trim spaces ' Trim spaces
If m_sections.ContainsKey(sSection) Then If m_sections.ContainsKey(sSection) Then
s = DirectCast(m_sections(sSection), IniSection) s = DirectCast(m_sections(sSection), IniSection)
Else Else
s = New IniSection(Me, sSection) s = New IniSection(Me, sSection)
m_sections(sSection) = s m_sections(sSection) = s
End If End If
Return s Return s
End Function End Function
' Removes a section by its name sSection, returns trus on success ' Removes a section by its name sSection, returns trus on success
Public Function RemoveSection(ByVal sSection As String) As Boolean Public Function RemoveSection(ByVal sSection As String) As Boolean
sSection = sSection.Trim() sSection = sSection.Trim()
Return RemoveSection(GetSection(sSection)) Return RemoveSection(GetSection(sSection))
End Function End Function
' Removes section by object, returns trus on success ' Removes section by object, returns trus on success
Public Function RemoveSection(ByVal Section As IniSection) As Boolean Public Function RemoveSection(ByVal Section As IniSection) As Boolean
If Section IsNot Nothing Then If Section IsNot Nothing Then
Try Try
m_sections.Remove(Section.Name) m_sections.Remove(Section.Name)
Return True Return True
Catch ex As Exception Catch ex As Exception
Trace.WriteLine(ex.Message) Trace.WriteLine(ex.Message)
End Try End Try
End If End If
Return False Return False
End Function End Function
' Removes all existing sections, returns trus on success ' Removes all existing sections, returns trus on success
Public Function RemoveAllSections() As Boolean Public Function RemoveAllSections() As Boolean
m_sections.Clear() m_sections.Clear()
Return (m_sections.Count = 0) Return (m_sections.Count = 0)
End Function End Function
' Returns an IniSection to the section by name, NULL if it was not found ' Returns an IniSection to the section by name, NULL if it was not found
Public Function GetSection(ByVal sSection As String) As IniSection Public Function GetSection(ByVal sSection As String) As IniSection
sSection = sSection.Trim() sSection = sSection.Trim()
' Trim spaces ' Trim spaces
If m_sections.ContainsKey(sSection) Then If m_sections.ContainsKey(sSection) Then
Return DirectCast(m_sections(sSection), IniSection) Return DirectCast(m_sections(sSection), IniSection)
End If End If
Return Nothing Return Nothing
End Function End Function
' Returns a KeyValue in a certain section ' Returns a KeyValue in a certain section
Public Function GetKeyValue(ByVal sSection As String, ByVal sKey As String) As String Public Function GetKeyValue(ByVal sSection As String, ByVal sKey As String) As String
Dim s As IniSection = GetSection(sSection) Dim s As IniSection = GetSection(sSection)
If s IsNot Nothing Then If s IsNot Nothing Then
Dim k As IniSection.IniKey = s.GetKey(sKey) Dim k As IniSection.IniKey = s.GetKey(sKey)
If k IsNot Nothing Then If k IsNot Nothing Then
Return k.Value Return k.Value
End If End If
End If End If
Return String.Empty Return String.Empty
End Function End Function
' Sets a KeyValuePair in a certain section ' Sets a KeyValuePair in a certain section
Public Function SetKeyValue(ByVal sSection As String, ByVal sKey As String, ByVal sValue As String) As Boolean Public Function SetKeyValue(ByVal sSection As String, ByVal sKey As String, ByVal sValue As String) As Boolean
Dim s As IniSection = AddSection(sSection) Dim s As IniSection = AddSection(sSection)
If s IsNot Nothing Then If s IsNot Nothing Then
Dim k As IniSection.IniKey = s.AddKey(sKey) Dim k As IniSection.IniKey = s.AddKey(sKey)
If k IsNot Nothing Then If k IsNot Nothing Then
k.Value = sValue k.Value = sValue
Return True Return True
End If End If
End If End If
Return False Return False
End Function End Function
' Renames an existing section returns true on success, false if the section didn't exist or there was another section with the same sNewSection ' Renames an existing section returns true on success, false if the section didn't exist or there was another section with the same sNewSection
Public Function RenameSection(ByVal sSection As String, ByVal sNewSection As String) As Boolean Public Function RenameSection(ByVal sSection As String, ByVal sNewSection As String) As Boolean
' Note string trims are done in lower calls. ' Note string trims are done in lower calls.
Dim bRval As Boolean = False Dim bRval As Boolean = False
Dim s As IniSection = GetSection(sSection) Dim s As IniSection = GetSection(sSection)
If s IsNot Nothing Then If s IsNot Nothing Then
bRval = s.SetName(sNewSection) bRval = s.SetName(sNewSection)
End If End If
Return bRval Return bRval
End Function End Function
' Renames an existing key returns true on success, false if the key didn't exist or there was another section with the same sNewKey ' Renames an existing key returns true on success, false if the key didn't exist or there was another section with the same sNewKey
Public Function RenameKey(ByVal sSection As String, ByVal sKey As String, ByVal sNewKey As String) As Boolean Public Function RenameKey(ByVal sSection As String, ByVal sKey As String, ByVal sNewKey As String) As Boolean
' Note string trims are done in lower calls. ' Note string trims are done in lower calls.
Dim s As IniSection = GetSection(sSection) Dim s As IniSection = GetSection(sSection)
If s IsNot Nothing Then If s IsNot Nothing Then
Dim k As IniSection.IniKey = s.GetKey(sKey) Dim k As IniSection.IniKey = s.GetKey(sKey)
If k IsNot Nothing Then If k IsNot Nothing Then
Return k.SetName(sNewKey) Return k.SetName(sNewKey)
End If End If
End If End If
Return False Return False
End Function End Function
' Remove a key by section name and key name ' Remove a key by section name and key name
Public Function RemoveKey(ByVal sSection As String, ByVal sKey As String) As Boolean Public Function RemoveKey(ByVal sSection As String, ByVal sKey As String) As Boolean
Dim s As IniSection = GetSection(sSection) Dim s As IniSection = GetSection(sSection)
If s IsNot Nothing Then If s IsNot Nothing Then
Return s.RemoveKey(sKey) Return s.RemoveKey(sKey)
End If End If
Return False Return False
End Function End Function
' IniSection class ' IniSection class
Public Class IniSection Public Class IniSection
' IniFile IniFile object instance ' IniFile IniFile object instance
Private m_pIniFile As IniFile Private m_pIniFile As IniFile
' Name of the section ' Name of the section
Private m_sSection As String Private m_sSection As String
' List of IniKeys in the section ' List of IniKeys in the section
Private m_keys As Hashtable Private m_keys As Hashtable
' Constuctor so objects are internally managed ' Constuctor so objects are internally managed
Protected Friend Sub New(ByVal parent As IniFile, ByVal sSection As String) Protected Friend Sub New(ByVal parent As IniFile, ByVal sSection As String)
m_pIniFile = parent m_pIniFile = parent
m_sSection = sSection m_sSection = sSection
m_keys = New Hashtable(StringComparer.InvariantCultureIgnoreCase) m_keys = New Hashtable(StringComparer.InvariantCultureIgnoreCase)
End Sub End Sub
' Returns all the keys in a section ' Returns all the keys in a section
Public ReadOnly Property Keys() As System.Collections.ICollection Public ReadOnly Property Keys() As System.Collections.ICollection
Get Get
Return m_keys.Values Return m_keys.Values
End Get End Get
End Property End Property
' Returns the section name ' Returns the section name
Public ReadOnly Property Name() As String Public ReadOnly Property Name() As String
Get Get
Return m_sSection Return m_sSection
End Get End Get
End Property End Property
' Adds a key to the IniSection object, returns a IniKey object to the new or existing object ' Adds a key to the IniSection object, returns a IniKey object to the new or existing object
Public Function AddKey(ByVal sKey As String) As IniKey Public Function AddKey(ByVal sKey As String) As IniKey
sKey = sKey.Trim() sKey = sKey.Trim()
Dim k As IniSection.IniKey = Nothing Dim k As IniSection.IniKey = Nothing
If sKey.Length <> 0 Then If sKey.Length <> 0 Then
If m_keys.ContainsKey(sKey) Then If m_keys.ContainsKey(sKey) Then
k = DirectCast(m_keys(sKey), IniKey) k = DirectCast(m_keys(sKey), IniKey)
Else Else
k = New IniSection.IniKey(Me, sKey) k = New IniSection.IniKey(Me, sKey)
m_keys(sKey) = k m_keys(sKey) = k
End If End If
End If End If
Return k Return k
End Function End Function
' Removes a single key by string ' Removes a single key by string
Public Function RemoveKey(ByVal sKey As String) As Boolean Public Function RemoveKey(ByVal sKey As String) As Boolean
Return RemoveKey(GetKey(sKey)) Return RemoveKey(GetKey(sKey))
End Function End Function
' Removes a single key by IniKey object ' Removes a single key by IniKey object
Public Function RemoveKey(ByVal Key As IniKey) As Boolean Public Function RemoveKey(ByVal Key As IniKey) As Boolean
If Key IsNot Nothing Then If Key IsNot Nothing Then
Try Try
m_keys.Remove(Key.Name) m_keys.Remove(Key.Name)
Return True Return True
Catch ex As Exception Catch ex As Exception
Trace.WriteLine(ex.Message) Trace.WriteLine(ex.Message)
End Try End Try
End If End If
Return False Return False
End Function End Function
' Removes all the keys in the section ' Removes all the keys in the section
Public Function RemoveAllKeys() As Boolean Public Function RemoveAllKeys() As Boolean
m_keys.Clear() m_keys.Clear()
Return (m_keys.Count = 0) Return (m_keys.Count = 0)
End Function End Function
' Returns a IniKey object to the key by name, NULL if it was not found ' Returns a IniKey object to the key by name, NULL if it was not found
Public Function GetKey(ByVal sKey As String) As IniKey Public Function GetKey(ByVal sKey As String) As IniKey
sKey = sKey.Trim() sKey = sKey.Trim()
If m_keys.ContainsKey(sKey) Then If m_keys.ContainsKey(sKey) Then
Return DirectCast(m_keys(sKey), IniKey) Return DirectCast(m_keys(sKey), IniKey)
End If End If
Return Nothing Return Nothing
End Function End Function
' Sets the section name, returns true on success, fails if the section ' Sets the section name, returns true on success, fails if the section
' name sSection already exists ' name sSection already exists
Public Function SetName(ByVal sSection As String) As Boolean Public Function SetName(ByVal sSection As String) As Boolean
sSection = sSection.Trim() sSection = sSection.Trim()
If sSection.Length <> 0 Then If sSection.Length <> 0 Then
' Get existing section if it even exists... ' Get existing section if it even exists...
Dim s As IniSection = m_pIniFile.GetSection(sSection) Dim s As IniSection = m_pIniFile.GetSection(sSection)
If s IsNot Me AndAlso s IsNot Nothing Then If s IsNot Me AndAlso s IsNot Nothing Then
Return False Return False
End If End If
Try Try
' Remove the current section ' Remove the current section
m_pIniFile.m_sections.Remove(m_sSection) m_pIniFile.m_sections.Remove(m_sSection)
' Set the new section name to this object ' Set the new section name to this object
m_pIniFile.m_sections(sSection) = Me m_pIniFile.m_sections(sSection) = Me
' Set the new section name ' Set the new section name
m_sSection = sSection m_sSection = sSection
Return True Return True
Catch ex As Exception Catch ex As Exception
Trace.WriteLine(ex.Message) Trace.WriteLine(ex.Message)
End Try End Try
End If End If
Return False Return False
End Function End Function
' Returns the section name ' Returns the section name
Public Function GetName() As String Public Function GetName() As String
Return m_sSection Return m_sSection
End Function End Function
' IniKey class ' IniKey class
Public Class IniKey Public Class IniKey
' Name of the Key ' Name of the Key
Private m_sKey As String Private m_sKey As String
' Value associated ' Value associated
Private m_sValue As String Private m_sValue As String
' Pointer to the parent CIniSection ' Pointer to the parent CIniSection
Private m_section As IniSection Private m_section As IniSection
' Constuctor so objects are internally managed ' Constuctor so objects are internally managed
Protected Friend Sub New(ByVal parent As IniSection, ByVal sKey As String) Protected Friend Sub New(ByVal parent As IniSection, ByVal sKey As String)
m_section = parent m_section = parent
m_sKey = sKey m_sKey = sKey
End Sub End Sub
' Returns the name of the Key ' Returns the name of the Key
Public ReadOnly Property Name() As String Public ReadOnly Property Name() As String
Get Get
Return m_sKey Return m_sKey
End Get End Get
End Property End Property
' Sets or Gets the value of the key ' Sets or Gets the value of the key
Public Property Value() As String Public Property Value() As String
Get Get
Return m_sValue Return m_sValue
End Get End Get
Set(ByVal value As String) Set(ByVal value As String)
m_sValue = value m_sValue = value
End Set End Set
End Property End Property
' Sets the value of the key ' Sets the value of the key
Public Sub SetValue(ByVal sValue As String) Public Sub SetValue(ByVal sValue As String)
m_sValue = sValue m_sValue = sValue
End Sub End Sub
' Returns the value of the Key ' Returns the value of the Key
Public Function GetValue() As String Public Function GetValue() As String
Return m_sValue Return m_sValue
End Function End Function
' Sets the key name ' Sets the key name
' Returns true on success, fails if the section name sKey already exists ' Returns true on success, fails if the section name sKey already exists
Public Function SetName(ByVal sKey As String) As Boolean Public Function SetName(ByVal sKey As String) As Boolean
sKey = sKey.Trim() sKey = sKey.Trim()
If sKey.Length <> 0 Then If sKey.Length <> 0 Then
Dim k As IniKey = m_section.GetKey(sKey) Dim k As IniKey = m_section.GetKey(sKey)
If k IsNot Me AndAlso k IsNot Nothing Then If k IsNot Me AndAlso k IsNot Nothing Then
Return False Return False
End If End If
Try Try
' Remove the current key ' Remove the current key
m_section.m_keys.Remove(m_sKey) m_section.m_keys.Remove(m_sKey)
' Set the new key name to this object ' Set the new key name to this object
m_section.m_keys(sKey) = Me m_section.m_keys(sKey) = Me
' Set the new key name ' Set the new key name
m_sKey = sKey m_sKey = sKey
Return True Return True
Catch ex As Exception Catch ex As Exception
Trace.WriteLine(ex.Message) Trace.WriteLine(ex.Message)
End Try End Try
End If End If
Return False Return False
End Function End Function
' Returns the name of the Key ' Returns the name of the Key
Public Function GetName() As String Public Function GetName() As String
Return m_sKey Return m_sKey
End Function End Function
End Class End Class
' End of IniKey class ' End of IniKey class
End Class End Class
' End of IniSection class ' End of IniSection class
End Class End Class
' End of IniFile class ' End of IniFile class

View file

@ -97,7 +97,7 @@
<Compile Include="exceptions\MissingRadiusSecret.vb" /> <Compile Include="exceptions\MissingRadiusSecret.vb" />
<Compile Include="exceptions\MissingUser.vb" /> <Compile Include="exceptions\MissingUser.vb" />
<Compile Include="handlers\RDSHandler.vb" /> <Compile Include="handlers\RDSHandler.vb" />
<Compile Include="IniFileVb.vb" /> <Compile Include="IniFile.vb" />
<Compile Include="Log.vb" /> <Compile Include="Log.vb" />
<Compile Include="My Project\Application.Designer.vb"> <Compile Include="My Project\Application.Designer.vb">
<AutoGen>True</AutoGen> <AutoGen>True</AutoGen>