代码改变世界

(原创)一个改自java的代码统计工具

2006-03-08 21:41  sun@live  阅读(814)  评论(0编辑  收藏  举报
  1using System;
  2using System.IO;
  3
  4namespace CodeTell{
  5    /// <summary>
  6    ///  CodeTell 的摘要说明。
  7    /// </summary>

  8
  9    class  CodeTell
 10    {
 11        private int codeLines = 0, commentLines = 0, fileCount = 0, floderCount = 0,characterCount = 0;
 12        private long totalFileLength = 0;
 13        /// <summary>
 14        /// 应用程序的主入口点。
 15        /// </summary>

 16        [STAThread]
 17        static void Main(string[] args)
 18        {
 19            //
 20            // TODO: 在此处添加代码以启动应用程序
 21            //
 22            (new CodeTell()).Start();
 23            
 24        }

 25
 26        public void Start()
 27        {
 28            DirectoryInfo folder = new DirectoryInfo(".");
 29
 30            examineList(folder);
 31
 32            //输出信息
 33            Console.WriteLine("Folder:\t\t{0}",floderCount);
 34            Console.WriteLine("Files:\t\t{0}",fileCount);
 35            Console.WriteLine("Comment Lines:\t{0}",commentLines);
 36            Console.WriteLine("Code Lines:\t{0}",codeLines);
 37            Console.WriteLine("Char Count:\t{0}",characterCount);
 38            Console.WriteLine("Files Length:\t{0}",totalFileLength);
 39        
 40            Console.Read();
 41        }

 42
 43        protected void examineList(DirectoryInfo dir)
 44        {
 45            FileInfo[] finfo = dir.GetFiles();
 46            DirectoryInfo[] dinfo = dir.GetDirectories();
 47        
 48            floderCount += dinfo.Length;
 49
 50            for(int i = 0; i<finfo.Length; i++)
 51            {
 52                calcFile(finfo[i]);
 53            }

 54            //遍历目录
 55            for(int i = 0; i< dinfo.Length; i++)
 56            {
 57                examineList(dinfo[i]);
 58            }

 59        }

 60
 61
 62        private void calcFile(FileInfo file)
 63        {
 64            StreamReader sreader;
 65            string fname,line;
 66            bool insideComment;
 67
 68            fname = file.FullName;
 69
 70            if(fname == null)
 71            {
 72                Console.WriteLine("Somehow the file was null");
 73            }

 74                //对.cs文件进行分析
 75            else if(fname.ToLower().EndsWith(".cs"))
 76            {
 77                totalFileLength += file.Length;
 78                fileCount++;
 79
 80                try
 81                {
 82                    sreader = new StreamReader(fname);
 83                    Console.WriteLine("    {0}",fname);
 84                    line = sreader.ReadLine();
 85                    if( line != null)
 86                        line = line.Trim();
 87                    insideComment = false;
 88                    int temp;
 89                    while(line != null)
 90                    {
 91                        characterCount += line.Length;
 92                        if(line.Length == 0)
 93                        {
 94                            if((temp = line.IndexOf("*/")) >= 0)
 95                            {
 96                                insideComment = false;
 97                                if(line.Length > temp)
 98                                    codeLines++;
 99                                if(temp > 2)
100                                    commentLines++;
101                            }

102                        }

103                        else
104                        {
105                            if(!line.StartsWith("//"))
106                                codeLines++;
107                            if(line.StartsWith("/*"))
108                                insideComment = true;
109                            if(line.IndexOf("//")>=0)
110                                commentLines++;
111                        }

112
113                        line = sreader.ReadLine();
114                        if(line != null)
115                            line = line.Trim();
116
117                    }
//end while
118                }

119                catch(Exception e)
120                {
121                    Console.WriteLine(e.Message);
122                }

123            }

124        }

125
126    }

127}

128