-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbindgen.pas
More file actions
478 lines (387 loc) · 11.3 KB
/
Copy pathbindgen.pas
File metadata and controls
478 lines (387 loc) · 11.3 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
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
program bindgen;
uses
DOM,
XMLRead,
FGL,
Sysutils;
type
TPropDict = specialize TFPGMap<String, TDOMElement>;
var
XML : TXMLDocument;
Prop : TPropDict;
StructOut, EnumOut, PropOut, ConstOut, FuncDefOut, VarOut, FuncOut : TextFile;
function PropToString(PropName : String) : String;
begin
PropToString := '';
if Prop[PropName].NodeName = 'integer' then PropToString := 'I';
if Prop[PropName].NodeName = 'string' then PropToString := 'S';
if Prop[PropName].NodeName = 'pixmap' then PropToString := 'V';
if (Prop[PropName].NodeName = 'struct') and (Prop[PropName].GetAttribute('pointer') = 'yes') then PropToString := 'V';
if Prop[PropName].NodeName = 'handler' then PropToString := 'C';
if Prop[PropName].GetAttribute('early') = 'yes' then PropToString := PropToString + 'E';
PropToString := PropToString + PropName;
end;
function TypeToPascal(Node : TDOMNode) : String;
begin
TypeToPascal := '';
if TDOMElement(Node).GetAttribute('pointer') = 'yes' then TypeToPascal := TypeToPascal + 'P';
if Node.NodeName = 'struct' then TypeToPascal := TypeToPascal + String(TDOMElement(Node).GetAttribute('defname'));
if (Node.NodeName = 'integer') and (TDOMElement(Node).GetAttribute('unsigned') = 'yes') then TypeToPascal := TypeToPascal + 'Cardinal';
if (Node.NodeName = 'integer') and not(TDOMElement(Node).GetAttribute('unsigned') = 'yes') then TypeToPascal := TypeToPascal + 'Integer';
if (Node.NodeName = 'short') and (TDOMElement(Node).GetAttribute('unsigned') = 'yes') then TypeToPascal := TypeToPascal + 'Word';
if (Node.NodeName = 'short') and not(TDOMElement(Node).GetAttribute('unsigned') = 'yes') then TypeToPascal := TypeToPascal + 'Smallint';
if Node.NodeName = 'string' then TypeToPascal := TypeToPascal + 'PChar';
if Node.NodeName = 'class' then TypeToPascal := TypeToPascal + 'MwClass';
if Node.NodeName = 'pointer' then TypeToPascal := TypeToPascal + 'Pointer';
if Node.NodeName = 'widget' then TypeToPascal := TypeToPascal + 'MwWidget';
if Node.NodeName = 'pixmap' then TypeToPascal := TypeToPascal + 'MwLLPixmap';
if Node.NodeName = 'handler' then TypeToPascal := TypeToPascal + 'MwUserHandler';
if Node.NodeName = 'error_handler' then TypeToPascal := TypeToPascal + 'MwErrorHandler';
end;
procedure ScanProperties();
var
Child : TDOMNode;
List : TDOMNodeList;
Suffix : String;
begin
List := XML.DocumentElement.GetElementsByTagName('properties');
WriteLn(PropOut, 'const');
Child := List[0].FirstChild;
while Assigned(Child) do
begin
if not(Child.NodeType = ELEMENT_NODE) then
begin
Child := Child.NextSibling;
continue;
end;
WriteLn('Property ' + TDOMElement(Child).GetAttribute('name'));
Suffix := '';
if TDOMElement(Child).TagName = 'handler' then
begin
Suffix := 'handler';
end;
Prop[String(TDOMElement(Child).GetAttribute('name'))] := TDOMElement(Child);
WriteLn(PropOut, ' MwN' + String(TDOMElement(Child).GetAttribute('name')) + Suffix + ' : PChar = ''' + PropToString(String(TDOMElement(Child).GetAttribute('name'))) + ''';');
Child := Child.NextSibling;
end;
List.Free();
end;
function IntegerTrans(Content : String) : String;
begin
IntegerTrans := Content;
if (Length(Content) > 2) and (Copy(Content, 1, 2) = '0x') then
begin
IntegerTrans := '$' + Copy(Content, 3);
end;
end;
procedure ScanEnumeration(Node : TDOMNode);
var
Child : TDOMNode;
Content : String;
EnumName : String;
begin
EnumName := String(TDOMElement(Node).GetAttribute('name'));
WriteLn('Enumeration ' + EnumName);
Write(EnumOut, ' ' + EnumName + '_ENUM = (');
Child := Node.FirstChild;
while Assigned(Child) do
begin
Write(EnumOut, TDOMElement(Child).GetAttribute('name'));
Content := String(Child.TextContent);
if Length(Content) > 0 then
begin
Write(EnumOut, ' := ' + IntegerTrans(Content));
end;
if Assigned(Child.NextSibling) then Write(EnumOut, ', ');
Child := Child.NextSibling;
end;
WriteLn(EnumOut, ');');
end;
procedure ScanEnumerations();
var
Child : TDOMNode;
List : TDOMNodeList;
begin
List := XML.DocumentElement.GetElementsByTagName('enumerations');
WriteLn(EnumOut, 'type');
Child := List[0].FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'enumeration' then ScanEnumeration(Child);
Child := Child.NextSibling;
end;
List.Free();
end;
procedure ScanConstants();
var
Child : TDOMNode;
List : TDOMNodeList;
begin
List := XML.DocumentElement.GetElementsByTagName('constants');
WriteLn(ConstOut, 'const');
Child := List[0].FirstChild;
while Assigned(Child) do
begin
WriteLn(ConstOut, ' ' + String(TDOMElement(Child).GetAttribute('name')) + ' : Cardinal = ' + IntegerTrans(String(Child.TextContent)) + ';');
Child := Child.NextSibling;
end;
List.Free();
end;
procedure ScanStruct(Node : TDOMNode);
var
Child : TDOMNode;
StructName : String;
begin
StructName := String(TDOMElement(Node).GetAttribute('name'));
WriteLn('Struct ' + StructName);
WriteLn(StructOut, ' ' + StructName + ' = packed record');
Child := Node.FirstChild;
while Assigned(Child) do
begin
WriteLn(StructOut, ' ' + String(TDOMElement(Child).GetAttribute('name')) + ' : ' + TypeToPascal(Child) + ';');
Child := Child.NextSibling;
end;
WriteLn(StructOut, ' end;');
WriteLn(StructOut, ' P' + StructName + ' = ^' + StructName + ';');
end;
procedure ScanStructs();
var
Child : TDOMNode;
List : TDOMNodeList;
begin
List := XML.DocumentElement.GetElementsByTagName('structs');
WriteLn(StructOut, 'type');
Child := List[0].FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'struct' then ScanStruct(Child);
Child := Child.NextSibling;
end;
List.Free();
end;
function GetReturn(Node : TDOMNode) : TDOMNode;
var
List : TDOMNodeList;
begin
List := TDOMElement(Node).GetElementsByTagName('return');
GetReturn := List[0].FirstChild;
List.Free();
end;
function HasReturn(Node : TDOMNode) : Boolean;
var
List : TDOMNodeList;
begin
List := TDOMElement(Node).GetElementsByTagName('return');
HasReturn := List.Count > 0;
List.Free();
end;
function GetArgs(Node : TDOMNode) : TDOMNode;
var
List : TDOMNodeList;
begin
List := TDOMElement(Node).GetElementsByTagName('arguments');
GetArgs := List[0];
List.Free();
end;
function HasArgs(Node : TDOMNode) : Boolean;
var
List : TDOMNodeList;
begin
List := TDOMElement(Node).GetElementsByTagName('arguments');
HasArgs := List.Count > 0;
List.Free();
end;
procedure ScanFunction(Prefix : String; Node : TDOMNode);
var
FuncName : String;
Ret : TDOMNode;
Child : TDOMNode;
HasVa : Boolean;
FuncDecl : String;
begin
FuncName := Prefix + String(TDOMElement(Node).GetAttribute('name'));
FuncDecl := '';
WriteLn('Function ' + FuncName);
if HasReturn(Node) then
begin
FuncDecl := FuncDecl + 'function ';
Ret := GetReturn(Node);
end
else FuncDecl := FuncDecl + 'procedure ';
FuncDecl := FuncDecl + FuncName;
HasVa := False;
FuncDecl := FuncDecl + '(';
if Length(Prefix) > 0 then
begin
FuncDecl := FuncDecl + 'handle : MwWidget';
if HasArgs(Node) then
begin
FuncDecl := FuncDecl + '; ';
end;
end;
if HasArgs(Node) then
begin
Child := GetArgs(Node).FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'variable' then
begin
HasVa := True;
end
else
begin
FuncDecl := FuncDecl + String(TDOMElement(Child).GetAttribute('name')) + ' : ' + TypeToPascal(Child);
if (Assigned(Child.NextSibling)) and not(Child.NextSibling.NodeName = 'variable') then FuncDecl := FuncDecl + '; ';
end;
Child := Child.NextSibling;
end;
end;
FuncDecl := FuncDecl + ')';
if HasReturn(Node) then
begin
FuncDecl := FuncDecl + ' : ' + TypeToPascal(Ret);
end;
if Length(Prefix) = 0 then
begin
FuncDecl := FuncDecl + '; cdecl; ';
if HasVa then FuncDecl := FuncDecl + 'varargs; ';
FuncDecl := FuncDecl + 'external name ''' + FuncName + ''';';
end
else
begin
FuncDecl := FuncDecl + ';';
WriteLn(FuncOut, FuncDecl);
if HasReturn(Node) then
begin
WriteLn(FuncOut, 'var');
WriteLn(FuncOut, ' RetVal : ' + TypeToPascal(Ret) + ';');
end;
WriteLn(FuncOut, 'begin');
Write(FuncOut, ' MwVaWidgetExecute(handle, ''m' + Copy(FuncName, 2) + ''', ');
if HasReturn(Node) then
begin
Write(FuncOut, 'Pointer(@RetVal)');
end
else Write(FuncOut, 'Nil');
if HasArgs(Node) then
begin
Child := GetArgs(Node).FirstChild;
while Assigned(Child) do
begin
Write(FuncOut, ', ' + String(TDOMElement(Child).GetAttribute('name')));
Child := Child.NextSibling;
end;
end;
WriteLn(FuncOut, ');');
if HasReturn(Node) then
begin
WriteLn(FuncOut, FuncName + ' := RetVal;');
end;
WriteLn(FuncOut, 'end;');
WriteLn(FuncOut, '');
end;
WriteLn(FuncDefOut, FuncDecl);
end;
procedure ScanFunctions(Prefix : String; Node : TDOMNode);
var
Child : TDOMNode;
begin
Child := Node.FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'function' then ScanFunction(Prefix, Child);
Child := Child.NextSibling;
end;
end;
procedure ScanHeader(Node : TDOMNode);
var
Child : TDOMNode;
begin
WriteLn('Header ' + TDOMElement(Node).GetAttribute('name'));
WriteLn(FuncDefOut, '(* Header ' + TDOMElement(Node).GetAttribute('name') + ' *)');
Child := Node.FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'functions' then ScanFunctions('', Child);
Child := Child.NextSibling;
end;
WriteLn(FuncDefOut, '');
end;
procedure ScanHeaders();
var
Child : TDOMNode;
List : TDOMNodeList;
begin
List := XML.DocumentElement.GetElementsByTagName('headers');
Child := List[0].FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'header' then ScanHeader(Child);
Child := Child.NextSibling;
end;
List.Free();
end;
procedure ScanWidget(Node : TDOMNode);
var
Child : TDOMNode;
begin
WriteLn('Widget ' + TDOMElement(Node).GetAttribute('name'));
WriteLn(FuncDefOut, '(* Widget ' + TDOMElement(Node).GetAttribute('name') + ' *)');
WriteLn(FuncOut, '(* Widget ' + TDOMElement(Node).GetAttribute('name') + ' *)');
Child := Node.FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'functions' then ScanFunctions('Mw' + String(TDOMElement(Node).GetAttribute('name')), Child);
Child := Child.NextSibling;
end;
WriteLn(FuncDefOut, '');
WriteLn(FuncOut, '');
WriteLn(VarOut, ' Mw' + TDOMElement(Node).GetAttribute('name') + 'Class : Pointer; external name ''Mw' + TDOMElement(Node).GetAttribute('name') + 'Class'';');
end;
procedure ScanWidgets();
var
Child : TDOMNode;
List : TDOMNodeList;
begin
List := XML.DocumentElement.GetElementsByTagName('widgets');
WriteLn(VarOut, 'var');
Child := List[0].FirstChild;
while Assigned(Child) do
begin
if Child.NodeName = 'widget' then ScanWidget(Child);
Child := Child.NextSibling;
end;
List.Free();
end;
begin
AssignFile(StructOut, 'src/structh.inc');
AssignFile(PropOut, 'src/proph.inc');
AssignFile(EnumOut, 'src/enumh.inc');
AssignFile(ConstOut, 'src/consth.inc');
AssignFile(FuncDefOut, 'src/funch.inc');
AssignFile(VarOut, 'src/varh.inc');
AssignFile(FuncOut, 'src/func.inc');
Rewrite(StructOut);
Rewrite(PropOut);
Rewrite(EnumOut);
Rewrite(ConstOut);
Rewrite(FuncDefOut);
Rewrite(VarOut);
Rewrite(FuncOut);
Prop := TPropDict.Create();
ReadXMLFile(XML, '../milsko/milsko.xml');
ScanStructs();
ScanProperties();
ScanEnumerations();
ScanConstants();
ScanHeaders();
ScanWidgets();
XML.Free();
CloseFile(FuncOut);
CloseFile(VarOut);
CloseFile(FuncDefOut);
CloseFile(ConstOut);
CloseFile(EnumOut);
CloseFile(PropOut);
CloseFile(StructOut);
end.