예제 #1
0
        private static void ProcessMethods(StreamWriter writer, String line)
        {
            if (line.StartsWith("#define"))
            {
                return;
            }
            if (line.StartsWith("typedef"))
            {
                return;
            }

            if (line.StartsWith("#if"))
            {
                ifdefStatements.Push(line);
                if (line.Contains("GL_GLEXT_FUNCTION_POINTER"))
                {
                    skip = true;
                    return;
                }
            }
            if (line.StartsWith("#else"))
            {
                if (ifdefStatements.Peek().Contains("GL_GLEXT_FUNCTION_POINTER"))
                {
                    skip = false;
                }
                return;
            }
            if (line.StartsWith("#endif"))
            {
                String previous = ifdefStatements.Pop();
                if (previous.Contains("GL_GLEXT_FUNCTION_POINTER"))
                {
                    skip = false;
                    return;
                }
                line = "#endif";
            }

            if (line.StartsWith("/*") && line.EndsWith("*/"))
            {
                return;
            }

            if (skip)
            {
                return;
            }

            if (line.StartsWith("extern"))
            {
                Match m = REGEX_EXTERN.Match(line);
                if (m.Success)
                {
                    String returnType = m.Groups[1].Value.Trim();
                    String method = m.Groups[2].Value.Trim();
                    String parameters = m.Groups[3].Value;

                    // Gather the parameters
                    ParsedParameterTuples parameterTuples = new ParsedParameterTuples();
                    foreach (String parameter in parameters.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        m = REGEX_PARAMETER.Match(parameter);
                        if (m.Success)
                        {
                            bool constant = m.Groups[1].Value.Trim() == "const";
                            String type = m.Groups[2].Value;
                            String indirection = m.Groups[3].Value.Replace(" ", String.Empty);
                            String name = m.Groups[4].Value;
                            if (String.IsNullOrEmpty(name))
                            {
                                name = "p" + parameterTuples.Count;
                            }

                            ParsedParameterTuple tuple = new ParsedParameterTuple(name, type, indirection, constant);
                            parameterTuples.Add(tuple);
                        }
                        else
                        {
                            Console.WriteLine("[WARN] Cannot parse " + line);
                            return;
                            //throw new NotSupportedException("Unable to parse: '" + parameter + "'");
                        }
                    }

                    // Change return type if needed
                    if (returnType == "const GLubyte *" ||
                        returnType == "GLvoid*" ||
                        returnType == "GLvoid *")
                    {
                        returnType = "IntPtr";
                    }

                    // Change parameter names if needed
                    ParsedParameterTuple parsedParameterTuple = parameterTuples.Find(p => p.Type == "void");
                    if (parsedParameterTuple != null)
                    {
                        parameterTuples.Remove(parsedParameterTuple);
                    }
                    foreach (ParsedParameterTuple parameterTuple in parameterTuples)
                    {
                        if (KEYWORDS.Contains(parameterTuple.Name))
                        {
                            parameterTuple.Name = "@" + parameterTuple.Name;
                        }
                    }

                    // Count the parameter that are pointers
                    int pointerCount = (from p in parameterTuples
                                        where !String.IsNullOrEmpty(p.Indirection)
                                        select p).Count();

                    if (pointerCount == 0)
                    {
                        OutputMethod(writer, line, method, returnType, parameterTuples.Select(p => new RealParameterTuple(p.Name, p.Type, false, false)));
                    }
                    else
                    {
                        IList<IList<RealParameterTuple>> realParameters = new List<IList<RealParameterTuple>>();
                        IList<RealParameterTuple> currentParameters = new List<RealParameterTuple>();
                        for (int i = 0; i < parameterTuples.Count; i++)
                        {
                            realParameters.Add(GetTuples(parameterTuples[i]));
                            currentParameters.Add(new RealParameterTuple("", "", false, false));
                        }

                        OutputMethod(writer, line, method, returnType, realParameters, 0, currentParameters);
                    }

                    return;
                }
            }
            writer.WriteLine(line);
        }
