c++代码函数统计
2010-09-23 11:54
330 查看
说明:
直接放入控制台程序运行即可
生成的程序放入c++代码文件夹下,即可以统计此文件夹及其子文件夹下的所有代码行数!
运行效果:
具体代码:
直接放入控制台程序运行即可
生成的程序放入c++代码文件夹下,即可以统计此文件夹及其子文件夹下的所有代码行数!
运行效果:
具体代码:
// CodeStat.cpp : Defines the entry point for the console application. // #pragma warning(disable:4786) #pragma warning(disable:4788) #include "stdafx.h" #include <string> #include <vector> #include <map> #include <Algorithm> #include <io.h> #include <conio.h> using namespace std; struct TCodeLines { unsigned int m_iEmpty; //空白行1 unsigned int m_iCode; //代码行2|4 unsigned int m_iNote; //注释行3|4 unsigned int m_iTotal; //总行数 TCodeLines() { clear(); } void clear() { m_iEmpty=0; m_iCode=0; m_iNote=0; m_iTotal=0; } void Add(const TCodeLines &p) { m_iEmpty+=p.m_iEmpty; m_iCode+=p.m_iCode; m_iNote+=p.m_iNote; m_iTotal+=p.m_iTotal; } }; typedef vector<string> TStrings; typedef map<string,TCodeLines*> TCodeLineStat; const char *FileEx[]={".C",".H",".CPP",".HPP"}; class TCodeStat { TCodeLineStat m_CLPages; TCodeLines m_Total; TCodeLines* m_TempCur; unsigned int m_iLineState; public: TCodeStat() { m_iLineState=1; m_TempCur=NULL; } TCodeStat(const char *path) { m_iLineState=1; if(-1!=_access(path,0)) { TStrings pageFiles; ReadFileNames(path,pageFiles); AddFiles(pageFiles); } } TCodeStat(const TStrings& pageFiles) { m_iLineState=1; AddFiles(pageFiles); } void AddFiles(const TStrings& pageFiles) { char *content=NULL; long len=0,lastLen=0; for(TStrings::const_iterator it =pageFiles.begin();it!=pageFiles.end();it++) { FILE *fp=fopen(it->c_str(),"rb"); if(!fp)continue; fseek(fp,0,SEEK_END); len=ftell(fp)+1; if(!content||lastLen<len) { if(content)delete[] content; lastLen=len; content=new char[len]; if(!content)continue; } fseek(fp,0,SEEK_SET); fread(content,len-1,1,fp); content[len-1]=0; AddFile(*it ,content); fclose(fp); } if(content)delete[] content; } ~TCodeStat() { Clear(); } void AddFile(const string &fileName ,const char *content) { if(Parse(content)) { m_Total.Add(*m_TempCur); m_CLPages[fileName]=m_TempCur; } } string GentResult() { string result; char a[50]={0}; TCodeLineStat::const_iterator it=m_CLPages.begin(); while(it!=m_CLPages.end()) { result+=it->first+":/r/n"; itoa(it->second->m_iTotal,a,10); result=result+"Total:"+a+","; itoa(it->second->m_iCode,a,10); result=result+"Code:"+a+","; itoa(it->second->m_iNote,a,10); result=result+"Note:"+a+","; itoa(it->second->m_iEmpty,a,10); result=result+"Empty:"+a+"/r/n/r/n"; it++; } result=result+"======================================================================/r/nProject Total:/r/n"; itoa(m_Total.m_iTotal,a,10); result=result+"Total:"+a+","; itoa(m_Total.m_iCode,a,10); result=result+"Code:"+a+","; itoa(m_Total.m_iNote,a,10); result=result+"Note:"+a+","; itoa(m_Total.m_iEmpty,a,10); result=result+"Empty:"+a+"/r/n/r/n"; return result; } TCodeLineStat* GetResult(){return &m_CLPages;} TCodeLines* GetResultTotal(){return &m_Total;} void Clear() { TCodeLineStat::iterator it=m_CLPages.begin(); while(it!=m_CLPages.end()) { delete it->second; it++; } m_CLPages.clear(); m_Total.clear(); m_iLineState=1; m_TempCur=NULL; } protected: void ReadFileNames( const string& path,TStrings &pageFiles) { WIN32_FIND_DATA fd; string searchFile; string path1=path; if('//'==path[path.size()-1]||'/'==path[path.size()-1]) { searchFile = path1 + "*.*"; } else { path1=path1+"//"; searchFile = path1 + "*.*"; } HANDLE hFind = ::FindFirstFile( searchFile.c_str(), &fd ); do { string filename(fd.cFileName); if ( !(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) ) { string Ex=filename.substr(filename.size()-2); transform(Ex.begin(),Ex.end(),Ex.begin(),toupper); if(Ex==FileEx[0]||Ex==FileEx[1]) { pageFiles.push_back(path1 + filename.c_str()); } else { Ex=filename.substr(filename.size()-4); transform(Ex.begin(),Ex.end(),Ex.begin(),toupper); if(Ex==FileEx[2]||Ex==FileEx[3]) { pageFiles.push_back(path1 + filename.c_str()); } } } else if ((filename != ".") && (filename != "..")) { ReadFileNames( path1 + filename.c_str() + "//" ,pageFiles); } } while (::FindNextFile (hFind, &fd)); (void) ::FindClose(hFind); } bool IsString(const char *ch) { return '/"'==*ch||'/''==*ch?true:false; } const char *AnalyzeString(const char *ch) { char k=*ch++; while(*ch&&k!=*ch) { if(IsLine(ch)){AddLine();ch++;continue;} if('//'==*ch){ch++;if(*ch){if(IsLine(ch))AddLine();ch++;}} else ch++; } if(k==*ch)ch++; return ch; } bool IsNote(const char *ch) { return (*ch&&*(ch+1)&&'/'==*ch&&('/'==*(ch+1)||'*'==*(ch+1)))?true:false; } const char *AnalyzeNote(const char *ch) { char k=*(ch+1); ch+=2; if(1==m_iLineState)m_iLineState=3; else if(2==m_iLineState)m_iLineState=4; while(*ch) { if(IsLine(ch)){AddLine();ch++;if('/'==k) return ch;continue;} if(*(ch+1)&&'*'==*ch&&'/'==*(ch+1))return ch+2; ch++; } return ch; } bool IsKeyword(const char *ch) { return '/t'==*ch||' '==*ch||'/r'==*ch?false :true; } bool IsLine(const char *ch) { return '/n'==*ch?true:false; } void AddLine() { switch(m_iLineState) { case 1:m_TempCur->m_iEmpty++;break; case 2:m_TempCur->m_iCode++;break; case 3:m_TempCur->m_iNote++;break; case 4:m_TempCur->m_iCode++;m_TempCur->m_iNote++;break; } m_iLineState=1; m_TempCur->m_iTotal++; } TCodeLines* Parse(const char *p) { m_TempCur=new TCodeLines(); if(!m_TempCur)return NULL; if(!p||!*p)return m_TempCur; while(*p) { if(IsLine(p)){AddLine();p++;continue;} if(IsString(p)){p=AnalyzeString(p);continue;} if(IsNote(p)){p=AnalyzeNote(p);continue;} if(IsKeyword(p)) { if(1==m_iLineState)m_iLineState=2; else if(3==m_iLineState)m_iLineState=4; } p++; } AddLine(); return m_TempCur; } }; int main(int argc, char* argv[]) { string path=argv[0]; path=path.substr(0,path.rfind('//')); if(argc>1)path=argv[1]; TCodeStat stat(path.c_str()); printf("%s/n",stat.GentResult().c_str()); getch(); return 0; }
相关文章推荐
- strstr()函数c++代码实现实例及运行结果
- c++截取屏幕图片并保存(函数代码实现)
- c/c++/MFC 调用 js 函数代码
- 使用VAssistX为VS2008 c++代码添加函数头注释 .
- c++截取屏幕图片并保存(函数代码实现)
- C++反汇编代码分析--函数调用
- c++ 普通函数 虚函数 纯虚函数 代码例子
- 编程算法 - 求1+2+...+n(函数继承) 代码(C++)
- 【高质量代码】如何写出更高质量的C/C++代码(2):函数设计
- C++程序运行时内存布局之----------局部变量,全局变量,静态变量,函数代码,new出来的变量
- C 代码调用 C++ 变量、函数提示未定义
- strstr()函数c++代码实现实例及运行结果
- 在C++中使用Lambda函数提高代码性能
- 【C++】统计代码覆盖率(三)
- c++虚表的使用 通过虚表调用虚函数的演示代码
- 教你快速在c#中调用C++代码(函数)
- strstr()函数c++代码实现实例及运行结果
- 使用VAssistX为VS2008 c++代码添加函数头注释
- c++ 统计函数执行时间class
- C++反汇编代码分析——函数调用