mirror of
https://github.com/PowerShell/PowerShell
synced 2026-06-08 12:12:50 +00:00
Fix style issues in Csv cmdlets (#8894)
* Remove _delimiter * Fix attributes * Formatting headers * Format properties * Fix comments * Fix Codacy issues
This commit is contained in:
@@ -30,23 +30,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
[Parameter(Position = 1, ParameterSetName = "Delimiter")]
|
||||
[ValidateNotNull]
|
||||
public char Delimiter
|
||||
{
|
||||
get
|
||||
{
|
||||
return _delimiter;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_delimiter = value;
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Delimiter to be used.
|
||||
/// </summary>
|
||||
private char _delimiter;
|
||||
public char Delimiter { get; set; }
|
||||
|
||||
///<summary>
|
||||
///Culture switch for csv conversion
|
||||
@@ -59,11 +43,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// Derived as Different Attributes.In ConvertTo-CSV, This is a positional parameter. Export-CSV not a Positional behaviour.
|
||||
/// </summary>
|
||||
|
||||
public abstract PSObject InputObject
|
||||
{
|
||||
get;
|
||||
set;
|
||||
}
|
||||
public abstract PSObject InputObject { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// IncludeTypeInformation : The #TYPE line should be generated. Default is false. Cannot specify with NoTypeInformation.
|
||||
@@ -105,7 +85,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
NoTypeInformation = !IncludeTypeInformation;
|
||||
}
|
||||
|
||||
_delimiter = ImportExportCSVHelper.SetDelimiter(this, ParameterSetName, _delimiter, UseCulture);
|
||||
Delimiter = ImportExportCSVHelper.SetDelimiter(this, ParameterSetName, Delimiter, UseCulture);
|
||||
}
|
||||
}
|
||||
#endregion
|
||||
@@ -113,7 +93,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
#region Export-CSV Command
|
||||
|
||||
/// <summary>
|
||||
/// Implementation for the export-csv command.
|
||||
/// Implementation for the Export-Csv command.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsData.Export, "Csv", SupportsShouldProcess = true, DefaultParameterSetName = "Delimiter", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113299")]
|
||||
public sealed class ExportCsvCommand : BaseCsvWritingCommand, IDisposable
|
||||
@@ -154,7 +134,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// The literal path of the mandatory file name to write to.
|
||||
/// </summary>
|
||||
[Parameter()]
|
||||
[Parameter]
|
||||
[ValidateNotNullOrEmpty]
|
||||
[Alias("PSPath", "LP")]
|
||||
[SuppressMessage("Microsoft.Performance", "CA1819:PropertiesShouldNotReturnArrays")]
|
||||
@@ -175,59 +155,35 @@ namespace Microsoft.PowerShell.Commands
|
||||
private bool _isLiteralPath = false;
|
||||
|
||||
/// <summary>
|
||||
/// Property that sets force parameter.
|
||||
/// Gets or sets property that sets force parameter.
|
||||
/// </summary>
|
||||
[Parameter()]
|
||||
public SwitchParameter Force
|
||||
{
|
||||
get
|
||||
{
|
||||
return _force;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_force = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _force;
|
||||
[Parameter]
|
||||
public SwitchParameter Force { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Property that prevents file overwrite.
|
||||
/// Gets or sets property that prevents file overwrite.
|
||||
/// </summary>
|
||||
[Parameter()]
|
||||
[Parameter]
|
||||
[Alias("NoOverwrite")]
|
||||
public SwitchParameter NoClobber
|
||||
{
|
||||
get
|
||||
{
|
||||
return _noclobber;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_noclobber = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _noclobber;
|
||||
public SwitchParameter NoClobber { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Encoding optional flag.
|
||||
/// Gets or sets encoding optional flag.
|
||||
/// </summary>
|
||||
[Parameter()]
|
||||
[ArgumentToEncodingTransformationAttribute()]
|
||||
[Parameter]
|
||||
[ArgumentToEncodingTransformationAttribute]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
|
||||
/// <summary>
|
||||
/// Property that sets append parameter.
|
||||
/// Gets or sets property that sets append parameter.
|
||||
/// </summary>
|
||||
[Parameter]
|
||||
public SwitchParameter Append { get; set; }
|
||||
private bool _isActuallyAppending; // true if Append=true AND the file written was not empty (or nonexistent) when the cmdlet was invoked
|
||||
|
||||
// true if Append=true AND the file written was not empty (or nonexistent) when the cmdlet was invoked
|
||||
private bool _isActuallyAppending;
|
||||
|
||||
#endregion
|
||||
|
||||
@@ -254,7 +210,10 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
_shouldProcess = ShouldProcess(Path);
|
||||
if (!_shouldProcess) return;
|
||||
if (!_shouldProcess)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
CreateFileStream();
|
||||
|
||||
@@ -264,21 +223,23 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Convert the current input object to Csv and write to file/WriteObject.
|
||||
/// </summary>
|
||||
protected override
|
||||
void
|
||||
ProcessRecord()
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (InputObject == null || _sw == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
if (!_shouldProcess) return;
|
||||
if (!_shouldProcess)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Process first object
|
||||
if (_propertyNames == null)
|
||||
{
|
||||
// figure out the column names (and lock-in their order)
|
||||
_propertyNames = _helper.BuildPropertyNames(InputObject, _propertyNames);
|
||||
_propertyNames = ExportCsvHelper.BuildPropertyNames(InputObject, _propertyNames);
|
||||
if (_isActuallyAppending && _preexistingPropertyNames != null)
|
||||
{
|
||||
this.ReconcilePreexistingPropertyNames();
|
||||
@@ -289,7 +250,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
if (NoTypeInformation == false)
|
||||
{
|
||||
WriteCsvLine(_helper.GetTypeString(InputObject));
|
||||
WriteCsvLine(ExportCsvHelper.GetTypeString(InputObject));
|
||||
}
|
||||
|
||||
WriteCsvLine(_helper.ConvertPropertyNamesCSV(_propertyNames));
|
||||
@@ -368,7 +329,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
this,
|
||||
this.Path,
|
||||
encodingObject,
|
||||
false, // defaultEncoding
|
||||
defaultEncoding: false,
|
||||
Append,
|
||||
Force,
|
||||
NoClobber,
|
||||
@@ -383,7 +344,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
this,
|
||||
this.Path,
|
||||
Encoding,
|
||||
false, // defaultEncoding
|
||||
defaultEncoding: false,
|
||||
Append,
|
||||
Force,
|
||||
NoClobber,
|
||||
@@ -394,9 +355,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
}
|
||||
|
||||
private
|
||||
void
|
||||
CleanUp()
|
||||
private void CleanUp()
|
||||
{
|
||||
if (_fs != null)
|
||||
{
|
||||
@@ -409,6 +368,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
_fs.Dispose();
|
||||
_fs = null;
|
||||
|
||||
// reset the read-only attribute
|
||||
if (_readOnlyFileInfo != null)
|
||||
_readOnlyFileInfo.Attributes |= FileAttributes.ReadOnly;
|
||||
@@ -463,9 +423,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Write the csv line to file.
|
||||
/// </summary>
|
||||
/// <param name="line"></param>
|
||||
public override void
|
||||
WriteCsvLine(string line)
|
||||
/// <param name="line">Line to write.</param>
|
||||
public override void WriteCsvLine(string line)
|
||||
{
|
||||
// NTRAID#Windows Out Of Band Releases-915851-2005/09/13
|
||||
if (_disposed)
|
||||
@@ -487,9 +446,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Public dispose method.
|
||||
/// </summary>
|
||||
public
|
||||
void
|
||||
Dispose()
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed == false)
|
||||
{
|
||||
@@ -510,14 +467,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// Implements Import-Csv command.
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsData.Import, "Csv", DefaultParameterSetName = "DelimiterPath", HelpUri = "https://go.microsoft.com/fwlink/?LinkID=113341")]
|
||||
public sealed
|
||||
class
|
||||
ImportCsvCommand : PSCmdlet
|
||||
public sealed class ImportCsvCommand : PSCmdlet
|
||||
{
|
||||
#region Command Line Parameters
|
||||
|
||||
/// <summary>
|
||||
/// Property that sets delimiter.
|
||||
/// Gets or sets property that sets delimiter.
|
||||
/// </summary>
|
||||
[Parameter(Position = 1, ParameterSetName = "DelimiterPath")]
|
||||
[Parameter(Position = 1, ParameterSetName = "DelimiterLiteralPath")]
|
||||
@@ -525,7 +480,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
public char Delimiter { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Mandatory file name to read from.
|
||||
/// Gets or sets mandatory file name to read from.
|
||||
/// </summary>
|
||||
[Parameter(Position = 0, ParameterSetName = "DelimiterPath", Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||
[Parameter(Position = 0, ParameterSetName = "CulturePath", Mandatory = true, ValueFromPipeline = true, ValueFromPipelineByPropertyName = true)]
|
||||
@@ -548,7 +503,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
private bool _specifiedPath = false;
|
||||
|
||||
/// <summary>
|
||||
/// The literal path of the mandatory file name to read from.
|
||||
/// Gets or sets the literal path of the mandatory file name to read from.
|
||||
/// </summary>
|
||||
[Parameter(ParameterSetName = "DelimiterLiteralPath", Mandatory = true, ValueFromPipelineByPropertyName = true)]
|
||||
[Parameter(ParameterSetName = "CultureLiteralPath", Mandatory = true, ValueFromPipelineByPropertyName = true)]
|
||||
@@ -572,28 +527,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
private bool _isLiteralPath = false;
|
||||
|
||||
/// <summary>
|
||||
/// Property that sets UseCulture parameter.
|
||||
/// Gets or sets property that sets UseCulture parameter.
|
||||
/// </summary>
|
||||
[Parameter(ParameterSetName = "CulturePath", Mandatory = true)]
|
||||
[Parameter(ParameterSetName = "CultureLiteralPath", Mandatory = true)]
|
||||
[ValidateNotNull]
|
||||
public SwitchParameter UseCulture
|
||||
{
|
||||
get
|
||||
{
|
||||
return _useculture;
|
||||
}
|
||||
|
||||
set
|
||||
{
|
||||
_useculture = value;
|
||||
}
|
||||
}
|
||||
|
||||
private bool _useculture;
|
||||
public SwitchParameter UseCulture { get; set; }
|
||||
|
||||
///<summary>
|
||||
/// Header property to customize the names.
|
||||
/// Gets or sets header property to customize the names.
|
||||
///</summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
[ValidateNotNullOrEmpty]
|
||||
@@ -601,10 +543,10 @@ namespace Microsoft.PowerShell.Commands
|
||||
public string[] Header { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Encoding optional flag.
|
||||
/// Gets or sets encoding optional flag.
|
||||
/// </summary>
|
||||
[Parameter()]
|
||||
[ArgumentToEncodingTransformationAttribute()]
|
||||
[Parameter]
|
||||
[ArgumentToEncodingTransformationAttribute]
|
||||
[ArgumentEncodingCompletionsAttribute]
|
||||
[ValidateNotNullOrEmpty]
|
||||
public Encoding Encoding { get; set; } = ClrFacade.GetDefaultEncoding();
|
||||
@@ -619,11 +561,13 @@ namespace Microsoft.PowerShell.Commands
|
||||
#region Override Methods
|
||||
|
||||
/// <summary>
|
||||
/// BeginProcessing override.
|
||||
/// </summary>
|
||||
protected override void BeginProcessing()
|
||||
{
|
||||
Delimiter = ImportExportCSVHelper.SetDelimiter(this, ParameterSetName, Delimiter, _useculture);
|
||||
Delimiter = ImportExportCSVHelper.SetDelimiter(this, ParameterSetName, Delimiter, UseCulture);
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// ProcessRecord overload.
|
||||
/// </summary>
|
||||
@@ -697,9 +641,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// BeginProcessing override.
|
||||
/// </summary>
|
||||
protected override
|
||||
void
|
||||
BeginProcessing()
|
||||
protected override void BeginProcessing()
|
||||
{
|
||||
base.BeginProcessing();
|
||||
_helper = new ExportCsvHelper(this, base.Delimiter);
|
||||
@@ -708,22 +650,22 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Convert the current input object to Csv and write to stream/WriteObject.
|
||||
/// </summary>
|
||||
protected override
|
||||
void
|
||||
ProcessRecord()
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
if (InputObject == null)
|
||||
{
|
||||
return;
|
||||
}
|
||||
|
||||
// Process first object
|
||||
if (_propertyNames == null)
|
||||
{
|
||||
_propertyNames = _helper.BuildPropertyNames(InputObject, _propertyNames);
|
||||
_propertyNames = ExportCsvHelper.BuildPropertyNames(InputObject, _propertyNames);
|
||||
if (NoTypeInformation == false)
|
||||
{
|
||||
WriteCsvLine(_helper.GetTypeString(InputObject));
|
||||
WriteCsvLine(ExportCsvHelper.GetTypeString(InputObject));
|
||||
}
|
||||
|
||||
// Write property information
|
||||
string properties = _helper.ConvertPropertyNamesCSV(_propertyNames);
|
||||
if (!properties.Equals(string.Empty))
|
||||
@@ -731,6 +673,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
string csv = _helper.ConvertPSObjectToCSV(InputObject, _propertyNames);
|
||||
|
||||
// write to the console
|
||||
if (csv != string.Empty)
|
||||
WriteCsvLine(csv);
|
||||
@@ -740,10 +683,10 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
#region CSV conversion
|
||||
/// <summary>
|
||||
/// Write the line to output.
|
||||
/// </summary>
|
||||
/// <param name="line"></param>
|
||||
public override void
|
||||
WriteCsvLine(string line)
|
||||
/// <param name="line">Line to write.</param>
|
||||
public override void WriteCsvLine(string line)
|
||||
{
|
||||
WriteObject(line);
|
||||
}
|
||||
@@ -760,9 +703,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
[Cmdlet(VerbsData.ConvertFrom, "Csv", DefaultParameterSetName = "DelimiterPath",
|
||||
HelpUri = "https://go.microsoft.com/fwlink/?LinkID=135201", RemotingCapability = RemotingCapability.None)]
|
||||
public sealed
|
||||
class
|
||||
ConvertFromCsvCommand : PSCmdlet
|
||||
public sealed class ConvertFromCsvCommand : PSCmdlet
|
||||
{
|
||||
#region Command Line Parameters
|
||||
|
||||
@@ -784,7 +725,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
public SwitchParameter UseCulture { get; set; }
|
||||
|
||||
/// <summary>
|
||||
/// Input Object which is written in Csv format.
|
||||
/// Gets or sets input object which is written in Csv format.
|
||||
/// </summary>
|
||||
[Parameter(ValueFromPipeline = true, Mandatory = true, ValueFromPipelineByPropertyName = true, Position = 0)]
|
||||
[ValidateNotNull]
|
||||
@@ -793,7 +734,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
public PSObject[] InputObject { get; set; }
|
||||
|
||||
///<summary>
|
||||
/// Header property to customize the names.
|
||||
/// Gets or sets header property to customize the names.
|
||||
///</summary>
|
||||
[Parameter(Mandatory = false)]
|
||||
[ValidateNotNull]
|
||||
@@ -813,9 +754,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// BeginProcessing override.
|
||||
/// </summary>
|
||||
protected override
|
||||
void
|
||||
BeginProcessing()
|
||||
protected override void BeginProcessing()
|
||||
{
|
||||
Delimiter = ImportExportCSVHelper.SetDelimiter(this, ParameterSetName, Delimiter, UseCulture);
|
||||
}
|
||||
@@ -823,13 +762,11 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Convert the current input object to Csv and write to stream/WriteObject.
|
||||
/// </summary>
|
||||
protected override
|
||||
void
|
||||
ProcessRecord()
|
||||
protected override void ProcessRecord()
|
||||
{
|
||||
foreach (PSObject pObject in InputObject)
|
||||
foreach (PSObject inputObject in InputObject)
|
||||
{
|
||||
using (MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(pObject.ToString())))
|
||||
using (MemoryStream memoryStream = new MemoryStream(Encoding.Unicode.GetBytes(inputObject.ToString())))
|
||||
using (StreamReader streamReader = new StreamReader(memoryStream, System.Text.Encoding.Unicode))
|
||||
{
|
||||
ImportCsvHelper helper = new ImportCsvHelper(this, Delimiter, Header, _typeName, streamReader);
|
||||
@@ -869,11 +806,10 @@ namespace Microsoft.PowerShell.Commands
|
||||
#region ExportHelperConversion
|
||||
|
||||
/// <summary>
|
||||
/// Helper class for Export-Csvlper.
|
||||
/// </summary>
|
||||
internal class ExportCsvHelper : IDisposable
|
||||
{
|
||||
/// <summary>
|
||||
/// </summary>
|
||||
private PSCmdlet _cmdlet;
|
||||
|
||||
private char _delimiter;
|
||||
@@ -883,8 +819,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <param name="cmdlet"></param>
|
||||
/// <param name="delimiter"></param>
|
||||
/// <exception cref="ArgumentNullException">Throw if cmdlet is null.</exception>
|
||||
internal
|
||||
ExportCsvHelper(PSCmdlet cmdlet, char delimiter)
|
||||
internal ExportCsvHelper(PSCmdlet cmdlet, char delimiter)
|
||||
{
|
||||
if (cmdlet == null)
|
||||
{
|
||||
@@ -900,9 +835,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Get the name of properties from source PSObject and add them to _propertyNames.
|
||||
/// </summary>
|
||||
internal
|
||||
IList<string>
|
||||
BuildPropertyNames(PSObject source, IList<string> propertyNames)
|
||||
internal static IList<string> BuildPropertyNames(PSObject source, IList<string> propertyNames)
|
||||
{
|
||||
if (propertyNames != null)
|
||||
{
|
||||
@@ -911,8 +844,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
// serialize only Extended and Adapted properties..
|
||||
PSMemberInfoCollection<PSPropertyInfo> srcPropertiesToSearch =
|
||||
new PSMemberInfoIntegratingCollection<PSPropertyInfo>(source,
|
||||
PSObject.GetPropertyCollection(PSMemberViewTypes.Extended | PSMemberViewTypes.Adapted));
|
||||
new PSMemberInfoIntegratingCollection<PSPropertyInfo>(
|
||||
source,
|
||||
PSObject.GetPropertyCollection(PSMemberViewTypes.Extended | PSMemberViewTypes.Adapted));
|
||||
|
||||
propertyNames = new Collection<string>();
|
||||
foreach (PSPropertyInfo prop in srcPropertiesToSearch)
|
||||
@@ -926,10 +860,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Converts PropertyNames in to a CSV string.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
internal
|
||||
string
|
||||
ConvertPropertyNamesCSV(IList<string> propertyNames)
|
||||
/// <returns>Converted string.</returns>
|
||||
internal string ConvertPropertyNamesCSV(IList<string> propertyNames)
|
||||
{
|
||||
if (propertyNames == null)
|
||||
{
|
||||
@@ -957,13 +889,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Convert PSObject to CSV string.
|
||||
/// </summary>
|
||||
/// <param name="mshObject"></param>
|
||||
/// <param name="propertyNames"></param>
|
||||
/// <param name="mshObject">PSObject to convert.</param>
|
||||
/// <param name="propertyNames">Property names.</param>
|
||||
/// <returns></returns>
|
||||
internal
|
||||
string
|
||||
ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyNames)
|
||||
internal string ConvertPSObjectToCSV(PSObject mshObject, IList<string> propertyNames)
|
||||
{
|
||||
if (propertyNames == null)
|
||||
{
|
||||
@@ -1001,11 +932,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Get value from property object.
|
||||
/// </summary>
|
||||
/// <param name="property"></param>
|
||||
/// <returns></returns>
|
||||
internal
|
||||
string
|
||||
GetToStringValueForProperty(PSPropertyInfo property)
|
||||
/// <param name="property"> Property to convert.</param>
|
||||
/// <returns>ToString() value.</returns>
|
||||
internal static string GetToStringValueForProperty(PSPropertyInfo property)
|
||||
{
|
||||
if (property == null)
|
||||
{
|
||||
@@ -1021,9 +950,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
value = temp.ToString();
|
||||
}
|
||||
}
|
||||
// If we cannot read some value, treat it as null.
|
||||
catch (Exception)
|
||||
{
|
||||
// If we cannot read some value, treat it as null.
|
||||
}
|
||||
|
||||
return value;
|
||||
@@ -1032,11 +961,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Prepares string for writing type information.
|
||||
/// </summary>
|
||||
/// <param name="source"></param>
|
||||
/// <returns></returns>
|
||||
internal
|
||||
string
|
||||
GetTypeString(PSObject source)
|
||||
/// <param name="source">PSObject whose type to determine.</param>
|
||||
/// <returns>String with type information.</returns>
|
||||
internal static string GetTypeString(PSObject source)
|
||||
{
|
||||
string type = null;
|
||||
|
||||
@@ -1054,6 +981,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
string temp = tnh[0];
|
||||
|
||||
// If type starts with CSV: remove it. This would happen when you export
|
||||
// an imported object. import-csv adds CSV. prefix to the type.
|
||||
if (temp.StartsWith(ImportExportCSVHelper.CSVTypePrefix, StringComparison.OrdinalIgnoreCase))
|
||||
@@ -1071,10 +999,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// Escapes the " in string if necessary.
|
||||
/// Encloses the string in double quotes if necessary.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
internal static
|
||||
void
|
||||
EscapeAndAppendString(StringBuilder dest, string source)
|
||||
internal static void EscapeAndAppendString(StringBuilder dest, string source)
|
||||
{
|
||||
if (source == null)
|
||||
{
|
||||
@@ -1085,8 +1010,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
for (int i = 0; i < source.Length; i++)
|
||||
{
|
||||
char c = source[i];
|
||||
|
||||
// Double quote in the string is escaped with double quote
|
||||
if ((c == '"'))
|
||||
if (c == '"')
|
||||
{
|
||||
dest.Append('"');
|
||||
}
|
||||
@@ -1096,6 +1022,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
|
||||
dest.Append('"');
|
||||
}
|
||||
|
||||
#region IDisposable Members
|
||||
|
||||
/// <summary>
|
||||
@@ -1106,9 +1033,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// Public dispose method.
|
||||
/// </summary>
|
||||
public
|
||||
void
|
||||
Dispose()
|
||||
public void Dispose()
|
||||
{
|
||||
if (_disposed == false)
|
||||
{
|
||||
@@ -1153,12 +1078,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
private bool _alreadyWarnedUnspecifiedName = false;
|
||||
|
||||
/// <summary>
|
||||
/// Reference to header values.
|
||||
/// Gets reference to header values.
|
||||
/// </summary>
|
||||
internal IList<string> Header { get; private set; }
|
||||
|
||||
/// <summary>
|
||||
/// ETS type name from the first line / comment in the CSV.
|
||||
/// Gets ETS type name from the first line / comment in the CSV.
|
||||
/// </summary>
|
||||
internal string TypeName { get; private set; }
|
||||
|
||||
@@ -1199,18 +1124,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <summary>
|
||||
/// This is set to true when end of file is reached.
|
||||
/// </summary>
|
||||
private
|
||||
bool EOF
|
||||
{
|
||||
get
|
||||
{
|
||||
return _sr.EndOfStream;
|
||||
}
|
||||
}
|
||||
private bool EOF => _sr.EndOfStream;
|
||||
|
||||
private
|
||||
char
|
||||
ReadChar()
|
||||
private char ReadChar()
|
||||
{
|
||||
if (EOF)
|
||||
{
|
||||
@@ -1226,9 +1142,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// </summary>
|
||||
/// <param name="c"></param>
|
||||
/// <returns></returns>
|
||||
private
|
||||
bool
|
||||
PeekNextChar(char c)
|
||||
private bool PeekNextChar(char c)
|
||||
{
|
||||
int i = _sr.Peek();
|
||||
if (i == -1)
|
||||
@@ -1236,19 +1150,15 @@ namespace Microsoft.PowerShell.Commands
|
||||
return false;
|
||||
}
|
||||
|
||||
return (c == (char)i);
|
||||
return c == (char)i;
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Reads a line from file. This consumes the end of line.
|
||||
/// Only use it when end of line chars are not important.
|
||||
/// </summary>
|
||||
/// <returns></returns>
|
||||
private string
|
||||
ReadLine()
|
||||
{
|
||||
return _sr.ReadLine();
|
||||
}
|
||||
/// <returns>Line from file.</returns>
|
||||
private string ReadLine() => _sr.ReadLine();
|
||||
|
||||
#endregion reading helpers
|
||||
|
||||
@@ -1299,9 +1209,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
}
|
||||
|
||||
internal
|
||||
void
|
||||
Import(ref bool alreadyWriteOutWarning)
|
||||
internal void Import(ref bool alreadyWriteOutWarning)
|
||||
{
|
||||
_alreadyWarnedUnspecifiedName = alreadyWriteOutWarning;
|
||||
ReadHeader();
|
||||
@@ -1355,9 +1263,10 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
// throw a terminating error as there are duplicate headers in the input.
|
||||
string memberAlreadyPresentMsg =
|
||||
string.Format(CultureInfo.InvariantCulture,
|
||||
ExtendedTypeSystem.MemberAlreadyPresent,
|
||||
currentHeader);
|
||||
string.Format(
|
||||
CultureInfo.InvariantCulture,
|
||||
ExtendedTypeSystem.MemberAlreadyPresent,
|
||||
currentHeader);
|
||||
|
||||
ExtendedTypeSystemException exception = new ExtendedTypeSystemException(memberAlreadyPresentMsg);
|
||||
throw exception;
|
||||
@@ -1367,12 +1276,12 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
/// <summary>
|
||||
/// Read the type information, if present.
|
||||
/// </summary>
|
||||
/// <returns>Type string if present else null.</returns>
|
||||
private string
|
||||
ReadTypeInformation()
|
||||
private string ReadTypeInformation()
|
||||
{
|
||||
string type = null;
|
||||
if (PeekNextChar('#'))
|
||||
@@ -1398,21 +1307,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// <returns>
|
||||
/// Parsed collection of strings.
|
||||
/// </returns>
|
||||
private void
|
||||
ParseNextRecord(List<string> result, StringBuilder current)
|
||||
private void ParseNextRecord(List<string> result, StringBuilder current)
|
||||
{
|
||||
result.Clear();
|
||||
|
||||
// current string
|
||||
current.Clear();
|
||||
|
||||
bool seenBeginQuote = false;
|
||||
// int i = 0;
|
||||
|
||||
while (!EOF)
|
||||
{
|
||||
// Read the next character
|
||||
char ch = ReadChar();
|
||||
|
||||
if ((ch == _delimiter))
|
||||
if (ch == _delimiter)
|
||||
{
|
||||
if (seenBeginQuote)
|
||||
{
|
||||
@@ -1442,7 +1351,6 @@ namespace Microsoft.PowerShell.Commands
|
||||
// ->foo"bar<-
|
||||
|
||||
// PeekNextChar only peeks. Read the next char.
|
||||
|
||||
ReadChar();
|
||||
current.Append('"');
|
||||
}
|
||||
@@ -1523,7 +1431,9 @@ namespace Microsoft.PowerShell.Commands
|
||||
current.Remove(0, current.Length);
|
||||
|
||||
if (endOfRecord)
|
||||
{
|
||||
break;
|
||||
}
|
||||
}
|
||||
}
|
||||
else if (IsNewLine(ch, out string newLine))
|
||||
@@ -1537,6 +1447,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
result.Add(current.ToString());
|
||||
current.Remove(0, current.Length);
|
||||
|
||||
// New line outside quote is end of word and end of record
|
||||
break;
|
||||
}
|
||||
@@ -1554,9 +1465,7 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
|
||||
// If we detect a newline we return it as a string "\r", "\n" or "\r\n"
|
||||
private
|
||||
bool
|
||||
IsNewLine(char ch, out string newLine)
|
||||
private bool IsNewLine(char ch, out string newLine)
|
||||
{
|
||||
newLine = string.Empty;
|
||||
if (ch == '\r')
|
||||
@@ -1591,11 +1500,10 @@ namespace Microsoft.PowerShell.Commands
|
||||
/// If this is true, eat the trailing blanks. Note:if there are non
|
||||
/// whitespace characters present, then trailing blanks are not consumed.
|
||||
/// </param>
|
||||
private
|
||||
void
|
||||
ReadTillNextDelimiter(StringBuilder current, ref bool endOfRecord, bool eatTrailingBlanks)
|
||||
private void ReadTillNextDelimiter(StringBuilder current, ref bool endOfRecord, bool eatTrailingBlanks)
|
||||
{
|
||||
StringBuilder temp = new StringBuilder();
|
||||
|
||||
// Did we see any non-whitespace character
|
||||
bool nonWhiteSpace = false;
|
||||
|
||||
@@ -1640,11 +1548,8 @@ namespace Microsoft.PowerShell.Commands
|
||||
}
|
||||
}
|
||||
|
||||
private
|
||||
PSObject
|
||||
BuildMshobject(string type, IList<string> names, List<string> values, char delimiter, bool preValidated = false)
|
||||
private PSObject BuildMshobject(string type, IList<string> names, List<string> values, char delimiter, bool preValidated = false)
|
||||
{
|
||||
// string[] namesarray = null;
|
||||
PSObject result = new PSObject(names.Count);
|
||||
char delimiterlocal = delimiter;
|
||||
int unspecifiedNameIndex = 1;
|
||||
@@ -1652,18 +1557,21 @@ namespace Microsoft.PowerShell.Commands
|
||||
{
|
||||
string name = names[i];
|
||||
string value = null;
|
||||
////if name is null and delimiter is '"', use a default property name 'UnspecifiedName'
|
||||
|
||||
// if name is null and delimiter is '"', use a default property name 'UnspecifiedName'
|
||||
if (name.Length == 0 && delimiterlocal == '"')
|
||||
{
|
||||
name = UnspecifiedName + unspecifiedNameIndex;
|
||||
unspecifiedNameIndex++;
|
||||
}
|
||||
////if name is null and delimiter is not '"', use a default property name 'UnspecifiedName'
|
||||
|
||||
// if name is null and delimiter is not '"', use a default property name 'UnspecifiedName'
|
||||
if (string.IsNullOrEmpty(name))
|
||||
{
|
||||
name = UnspecifiedName + unspecifiedNameIndex;
|
||||
unspecifiedNameIndex++;
|
||||
}
|
||||
|
||||
// If no value was present in CSV file, we write null.
|
||||
if (i < values.Count)
|
||||
{
|
||||
@@ -1702,40 +1610,42 @@ namespace Microsoft.PowerShell.Commands
|
||||
internal const char CSVDelimiter = ',';
|
||||
internal const string CSVTypePrefix = "CSV:";
|
||||
|
||||
internal static char SetDelimiter(PSCmdlet Cmdlet, string ParameterSetName, char Delimiter, bool UseCulture)
|
||||
internal static char SetDelimiter(PSCmdlet cmdlet, string parameterSetName, char explicitDelimiter, bool useCulture)
|
||||
{
|
||||
switch (ParameterSetName)
|
||||
char delimiter = explicitDelimiter;
|
||||
switch (parameterSetName)
|
||||
{
|
||||
case "Delimiter":
|
||||
case "DelimiterPath":
|
||||
case "DelimiterLiteralPath":
|
||||
|
||||
// if delimiter is not given, it should take , as value
|
||||
if (Delimiter == '\0')
|
||||
if (explicitDelimiter == '\0')
|
||||
{
|
||||
Delimiter = ImportExportCSVHelper.CSVDelimiter;
|
||||
delimiter = ImportExportCSVHelper.CSVDelimiter;
|
||||
}
|
||||
|
||||
break;
|
||||
case "UseCulture":
|
||||
case "CulturePath":
|
||||
case "CultureLiteralPath":
|
||||
if (UseCulture == true)
|
||||
if (useCulture == true)
|
||||
{
|
||||
// ListSeparator is apparently always a character even though the property returns a string, checked via:
|
||||
// [CultureInfo]::GetCultures("AllCultures") | % { ([CultureInfo]($_.Name)).TextInfo.ListSeparator } | ? Length -ne 1
|
||||
Delimiter = CultureInfo.CurrentCulture.TextInfo.ListSeparator[0];
|
||||
delimiter = CultureInfo.CurrentCulture.TextInfo.ListSeparator[0];
|
||||
}
|
||||
|
||||
break;
|
||||
default:
|
||||
{
|
||||
Delimiter = ImportExportCSVHelper.CSVDelimiter;
|
||||
delimiter = ImportExportCSVHelper.CSVDelimiter;
|
||||
}
|
||||
|
||||
break;
|
||||
}
|
||||
|
||||
return Delimiter;
|
||||
return delimiter;
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user