Neuron®
The Neuron® is the basis for the creation of open and secure federated networks for smart societies.
Loading...
Searching...
No Matches
Decode.cs
1using System;
2using System.Collections.Generic;
3using System.Threading.Tasks;
4using Waher.Content;
10
12{
17 {
26 public Decode(ScriptNode Content, ScriptNode ContentType, int Start, int Length, Expression Expression)
27 : base(Content, ContentType, Start, Length, Expression)
28 {
29 }
30
34 public override string FunctionName => nameof(Decode);
35
40 public override bool IsAsynchronous => true;
41
50 {
51 return this.EvaluateScalarAsync(Argument1, Argument2, Variables).Result;
52 }
53
62 {
63 if (!(Argument1.AssociatedObjectValue is byte[] Bin))
64 throw new ScriptRuntimeException("Binary data expected.", this);
65
66 string ContentType = Argument2.AssociatedObjectValue is string s2 ? s2 : Expression.ToString(Argument2.AssociatedObjectValue);
67
68 return this.DoDecodeAsync(Bin, ContentType, null);
69 }
70
71 private async Task<IElement> DoDecodeAsync(byte[] Data, string ContentType, System.Text.Encoding Encoding)
72 {
73 object Decoded;
74
75 if (Encoding is null)
76 Decoded = await InternetContent.DecodeAsync(ContentType, Data, null);
77 else
78 Decoded = await InternetContent.DecodeAsync(ContentType, Data, Encoding, new KeyValuePair<string, string>[0], null);
79
80 if (Decoded is string[][] Records)
81 {
82 int Rows = Records.Length;
83 int MaxCols = 0;
84 int i, c;
85
86 foreach (string[] Rec in Records)
87 {
88 if (Rec is null)
89 continue;
90
91 c = Rec.Length;
92 if (c > MaxCols)
93 MaxCols = c;
94 }
95
96 LinkedList<IElement> Elements = new LinkedList<IElement>();
97
98 foreach (string[] Rec in Records)
99 {
100 i = 0;
101
102 if (!(Rec is null))
103 {
104 foreach (string s in Rec)
105 {
106 if (s is null || string.IsNullOrEmpty(s))
107 Elements.AddLast(new ObjectValue(null));
108 else if (CommonTypes.TryParse(s, out double dbl))
109 Elements.AddLast(new DoubleNumber(dbl));
110 else if (CommonTypes.TryParse(s, out bool b))
111 Elements.AddLast(new BooleanValue(b));
112 else if (XML.TryParse(s, out DateTime TP))
113 Elements.AddLast(new DateTimeValue(TP));
114 else if (TimeSpan.TryParse(s, out TimeSpan TS))
115 Elements.AddLast(new ObjectValue(TS));
116 else
117 Elements.AddLast(new StringValue(s));
118
119 i++;
120 }
121 }
122
123 while (i++ < MaxCols)
124 Elements.AddLast(new StringValue(string.Empty));
125 }
126
127 return Operators.Matrices.MatrixDefinition.Encapsulate(Elements, Rows, MaxCols, this);
128 }
129 else
130 return Expression.Encapsulate(Decoded);
131 }
132
141 {
142 return this.EvaluateScalarAsync(Argument1, Argument2, Variables).Result;
143 }
144
152 public override Task<IElement> EvaluateScalarAsync(string Argument1, string Argument2, Variables Variables)
153 {
154 System.Text.Encoding Encoding = System.Text.Encoding.UTF8;
155 byte[] Bin = Encoding.GetBytes(Argument1);
156
157 return this.DoDecodeAsync(Bin, Argument2, Encoding);
158 }
159
160 }
161}
Helps with parsing of commong data types.
Definition: CommonTypes.cs:13
static bool TryParse(string s, out double Value)
Tries to decode a string encoded double.
Definition: CommonTypes.cs:46
Static class managing encoding and decoding of internet content.
static Task< object > DecodeAsync(string ContentType, byte[] Data, Encoding Encoding, KeyValuePair< string, string >[] Fields, Uri BaseUri)
Decodes an object.
Helps with common XML-related tasks.
Definition: XML.cs:19
static bool TryParse(string s, out DateTime Value)
Tries to decode a string encoded DateTime.
Definition: XML.cs:744
Decode(Content,ContentType)
Definition: Decode.cs:17
override Task< IElement > EvaluateScalarAsync(string Argument1, string Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:152
override string FunctionName
Name of the function
Definition: Decode.cs:34
override Task< IElement > EvaluateScalarAsync(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:61
override bool IsAsynchronous
If the node (or its decendants) include asynchronous evaluation. Asynchronous nodes should be evaluat...
Definition: Decode.cs:40
override IElement EvaluateScalar(string Argument1, string Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:140
Decode(ScriptNode Content, ScriptNode ContentType, int Start, int Length, Expression Expression)
Decode(Content,ContentType)
Definition: Decode.cs:26
override IElement EvaluateScalar(IElement Argument1, IElement Argument2, Variables Variables)
Evaluates the function on two scalar arguments.
Definition: Decode.cs:49
Class managing a script expression.
Definition: Expression.cs:39
static IElement Encapsulate(object Value)
Encapsulates an object.
Definition: Expression.cs:4955
static string ToString(double Value)
Converts a value to a string, that can be parsed as part of an expression.
Definition: Expression.cs:4496
Base class for funcions of two scalar variables.
ScriptNode Argument2
Function argument 2.
ScriptNode Argument1
Function argument 1.
Base class for all nodes in a parsed script tree.
Definition: ScriptNode.cs:69
int Length
Length of expression covered by node.
Definition: ScriptNode.cs:101
int Start
Start position in script expression.
Definition: ScriptNode.cs:92
Boolean-valued number.
Definition: BooleanValue.cs:12
Collection of variables.
Definition: Variables.cs:25
Basic interface for all types of elements.
Definition: IElement.cs:20