forked from IcemanF1/ALTTPRCropDashboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathIniParser.vb
75 lines (63 loc) · 3.43 KB
/
IniParser.vb
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
Imports System.IO
Imports System.Text.RegularExpressions
Imports System.Runtime.InteropServices
Public NotInheritable Class IniParser
Private Shared ReadOnly SectionRegex As New Regex("\[(?<section>[^\n\[\]]+)\]\n*(?<valuelist>(.(?!\[[^\n\[\]]+\]))*)", RegexOptions.Singleline Or RegexOptions.CultureInvariant Or RegexOptions.Compiled)
Private Shared ReadOnly ValueRegex As New Regex("(?<valuename>[^=\n]+)=(?<value>[^\n]*)", RegexOptions.CultureInvariant Or RegexOptions.Compiled)
<DllImportAttribute("kernel32.dll", EntryPoint:="WritePrivateProfileStringW")>
Public Shared Function WritePrivateProfileStringW(<InAttribute(), MarshalAs(UnmanagedType.LPWStr)> lpSecName As String, <InAttribute(), MarshalAs(UnmanagedType.LPWStr)> lpKeyName As String, <InAttribute(), MarshalAs(UnmanagedType.LPWStr)> lpString As String, <InAttribute(), MarshalAs(UnmanagedType.LPWStr)> lpFileName As String) As <MarshalAs(UnmanagedType.Bool)> Boolean
End Function
''' <summary>
''' Parses an .ini-file.
''' </summary>
''' <param name="fileName">The path to the file to parse.</param>
''' <remarks></remarks>
Public Shared Function ParseFile(fileName As String) As Dictionary(Of String, Dictionary(Of String, String))
Return Parse(File.ReadAllText(fileName))
End Function
''' <summary>
''' Parses a text of .ini-format.
''' </summary>
''' <param name="data">The text to parse.</param>
''' <remarks></remarks>
Public Shared Function Parse(data As String) As Dictionary(Of String, Dictionary(Of String, String))
Dim result As New Dictionary(Of String, Dictionary(Of String, String)) '(Section, (Value name, Value))
Dim sections As MatchCollection = SectionRegex.Matches(data)
'Iterate each section.
For Each sectionMatch As Match In sections
Dim section As New Dictionary(Of String, String)
Dim sectionName As String = sectionMatch.Groups("section").Value
Dim values As MatchCollection = ValueRegex.Matches(sectionMatch.Groups("valuelist").Value)
If result.ContainsKey(sectionName) = True Then
'A section by this name already exists.
Dim i = 1
'Append a number to the section name until a unique name is found.
While result.ContainsKey(sectionName & i)
i += 1
End While
result.Add(sectionName & i, section)
Else
'A section by this name does not exist.
result.Add(sectionName, section)
End If
'Iterate each value of this section.
For Each valueMatch As Match In values
Dim valueName As String = valueMatch.Groups("valuename").Value
Dim value As String = valueMatch.Groups("value").Value
If section.ContainsKey(valueName) = True Then
'A value by this name already exists.
Dim i = 1
'Append a number to the value name until a unique name is found.
While section.ContainsKey(valueName & i)
i += 1
End While
section.Add(valueName & i, value)
Else
'A value by this name does not exist.
section.Add(valueName, value)
End If
Next
Next
Return result
End Function
End Class