Guest User

Untitled

a guest
Apr 17th, 2018
173
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
XBasic 3.06 KB | None | 0 0
  1. Imports System.IO
  2. Imports System.Text.RegularExpressions
  3.  
  4. Public Class IniFile : Inherits Dictionary(Of String, IniGroup)
  5.  
  6.     Public Sub New()
  7.         MyBase.New()
  8.     End Sub
  9.  
  10.     Public Shared Function Parse(ByVal ini As String) As IniFile
  11.         If IO.File.Exists(ini) Then
  12.             Dim file As New IniFile()
  13.             Dim curGroup As String = String.Empty
  14.  
  15.             Using sReader As New IO.StreamReader(ini)
  16.                 Dim line As String = String.Empty
  17.                 While Not sReader.EndOfStream
  18.                     line = sReader.ReadLine()
  19.                     Dim m As Match = Regex.Match(line, "^\s*\[(?<group>[^\]]*)]\s*$", RegexOptions.Compiled)
  20.                     If m.Success AndAlso m.Groups("group") IsNot Nothing AndAlso Not String.IsNullOrEmpty(m.Groups("group").Value) Then
  21.                         Dim group As String = m.Groups("group").Value
  22.                         If Not file.ContainsKey(group) Then file.Add(group, New IniGroup(group))
  23.                         curGroup = group
  24.                     ElseIf Not String.IsNullOrEmpty(curGroup) Then
  25.                         file(curGroup).AddProperty(line)
  26.                     End If
  27.                 End While
  28.             End Using
  29.  
  30.             Return file
  31.         Else
  32.             Return Nothing
  33.         End If
  34.     End Function
  35.  
  36.     Public Shared Function Write(ByVal ini As IniFile, ByVal filepath As String) As Boolean
  37.         If ini IsNot Nothing Then
  38.             Try
  39.                 IO.File.WriteAllText(filepath, String.Join(Environment.NewLine, ini.Values.Select(Function(g As IniGroup) g.ToString()).ToArray()))
  40.                 Return True
  41.             Catch ex As Exception
  42.                 Return False
  43.             End Try
  44.         Else
  45.             Return False
  46.         End If
  47.     End Function
  48. End Class
  49.  
  50. Public Class IniGroup : Inherits Dictionary(Of String, String)
  51.     Public Property Name As String
  52.  
  53.     Public Sub New(ByVal name As String)
  54.         MyBase.New()
  55.         Me.Name = name
  56.     End Sub
  57.  
  58.     Public Function AddProperty(ByVal propertyLine As String) As Boolean
  59.         If Not String.IsNullOrEmpty(propertyLine) AndAlso propertyLine.Contains("=") Then
  60.             Dim sections As String() = propertyLine.Split("="c)
  61.             If sections.Length = 2 Then
  62.                 sections(0) = sections(0).Trim()
  63.                 sections(1) = sections(1).Trim()
  64.                 If Not Me.ContainsKey(sections(0)) Then
  65.                     Me.Add(sections(0), sections(1))
  66.                 Else
  67.                     Me(sections(0)) = sections(1)
  68.                 End If
  69.                 Return True
  70.             End If
  71.         End If
  72.         Return False
  73.     End Function
  74.  
  75.     Public Overrides Function ToString() As String
  76.         Dim sBuilder As New System.Text.StringBuilder()
  77.         sBuilder.AppendLine(String.Format("[{0}]", Me.Name))
  78.         For Each kvp As KeyValuePair(Of String, String) In Me
  79.             sBuilder.AppendLine(String.Format("{0} = {1}", kvp.Key, kvp.Value))
  80.         Next
  81.         Return sBuilder.ToString()
  82.     End Function
  83. End Class
Add Comment
Please, Sign In to add comment