public bool BuildDeclarations(string cppFilePath)
        {
            DeclarationList d = DeclarationList.Construct(cppFilePath);

            if (d != null)
            {
                m_declarations.Add(d);
            }
            return(d != null);
        }
Example #2
0
        public bool BuildDeclarations(string cppFilePath, List <string> preprocessorDefines)
        {
            DeclarationList d = DeclarationList.Construct(cppFilePath, preprocessorDefines);

            if (d != null)
            {
                m_declarations.Add(d);
            }
            return(d != null);
        }
    public static DeclarationList Construct(string cppFileName)
    {
      const string EXPORT_DECLARE = "RH_C_FUNCTION";
      const string MANUAL = "/*MANUAL*/";
      DeclarationList d = new DeclarationList();
      d.m_source_filename = cppFileName;
      string sourceCode = System.IO.File.ReadAllText(cppFileName);
      // If you have multi-line comments that need to be dealt with, this would probably suffice:
      // sourceCode = System.Text.RegularExpressions.Regex.Replace(sourceCode, @"/\*.*?\*/", "");
      List<int> startIndices = new List<int>();
      int prevIndex = -1;
      while (true)
      {
        int index = sourceCode.IndexOf(EXPORT_DECLARE, prevIndex + 1);
        if (-1 == index)
          break;
        prevIndex = index;
        // make sure this function is not commented out
        // walk backward to the newline and try to find a //
        if (index > 2)
        {
          bool skipThisDeclaration = false;
          int testIndex = index - 1;
          while (testIndex > 0)
          {
            if (sourceCode[testIndex] == '/' && sourceCode[testIndex - 1] == '/')
            {
              skipThisDeclaration = true;
              break;
            }
            if (sourceCode[testIndex] == '#')
            {
              skipThisDeclaration = true;
              break;
            }
            if (sourceCode[testIndex] == '\n')
              break;
            testIndex--;
          }
          if (skipThisDeclaration)
            continue;
        }
        //if (index > 2 && sourceCode[index - 1] == '/' && sourceCode[index - 2] == '/')
        //  continue;
        // make sure this function is not defined as a MANUAL definition
        if (index > MANUAL.Length)
        {
          int manStart = index - MANUAL.Length;
          if (sourceCode.Substring(manStart, MANUAL.Length) == MANUAL)
            continue;
        }
        startIndices.Add(index);
      }

      // add all of the c function declarations to the cdecls list
      for (int i = 0; i < startIndices.Count; i++)
      {
        int start = startIndices[i] + EXPORT_DECLARE.Length;
        int end = sourceCode.IndexOf(')', start) + 1;
        string decl = sourceCode.Substring(start, end - start);
        decl = decl.Trim();
        d.m_declarations.Add(new Declaration(decl));
      }
      return d;
    }
        public static DeclarationList Construct(string cppFileName)
        {
            const string    EXPORT_DECLARE = "RH_C_FUNCTION";
            const string    MANUAL         = "/*MANUAL*/";
            DeclarationList d = new DeclarationList();

            d.m_source_filename = cppFileName;
            string sourceCode = System.IO.File.ReadAllText(cppFileName);
            // If you have multi-line comments that need to be dealt with, this would probably suffice:
            // sourceCode = System.Text.RegularExpressions.Regex.Replace(sourceCode, @"/\*.*?\*/", "");
            List <int> startIndices = new List <int>();
            int        prevIndex    = -1;

            while (true)
            {
                int index = sourceCode.IndexOf(EXPORT_DECLARE, prevIndex + 1);
                if (-1 == index)
                {
                    break;
                }
                prevIndex = index;
                // make sure this function is not commented out
                // walk backward to the newline and try to find a //
                if (index > 2)
                {
                    bool skipThisDeclaration = false;
                    int  testIndex           = index - 1;
                    while (testIndex > 0)
                    {
                        if (sourceCode[testIndex] == '/' && sourceCode[testIndex - 1] == '/')
                        {
                            skipThisDeclaration = true;
                            break;
                        }
                        if (sourceCode[testIndex] == '#')
                        {
                            skipThisDeclaration = true;
                            break;
                        }
                        if (sourceCode[testIndex] == '\n')
                        {
                            break;
                        }
                        testIndex--;
                    }
                    if (skipThisDeclaration)
                    {
                        continue;
                    }
                }
                //if (index > 2 && sourceCode[index - 1] == '/' && sourceCode[index - 2] == '/')
                //  continue;
                // make sure this function is not defined as a MANUAL definition
                if (index > MANUAL.Length)
                {
                    int manStart = index - MANUAL.Length;
                    if (sourceCode.Substring(manStart, MANUAL.Length) == MANUAL)
                    {
                        continue;
                    }
                }
                startIndices.Add(index);
            }

            // add all of the c function declarations to the cdecls list
            for (int i = 0; i < startIndices.Count; i++)
            {
                int    start = startIndices[i] + EXPORT_DECLARE.Length;
                int    end   = sourceCode.IndexOf(')', start) + 1;
                string decl  = sourceCode.Substring(start, end - start);
                decl = decl.Trim();
                d.m_declarations.Add(new Declaration(decl));
            }
            return(d);
        }