예제 #2
0
        private static void ProcessMethods(StreamWriter writer, String line)
        {
            if (line.StartsWith("#define"))
            {
                return;
            }
            if (line.StartsWith("typedef"))
            {
                return;
            }

            if (line.StartsWith("#if"))
            {
                ifdefStatements.Push(line);
                if (line.Contains("GL_GLEXT_FUNCTION_POINTER"))
                {
                    skip = true;
                    return;
                }
            }
            if (line.StartsWith("#else"))
            {
                if (ifdefStatements.Peek().Contains("GL_GLEXT_FUNCTION_POINTER"))
                {
                    skip = false;
                }
                return;
            }
            if (line.StartsWith("#endif"))
            {
                String previous = ifdefStatements.Pop();
                if (previous.Contains("GL_GLEXT_FUNCTION_POINTER"))
                {
                    skip = false;
                    return;
                }
                line = "#endif";
            }

            if (line.StartsWith("/*") && line.EndsWith("*/"))
            {
                return;
            }

            if (skip)
            {
                return;
            }

            if (line.StartsWith("extern"))
            {
                Match m = REGEX_EXTERN.Match(line);
                if (m.Success)
                {
                    String returnType = m.Groups[1].Value.Trim();
                    String method     = m.Groups[2].Value.Trim();
                    String parameters = m.Groups[3].Value;

                    // Gather the parameters
                    ParsedParameterTuples parameterTuples = new ParsedParameterTuples();
                    foreach (String parameter in parameters.Split(new[] { ',' }, StringSplitOptions.RemoveEmptyEntries))
                    {
                        m = REGEX_PARAMETER.Match(parameter);
                        if (m.Success)
                        {
                            bool   constant    = m.Groups[1].Value.Trim() == "const";
                            String type        = m.Groups[2].Value;
                            String indirection = m.Groups[3].Value.Replace(" ", String.Empty);
                            String name        = m.Groups[4].Value;
                            if (String.IsNullOrEmpty(name))
                            {
                                name = "p" + parameterTuples.Count;
                            }

                            ParsedParameterTuple tuple = new ParsedParameterTuple(name, type, indirection, constant);
                            parameterTuples.Add(tuple);
                        }
                        else
                        {
                            Console.WriteLine("[WARN] Cannot parse " + line);
                            return;
                            //throw new NotSupportedException("Unable to parse: '" + parameter + "'");
                        }
                    }

                    // Change return type if needed
                    if (returnType == "const GLubyte *" ||
                        returnType == "GLvoid*" ||
                        returnType == "GLvoid *")
                    {
                        returnType = "IntPtr";
                    }

                    // Change parameter names if needed
                    ParsedParameterTuple parsedParameterTuple = parameterTuples.Find(p => p.Type == "void");
                    if (parsedParameterTuple != null)
                    {
                        parameterTuples.Remove(parsedParameterTuple);
                    }
                    foreach (ParsedParameterTuple parameterTuple in parameterTuples)
                    {
                        if (KEYWORDS.Contains(parameterTuple.Name))
                        {
                            parameterTuple.Name = "@" + parameterTuple.Name;
                        }
                    }

                    // Count the parameter that are pointers
                    int pointerCount = (from p in parameterTuples
                                        where !String.IsNullOrEmpty(p.Indirection)
                                        select p).Count();

                    if (pointerCount == 0)
                    {
                        OutputMethod(writer, line, method, returnType, parameterTuples.Select(p => new RealParameterTuple(p.Name, p.Type, false, false)));
                    }
                    else
                    {
                        IList <IList <RealParameterTuple> > realParameters    = new List <IList <RealParameterTuple> >();
                        IList <RealParameterTuple>          currentParameters = new List <RealParameterTuple>();
                        for (int i = 0; i < parameterTuples.Count; i++)
                        {
                            realParameters.Add(GetTuples(parameterTuples[i]));
                            currentParameters.Add(new RealParameterTuple("", "", false, false));
                        }

                        OutputMethod(writer, line, method, returnType, realParameters, 0, currentParameters);
                    }

                    return;
                }
            }
            writer.WriteLine(line);
        }