Imports System.IO
Imports System.Text.RegularExpressions
Namespace configs
'''
''' Used to read the lines from a specified config file.
'''
Public Class ConfigsTextmapper
'''
''' Get all configs from a specified file.
'''
''' List of every line from the config.
Public Function GetAllValues(configFilePath As String) As List(Of String)
If configFilePath Is Nothing OrElse configFilePath.Equals(String.Empty) Then
'Empty path is not allowed.
Throw New InternalException("The path to the config file can't be empty for fetching all the lines of the config file.")
End If
'Check if file exist.
If Not File.Exists(configFilePath) Then
'File doesn't exist.
Throw New ConfigurationException($"The file '{configFilePath}' was not found.")
End If
Try
'File exists, read all text.
Dim allText As String = My.Computer.FileSystem.ReadAllText(configFilePath)
'Text between two 'µ' gets converted to one line.
allText = ConvertMultiLineToSingleLine(allText)
'Seperate into lines.
Dim lines As String() = allText.Split(New String() {Environment.NewLine}, StringSplitOptions.None)
'Iterate every line, only the important configs.
Dim correctLines As New List(Of String)
'Extract the lines which are not comment lines (doesn't start with '%') and are not empty.
For Each line As String In lines
line = line.Trim()
'Check if line is valid.
If Not line.StartsWith("%") And Not line.Equals(String.Empty) Then
correctLines.Add(line)
End If
Next
'Return the correct lines.
Return correctLines
Catch ex As ReadOnlyException
Throw New ConfigurationException($"File is readonly: '{configFilePath}'")
Catch ex As PathTooLongException
Throw New ConfigurationException($"Path is too long: '{configFilePath}'")
Catch ex As IOException
Throw New ConfigurationException($"File is unreachable: '{configFilePath}'")
Catch ex As Exception
Throw New ConfigurationException($"File could not be read: '{configFilePath}'{vbNewLine}{ex.Message}")
End Try
End Function
'''
''' Text between two 'µ' gets converted to one line.
'''
'''
'''
Private Function ConvertMultiLineToSingleLine(allText As String) As String
'Check if it's necessary to run other code.
If Not allText.Contains("__") Then
Return allText
End If
'Invoke the Match method, all text between and including the 'µ'.
Dim matchCollection = Regex.Matches(allText, "__[\S\s]*?__")
If (matchCollection IsNot Nothing AndAlso matchCollection.Count > 0) Then
'If successful, overwrite each multiline from the groups as one line, without the seperator symbols 'µ'.
For Each match In matchCollection
'Remove newLines, replace by space.
Dim onelineString = match.Value.Replace(vbCr, " ").Replace(vbLf, " ")
'Remove special characters.
onelineString = onelineString.Replace("__", String.Empty)
'Overwrite multiline entry in text.
allText = allText.Replace(match.Value, onelineString)
Next
End If
'Return the probably changed text.
Return allText
End Function
End Class
End Namespace