Subversion Repositories AndroidProjects

Rev

Details | Last modification | View Log | RSS feed

Rev Author Line No. Line
1452 chris 1
#region --- License ---
2
/* Copyright (c) 2006, 2007 Stefanos Apostolopoulos
3
 * See license.txt for license info
4
 */
5
#endregion
6
 
7
using System;
8
using System.Collections.Generic;
9
using System.IO;
10
using System.Text.RegularExpressions;
11
using Bind.Structures;
12
using Delegate=Bind.Structures.Delegate;
13
using Enum=Bind.Structures.Enum;
14
 
15
namespace Bind
16
{
17
    #region WrapperTypes enum
18
 
19
    [Flags]
20
    public enum WrapperTypes
21
    {
22
        /// <summary>
23
        /// No wrapper needed.
24
        /// </summary>
25
        None = 0,
26
        /// <summary>
27
        /// Function takes bool parameter - C uses Int for bools, so we have to marshal.
28
        /// </summary>
29
        BoolParameter,
30
        /// <summary>
31
        /// Function takes generic parameters - add ref/out generic and generic overloads.
32
        /// </summary>
33
        GenericParameter,
34
        /// <summary>
35
        /// Function takes arrays as parameters - add ref/out and ([Out]) array overloads.
36
        /// </summary>
37
        ArrayParameter,
38
        /// <summary>
39
        /// Function with bitmask parameters. Bitmask parameters map to UInt, but since we can only use signed
40
        /// types (for CLS compliance), we must add the unchecked keyword.
41
        /// Usually found in bitmasks
42
        /// </summary>
43
        UncheckedParameter,
44
        /// <summary>
45
        /// Function that takes (in/ref/out) a naked pointer as a parameter - we pass an IntPtr.
46
        /// </summary>
47
        PointerParameter,
48
        /// <summary>
49
        /// Function that takes a reference to a struct.
50
        /// </summary>
51
        ReferenceParameter,
52
        /// <summary>
53
        /// Function returns string - needs manual marshalling through IntPtr to prevent the managed GC
54
        /// from freeing memory allocated on the unmanaged side (e.g. glGetString).
55
        /// </summary>
56
        StringReturnType,
57
        /// <summary>
58
        /// Function returns a void pointer - maps to IntPtr, and the user has to manually marshal the type.
59
        /// </summary>
60
        GenericReturnType,
61
        /// <summary>
62
        /// Function returns a typed pointer - we have to copy the data to an array to protect it from the GC.
63
        /// </summary>
64
        ArrayReturnType
65
    }
66
 
67
    #endregion
68
 
69
    public static class Utilities
70
    {
71
        public static readonly char[] Separators = { ' ', '\n', ',', '(', ')', ';', '#' };
72
        public static readonly Regex Extensions = new Regex(
73
            "(ARB|EXT|ATI|NV|SUNX|SUN|SGIS|SGIX|SGI|MESA|3DFX|IBM|GREMEDY|HP|INTEL|PGI|INGR|APPLE|OML|I3D)",
74
            RegexOptions.Compiled);
75
 
76
        #region internal StreamReader OpenSpecFile(string file)
77
 
78
        internal static StreamReader OpenSpecFile(string folder, string file)
79
        {
80
            if (String.IsNullOrEmpty(folder) || String.IsNullOrEmpty(file))
81
                return null;
82
 
83
            Console.WriteLine(folder);
84
            Console.WriteLine(file);
85
            string path = Path.Combine(folder, file);
86
            Console.WriteLine(path);
87
            return new StreamReader(path);
88
        }
89
 
90
        #endregion
91
 
92
        #region C# keywords
93
 
94
        public static readonly List<string> Keywords = new List<string>(
95
            new string[]
96
            {
97
                "abstract", "event", "new", "struct",
98
                "as", "explicit", "null", "switch",
99
                "base", "extern", "object", "this",
100
                "bool", "false", "operator", "throw",
101
                "break", "finally", "out", "true",
102
                "byte", "fixed", "override", "try",
103
                "case", "float", "params", "typeof",
104
                "catch", "for", "private", "uint",
105
                "char", "foreach", "protected", "ulong",
106
                "checked", "goto", "public", "unchecked",
107
                "class", "if", "readonly", "unsafe",
108
                "const", "implicit", "ref", "ushort",
109
                "continue", "in", "return", "using",
110
                "decimal", "int", "sbyte", "virtual",
111
                "default", "interface", "sealed", "volatile",
112
                "delegate", "internal", "short", "void",
113
                "do", "is", "sizeof", "while",
114
                "double", "lock", "stackalloc",
115
                "else", "long", "static",
116
                "enum", "namespace", "string"
117
            }
118
        );
119
 
120
        #endregion
121
 
122
        #region internal static void Merge(EnumCollection enums, Bind.Structures.Enum t)
123
 
124
        /// <summary>
125
        /// Merges the given enum into the enum list. If an enum of the same name exists,
126
        /// it merges their respective constants.
127
        /// </summary>
128
        /// <param name="enums"></param>
129
        /// <param name="t"></param>
130
        internal static void Merge(EnumCollection enums, Enum t)
131
        {
132
            if (!enums.ContainsKey(t.Name))
133
            {
134
                enums.Add(t.Name, t);
135
            }
136
            else
137
            {
138
                Enum e = enums[t.Name];
139
                foreach (Constant c in t.ConstantCollection.Values)
140
                {
141
                    Merge(e, c);
142
                }
143
            }
144
        }
145
 
146
        #endregion
147
 
148
        #region internal static Bind.Structures.Enum Merge(Bind.Structures.Enum s, Bind.Structures.Constant t)
149
 
150
        /// <summary>
151
        /// Places a new constant in the specified enum, if it doesn't already exist.
152
        /// The existing constant is replaced iff the new has a numeric value and the old
153
        /// has a reference value (eg 0x5 is preferred over AttribMask.Foo)
154
        /// </summary>
155
        /// <param name="s"></param>
156
        /// <param name="t"></param>
157
        /// <returns></returns>
158
        internal static Enum Merge(Enum s, Constant t)
159
        {
160
            if (!s.ConstantCollection.ContainsKey(t.Name))
161
            {
162
                s.ConstantCollection.Add(t.Name, t);
163
            }
164
            else
165
            {
166
                // Tried to add a constant that already exists. If one constant
167
                // is like: 'Foo = 0x5' and the other like: 'Foo = Bar.Foo', then 
168
                // keep the first one.
169
                if (!Char.IsDigit(((Constant)s.ConstantCollection[t.Name]).Value[0]))
170
                {
171
                    s.ConstantCollection.Remove(t.Name);
172
                    s.ConstantCollection.Add(t.Name, t);
173
                }
174
            }
175
 
176
            return s;
177
        }
178
 
179
        #endregion
180
 
181
        #region internal static void Merge(EnumCollection enums, Bind.Structures.Enum t)
182
 
183
        /// <summary>
184
        /// Merges the given enum into the enum list. If an enum of the same name exists,
185
        /// it merges their respective constants.
186
        /// </summary>
187
        /// <param name="enums"></param>
188
        /// <param name="t"></param>
189
        internal static void Merge(DelegateCollection delegates, Delegate t)
190
        {
191
            if (!delegates.ContainsKey(t.Name))
192
            {
193
                delegates.Add(t.Name, t);
194
            }
195
        }
196
 
197
        #endregion
198
 
199
        #region internal static string GetGL2Extension(string name)
200
 
201
        internal static string GetGL2Extension(string name)
202
        {
203
            if (name.EndsWith("3DFX")) { return "3dfx"; }
204
            if (name.EndsWith("APPLE")) { return "Apple"; }
205
            if (name.EndsWith("ARB")) { return "Arb"; }
206
            if (name.EndsWith("AMD")) { return "Amd"; }
207
            if (name.EndsWith("ATI")) { return "Ati"; }
208
            if (name.EndsWith("ATIX")) { return "Atix"; }
209
            if (name.EndsWith("EXT")) { return "Ext"; }
210
            if (name.EndsWith("GREMEDY")) { return "Gremedy"; }
211
            if (name.EndsWith("HP")) { return "HP"; }
212
            if (name.EndsWith("I3D")) { return "I3d"; }
213
            if (name.EndsWith("IBM")) { return "Ibm"; }
214
            if (name.EndsWith("INGR")) { return "Ingr"; }
215
            if (name.EndsWith("INTEL")) { return "Intel"; }
216
            if (name.EndsWith("MESA")) { return "Mesa"; }
217
            if (name.EndsWith("NV")) { return "NV"; }
218
            if (name.EndsWith("OES")) { return "Oes"; }
219
            if (name.EndsWith("OML")) { return "Oml"; }
220
            if (name.EndsWith("PGI")) { return "Pgi"; }
221
            if (name.EndsWith("SGI")) { return "Sgi"; }
222
            if (name.EndsWith("SGIS")) { return "Sgis"; }
223
            if (name.EndsWith("SGIX")) { return "Sgix"; }
224
            if (name.EndsWith("SUN")) { return "Sun"; }
225
            if (name.EndsWith("SUNX")) { return "Sunx"; }
226
            return String.Empty;
227
        }
228
 
229
        #endregion
230
 
231
        #region private static bool IsGL2Extension(string function)
232
 
233
        private static bool IsGL2Extension(string function)
234
        {
235
            return !String.IsNullOrEmpty(GetGL2Extension(function));
236
        }
237
 
238
        #endregion
239
 
240
        #region internal static string StripGL2Extension(string p)
241
 
242
        internal static string StripGL2Extension(string p)
243
        {
244
            return p.Substring(0, p.Length - GetGL2Extension(p).Length);
245
        }
246
 
247
        #endregion
248
    }
249
}