Skip to content

Instantly share code, notes, and snippets.

@XPlantefeve
Last active August 29, 2015 14:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save XPlantefeve/adf352d758ccf276959d to your computer and use it in GitHub Desktop.
Save XPlantefeve/adf352d758ccf276959d to your computer and use it in GitHub Desktop.
Rewriting of ConvertFrom-Csv, as an exercise. Quickly done and unfinished.
Function ConvertFrom-Csave {
[CmdletBinding(DefaultParameterSetName='UseDelimiter')]
Param (
[Parameter(Position=2,ParameterSetName='UseDelimiter')]
[char]$Delimiter = ',',
# FIXME
[string[]] $Header,
[Parameter(Mandatory=$True,Position=1,ValueFromPipeline=$True,ValueFromPipelineByPropertyName=$true)]
[PSObject] $InputObject,
[Parameter(Mandatory=$True,Position=2,ParameterSetName='UseCulture')]
[switch] $UseCulture
)
Begin {
$OutputObject = New-Object PSCustomObject
$HeadersDone = $false
$Properties = @()
If ( $UseCulture ) { $Delimiter = (Get-Culture).TextInfo.ListSeparator }
}
Process {
If ( $InputObject -like '#TYPE *') {
$OutputObject.pstypenames.insert(0,'CSV:' + $InputObject.ToString().Replace('#TYPE ',''))
} ElseIf ( !$HeadersDone ) {
foreach ( $Property in $InputObject.Split($Delimiter) ) {
$Properties += $property -Replace('^"(.*)"$', '$1')
Add-Member -InputObject $OutputObject -MemberType NoteProperty -Name ($property -Replace('^"(.*)"$', '$1')) -Value ""
}
$HeadersDone = $True
} else {
$i = 0
foreach ( $Property in $InputObject.Split($Delimiter) ) {
$OutputObject."$($Properties[$i])" = $property -Replace('^"(.*)"$', '$1')
$i++
}
$OutputObject
}
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment