计算工程的代码行数

看到一坨坨的代码,实在不能继续下去,有个想法,统计一下这个工程的代码量,估计一下自己的工作量。最后,很失望。

本文代码只是用作统计*.cs文件,设计文件不包括。


12.19  

1.增加文件夹拖放功能

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;


namespace TextLineCounter
{
    public partial class mainFrm : Form
    {
        public mainFrm()
        {
            InitializeComponent();
        }


        private void btnCheck_Click(object sender, EventArgs e)
        {
            string folderPath = this.textBoxPath.Text;
            if (folderPath == "")
            {
                FolderBrowserDialog folderDlg = new FolderBrowserDialog();
                folderDlg.ShowDialog();
                folderPath = folderDlg.SelectedPath;
                this.textBoxPath.Text = folderPath;
            }
            //存放符合条件的文件名
            List<string> fileList = new List<string>();
            if (folderPath != "")
            {
                //目录信息
                DirectoryInfo theFolder = new DirectoryInfo(folderPath);
                //文件
                foreach (FileInfo NextFile in theFolder.GetFiles())
                {
                    string fileName = NextFile.Name.ToString();
                    int csLocate = fileName.LastIndexOf(".cs");
                    int designLocate = fileName.LastIndexOf(".D");
                    int cspLocate = fileName.LastIndexOf(".csp");
                    //MessageBox.Show(designLocate + "-" + csLocate.ToString() + "/" + fileName);
                    if (designLocate < 0 && cspLocate < 0 && csLocate > 0)
                    {
                        fileList.Add(fileName);
                    }
                }


                int[] LineArray = new int[fileList.Count];
                for (int i = 0; i < fileList.Count; i++)
                {
                    string path = folderPath + @"\" + fileList[i];
                    //MessageBox.Show(path);
                    LineArray[i] = CountLines(path);
                }


                int sum = 0;
                for (int j = 0; j < LineArray.Length; j++)
                {
                    sum += LineArray[j];
                    //MessageBox.Show(LineArray[j].ToString());
                }
                MessageBox.Show(sum.ToString());
            }
        }


        /// <summary>
        /// 根据文件路径统计文件的行数
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public static int CountLines(string path)
        {
            //string path = @"c:\temp\MyTest.txt";
            int lineCount = 0;
            try
            {


                using (StreamReader sr = new StreamReader(path))
                {
                    while (sr.Peek() >= 0)
                    {
                        sr.ReadLine();
                        lineCount++;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return lineCount;
        }


        private void textBoxPath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.textBoxPath.Cursor = System.Windows.Forms.Cursors.Arrow;//指定鼠标形状(更好看)  
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }


        private void textBoxPath_DragDrop(object sender, DragEventArgs e)
        {
            //DataFormats 数据的格式,下有多个静态属性都为string型,除FileDrop格式外还有Bitmap,Text,WaveAudio等格式  
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            this.textBoxPath.Text = path;
            this.textBoxPath.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状 
        }


    }
}

结果如下:大概有1/3的量。

时间:6个月



2014.12.22

1.之前只能统计一层文件夹,现在能计算多层文件夹

using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.IO;

namespace TextLineCounter
{
    public partial class mainFrm : Form
    {
        public mainFrm()
        {
            InitializeComponent();
        }

        //存放文件
        List<string> fileList = new List<string>();

        private void btnCheck_Click(object sender, EventArgs e)
        {
            string folderPath = this.textBoxPath.Text;
            if (folderPath == "")
            {
                FolderBrowserDialog folderDlg = new FolderBrowserDialog();
                folderDlg.ShowDialog();
                folderPath = folderDlg.SelectedPath;
                this.textBoxPath.Text = folderPath;
            }
            if (folderPath != "")
            {
                SearchFolders(folderPath);
                int[] LineArray = new int[fileList.Count];
                for (int i = 0; i < fileList.Count; i++)
                {
                    if (File.Exists(fileList[i]) && Path.GetFileName(fileList[i]) != "AssemblyInfo.cs")
                        {
                            //MessageBox.Show(fileList[i].ToString());
                            LineArray[i] = CountLines(fileList[i]);
                        }
                }

                int sum = 0;
                for (int j = 0; j < LineArray.Length; j++)
                {
                    sum += LineArray[j];
                    //MessageBox.Show(LineArray[j].ToString());
                }
                if (MessageBox.Show(sum.ToString()) == DialogResult.OK)
                {
                    this.textBoxPath.Text = "";
                    fileList.Clear();
                }
            }
        }

