/
FieldDefinition.cs
70 lines (60 loc) · 1.55 KB
/
FieldDefinition.cs
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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
namespace MavLinkGenerator
{
public class FieldDefinition
{
private static Regex _arrayRegEx = new Regex(@"\[(?<length>\d+)\]", RegexOptions.Compiled);
public string Type;
public string Name;
public string Description;
public int ArrayLength;
public int TypeSize;
public FieldDefinition(GeneratorBase generator, XmlNode field)
{
Type = field.GetAttribute("type");
Name = field.GetAttribute("name");
Description = field.InnerText;
ArrayLength = 0;
string alternativeName;
if (generator.ReservedWords.TryGetValue(Name, out alternativeName))
Name = alternativeName;
Match match = _arrayRegEx.Match(Type);
if (match.Success)
{
// It's an array, modify Type and remember length of array
ArrayLength = int.Parse(match.Groups["length"].Value);
Type = Type.Replace(match.Value, "");
}
Tuple<string, int> csharpType;
if (generator.TypeMap.TryGetValue(Type, out csharpType))
{
Type = csharpType.Item1;
TypeSize = csharpType.Item2;
}
if (ArrayLength > 0)
{
Type = generator.GetArrayDefinitionForType(Type, ArrayLength, TypeSize);
}
}
public bool IsArray
{
get
{
return ArrayLength > 0;
}
}
public bool IsValid
{
get
{
return !string.IsNullOrEmpty(Type) && !string.IsNullOrEmpty(Name);
}
}
public int OriginalIndex { get; set; }
}
}