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
|
// A Qt to C# binding generator.
//
// Copyright (C) 2002 Adam Treat ([email protected])
//
// This program is free software; you can redistribute it and/or
// modify it under the terms of the GNU General Public License
// as published by the Free Software Foundation; either version 2
// of the License, or (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//
// You should have received a copy of the GNU General Public License
// along with this program; if not, write to the Free Software
// Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
using System;
using System.Text;
using System.Collections;
using System.Collections.Specialized;
//using System.Text.RegularExpressions;
namespace QtCSharp {
public class Converter {
ArrayList qtypes;
TQType qtype;
TQTypeMap qmap;
StringCollection sigs;
StringBuilder sig;
public Converter (ArrayList qtypes, TQType qtype, TQTypeMap qmap)
{
this.qtypes = qtypes;
this.qtype = qtype;
this.qmap = qmap;
sigs = new StringCollection ();
sig = new StringBuilder ();
if (!qtype.IsConverted)
Convert ();
Ancestors ();
qtype.IsConverted = true;
}
public TQType GetTQType ()
{
return qtype;
}
public void Convert ()
{
foreach (TQCtor qctor in qtype.TQCtors) {
qctor.Name = qmap.ReservedType (qctor.Name);
if (!qctor.Overload) {
ConvertCSharpParams (qctor.CSharpParams);
ConvertPinvokeCallParams (qctor.PinvokeCallParams);
ConvertPinvokeParams (qctor.PinvokeParams);
} else {
ConvertOverloadParams (qctor.OverloadParams);
}
CheckSig (qctor);
}
foreach (TQMethod qmethod in qtype.TQMethods) {
if (qmethod.Name.StartsWith ("protected_"))
qmethod.Name = qmethod.Name.Replace ("protected_", "");
qmethod.Name = qmap.ReservedType (qmethod.Name);
if (!qmethod.Overload) {
ConvertCSharpParams (qmethod.CSharpParams);
ConvertPinvokeCallParams (qmethod.PinvokeCallParams);
ConvertPinvokeParams (qmethod.PinvokeParams);
ConvertReturnType (qmethod);
} else {
ConvertOverloadParams (qmethod.OverloadParams);
ConvertReturnType (qmethod);
}
qmethod.PascalName = ToPascalCase (qmethod.Name);
CheckSig (qmethod);
}
}
public void CheckSig (TQMethod qmethod)
{
sig.Append (qmethod.PascalName);
foreach (TQParam qparam in qmethod.CSharpParams) {
sig.Append (qparam.Type);
}
if (!sigs.Contains (sig.ToString ()) && !sigs.Contains ("The"+sig.ToString ())) {
sigs.Add (sig.ToString ());
} else {
Console.WriteLine ("Throttling "+qtype.Name+" "+qmethod.PascalName);
qmethod.Throttle = true;
}
sig.Length = 0;
}
public void CheckSig (TQCtor qctor)
{
sig.Append (qctor.Name);
foreach (TQParam qparam in qctor.CSharpParams) {
//if (qparam.Type == "TTQWidget" && qparam.Name == "parent")
if (qparam.Name == "parent")
qctor.Parent = true;
sig.Append (qparam.Type);
}
if (!sigs.Contains (sig.ToString ()))
sigs.Add (sig.ToString ());
else {
Console.WriteLine ("Throttling "+qtype.Name+" "+qctor.Access+" "+qctor.Name);
qctor.Throttle = true;
}
sig.Length = 0;
}
public void ConvertCSharpParams (ArrayList qparams)
{
foreach (TQParam qparam in qparams) {
qparam.Type = qmap.ArrayType (qparam.Type);
qparam.Type = StripBad (qparam.Type);
qparam.Type = qmap.CSharpType (qparam.Type);
qparam.Type = ConvertTQString (qparam.Type);
qparam.Name = qmap.ReservedType (qparam.Name);
}
}
public void ConvertPinvokeCallParams (ArrayList qparams)
{
foreach (TQParam qparam in qparams) {
qparam.Type = qmap.ArrayType (qparam.Type);
qparam.Type = StripBad (qparam.Type);
qparam.Type = qmap.CSharpType (qparam.Type);
qparam.Name = qmap.ReservedType (qparam.Name);
if (IsTQObject (qparam.Type))
qparam.Name = qparam.Name + ".RawObject";
if (IsITQObject (qparam.Type))
qparam.Name = qparam.Name + "." + StripInterface (qparam.Type) + " ()";
/* if (IsTQString (qparam.Type))
qparam.Name = "new TTQString ("+StripPtr(qparam.Name)+").RawObject";*/
qparam.Type = "";
}
}
public void ConvertPinvokeParams (ArrayList qparams)
{
foreach (TQParam qparam in qparams) {
qparam.Type = qmap.ArrayType (qparam.Type);
qparam.Type = StripBad (qparam.Type);
qparam.Type = qmap.PinvokeType (qparam.Type);
qparam.Name = qmap.ReservedType (qparam.Name);
if (IsTQObject (qparam.Type) || IsITQObject (qparam.Type))
qparam.Type = "IntPtr";
}
}
public void ConvertOverloadParams (ArrayList qparams)
{
foreach (TQParam qparam in qparams) {
qparam.Type = qmap.ArrayType (qparam.Type);
qparam.Type = StripBad (qparam.Type);
qparam.Type = qmap.CSharpType (qparam.Type);
OverloadedLastParam (qparam, qparams);
OverloadedNull (qparam);
OverloadedTQString (qparam);
OverloadedTQObject (qparam);
OverloadedNestedEnum (qparam);
OverloadedNullString (qparam);
OverloadedBool (qparam);
OverloadedEnum (qparam);
OverloadedArray (qparam);
OverloadedHex (qparam);
OverloadedDefault (qparam);
}
}
public void OverloadedLastParam (TQParam qparam, ArrayList qparams)
{
if (qparams.IndexOf (qparam) != qparams.Count - 1)
qparam.Default = null;
}
public void OverloadedNull (TQParam qparam)
{
if (qparam.Default == null)
qparam.Type = "";
}
public void OverloadedTQString (TQParam qparam)
{
if (IsTQString (qparam.Type)){
qparam.Type = "TTQString";
if (qparam.Default == "TTQString::null")
qparam.Default = "null";
else if (qparam.Default == "quotquot")
qparam.Default = "null";
else
qparam.Default = "\""+qparam.Default+"\"";
}
}
public void OverloadedTQObject (TQParam qparam)
{
if (IsTQObject (qparam.Type)) {
qparam.Name = "new "+qparam.Type+" ()";
qparam.Type = "";
}
}
public void OverloadedNestedEnum (TQParam qparam)
{
foreach (TQEnum qenum in qtype.TQEnums) {
if (qparam.Type == qenum.Name) {
foreach (TQItem qitem in qenum.TQItems) {
if (qparam.Default == qitem.Name) {
qparam.Name = qparam.Type+"."+qparam.Default;
qparam.Type = "";
}
}
}
}
}
public void OverloadedNullString (TQParam qparam)
{
if (qmap.OverloadType (qparam.Type) == "string" && qparam.Default == "0") {
qparam.Type = "";
qparam.Name = "\"\"";
}
}
public void OverloadedBool (TQParam qparam)
{
if (qparam.Default == "TRUE") {
qparam.Type = "";
qparam.Name = "true";
} else if (qparam.Default == "FALSE") {
qparam.Type = "";
qparam.Name = "false";
} else if (qparam.Type == "bool" && qparam.Default == "1") {
qparam.Type = "";
qparam.Name = "true";
} else if (qparam.Type == "bool" && qparam.Default == "0") {
qparam.Type = "";
qparam.Name = "false";
}
}
public void OverloadedEnum (TQParam qparam)
{
if (IsEnum (qparam.Type)) {
qparam.Name = qparam.Type + "." + EnumValue (qparam.Type, qparam.Default);
qparam.Type = "";
}
}
public void OverloadedArray (TQParam qparam)
{
if (IsArray (qparam.Type)) {
qparam.Name = "new "+qparam.Type+"{"+qparam.Default+"}";
qparam.Type = "";
}
}
public void OverloadedHex (TQParam qparam)
{
if (qparam.Default == "0xffffffff")
qparam.Default = "1";
}
public void OverloadedDefault (TQParam qparam)
{
if (qparam.Type != "") {
qparam.Type = "("+qmap.OverloadType (qparam.Type)+")";
qparam.Name = qparam.Default;
}
}
public void ConvertReturnType (TQMethod qmethod)
{
qmethod.Return = qmap.ArrayType (qmethod.Return);
qmethod.Return = qmap.PinvokeType (StripBad (qmethod.Return));
if (IsTQObject(qmethod.Return)) {
qmethod.Boxer = true;
qmethod.PinvokeReturn = "IntPtr";
} else {
qmethod.PinvokeReturn = qmethod.Return;
}
if (qmethod.Return == "TTQString") {
qmethod.TQStringReturn = true;
}
}
public string StripBad (string str)
{
str = StripPointer (str);
str = StripColon (str);
return str;
}
public string StripPointer (string str)
{
str = str.Replace ("*", "");
str = str.Replace ("&", "");
if (str.StartsWith ("amp"))
str = str.Replace ("amp", "");
if (str.EndsWith ("amp"))
str = str.Replace ("amp", "");
return str;
}
public string ConvertTQString (string str)
{
if (IsTQString (str))
return "TTQString";
else
return str;
}
public string StripColon (string str)
{
return str = str.Replace ("::", ".");
}
public string StripPtr (string str)
{
return str = str.Replace (".RawObject", "");
}
public string StripInterface (string str)
{
return str = str.Replace ("I", "");
}
public string StripEnum (string str)
{
str = StripColon (str);
if (str.IndexOf (".") > 0)
return str.Substring (str.IndexOf (".")+1);
else
return str;
}
public string ToPascalCase (string name)
{
string pascal = System.Char.ToUpper (name[0]).ToString ()+name.Substring (1, name.Length -1);
foreach (TQEnum qenum in qtype.TQEnums) {
if (pascal == qenum.Name)
pascal = "The"+pascal;
}
return pascal;
}
public string EnumValue (string type, string value)
{
bool match = false;
string enumname = StripEnum (type);
value = StripEnum (value);
// There _has_ to be a better way, but I'm tired...
foreach (TQType qtype in qtypes) {
foreach (TQEnum qenum in qtype.TQEnums) {
if (enumname == qenum.Name) {
foreach (TQItem qitem in qenum.TQItems) {
if (value == qitem.Name) {
match = true;
}
}
if (!match) {
foreach (TQItem qitem in qenum.TQItems) {
value = qitem.Name;
break;
}
}
}
}
}
return value;
}
public void Ancestors ()
{
if (qtype.IsInterface || qtype.TQAncestors.Count < 2)
return;
string iname = "";
foreach (TQAncestor qancestor in qtype.TQAncestors) {
iname = qmap.InterfaceType (qancestor.Name);
foreach (TQType _qtype in qtypes) {
if (_qtype.Name == qancestor.Name && iname != qancestor.Name) {
if (!_qtype.IsConverted) {
Converter converter = new Converter (qtypes, _qtype, qmap);
}
qtype.AddTQMethod (instPointer (qancestor.Name));
qancestor.TQMethods = _qtype.TQMethods;
qancestor.IsInterface = true;
qancestor.IName = iname;
foreach (TQMethod qmethod in qancestor.TQMethods) {
CheckSig (qmethod);
}
}
}
}
}
public TQMethod instPointer (string name)
{
TQMethod qmethod = new TQMethod ();
qmethod.Name = name;
qmethod.PascalName = name;
qmethod.Access = "public";
qmethod.PinvokeReturn = "IntPtr";
qmethod.Return = "IntPtr";
qmethod.Id = "0";
return qmethod;
}
public bool IsTQString (string str)
{
if (qtype.Name == "TTQString")
return true;
else if (IsTQObject (str) && str == "TTQString")
return true;
else
return false;
}
public bool IsTQObject (string str)
{
//IndexOf is a hack to search for a char ;-)
if (str.StartsWith ("Q") && str.IndexOf (".") < 0)
return true;
else
return false;
}
public bool IsITQObject (string str)
{
//IndexOf is a hack to search for a char ;-)
if (str == "IntPtr") return false;
if (str.StartsWith ("I") && str.IndexOf (".") < 0)
return true;
else
return false;
}
public bool IsEnum (string str)
{
//IndexOf is a hack to search for a char ;-)
if (str.IndexOf (".") > 0)
return true;
else
return false;
}
public bool IsArray (string str)
{
if (str.EndsWith ("[]"))
return true;
else
return false;
}
}
}
|