/
Utilities.cs
275 lines (243 loc) · 9.3 KB
/
Utilities.cs
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
#region --- License ---
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
* See license.txt for license info
*/
#endregion
using System;
using System.Collections.Generic;
using System.IO;
using System.Text.RegularExpressions;
using Bind.Structures;
using Delegate=Bind.Structures.Delegate;
using Enum=Bind.Structures.Enum;
namespace Bind
{
#region WrapperTypes enum
[Flags]
public enum WrapperTypes
{
/// <summary>
/// No wrapper needed.
/// </summary>
None = 0,
/// <summary>
/// Function takes bool parameter - C uses Int for bools, so we have to marshal.
/// </summary>
BoolParameter,
/// <summary>
/// Function takes generic parameters - add ref/out generic and generic overloads.
/// </summary>
GenericParameter,
/// <summary>
/// Function takes arrays as parameters - add ref/out and ([Out]) array overloads.
/// </summary>
ArrayParameter,
/// <summary>
/// Function with bitmask parameters. Bitmask parameters map to UInt, but since we can only use signed
/// types (for CLS compliance), we must add the unchecked keyword.
/// Usually found in bitmasks
/// </summary>
UncheckedParameter,
/// <summary>
/// Function that takes (in/ref/out) a naked pointer as a parameter - we pass an IntPtr.
/// </summary>
PointerParameter,
/// <summary>
/// Function that takes a reference to a struct.
/// </summary>
ReferenceParameter,
/// <summary>
/// Function returns string - needs manual marshalling through IntPtr to prevent the managed GC
/// from freeing memory allocated on the unmanaged side (e.g. glGetString).
/// </summary>
StringReturnType,
/// <summary>
/// Function returns a void pointer - maps to IntPtr, and the user has to manually marshal the type.
/// </summary>
GenericReturnType,
/// <summary>
/// Function returns a typed pointer - we have to copy the data to an array to protect it from the GC.
/// </summary>
ArrayReturnType
}
#endregion
public static class Utilities
{
public static readonly char[] Separators = { ' ', '\n', ',', '(', ')', ';', '#' };
public static readonly Regex Extensions = new Regex(
"ARB|EXT|ATIX|ATI|AMDX|AMD|NV|NVX|SUNX|SUN|SGIS|SGIX|SGI|MESAX|MESA|3DFX|IBM|GREMEDY|HP|INTEL|PGI|INGR|APPLE|OML|I3D|ARM|ANGLE|OES|QCOM|VIV|IMG",
RegexOptions.Compiled);
public static readonly Regex Acronyms = new Regex(Extensions.ToString() + "|EGL|3TC|DXT|ES|GL|CL|RGBA|BGRA|RGB|BGR|ETC",
RegexOptions.Compiled);
#region internal StreamReader OpenSpecFile(string file)
internal static StreamReader OpenSpecFile(string folder, string file)
{
if (String.IsNullOrEmpty(folder) || String.IsNullOrEmpty(file))
return null;
Console.WriteLine(folder);
Console.WriteLine(file);
string path = Path.Combine(folder, file);
Console.WriteLine(path);
return new StreamReader(path);
}
#endregion
#region Keywords
public static List<string> Keywords
{
get
{
switch (Settings.Language)
{
case GeneratorLanguage.CSharp: return CSharpKeywords;
case GeneratorLanguage.Cpp: return CppKeywords;
case GeneratorLanguage.Java: return JavaKeywords;
default: throw new NotImplementedException();
}
}
}
static readonly List<string> CSharpKeywords = new List<string>(
new string[]
{
"abstract", "event", "new", "struct",
"as", "explicit", "null", "switch",
"base", "extern", "object", "this",
"bool", "false", "operator", "throw",
"break", "finally", "out", "true",
"byte", "fixed", "override", "try",
"case", "float", "params", "typeof",
"catch", "for", "private", "uint",
"char", "foreach", "protected", "ulong",
"checked", "goto", "public", "unchecked",
"class", "if", "readonly", "unsafe",
"const", "implicit", "ref", "ushort",
"continue", "in", "return", "using",
"decimal", "int", "sbyte", "virtual",
"default", "interface", "sealed", "volatile",
"delegate", "internal", "short", "void",
"do", "is", "sizeof", "while",
"double", "lock", "stackalloc",
"else", "long", "static",
"enum", "namespace", "string"
}
);
static readonly List<string> JavaKeywords = new List<string>(
new string[]
{
"abstract", "continue", "for", "new", "switch",
"assert", "default", "goto", "package", "synchronized",
"boolean", "do", "if", "private", "this",
"break", "double", "implements", "protected", "throw",
"byte", "else", "import", "public", "throws",
"case", "enum", "instanceof", "return", "transient",
"catch", "extends", "int", "short", "try",
"char", "final", "interface", "static", "void",
"class", "finally", "long", "strictfp", "volatile",
"const", "float", "native", "super", "while",
"callback"
});
static readonly List<string> CppKeywords = new List<string>();
#endregion
#region Merge
// Merges the specified enum collections.
internal static void Merge(EnumCollection enums, EnumCollection new_enums)
{
foreach (var e in new_enums)
{
Merge(enums, e.Value);
}
}
/// <summary>
/// Merges the given enum into the enum list. If an enum of the same name exists,
/// it merges their respective constants.
/// </summary>
/// <param name="enums"></param>
/// <param name="t"></param>
internal static void Merge(EnumCollection enums, Enum t)
{
if (!enums.ContainsKey(t.Name))
{
enums.Add(t.Name, t);
}
else
{
Enum e = enums[t.Name];
foreach (Constant c in t.ConstantCollection.Values)
{
Merge(e, c);
}
}
}
/// <summary>
/// Places a new constant in the specified enum, if it doesn't already exist.
/// The existing constant is replaced iff the new has a numeric value and the old
/// has a reference value (eg 0x5 is preferred over AttribMask.Foo)
/// </summary>
/// <param name="s"></param>
/// <param name="t"></param>
/// <returns></returns>
internal static Enum Merge(Enum s, Constant t)
{
if (!s.ConstantCollection.ContainsKey(t.Name))
{
s.ConstantCollection.Add(t.Name, t);
}
else
{
// Tried to add a constant that already exists. If one constant
// is like: 'Foo = 0x5' and the other like: 'Foo = Bar.Foo', then
// keep the first one.
if (!Char.IsDigit(((Constant)s.ConstantCollection[t.Name]).Value[0]))
{
s.ConstantCollection.Remove(t.Name);
s.ConstantCollection.Add(t.Name, t);
}
}
return s;
}
// Merges the specified delegate collections.
internal static void Merge(DelegateCollection delegates, DelegateCollection new_delegates)
{
foreach (var d in new_delegates)
{
Merge(delegates, d.Value);
}
}
// Merges the given delegate into the delegate list.
internal static void Merge(DelegateCollection delegates, Delegate t)
{
delegates.Add(t.Name, t);
}
#endregion
#region internal static string GetGL2Extension(string name)
internal static string GetGL2Extension(string name)
{
var match = Extensions.Match(name);
if (match.Success)
{
string ext = match.Value;
if (ext.Length > 2)
{
ext = ext[0] + ext.Substring(1).ToLower();
}
return ext;
}
else
{
return String.Empty;
}
}
#endregion
#region private static bool IsGL2Extension(string function)
private static bool IsGL2Extension(string function)
{
return !String.IsNullOrEmpty(GetGL2Extension(function));
}
#endregion
#region internal static string StripGL2Extension(string p)
internal static string StripGL2Extension(string p)
{
return p.Substring(0, p.Length - GetGL2Extension(p).Length);
}
#endregion
}
}