Debian Codegraph @

src/vendor/azure-uamqp-c/uamqp_generator/Program.cs

raw
1 // Copyright (c) Microsoft. All rights reserved.
2 // Licensed under the MIT license. See LICENSE file in the project root for full license information.
3
4 using System;
5 using System.Collections.Generic;
6 using System.IO;
7 using System.Linq;
8 using System.Text;
9 using System.Threading.Tasks;
10 using System.Xml;
11 using System.Xml.Serialization;
12
13 namespace amqplib_generator
14 {
15 class Program
16 {
17 private static type _currentTypeObject;
18 public static type CurrentTypeObject
19 {
20 get { return _currentTypeObject; }
21 }
22
23 private static List<type> types;
24
25 public static amqp LoadAMQPTypes()
26 {
27 XmlSerializer serializer = new XmlSerializer(typeof(amqp));
28 StreamReader reader = new StreamReader("../../amqp_definitions.xml");
29 amqp amqp = (amqp)serializer.Deserialize(reader);
30 reader.Close();
31 return amqp;
32 }
33
34 public static string GetCType(string amqp_type, bool multiple)
35 {
36 string result;
37
38 if (multiple)
39 {
40 result = "AMQP_VALUE";
41 }
42 else
43 {
44 switch (amqp_type)
45 {
46 default:
47 result = amqp_type.ToLower().Replace('-', '_').Replace(':', '_');
48 break;
49
50 case "binary":
51 result = "amqp_binary";
52 break;
53
54 case "boolean":
55 result = "bool";
56 break;
57
58 case "uint":
59 result = "uint32_t";
60 break;
61
62 case "ubyte":
63 result = "uint8_t";
64 break;
65
66 case "ushort":
67 result = "uint16_t";
68 break;
69
70 case "ulong":
71 result = "uint64_t";
72 break;
73
74 case "string":
75 result = "const char*";
76 break;
77
78 case "symbol":
79 result = "const char*";
80 break;
81
82 case "map":
83 result = "AMQP_VALUE";
84 break;
85
86 case "list":
87 result = "AMQP_VALUE";
88 break;
89
90 case "*":
91 result = "AMQP_VALUE";
92 break;
93 }
94 }
95
96 return result;
97 }
98
99 public static type GetTypeByName(string type_name)
100 {
101 type result;
102 IEnumerable<type> result_query = types.Where(t => t.name == type_name);
103
104 if (result_query.Count() != 1)
105 {
106 result = null;
107 }
108 else
109 {
110 result = result_query.First();
111 }
112
113 return result;
114 }
115
116 public static descriptor GetDescriptor(type type)
117 {
118 descriptor result;
119 IEnumerable<descriptor> result_query = type.Items.Where(t => t is descriptor).Cast<descriptor>();
120 if (result_query.Count() != 1)
121 {
122 result = null;
123 }
124 else
125 {
126 result = result_query.First();
127 }
128
129 return result;
130 }
131
132 public static UInt64 GetDescriptorCode(descriptor descriptor)
133 {
134 UInt64 result;
135 string[] strings = descriptor.code.Split(new char[] { ':' });
136 result = (Convert.ToUInt64(strings[0], 16) << 32) + Convert.ToUInt64(strings[1], 16);
137 return result;
138 }
139
140 public static string GetMandatoryArgList(type type)
141 {
142 string result = string.Empty;
143
144 foreach(field field in type.Items.Where(item => (item is field) && ((item as field).mandatory == "true")))
145 {
146 if (result.Length > 0)
147 {
148 result += ", ";
149 }
150
151 result += GetCType(field.type, field.multiple == "true").Replace('-', '_').Replace(':', '_') + " " + field.name.Replace('-', '_').Replace(':', '_') + "_value";
152 }
153
154 if (string.IsNullOrEmpty(result))
155 {
156 result = "void";
157 }
158
159 return result;
160 }
161
162 public static string GetMandatoryArgListMock(type type)
163 {
164 string result = string.Empty;
165
166 foreach (field field in type.Items.Where(item => (item is field) && ((item as field).mandatory == "true")))
167 {
168 result += ", ";
169 result += GetCType(field.type, field.multiple == "true").Replace('-', '_').Replace(':', '_') + ", " + field.name.Replace('-', '_').Replace(':', '_') + "_value";
170 }
171
172 return result;
173 }
174
175 public static ICollection<KeyValuePair<field, int>> GetMandatoryArgs(type type)
176 {
177 List<KeyValuePair<field, int>> mandatory_args = new List<KeyValuePair<field, int>>();
178 int fieldCount = 0;
179 for (int i = 0; i < type.Items.Length; i++)
180 {
181 if (type.Items[i] is field)
182 {
183 if ((type.Items[i] as field).mandatory == "true")
184 {
185 mandatory_args.Add(new KeyValuePair<field, int>(type.Items[i] as field, fieldCount));
186 }
187
188 fieldCount++;
189 }
190 }
191 return mandatory_args;
192 }
193
194 static void Main(string[] args)
195 {
196 amqp amqp = LoadAMQPTypes();
197
198 foreach (section section in amqp.Items.Where(item => item is section))
199 {
200 types = new List<type>();
201 types.AddRange(section.Items.Where(item => item is type).Cast<type>());
202 foreach (type type in types)
203 {
204 amqp_definitions_type_h amqp_definitions_type_h = new amqp_definitions_type_h();
205
206 _currentTypeObject = type;
207 System.IO.File.WriteAllText($"../../../inc/azure_uamqp_c/amqp_definitions_{type.name.Replace("-","_")}.h", amqp_definitions_type_h.TransformText());
208
209 }
210 }
211
212 amqp_definitions_h amqp_definitions_h = new amqp_definitions_h();
213 System.IO.File.WriteAllText("../../../inc/azure_uamqp_c/amqp_definitions.h", amqp_definitions_h.TransformText());
214
215 amqp_definitions_c amqp_definitions_c = new amqp_definitions_c();
216 System.IO.File.WriteAllText("../../../src/amqp_definitions.c", amqp_definitions_c.TransformText());
217 }
218 }
219 }