-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathGeneratorBase.cs
More file actions
104 lines (86 loc) · 2.93 KB
/
GeneratorBase.cs
File metadata and controls
104 lines (86 loc) · 2.93 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
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Text.RegularExpressions;
using System.Xml;
using System.Reflection;
using System.IO;
namespace MavLinkGenerator
{
public abstract class GeneratorBase
{
protected abstract string LoadTemplate();
protected abstract void GenerateEnums(XmlDocument document, ref string template);
protected abstract void GenerateMessages(XmlDocument document, ref string template);
public abstract string GetArrayDefinitionForType(string type, int arrayLength, int typeSize);
public abstract Dictionary<string, Tuple<string, int>> TypeMap { get; }
public abstract Dictionary<string, string> ReservedWords { get; }
public void Generate(string fileName, string xmlDefinitionFile)
{
if (File.Exists(fileName))
File.Delete(fileName);
XmlDocument document = new XmlDocument();
document.Load(xmlDefinitionFile);
string template = LoadTemplate();
GenerateEnums(document, ref template);
GenerateMessages(document, ref template);
File.WriteAllText(fileName, template);
}
protected string CamelCase(string name)
{
string[] words = name.Split('_', ' ', '-', '.');
return string.Join("", words.Select(w => w.Substring(0, 1).ToUpper() + w.Substring(1).ToLower()));
}
protected string lowerCamelCase(string name)
{
string camelCase = CamelCase(name);
return camelCase.Substring(0, 1).ToLower() + camelCase.Substring(1);
}
protected string LoadTemplate(string name)
{
Assembly assembly = Assembly.GetExecutingAssembly();
string[] resourceNames = assembly.GetManifestResourceNames();
name = name.ToLower();
foreach (string resourceName in resourceNames)
{
if (resourceName.ToLower().EndsWith(name.ToLower()))
{
using (Stream stream = assembly.GetManifestResourceStream(resourceName))
using (StreamReader reader = new StreamReader(stream))
{
return reader.ReadToEnd();
}
}
}
return null;
}
protected string Indent(int level)
{
return new string('\t', level);
}
protected List<FieldDefinition> GetFields(XmlNode messageNode)
{
XmlNodeList fieldNodes = messageNode.SelectNodes("field");
List<FieldDefinition> fields = new List<FieldDefinition>();
foreach (XmlNode field in fieldNodes)
{
FieldDefinition fieldDefinition = new FieldDefinition(this, field);
if (fieldDefinition.IsValid)
{
fieldDefinition.OriginalIndex = fields.Count;
fields.Add(fieldDefinition);
}
}
// Sort fields at the beginning so we don't need to care about the ordering when serializing/deserializing
fields.Sort((left, right) =>
{
int compareResult = -left.TypeSize.CompareTo(right.TypeSize);
if (compareResult == 0)
return left.OriginalIndex.CompareTo(right.OriginalIndex);
return compareResult;
});
return fields;
}
}
}