-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathProgram.cs
More file actions
274 lines (246 loc) · 11.1 KB
/
Program.cs
File metadata and controls
274 lines (246 loc) · 11.1 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
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
using System;
using System.IO;
using System.Linq;
using System.Collections.Generic;
using DataStructures;
using System.Text;
namespace CToPython
{
class Program
{
public static Dictionary<string, string> datatypes = new Dictionary<string, string>();
static void Main(string[] args)
{
try
{
datatypes.Add("CHAR", "c_char");
datatypes.Add("BYTE", "c_ubyte");
datatypes.Add("Array", "c_char*");
processHeader("cpp.h", "python.py");
} catch (Exception e) {
Console.WriteLine(e);
}
}
private static void processHeader(string headerFile, string pythonFile) {
if (File.Exists(headerFile)) {
var content = File.ReadAllLines(headerFile);
if (content.Length > 0) {
Header header = new Header();
Comment comment = null;
for (int i = 0; i < content.Length; i++) {
var line = content[i].Trim();
if (line.StartsWith("//"))
{
if(comment == null)
{
comment = new Comment();
}
comment.Content += line.Replace("//", "").Trim();
}
if (line.StartsWith("const"))
{
var cons = line.Split(" ");
header.Expressions.Enqueue(new Const
{
Name = cons[2],
Value = cons[4].Substring(0, cons[4].IndexOf(";")),
Comment = comment
});
comment = null;
}
else if (line.StartsWith("#define"))
{
// #define name value
var define = line.Split(" ");
header.Expressions.Enqueue(new Define
{
Name = define[1],
Value = define[2],
Comment = comment
});
comment = null;
}
else if (line.StartsWith("typedef struct"))
{
// #typedef struct {
// type name;
// } name, *ptr;
var structure = line.Split(" ");
var fields = new List<Field>();
line = content[++i];
do
{
var tokens = line.Trim().Split(" ");
try
{
var _field = new Field();
if (tokens[0].StartsWith("Array"))
{
var _ctype = tokens[0];
int _len = int.Parse(_ctype.Substring(5)) + 1;
_field.Type = datatypes["Array"] + (_len);
}
else
{
_field.Type = datatypes[tokens[0]];
}
_field.Name = tokens[1].Replace(";", "");
fields.Add(_field);
}
catch (Exception e)
{
Console.WriteLine(e.ToString());
}
line = content[++i];
} while (!(line.Contains("}") && line.Contains(";")));
line = line.Replace("}", "").Replace(";", "");
var names = line.Split(",");
header.Expressions.Enqueue(new Structure
{
Name = names[0].Trim(),
Fields = fields,
Comment = comment
});
comment = null;
}
else if (line.StartsWith("typedef VOID")
|| line.StartsWith("typedef INT")
|| line.StartsWith("typedef LPCSTR"))
{
// Ignore
}
else if (line.StartsWith("typedef"))
{
// #typedef type alias
var typedef = line.Split(" ");
header.Expressions.Enqueue(new TypeDefine
{
Type = typedef[1],
Alias = typedef[2].Replace(";", ""),
Comment = comment
});
comment = null;
}
else if (line.StartsWith("VOID WINAPI")) {
// VOID WINAPI name(paramname name);
// name = CFUNCTRPE(None, POINTE(paramname))
line = line.Replace("VOID WINAPI", "").Trim();
var callback = line.Split("(");
var _cb = new Callback();
// _cb.Name = callback[2].Substring(0, callback[2].IndexOf('('));
_cb.Name = callback[0];
var param = new Parameter();
if (callback[1] == ");")
{
param.Name = "None";
}
else
{
param.Name = callback[1].Split(" ")[0].Replace("Ptr", "").Trim();
}
_cb.Param = new List<Parameter> { param };
_cb.Comment = comment;
comment = null;
header.Expressions.Enqueue(_cb);
}
}
writePython(header, pythonFile);
Console.WriteLine("Processed header and written in Python file.");
} else {
Console.WriteLine("Header file is empty.");
}
} else {
Console.WriteLine("File {0} does not exists.", headerFile);
}
}
private static void writePython(Header header, string pythonFile) {
if(header == null || string.IsNullOrEmpty(pythonFile)) {
return;
}
StringBuilder builder = new StringBuilder();
while (header.Expressions.Count > 0)
{
var expression = header.Expressions.Dequeue();
if(expression is Const)
{
//appendSectionSeparator(builder, "Consts");
var cons = expression as Const;
if (cons.Comment != null)
{
builder.AppendLine();
builder.AppendLine($"# {cons.Comment.Content}");
}
builder.AppendLine($"{cons.Name} = {cons.Value}");
} else if (expression is Define)
{
//appendSectionSeparator(builder, "Defines");
var define = expression as Define;
if (define.Comment != null)
{
builder.AppendLine();
builder.AppendLine($"# {define.Comment.Content}");
}
builder.AppendLine($"{define.Name} = {define.Value}");
} else if (expression is TypeDefine)
{
//appendSectionSeparator(builder, "Typedefs");
var typedef = expression as TypeDefine;
if (typedef.Comment != null)
{
builder.AppendLine();
builder.AppendLine($"# {typedef.Comment.Content}");
}
builder.AppendLine($"{typedef.Alias} = {typedef.Type}");
} else if (expression is Structure)
{
//appendSectionSeparator(builder, "Structures");
var structure = expression as Structure;
if (structure.Comment != null)
{
builder.AppendLine();
builder.AppendLine($"# {structure.Comment.Content}");
}
builder.AppendLine($"class {structure.Name}(Structure):");
builder.Append(" _fields_ = [");
for (int i = 0; i < structure.Fields.Count; i++)
{
var field = structure.Fields[i];
if (i == structure.Fields.Count - 1)
{
builder.Append($"(\"{field.Name}\", {field.Type})");
}
else
{
builder.Append($"(\"{field.Name}\", {field.Type}),\n\t\t\t ");
}
}
builder.AppendLine("]");
} else if (expression is Callback)
{
//appendSectionSeparator(builder, "Callbacks");
var callback = expression as Callback;
if (expression.Comment != null)
{
builder.AppendLine();
builder.AppendLine($"# {expression.Comment.Content}");
}
builder.AppendLine($"{callback.Name} = CFUNCTYPE(None, POINTER({callback.Param[0].Name}))");
}
}
File.WriteAllText(pythonFile, builder.ToString());
}
#region Helper Method
private static void dicDatatypes()
{
datatypes = new Dictionary<string, string>();
datatypes.Add("CHAR", "c_char");
datatypes.Add("BYTE", "c_ubyte");
datatypes.Add("Array", "c_char*");
}
private static void appendSectionSeparator(StringBuilder builder , string section)
{
builder.AppendLine($"\n############################################## {section} ##############################################\n");
}
#endregion Helper Method
}
}