        /// <summary>
        /// 根据文件路径统计文件的行数
        /// </summary>
        /// <param name="path">路径</param>
        /// <returns></returns>
        public static int CountLines(string path)
        {
            //string path = @"c:\temp\MyTest.txt";
            int lineCount = 0;
            try
            {
                using (StreamReader sr = new StreamReader(path))
                {
                    while (sr.Peek() >= 0)
                    {
                        sr.ReadLine();
                        lineCount++;
                    }
                }
            }
            catch (Exception e)
            {
                MessageBox.Show(e.Message);
            }
            return lineCount;
        }

        /// <summary>
        /// 找到路径下的文件夹
        /// </summary>
        /// <param name="searchDir">目录路径</param>
        public void SearchFolders(string searchDir)
        {
            SearchFiles(searchDir);
            string[] dirs;
            try
            {
                dirs = Directory.GetDirectories(searchDir);
            }
            catch
            {
                dirs = null;
            }

            try
            {
                if (dirs != null)
                {
                    foreach (string d in dirs)
                    {
                        this.SearchFolders(d);
                    }
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        /// <summary>
        /// 根据文件夹路径寻找文件
        /// </summary>
        /// <param name="folderPath">文件夹路径</param>
        public void SearchFiles(string folderPath)
        {
            //目录信息
            DirectoryInfo theFolder = new DirectoryInfo(folderPath);
            //文件
            foreach (FileInfo NextFile in theFolder.GetFiles())
            {
                string fileName = NextFile.Name.ToString();
                int csLocate = fileName.LastIndexOf(".cs");
                int designLocate = fileName.LastIndexOf(".D");
                int cspLocate = fileName.LastIndexOf(".csp");
                //MessageBox.Show(designLocate + "-" + csLocate.ToString() + "/" + fileName);
                if (designLocate < 0 && cspLocate < 0 && csLocate > 0)
                {
                    fileList.Add(theFolder + @"\" + fileName);
                    //MessageBox.Show(theFolder + "-" + fileName);
                }
            }
        }

        private void textBoxPath_DragEnter(object sender, DragEventArgs e)
        {
            if (e.Data.GetDataPresent(DataFormats.FileDrop))
            {
                e.Effect = DragDropEffects.Link;
                this.textBoxPath.Cursor = System.Windows.Forms.Cursors.Arrow;//指定鼠标形状(更好看)  
            }
            else
            {
                e.Effect = DragDropEffects.None;
            }
        }

        private void textBoxPath_DragDrop(object sender, DragEventArgs e)
        {
            //DataFormats 数据的格式,下有多个静态属性都为string型,除FileDrop格式外还有Bitmap,Text,WaveAudio等格式  
            string path = ((System.Array)e.Data.GetData(DataFormats.FileDrop)).GetValue(0).ToString();
            this.textBoxPath.Text = path;
            this.textBoxPath.Cursor = System.Windows.Forms.Cursors.IBeam; //还原鼠标形状 
        }

    }
}


2014.12.27
1.使用EndsWith方法判断文件后缀,不用Path.GetExtension,是因为不能判断设计文件,.designer.cs

           if (fileName.ToLower().EndsWith(".cs") && !fileName.ToLower().EndsWith(".designer.cs"))
                {
                    fileList.Add(theFolder + @"\" + fileName);
                }

### 回答1: FPGA代码行数统计是指对FPGA的设计代码进行计数和统计,以了解设计的规模和复杂程度。FPGA是一种可编程逻辑器件,可以根据需求进行配置和重新编程,因此设计代码行数对于了解FPGA设计的复杂性和工作量具有重要意义。 FPGA设计代码可以使用不同的编程语言进行编写,常见的有VHDL(VHSIC硬件描述语言)和Verilog等。在进行代码行数统计时,可以通过各种编程开发工具或者简单的文本编辑器实现。 要进行FPGA代码行数统计,首先需要打开设计代码文件。然后,使用计数工具或者功能强大的文本编辑器的统计功能,可以方便地统计代码行数。可以统计总代码行数、空白行数、注释行数和实际有效代码行数等。 FPGA设计代码可以较大地分为模块化代码和顶层代码两种。模块化代码是FPGA设计中的模块和组件,每个模块通常都有自己的代码文件。顶层代码是FPGA设计的主控制代码,负责各个模块的连接和协调。对于模块代码,可以逐个打开文件进行行数统计;对于顶层代码,需要统计所有相关文件的行数。 通过统计FPGA设计代码行数,可以更好地了解设计的规模和复杂程度,从而更好地组织和管理设计过程。代码行数统计也可以与项目进度和工时估算相结合,为项目管理提供参考。此外,代码行数统计还可以用于做设计质量的评估和比较,判断设计的精简性和可维护性。 总之,FPGA代码行数统计是一项重要的设计管理和质量评估工作,可以帮助了解设计的复杂性和工作量,并且在项目管理中起到指导和参考的作用。 ### 回答2: FPGA代码行数统计是指对FPGA设计中的代码文件进行统计分析,得出代码行数信息。FPGA代码行数的统计对于设计工程师来说非常重要,可以帮助他们评估设计的规模、复杂度和实现难度。 FPGA设计中的代码包括硬件描述语言(如VHDL、Verilog等)代码和约束文件。第一步是将所有的代码文件收集起来,包括顶层模块和子模块的代码文件。然后,可以使用代码编辑器或命令行工具对代码进行统计。 在代码行数统计中,通常包括以下几种指标: 1. 总行数代码文件中的所有行数包括空行和注释行。 2. 有效代码行数代码文件中的有效代码行数,即排除空行和注释行后的行数。有效代码是实际执行操作和逻辑的行。 3. 注释行数代码文件中的注释行数,可以帮助理解代码的功能和设计意图。 4. 空行数代码文件中的空行数,用于提高代码的可读性。 通过对代码行数的统计,设计工程师可以了解到设计的规模和复杂度。行数统计还可以帮助设计工程师评估设计进度和实现难度。此外,统计的结果还可以作为代码维护和优化的依据,有助于优化代码的可读性和性能。 总之,FPGA代码行数统计是设计工程师在FPGA设计过程中的一项重要工作,通过统计代码行数,可以对设计的规模、复杂度和实现难度有一个直观的了解,为后续的设计工作提供依据。 ### 回答3: FPGA(Field Programmable Gate Array)是一种可编程逻辑电路器件,用户可以通过编程实现不同的电路功能。在FPGA设计过程中,代码行数统计是一项很重要的任务,它可以帮助设计人员评估设计的规模、复杂度和开发工作量。 要进行FPGA代码行数统计,首先需要使用的是一个文本编辑器,如Vim、Notepad++等。其次,我们需要将FPGA设计代码文件打开,并浏览代码文件中的内容。代码行数统计可以按下面的步骤进行: 1. 打开代码文件:通过文本编辑器打开FPGA设计代码文件。 2. 遍历代码文件:逐行读取代码文件的内容,并计算出每一行的字符数。 3. 忽略注释行:注释行通常不包含实际的代码,我们可以通过识别注释符号(如“//”、“/*”等)来跳过这些行。 4. 统计有效代码行数:将不包含注释的代码行累加起来,就可以得到有效的代码行数。 5. 结果输出:将得到的代码行数输出到一个统计结果文件中。 在进行代码行数统计时,需要注意以下几点: 1. 一行可能包含多个语句:在某些情况下,一行代码中可能包含多条语句,例如使用分号(;)分隔的多个语句。在统计过程中,需要对这种情况进行处理,确保每一行只统计为一行代码。 2. 排除空行:空行通常不包含任何代码,统计其行数可能会导致误差。因此,在统计过程中,需要排除空行,只统计包含有效代码的行。 3. 考虑复杂度因素:代码行数统计并不完全反映设计的复杂度,还应该考虑各种其他因素,如模块数量、电路连接等。 通过以上步骤,我们可以实现对FPGA设计代码行数的简单统计。代码行数统计可以让设计人员更好地了解设计的规模和复杂度,有助于进行工作量评估和项目管理。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值