Example #5
0
        public static DeclarationList Construct(string cppFileName, List <string> preprocessorDefines)
        {
            const string    EXPORT_DECLARE = "RH_C_FUNCTION";
            const string    MANUAL         = "/*MANUAL*/";
            DeclarationList d = new DeclarationList();

            d.m_source_filename = cppFileName;
            string source_code = System.IO.File.ReadAllText(cppFileName);

            if (preprocessorDefines != null)
            {
                // perform a very simplified preprocessor stage on the source
                while (true)
                {
                    const string token = "#if !defined";
                    int          start = source_code.IndexOf(token);
                    if (-1 == start)
                    {
                        break;
                    }
                    int end = source_code.IndexOf("#endif", start);

                    bool add_section = true;
                    foreach (string define in preprocessorDefines)
                    {
                        if (source_code.IndexOf(define, start + token.Length + 1) == (start + token.Length + 1))
                        {
                            add_section = false;
                            break;
                        }
                    }

                    string source_start = source_code.Substring(0, start);
                    string source_end   = source_code.Substring(end + "#endif".Length);
                    if (add_section)
                    {
                        start = start + token.Length + ")".Length;
                        string source_mid = source_code.Substring(start, end - start);
                        source_code = source_start + source_mid + source_end;
                    }
                    else
                    {
                        source_code = source_start + source_end;
                    }
                }
            }


            // If you have multi-line comments that need to be dealt with, this would probably suffice:
            // sourceCode = System.Text.RegularExpressions.Regex.Replace(sourceCode, @"/\*.*?\*/", "");
            List <int> startIndices   = new List <int>();
            int        previous_index = -1;

            while (true)
            {
                int index = source_code.IndexOf(EXPORT_DECLARE, previous_index + 1);
                if (-1 == index)
                {
                    break;
                }
                previous_index = index;
                // make sure this function is not commented out
                // walk backward to the newline and try to find a //
                if (index > 2)
                {
                    bool skipThisDeclaration = false;
                    int  testIndex           = index - 1;
                    while (testIndex > 0)
                    {
                        if (source_code[testIndex] == '/' && source_code[testIndex - 1] == '/')
                        {
                            skipThisDeclaration = true;
                            break;
                        }
                        if (source_code[testIndex] == '#')
                        {
                            skipThisDeclaration = true;
                            break;
                        }
                        if (source_code[testIndex] == '\n')
                        {
                            break;
                        }
                        testIndex--;
                    }
                    if (skipThisDeclaration)
                    {
                        continue;
                    }
                }
                //if (index > 2 && sourceCode[index - 1] == '/' && sourceCode[index - 2] == '/')
                //  continue;
                // make sure this function is not defined as a MANUAL definition
                if (index > MANUAL.Length)
                {
                    int manStart = index - MANUAL.Length;
                    if (source_code.Substring(manStart, MANUAL.Length) == MANUAL)
                    {
                        continue;
                    }
                }
                startIndices.Add(index);
            }

            // add all of the c function declarations to the cdecls list
            for (int i = 0; i < startIndices.Count; i++)
            {
                int    start = startIndices[i] + EXPORT_DECLARE.Length;
                int    end   = source_code.IndexOf(')', start) + 1;
                string decl  = source_code.Substring(start, end - start);
                decl = decl.Trim();
                d.m_declarations.Add(new Declaration(decl));
            }

            // walk through file and attempt to find all enum declarations
            previous_index = -1;
            while (true)
            {
                int index = source_code.IndexOf("enum ", previous_index + 1);
                if (-1 == index)
                {
                    break;
                }
                previous_index = index;

                // now see if the enum word is a declaration or inside a function declaration
                int colon_index = source_code.IndexOf(':', index);
                int brace_index = source_code.IndexOf('{', index);
                int paren_index = source_code.IndexOf(')', index);
                if (paren_index < colon_index || brace_index < colon_index)
                {
                    continue;
                }

                int semi_colon = source_code.IndexOf(';', index);
                if (colon_index == -1 || semi_colon == -1 || brace_index == -1)
                {
                    continue;
                }

                string enumdecl = source_code.Substring(index, semi_colon - index + 1);

                // special case for enums deriving from "unsigned int". Convert this to uint
                // for C#
                enumdecl = enumdecl.Replace(" unsigned int", " uint");

                d.m_enums.Add(new EnumDeclaration(enumdecl));
            }
            return(d);
        }
    public static DeclarationList Construct(string cppFileName, List<string> preprocessorDefines)
    {
      const string EXPORT_DECLARE = "RH_C_FUNCTION";
      const string MANUAL = "/*MANUAL*/";
      DeclarationList d = new DeclarationList();
      d.m_source_filename = cppFileName;
      string source_code = System.IO.File.ReadAllText(cppFileName);

      if (preprocessorDefines != null)
      {
        // perform a very simplified preprocessor stage on the source
        while (true)
        {
          const string token = "#if !defined";
          int start = source_code.IndexOf(token);
          if (-1 == start)
            break;
          int end = source_code.IndexOf("#endif", start);

          bool add_section = true;
          foreach (string define in preprocessorDefines)
          {
            if( source_code.IndexOf(define, start + token.Length + 1) == (start+token.Length + 1) )
            {
              add_section = false;
              break;
            }
          }

          string source_start = source_code.Substring(0, start);
          string source_end = source_code.Substring(end + "#endif".Length);
          if (add_section)
          {
            start = start + token.Length + ")".Length;
            string source_mid = source_code.Substring(start, end - start);
            source_code = source_start + source_mid + source_end;
          }
          else
          {
            source_code = source_start + source_end;
          }
        }
      }


      // If you have multi-line comments that need to be dealt with, this would probably suffice:
      // sourceCode = System.Text.RegularExpressions.Regex.Replace(sourceCode, @"/\*.*?\*/", "");
      List<int> startIndices = new List<int>();
      int previous_index = -1;
      while (true)
      {
        int index = source_code.IndexOf(EXPORT_DECLARE, previous_index + 1);
        if (-1 == index)
          break;
        previous_index = index;
        // make sure this function is not commented out
        // walk backward to the newline and try to find a //
        if (index > 2)
        {
          bool skipThisDeclaration = false;
          int testIndex = index - 1;
          while (testIndex > 0)
          {
            if (source_code[testIndex] == '/' && source_code[testIndex - 1] == '/')
            {
              skipThisDeclaration = true;
              break;
            }
            if (source_code[testIndex] == '#')
            {
              skipThisDeclaration = true;
              break;
            }
            if (source_code[testIndex] == '\n')
              break;
            testIndex--;
          }
          if (skipThisDeclaration)
            continue;
        }
        //if (index > 2 && sourceCode[index - 1] == '/' && sourceCode[index - 2] == '/')
        //  continue;
        // make sure this function is not defined as a MANUAL definition
        if (index > MANUAL.Length)
        {
          int manStart = index - MANUAL.Length;
          if (source_code.Substring(manStart, MANUAL.Length) == MANUAL)
            continue;
        }
        startIndices.Add(index);
      }

      // add all of the c function declarations to the cdecls list
      for (int i = 0; i < startIndices.Count; i++)
      {
        int start = startIndices[i] + EXPORT_DECLARE.Length;
        int end = source_code.IndexOf(')', start) + 1;
        string decl = source_code.Substring(start, end - start);
        decl = decl.Trim();
        d.m_declarations.Add(new Declaration(decl));
      }

      // walk through file and attempt to find all enum declarations
      previous_index = -1;
      while (true)
      {
        int index = source_code.IndexOf("enum ", previous_index + 1);
        if (-1 == index)
          break;
        previous_index = index;

        // now see if the enum word is a declaration or inside a function declaration
        int colon_index = source_code.IndexOf(':', index);
        int brace_index = source_code.IndexOf('{', index);
        int paren_index = source_code.IndexOf(')', index);
        if (paren_index < colon_index || brace_index < colon_index)
          continue;

        int semi_colon = source_code.IndexOf(';', index);
        if (colon_index == -1 || semi_colon == -1 || brace_index == -1)
          continue;

        string enumdecl = source_code.Substring(index, semi_colon - index + 1);

        // special case for enums deriving from "unsigned int". Convert this to uint
        // for C#
        enumdecl = enumdecl.Replace(" unsigned int", " uint");

        d.m_enums.Add(new EnumDeclaration(enumdecl));
      }
      return d;
    }