Example #1
0
        public override bool Equals(object obj)
        {
            ClassBlock block = obj as ClassBlock;

            if (NameSpace != block.NameSpace)
            {
                return(false);
            }

            if (Lines.Count != block.Lines.Count)
            {
                return(false);
            }

            for (int i = 0; i < Lines.Count; i++)
            {
                if (Lines[i] != block.Lines[i])
                {
                    return(false);
                }
            }
            return(true);
        }
Example #2
0
        public List <ClassBlock> ExtractPreDeclare(FileInfo fileInfo)
        {
            List <ClassBlock> result = new List <ClassBlock>();

            var  lines     = File.ReadAllLines(fileInfo.FullName);
            bool isInclude = false;

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i];
                lines[i] = line.Trim();

                if (line.Contains("//[IGNORE_PRE_DECLARE_FILE]"))
                {
                    return(result);
                }


                if (line == "//[PRE_DECLARE_BEGIN]")
                {
                    isInclude = true;
                }
                else if (line == "//[PRE_DECLARE_END]")
                {
                    isInclude = false;
                }

                if (isInclude && line != "//[PRE_DECLARE_BEGIN]" && !string.IsNullOrWhiteSpace(line))
                {
                    mTypedefStrings.Add(line);
                }
            }

            int preClassIndex = 0;

            for (int i = 0; i < lines.Length; i++)
            {
                var line = lines[i];
                if (line.StartsWith("namespace") && line.Contains("//[IGNORE_PRE_DECLARE]"))
                {
                    lines[i] = string.Empty;
                    continue;
                }


                if ((line.StartsWith("class") || line.StartsWith("struct")) && !line.Contains("\\") && !line.StartsWith("//") &&
                    !line.Contains("//[IGNORE_PRE_DECLARE]") && !line.Contains("/*") && !line.Contains("::Schema") && !line.Contains("struct Schema;"))
                {
                    ClassBlock block = new ClassBlock();

                    for (int j = i - 1; j > 0; j--)
                    {
                        var preLine = lines[j];
                        if (preLine.StartsWith("namespace"))  //ignore private
                        {
                            preLine         = preLine.Replace("namespace", string.Empty);
                            preLine         = preLine.Trim();
                            block.NameSpace = preLine;
                            break;
                        }
                    }

                    Stack <string> preLines   = new Stack <string>();
                    bool           isTemplate = false;
                    bool           hasDefault = false;
                    for (int j = i - 1; j > preClassIndex; j--)
                    {
                        var preLine = lines[j];
                        if (!preLine.Contains(">")) //template should be one line
                        {
                            break;
                        }

                        //foreach (var ignoreString in mIgnoreStrings)
                        //{
                        //    preLine = preLine.Replace(ignoreString, string.Empty);
                        //}

                        preLines.Push(preLine);
                        if (preLine.StartsWith("template"))
                        {
                            if (preLine.Contains('='))
                            {
                                if (preLine.Contains("template <typename TId,typename TObject,typename TObjectNewer=DefaultNewerById<TId,TObject>,typename THashCoder=DefaultHashCoder<TId>,typename TKeyCompare=DefaultCompare<TId>,typename TObjectCompare=DefaultCompare<TObject> >"))
                                {
                                    foreach (var ignoreString in mIgnoreStrings)
                                    {
                                        preLine = preLine.Replace(ignoreString, string.Empty);
                                    }
                                }

                                foreach (var ignoreString in mIgnoreStrings)
                                {
                                    preLine = preLine.Replace(ignoreString, string.Empty);
                                }

                                //try to remove default
                                preLines.Pop();
                                do
                                {
                                    int index1 = preLine.IndexOf('=');
                                    if (index1 >= 0)
                                    {
                                        int index2 = preLine.IndexOf(',', index1);
                                        if (index2 < 0)
                                        {
                                            index2 = preLine.LastIndexOf('>');
                                        }

                                        preLine = preLine.Remove(index1, index2 - index1);
                                    }
                                    else
                                    {
                                        break;
                                    }
                                } while (true);
                                preLines.Push(preLine);

                                //hasDefault = true;
                            }
                            isTemplate = true;
                            break;
                        }
                    }

                    if (isTemplate)
                    {
                        if (hasDefault)
                        {
                            continue;
                        }
                        //continue;
                        block.Lines.AddRange(preLines);   //we have to pre declare template so that some template instance can be pre delcared
                    }



                    if (Regex.IsMatch(line, @"\w+\s+\w+\s+\w+;"))
                    {
                        continue;
                    }

                    if (line.Contains(":"))
                    {
                        if (line.Contains("public") || line.Contains("protected") || line.Contains("private"))
                        {
                            int x     = line.IndexOf("public");
                            int y     = line.IndexOf("protected");
                            int z     = line.IndexOf("private");
                            int index = int.MaxValue;
                            if (x >= 0)
                            {
                                index = Math.Min(index, x);
                            }
                            if (y >= 0)
                            {
                                index = Math.Min(index, y);
                            }
                            if (z >= 0)
                            {
                                index = Math.Min(index, z);
                            }

                            while (true)
                            {
                                --index;
                                if (line[index] == ':')
                                {
                                    break;
                                }
                            }

                            line = line.Substring(0, index);
                        }
                        else
                        {
                            int index = line.IndexOf(":");
                            if (line.Contains("::"))
                            {
                            }
                            else
                            {
                                line = line.Substring(0, index);
                            }
                        }
                    }


                    if (line.Contains("{"))
                    {
                        line = line.Substring(0, line.IndexOf("{"));
                    }
                    line = line.TrimEnd();
                    if (line == "class" || line == "struct" || line == "interface" || line.Contains("<>"))
                    {
                        continue;
                    }

                    if (!line.EndsWith(";"))
                    {
                        line += ";";
                    }
                    block.Lines.Add(line);
                    block.Lines.Add(String.Empty);
                    preClassIndex = i;

                    result.Add(block);
                }
            }
            return(result);
        }