Read a configuration file: Difference between revisions

(→‎{{header|PowerShell}}: Using Switch -Regex)
(→‎Using Switch -Regex: Using regex \s)
Line 3,430:
=== Using Switch -Regex ===
<lang PowerShell>
Function Houdini-Value ([String]$_Text) {
$__Aux = $_Text.Trim(" `t")
If ($__Aux.Length -eq 0) {
$__Aux = $true
} ElseIf ($__Aux.Contains(',')) {
$__Aux = $__Aux.Split(',') |
ForEach-Object {
If ($PSItem.Trim(" `t").Length -ne 0) {
$PSItem.Trim(" `t")
}
}
}
Return $__Aux
}
 
Function Read-ConfigurationFile {
[CmdletBinding()]
Line 3,463 ⟶ 3,448:
)
 
Begin {
Function Houdini-Value ([String]$_Text) {
$__Aux = $_Text.Trim(" `t")
If ($__Aux.Length -eq 0) {
$__Aux = $true
} ElseIf ($__Aux.Contains(',')) {
$__Aux = $__Aux.Split(',') |
ForEach-Object {
If ($PSItem.Trim(" `t").Length -ne 0) {
$PSItem.Trim(" `t")
}
}
}
Return $__Aux
}
}
Process {
$__Configuration = [Ordered]@{}
# Config equivalent pattern
# Select-String -Pattern '^((\t| )s*)([^\ts;#= ]+)(.*)((\t| )s*)$' -LiteralPath '.\filename.cfg'
Switch -Regex -File $_LiteralPath {
 
'^((\t| )s*)[;#=]|^((\t| )s*)$' {
Write-Verbose -Message "v$('↓← ← ← ← ← ← ← ← ← ← '*20)ignored'"
Write-Verbose -Message $Matches[0]
Continue
Line 3,479 ⟶ 3,481:
Write-Verbose -Message $Matches[0]
$__Name,$__Value = $Matches[1..2]
$__Configuration[$__Name.Trim(" `t")] = Houdini-Value($__Value)
Continue
}
 
'^((\t| )s*)([^\ts;#= ]+)(.*)((\t| )s*)$' {
Write-Verbose -Message '↓← ← ← ← ← ← ← ← ← ← space or tab pattern or only name'
Write-Verbose -Message $Matches[0]
$__Name,$__Value = $Matches[31..42]
$__Configuration[$__Name.Trim(" `t")] = Houdini-Value($__Value)
Continue
}
Anonymous user