Click here to Skip to main content
15,896,557 members
Articles / Programming Languages / C#

XDD-Tools for developer

Rate me:
Please Sign up or sign in to vote.
5.00/5 (9 votes)
24 Jun 2012CPOL2 min read 22.6K   449   14  
XDD-Tools for developer , count source code line,copy project file, remove comment in source code.
using System;
using System.Collections.Generic;
using System.Text;
using System.IO;

namespace XDesigner.Development.Dom
{
    public class DelphiHelper
    {
        /// <summary>
        /// 加载项目文件
        /// </summary>
        /// <param name="strFileName"></param>
        public static bool LoadSolutionFile( Solution sln , string strFileName)
        {
            sln.Projects.Clear();
            sln.FileName = strFileName;
            sln.Files.Clear();
            sln.Style = SolutionStyle.Delphi;
            sln.Name = Path.GetFileNameWithoutExtension(strFileName);
            string rootPath = System.IO.Path.GetDirectoryName(strFileName);

            System.Xml.XmlDocument doc = new System.Xml.XmlDocument();
            doc.Load(strFileName);
            foreach (System.Xml.XmlElement element in doc.SelectNodes("BorlandProject/Delphi.Personality/Source/Source"))
            {
                string name = element.InnerText;
                name = System.IO.Path.Combine( rootPath, name);
                if (System.IO.File.Exists(name))
                {
                    ProjectElement prj = new ProjectElement();
                    prj.Style = ProjectStyle.Delphi;
                    if (LoadProject(prj, name))
                    {
                        sln.Projects.Add(prj);
                    }
                }
            }//foreach
            return true;
        }

        public static bool LoadProject( ProjectElement prj , string strFileName)
        {
            string rootPath = Path.GetDirectoryName(strFileName);
            prj.ProjectFileName = strFileName;
            prj.Name = Path.GetFileNameWithoutExtension(strFileName);
            prj.RootPath = Path.GetDirectoryName(strFileName);
            using (StreamReader myReader = new StreamReader(strFileName, System.Text.Encoding.Default, true))
            {
                string strLine = myReader.ReadLine();
                bool ReadingContains = false;
                while (strLine != null)
                {
                    int index = 0;
                    strLine = strLine.Trim();
                    if (strLine.Length == 0 || strLine == "{$R *.res}")
                    {
                        strLine = myReader.ReadLine();
                        continue;
                    }
                    if (strLine == "contains")
                    {
                        ReadingContains = true;
                    }
                    else if (strLine == "end.")
                    {
                        ReadingContains = false;
                    }
                    else if (strLine == "uses")
                    {
                        ReadingContains = true;
                    }
                    if (ReadingContains == false)
                    {
                        if (strLine.StartsWith("{$") && strLine.EndsWith("}"))
                        {
                            strLine = strLine.Substring(2, strLine.Length - 3);
                            index = strLine.IndexOf(' ');
                            if (index > 0)
                            {
                                string name = strLine.Substring(0, index).Trim();
                                strLine = strLine.Substring(index + 1).Trim();
                                if (name == "DESCRIPTION")
                                {
                                    if ( string.IsNullOrEmpty( prj.Name ))
                                    {
                                        prj.Name = RemoveFlag(strLine);
                                    }
                                }
                                else if (name == "R")
                                {
                                    FileElement rf = new FileElement();
                                    rf.FileName = RemoveFlag(strLine);
                                    rf.FullFileName = System.IO.Path.Combine(rootPath, rf.FileName);
                                    prj.Files.Add(rf);
                                }
                                else if (name == "I")
                                {
                                    FileElement ifile = new FileElement();
                                    ifile.FileName = RemoveFlag(strLine);
                                    ifile.FullFileName = System.IO.Path.Combine(rootPath, ifile.FileName);
                                    prj.Files.Add(ifile);
                                }
                            }
                        }
                    }
                    else
                    {
                        index = strLine.IndexOf(" in ");
                        if (index > 0)
                        {
                            strLine = strLine.Substring(index + 4).Trim();
                            if (strLine.EndsWith(",") || strLine.EndsWith(";"))
                            {
                                strLine = strLine.Substring(0, strLine.Length - 1);
                            }
                            strLine = RemoveFlag(strLine);
                            FileElement NewFile = new FileElement();
                            NewFile.FileName = strLine;
                            NewFile.FullFileName = System.IO.Path.Combine(rootPath, strLine);
                            if (strLine.ToLower().EndsWith(".pas"))
                            {
                                NewFile.Style = FileStyle.SourceCode;
                            }
                            prj.Files.Add(NewFile);

                            string name2 = System.IO.Path.ChangeExtension(strLine, "dfm");
                            string name3 = System.IO.Path.Combine(rootPath, name2);
                            if (System.IO.File.Exists(name3))
                            {
                                NewFile = new FileElement();
                                NewFile.FileName = name2;
                                NewFile.FullFileName = name3;
                                NewFile.Style = FileStyle.Resource;
                                prj.Files.Add(NewFile);
                            }
                        }
                    }//else
                    strLine = myReader.ReadLine();
                }//while
            }//using
            return true;
        }

        private static string RemoveFlag(string txt)
        {
            txt = txt.Trim();
            if (txt.StartsWith("'") )
            {
                int index = txt.IndexOf("'", 1);
                txt = txt.Substring(1, index - 1);
            }
            return txt.Trim();
        }
    }
}

By viewing downloads associated with this article you agree to the Terms of Service and the article's licence.

If a file you wish to view isn't highlighted, and is a text file (not binary), please let us know and we'll add colourisation support for it.

License

This article, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)


Written By
Web Developer duchang soft
China China
yuan yong fu of duchang soft , come from CHINA , 2008 Microsoft MVP,Use GDI+,XML/XSLT, site:http://www.cnblogs.com/xdesigner/

Comments and Discussions