-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdataParser.cs
More file actions
105 lines (102 loc) · 3.6 KB
/
dataParser.cs
File metadata and controls
105 lines (102 loc) · 3.6 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
93
94
95
96
97
98
99
100
101
102
103
104
105
using System;
using System.Collections.Generic;
namespace Vorfol.Data {
public class ParsedValue {
public readonly byte id = 0;
public readonly byte size = 0;
public readonly int offset = 0;
public readonly bool valid = false;
private byte[] baseData = null;
private int start = 0;
public ParsedValue(byte[] baseData, int start) {
if (baseData != null) {
this.baseData = baseData;
if (start >= 0 && start < baseData.Length ) {
this.start = start;
this.id = baseData[start];
if ((this.id & 0x80) != 0) {
// acquiring data
this.id = (byte)(this.id ^ 0xFF);
this.offset = sizeof(byte);
this.valid = true;
} else {
// sending data
this.size = baseData[start + sizeof(byte)];
this.offset = sizeof(byte)*2 + size;
if (this.start + this.offset <= this.baseData.Length) {
this.valid = true;
}
}
}
}
}
public byte[] data {
get {
byte[] retData = new byte[this.size];
if (this.valid && this.size > 0) {
Array.Copy(this.baseData, this.start + sizeof(byte)*2, retData, 0, retData.Length);
}
return retData;
}
}
}
public class ParsedBlock {
public readonly Dictionary<byte, List<ParsedValue>> values = new Dictionary<byte, List<ParsedValue>>();
public ParsedBlock(byte[] data) {
int offset = 0;
ParsedValue parsedValue = new ParsedValue(data, offset);
while (parsedValue.valid) {
List<ParsedValue> entries;
if (!this.values.TryGetValue(parsedValue.id, out entries)) {
entries = new List<ParsedValue>();
this.values.Add(parsedValue.id, entries);
}
entries.Add(parsedValue);
offset += parsedValue.offset;
parsedValue = new ParsedValue(data, offset);
}
}
public bool lastValue(byte id, out ParsedValue value) {
List<ParsedValue> entries;
if (values.TryGetValue(id, out entries)) {
value = entries[entries.Count - 1];
return true;
}
value = null;
return false;
}
}
public class BlockToSend {
private List<byte> sendData;
public BlockToSend() {
this.sendData = new List<byte>();
}
public void push(byte id, byte[] data) {
int size = data.Length;
int pos = 0;
while(size > 255) {
this.sendData.Add(id);
this.sendData.Add(255);
this.sendData.AddRange(new ArraySegment<byte>(data, pos, 255));
pos += 255;
size -= 255;
}
this.sendData.Add(id);
this.sendData.Add((byte)size);
this.sendData.AddRange(data);
}
public void acquire(byte id) {
this.sendData.Add((byte)(id ^ 0xFF));
}
public int count {
get {
return this.sendData.Count;
}
}
public byte[] data {
get {
return this.sendData.ToArray();
}
}
}
}