[译]代码计数器工具
By robot-v1.0
本文链接 https://www.kyfws.com/applications/code-counter-tool-zh/
版权声明 本博客所有文章除特别声明外,均采用 BY-NC-SA 许可协议。转载请注明出处!
- 6 分钟阅读 - 2555 个词 阅读量 0[译]代码计数器工具
原文地址:https://www.codeproject.com/Articles/1681/Code-Counter-Tool
原文作者:Prashanth Uppunda
译文由本站 robot-v1.0 翻译
前言
Code Counter is a GUI tool, which can be used for counting the number of source lines, commented lines and blank lines in a VC++ project.
代码计数器是一种GUI工具,可用于计算VC ++项目中的源代码行,注释行和空白行的数量.
介绍(Introduction)
Code Counter是使用VC ++开发的GUI工具,主要用于VC ++项目中,可用于计算源代码行,注释行和空白行的数量.对于VC ++项目,(Code Counter is a GUI tool developed using VC++, which can be used for counting number of source lines, commented lines and blank lines, mainly in a VC++ project. For VC++ projects, the).dsp(.dsp*)*将分析文件以获取项目中的文件列表.但是,您也可以在非VC ++项目(例如,在UNIX等上开发的Java项目或C/C ++项目)中使用代码计数器,只需在文件中以(*file will be parsed to get the list of files in the project. However, you can also use Code Counter for non VC++ projects like, a Java project or a C/C++ project developed on UNIX etc., by giving the list of files, in a*)*.地图(*.map*)*文本文件.代码计数器结果可以以CSV/txt格式存储,也可以打印.(*text file. The Code Counter results can be stored in CSV/txt format, and can also be printed.*)
使用工具(Using the Tool)
首先要做的是选择一个项目.单击"选择项目"按钮后,将显示文件打开对话框,您可以选择(The first thing to do is to select a project. On clicking the “Select Project” button, the file open dialog appears, and you can select either a).dsp(.dsp*)*文件或(*file or*)*.地图(*.map*)*在这里归档.选择项目后,可以在树形清单框中进行选择,以计入所需文件中.代码计数的结果将显示在列表控件中,可以将其保存到(*file here. Once the project is selected, selection can be made in the tree checklist box, for counting in required files. The result of Code Counting will be displayed in the list control, which can be saved to a*)*.csv(*.csv*)*文件,或(*file, or*)*.文本(*.txt*)*文件,也可以打印.(*file and can also be printed.*)
实作(Implementation)
代码计数器使用VC ++ 6.0开发为MFC应用程序.该实现主要由用于读取/解析(Code Counter is developed as an MFC application using VC++ 6.0. The implementation mainly consists of classes for reading/parsing the).dsp(.dsp*)*用于获取项目中源文件列表的文件,用于允许选择文件的树形复选框列表框,以及用于检查注释的语法分析源文件.(*file for getting the list of source files in a project, the tree check list box for allowing file selection, and parsing source file for checking comments.*)
读取.dmp文件和解析源文件以获取注释(Reading .dsp file & Parsing source file for comments)
的(The) CLnCountFile
类实现了这些功能.的(class implements these functionalities. The).dsp(.dsp*)*文件是一个文本文件,使用一些字符串操作功能可以读取和解析该文件.源文件将带有特殊标记(*file is a text file which is read and parsed using some string manipulation functions. Source files will be succeeded by a special tag*) "# Begin Source File"
在里面(*in the*)*.dsp(*.dsp*)*文件.如果(*file. If*)*.地图(*.map*)*选择文件后,不需要特殊分析,因为该文件直接包含文件列表.在这种情况下,除非映射文件位于所有文件的存储目录中,否则应将文件及其绝对路径一起给出.如果无法打开一个或多个源文件,则会显示一个错误对话框,显示无法打开进行解析的文件列表.(*file is selected, no special parsing is required, as the file directly contains the list of files. In this case, the files should be given along with their absolute path, unless the map file resides in the directory where all the files are stored. If one or more source files could not be opened, then an error dialog will be displayed showing the list of files which could not be opened for parsing.*)
BOOL CLnCountFile::GetFileList(CString strMakFile)
{
CString strTemp;
CStdioFile makFile(strMakFile,CFile::modeRead|CFile::typeText );
if(makFile.m_pStream==NULL)
{
AfxMessageBox("Can not Open Project Make file");
return FALSE;
}
m_aryFiles.RemoveAll();
m_aryErrorFiles.RemoveAll();
//IF Generic
if(strMakFile.Find(".map") != -1)
{
AddFilesToArrayForGeneric(makFile);
}
else
{
while(makFile.ReadString(strTemp)!=FALSE)
{
if(strTemp.Find("# Begin Source File")!=-1)
if(AddFilesToArray(makFile)==FALSE)
return FALSE;
}
}
m_nFileCount=m_nCurIndex;
m_nCurIndex=0;
return TRUE;
}
BOOL CLnCountFile::AddFilesToArray(CStdioFile &myFile)
{
BOOL bLocalFlag=FALSE;
CString strTemp;
while(myFile.ReadString(strTemp)!=FALSE)
{
if(strTemp.Find("# Begin Source File")!=-1)
{
AfxMessageBox("There is some error in the Make file");
return FALSE;
}
if(strTemp.Find("# End Source File")!=-1)
{
bLocalFlag=TRUE;
break;
}
if(strTemp.Find(".hlp")!=-1) continue;
if(strTemp.Find(".hpj")!=-1) continue;
if(strTemp.Find("_AFXDLL")!=-1) continue;
if(strTemp.Find("# ADD CPP")!=-1) continue;
if(strTemp.Find(".pch")!=-1) continue;
if(strTemp.Find("afxcom_.h")!=-1) continue;
if(strTemp.Find(".cpp")!=-1 ||
strTemp.Find(".c")!=-1 ||
strTemp.Find(".h")!=-1)
{
FilterFileString(strTemp);
if(CheckFileAdded(strTemp)==FALSE)
{
m_aryFiles.Add(strTemp);
m_nCurIndex++;
}
}
}
if(bLocalFlag==FALSE)
{
AfxMessageBox("There is some error in the Make file");
return FALSE;
}
return TRUE;
}
获取文件列表后,将对树中选择的每个文件进行解析,以获取注释,空白行和源代码行.这里将考虑C ++和C样式的注释.同样,如果一行包含源代码和注释,则将其视为源代码行.(Once the list of files is got, then each file selected in the tree will be parsed for comments, blank lines and source lines. Both C++ and C style comments will be considered here. Also, if a line contains source code as well as comment, it will be considered as a source line.)
树状清单框(Tree CheckList box)
可以在树控件中选择/取消选择文件以进行代码计数的文件检查/取消检查.的(The file check/uncheck can be done in the tree control for selecting/deselecting files for code counting. The) CTreeCheckListBox
类通过派生自己来实现此功能(class implements this functionality by deriving itself from) CTreeCtrl
类.(class.)
.map文件(.map file)
如果使用Microsoft Developer Studio开发了VC ++项目,则(In case of VC++ projects developed using Microsoft developer studio, the).dsp(.dsp*)*可以解析文件以获取项目中的文件列表.但是代码计数器使用(*file can be parsed for getting the list of files in the project. But Code Counter uses a*)*.地图(*.map*)*文件,用于对在其他环境/平台中开发的C/C ++/Java项目中的行进行计数.例如:我们可以准备一个(*file for counting lines in C/C++/Java projects developed in other environments/platforms. For example: we can prepare a*)*.地图(*.map*)*使用FTP复制文件后,该文件用于在UNIX上开发的C/C ++项目中对行进行计数.该文件是一个简单的文本文件,包含项目中文件的列表.可以使用绝对路径指定文件名.一个样品(*file for counting lines in a C/C++ project developed on UNIX, after copying the files using FTP. This file is a simple text file, containing the list of files in the project. The file names can be given with absolute path necessary. A sample*)*.地图(*.map*)*文件如下. (此样本(*file is given below. (This sample*)*.地图(*.map*)*文件用于CodeCounter项目本身.如果映射文件存储在源目录中,则此处不需要绝对路径.但是,给出绝对路径不会导致任何问题.)(*file is for CodeCounter project itself. Here the absolute paths are not required, if the map file is stored in the source directory. However, giving absolute path does not lead to any problems.)*)
///////////////////////////////////////////////////////////
C:\Users\Prashanth\Projects\VC\CodeCounter\CodeCounter.cpp
C:\Users\Prashanth\Projects\VC\CodeCounter\CodeCounterDlg.cpp
C:\Users\Prashanth\Projects\VC\CodeCounter\LnCountFile.cpp
PrintInfo.cpp
SplashDlg.cpp
StdAfx.cpp
TreeCheckListBox.cpp
CodeCounter.h
CodeCounterDlg.h
LnCountFile.h
PrintInfo.h
SplashDlg.h
StdAfx.h
TreeCheckListBox.h
///////////////////////////////////////////////////////////
结论(Conclusion)
可以在项目开发结束时使用代码计数器,以获取为项目编写的确切源代码.因此,这可以作为良好的生产率工具.同样,使用此工具,可以通过将每个代码计数结果存储在外部文件中,来衡量一天或一周内编写了多少代码.代码计数器还可以衡量项目中提供了多少注释.(The Code Counter can be used at the end of project development, to get the exact amount of source written for the project. Hence this may serve as a good productivity tool. Also, using this tool, one can measure how much of code has been written in a day or a week, by storing each code counting result in an external file. Code counter can also put a measure on how much comments are provided in the project.)
许可
本文以及所有相关的源代码和文件均已获得The Code Project Open License (CPOL)的许可。
VC7.0 C++ VC6 WinXP Windows Win2K MFC Visual-Studio Dev 新闻 翻译