示例#1
0
        public void Add(string absoluteImportingCSourceFile, string importLiteral)
        {
            var           imports = importLiteral.Split(new[] { '\"' }, StringSplitOptions.None);
            List <string> args    = new List <string>();
            bool          even    = true;

            for (int i = 0; i < imports.Length; i++)
            {
                if (imports[i].EndsWith(@"\") && !imports[i].EndsWith(@"\\") && imports.Length > i + 1)
                {
                    even           = !even;
                    imports[i + 1] = imports[i] + "\"" + imports[i + 1];
                    continue;
                }

                if (!even)
                {
                    args.Add(imports[i]);
                }
                even = !even;
            }

            if (args.Count < 1 || args.Count > 3)
            {
                throw new NotSupportedException("The " + CppSharedEnums.RH_C_SHARED_ENUM_PARSE_FILE +
                                                " call has to contain at least a relative location and optionally a prefix, and a suffix for the string.");
            }

            //in case of backslashes and similar escape characters, we need the program to behave as though this was actually a parsed escaped C file.
            //the Regex.Unescape method seems to behave as expected for our needs. Uri.UnescapeDataString does not work for this.
            string relative_cpp_definition_file = Regex.Unescape(args[0]);
            string section_prefix = args.Count > 1 ? args[1] : string.Empty;
            string section_suffix = args.Count > 2 ? args[2] : string.Empty;

            if (Path.IsPathRooted(relative_cpp_definition_file))
            {
                throw new NotSupportedException(
                          string.Format("relativeCppDefinitionFile\n{0}\n is rooted. It needs to be relative for this program to work in general.",
                                        relative_cpp_definition_file));
            }

            var absolute_folder = Path.GetDirectoryName(absoluteImportingCSourceFile);

            if (string.IsNullOrEmpty(absolute_folder))
            {
                throw new InvalidOperationException("absoluteImportingCSourceFile is not a file in a folder.");
            }

            var composite = Path.Combine(absolute_folder, relative_cpp_definition_file);

            composite = Path.GetFullPath(composite);

            if (!File.Exists(composite))
            {
                throw new FileNotFoundException(
                          string.Format("File was not found;\n {0} \nand\n {1} was resolved to:\n {2}.",
                                        absoluteImportingCSourceFile,
                                        relative_cpp_definition_file,
                                        composite
                                        ),
                          composite);
            }

            var cpp_info = new CppFileLink
            {
                AbsoluteImportingCSourceFile = absoluteImportingCSourceFile,
                CompositeImportedLocation    = composite,
                SectionPrefix = section_prefix,
                SectionSuffix = section_suffix
            };


            ReadFileLinkIntoSections(cpp_info);
        }
示例#2
0
        private void ReadFileLinkIntoSections(CppFileLink cppInfo)
        {
            bool region_written = false;

            using (var text_stream = File.OpenText(cppInfo.CompositeImportedLocation))
            {
                int line_count      = 0;
                int last_enum_start = -1;
                int nesting         = 0;

                while (!text_stream.EndOfStream)
                {
                    string shared_enum_start = string.Empty;
                    string line;
                    while ((line = text_stream.ReadLine()) != null)
                    {
                        line_count++;

                        int enum_start = line.IndexOf(RH_C_SHARED_ENUM);
                        if (enum_start != -1)
                        {
                            shared_enum_start = line.Substring(enum_start + RH_C_SHARED_ENUM.Length);
                            last_enum_start   = line_count;
                            nesting           = 1;
                            break;
                        }
                    }
                    if (!string.IsNullOrEmpty(shared_enum_start))
                    {
                        StringBuilder enum_text = new StringBuilder();
                        while ((line = text_stream.ReadLine()) != null)
                        {
                            line_count++;
                            int begin_end = line.IndexOf(RH_C_SHARED_ENUM_END);
                            if (begin_end != -1)
                            {
                                nesting--;
                                if (nesting <= 0)
                                {
                                    break;
                                }
                            }
                            int any_start = line.IndexOf(RH_C_ANY_REGION);
                            if (any_start != -1)
                            {
                                nesting++;
                            }
                            enum_text.AppendLine(line);
                        }

                        string enum_body = enum_text.ToString();

                        var csharp_enum = CppEnumSection.FromParsedEnumBody(
                            shared_enum_start,
                            enum_body,
                            last_enum_start,
                            cppInfo.CompositeImportedLocation,
                            m_cpp_to_cs_translations);

                        if (!region_written)
                        {
                            this.m_pieces.Add(new CppEnumSection(@"#region "
                                                                 + Path.GetFileName(cppInfo.CompositeImportedLocation) + Environment.NewLine
                                                                 ));

                            if (cppInfo.HasSectionPrefix)
                            {
                                this.m_pieces.Add(new CppEnumSection(cppInfo.SectionPrefix + Environment.NewLine));
                            }
                            region_written = true;
                        }

                        this.m_pieces.Add(csharp_enum);
                    }
                }
            }
            if (region_written)
            {
                this.m_pieces.Add(new CppEnumSection(Environment.NewLine));
                if (cppInfo.HasSectionSuffix)
                {
                    this.m_pieces.Add(new CppEnumSection(
                                          Environment.NewLine + @"// (" + cppInfo.SectionPrefix + ")" + Environment.NewLine +
                                          cppInfo.SectionSuffix + Environment.NewLine));
                }
                this.m_pieces.Add(new CppEnumSection("#endregion" + Environment.NewLine + Environment.NewLine));
            }
        }