forked from PowerShell/PowerShell
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathConvertFrom-StringData.cs
More file actions
93 lines (77 loc) · 2.63 KB
/
Copy pathConvertFrom-StringData.cs
File metadata and controls
93 lines (77 loc) · 2.63 KB
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
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
/********************************************************************++
Copyright (c) Microsoft Corporation. All rights reserved.
--********************************************************************/
using System;
using System.Management.Automation;
using System.Collections;
using System.Text.RegularExpressions;
namespace Microsoft.PowerShell.Commands
{
/// <summary>
/// Class comment
/// </summary>
[Cmdlet(VerbsData.ConvertFrom, "StringData", HelpUri = "http://go.microsoft.com/fwlink/?LinkID=113288", RemotingCapability = RemotingCapability.None)]
[OutputType(typeof(Hashtable))]
public sealed class ConvertFromStringDataCommand : PSCmdlet
{
private string _stringData;
/// <summary>
/// The list of properties to display
/// These take the form of an MshExpression
/// </summary>
/// <value></value>
[Parameter(Mandatory = true, Position = 0, ValueFromPipeline = true)]
[AllowEmptyString]
public string StringData
{
get
{
return _stringData;
}
set
{
_stringData = value;
}
}
/// <summary>
///
/// </summary>
protected override void ProcessRecord()
{
Hashtable result = new Hashtable(StringComparer.OrdinalIgnoreCase);
if (String.IsNullOrEmpty(_stringData))
{
WriteObject(result);
return;
}
string[] lines = _stringData.Split('\n');
foreach (string line in lines)
{
string s = line.Trim();
if (String.IsNullOrEmpty(s) || s[0] == '#')
continue;
int index = s.IndexOf('=');
if (index <= 0)
{
throw PSTraceSource.NewInvalidOperationException(
ConvertFromStringData.InvalidDataLine,
line);
}
string name = s.Substring(0, index);
name = name.Trim();
if (result.ContainsKey(name))
{
throw PSTraceSource.NewInvalidOperationException(
ConvertFromStringData.DataItemAlreadyDefined,
line,
name);
}
string value = s.Substring(index + 1);
value = value.Trim();
value = Regex.Unescape(value);
result.Add(name, value);
}
WriteObject(result);
}
}
}