某公司面试题:使用多线程,统计项目程序源代码行数

请设计一个程序:使用多线程,统计项目程序源代码行数;源代码是可以编译通过的合法的代码,统计其物理总行数、其中的空行行数、其中含有有效代码的行数、其中含有注释内容的行数;(要求必须利用多线程编程,如果代码框架能更容易的扩展到支持多种语言的源代码行数统计,将获得更高的评价。)


其中包括以下文件:

Test4.h /Test.cpp /TestStaticHtmlFile.h /TestStaticHtmlFile.cpp /TestStaticCFile.h /TestStaticCFile.cpp /TestStaticPythonFile.h /TestStaticPythonFile.cpp /TestStaticShellFile.h /TestStaticShellFile.cpp


Test4.h

#ifndef TEST_H
#define TEST_H

#include <iostream>
#include <string>
#include <vector>
#include <map> 
#include <fstream>

#include <linux/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>

using namespace std;

#define MAX_LEN 			128
#define MAX_NUM 			256


class CTestObjectDir
{
	public:
		CTestObjectDir(string& sFilePath);
		~CTestObjectDir();

	public:
		void InitObjectDir();
		
		
		map<string,string> GetFileMap(){return _mFileName;}

		static void TrimString(string& sStr);

	private:
		int JudgeDirType();
		string GetFileName();
		void GetFile(const string& sDirName);
		void JudgeFileType(const string& sDirName,const string& sFile);

	private:
		string					_sFilePath;  
		string 					_sSingleFileName;

		int						_iType;

		map<string,string> 		_mFileName;
};

#endif


Test.cpp

#include "Test4.h"

using namespace std;

CTestObjectDir::CTestObjectDir(string& filepath)
{
	cout<<"TestDir Test!!"<<endl;
	_sFilePath = filepath;
	

}

CTestObjectDir::~CTestObjectDir()
{
	cout<<"~TestDir Test!!"<<endl;
	
}

void CTestObjectDir::InitObjectDir()
{
	if(_sFilePath.empty())
		return;
	
	_sSingleFileName = "";
	_iType = -1;

	
	//cout<<"[main]"<<endl;
	_iType = this->JudgeDirType();
	//cout<<"[main] file : "<<this->_sFilePath<<"	_sSingleFileName:"<<this->_sSingleFileName<<"	_iType:"<<this->_iType<<"	"<<this->GetFileName()<<endl;

	this->GetFile(this->_sFilePath);
}

int CTestObjectDir::JudgeDirType()
{
	if(_sFilePath.empty())
		return -1;
	
	int index = _sFilePath.find(".");
	if(0 < index)
	{
		int index_0 = _sFilePath.find_last_of("/");
		if(0 < index_0)
		{
			_sSingleFileName = _sFilePath.substr(index_0+1,_sFilePath.length());
			//cout<<"filename:"<<_sSingleFileName<<endl;
			
		}
		else
		{
			//cout<<"###000"<<endl;
			_sSingleFileName = _sFilePath;
		}
		return 1;
	}
	else
	{
		//cout<<"###111"<<endl;
		int index_1 = _sFilePath.find_last_of("/");
		if(0 <= index_1)
		{
			if(index_1+1 != _sFilePath.length())
				_sFilePath.append("/");
			return 0;
		}
		
	}
}

string CTestObjectDir::GetFileName()//  /XX.cpp  or XX.cpp
{
	if(_sFilePath.empty())
		return "";
	
	int index = _sFilePath.find_last_of("/");
	if(0 <= index)
		return _sFilePath.substr(index+1,_sFilePath.length());
	else
		return _sFilePath;
	
}

void CTestObjectDir::GetFile(const string& sDirName)
{ 
	struct dirent *sDir = NULL;
	DIR *dDir;
	
	cout<<sDirName<<endl;
	dDir = opendir(sDirName.c_str());
	if(NULL == dDir)
		return;

	//cout<<"***000 sDirName = "<<sDirName<<endl;

	sDir = readdir(dDir);
	//cout<<"***000 "<<endl;
	
	while(NULL != (sDir = readdir(dDir)))
	{
		//cout<<"***111	"<<sDir->d_name<<"	"<<(int)(sDir->d_type)<<endl;
		
		if('.' == sDir->d_name[0])
		{
			//cout<<"$"<<endl;
			continue;
		}

		if(4 == (int)(sDir->d_type))
		{
			//cout<<"$$"<<endl;
			string sNewDir = "";
			sNewDir.append(sDirName);
			sNewDir.append(sDir->d_name);
			sNewDir.append("/");
			//cout<<"sNewDir : "<<sNewDir<<endl;
			GetFile(sNewDir);
		}
		else if(8 == (int)(sDir->d_type))
		{
			
			string sFile = "";
			sFile.append(sDir->d_name);
			//cout<<"$$$	"<<sFile<<endl;

			JudgeFileType(sDirName,sFile);
		}
	}
}

void CTestObjectDir::JudgeFileType(const string& sDirName,const string& sFile)
{
	if(sDirName.empty() || sFile.empty())
		return;

	int index = sFile.find(".");
	if(0 < index)
	{
		if(".c" == sFile.substr(index,sFile.length()) || ".h" == sFile.substr(index,sFile.length()) ||
			".cpp" == sFile.substr(index,sFile.length()) || ".hpp" == sFile.substr(index,sFile.length()) ||
			".java" == sFile.substr(index,sFile.length()))
		{
			string sFileUrl = "";
			sFileUrl.append(sDirName);
			sFileUrl.append(sFile);
			_mFileName[sFileUrl] = "C";
			//cout<<"get a [C] file name:"<<sFileUrl<<"	size:"<<_mFileName.size()<<endl;
			return;
		}
		else if(".sh" == sFile.substr(index,sFile.length()))
		{
			string sFileUrl = "";
			sFileUrl.append(sDirName);
			sFileUrl.append(sFile);
			_mFileName[sFileUrl] = "Shell";
			//cout<<"get a [Shell] file name:"<<sFileUrl<<"	size:"<<_mFileName.size()<<endl;
			return;
		}
		else if(".py" == sFile.substr(index,sFile.length()))
		{
			string sFileUrl = "";
			sFileUrl.append(sDirName);
			sFileUrl.append(sFile);
			_mFileName[sFileUrl] = "Python";
			//cout<<"get a [Python] file name:"<<sFileUrl<<"	size:"<<_mFileName.size()<<endl;
			return;
		}
		else if(".html" == sFile.substr(index,sFile.length()))
		{
			string sFileUrl = "";
			sFileUrl.append(sDirName);
			sFileUrl.append(sFile);
			_mFileName[sFileUrl] = "Html";
			//cout<<"get a [Html] file name:"<<sFileUrl<<"	size:"<<_mFileName.size()<<endl;
			return;
		}
	}
	else if(0 == strcasecmp(sFile.c_str(),"makefile"))
	{
		string sFileUrl = "";
		sFileUrl.append(sDirName);
		sFileUrl.append(sFile);
		_mFileName[sFileUrl] = "C";
		//cout<<"get a [Makefile] file name:"<<sFileUrl<<"	size:"<<_mFileName.size()<<endl;
		return;
	}
}

void CTestObjectDir::TrimString(string& sStr)
{
	if(sStr.empty()) 
		return;
	
	sStr.erase(0,sStr.find_first_not_of("\t"));
	sStr.erase(sStr.find_last_not_of("\t") + 1);

	sStr.erase(0,sStr.find_first_not_of("\r"));
	sStr.erase(sStr.find_last_not_of("\r") + 1);

	sStr.erase(0,sStr.find_first_not_of("\n"));
	sStr.erase(sStr.find_last_not_of("\n") + 1);

	sStr.erase(0,sStr.find_first_not_of(" "));
	sStr.erase(sStr.find_last_not_of(" ") + 1);
}


TestStaticCFile.h

#ifndef TEST_STATIC_C_FILE_H
#define TEST_STATIC_C_FILE_H

#include <iostream>
#include <string>
#include <vector>
#include <map> 
#include <fstream>

#include <linux/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>


using namespace std;

class CTestStaticCFile
{
	public:
		CTestStaticCFile(const string& sFilePath):_sFilePath(sFilePath) {}
		~CTestStaticCFile(){}

	public:
		void InitStaticCFile();
		
		static void *GetTotalLine(void *pParam);
		static void *GetBlankLine(void *pParam);
		static void *GetValidLine(void *pParam);
		static void *GetNoteLine(void *pParam);
		
		inline int GetTotalNum() {return _iTotalLine;}
		inline int GetBlankNum() {return _iBlankLine;}
		inline int GetValidNum() {return _iValidLine;}
		inline int GetNoteNum()  {return _iNoteLine;}
		inline string GetFilePath() {return _sFilePath;}

	private:
		void CreateThread();

	private:
		
		string						_sFilePath;

		long						_iTotalLine;
		long						_iBlankLine;
		long						_iValidLine;
		long						_iNoteLine;

};



#endif


TestStaticCFile.cpp

#include "TestStaticCFile.h"
#include "Test4.h"

using namespace std;

void CTestStaticCFile::InitStaticCFile()
{
	this->_iTotalLine = -1;
	this->_iBlankLine = -1;
	this->_iValidLine = -1;
	this->_iNoteLine  = -1;

	this->CreateThread();
}

void CTestStaticCFile::CreateThread()
{
	void *it,*ib,*iv,*in;
	pthread_t pidt,pidb,pidv,pidn;
	
	int iError_t = pthread_create(&pidt,NULL,GetTotalLine,this);  
	if(iError_t)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetTotalLine> is failed!"<<endl;
	    return;
    }

	int iError_b = pthread_create(&pidb,NULL,GetBlankLine,this);  
	if(iError_b)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetBlankLine> is failed!"<<endl;
	    return;
    }

	int iError_v = pthread_create(&pidv,NULL,GetValidLine,this);  
	if(iError_v)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetValidLine> is failed!"<<endl;
	    return;
    }

	int iError_n = pthread_create(&pidn,NULL,GetNoteLine,this);  
	if(iError_n)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetNoteLine> is failed!"<<endl;
	    return;
    }
	
	int iError_rt = pthread_join(pidt,&it);
	if(iError_rt)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidt> is failed!"<<endl;
	    return;
    }

	int iError_rb = pthread_join(pidb,&ib);
	if(iError_rb)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidb> is failed!"<<endl;
	    return;
    }

	int iError_rv = pthread_join(pidv,&iv);
	if(iError_rv)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidv> is failed!"<<endl;
	    return;
    }

	int iError_rn = pthread_join(pidn,&in);
	if(iError_rn)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidn> is failed!"<<endl;
	    return;
    }
	
	_iTotalLine = (long)it;
	_iBlankLine = (long)ib;
	_iValidLine = (long)iv;
	_iNoteLine = (long)in;
}


void *CTestStaticCFile::GetTotalLine(void *pParam)
{
	CTestStaticCFile *pThis = (CTestStaticCFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;
	
	char cBuffer[MAX_NUM];	
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticBase::GetTotalLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return NULL; 
	}

	int iCount = 0;
	while(!iStaticFile.eof() && iStaticFile.getline(cBuffer,MAX_NUM-1)) 
		iCount++;

	iStaticFile.close();
	
	return (void *)iCount;
}

void *CTestStaticCFile::GetBlankLine(void *pParam)
{
	CTestStaticCFile *pThis = (CTestStaticCFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;

	char cBuffer[MAX_NUM];
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticBase::GetBlankLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return 0; 
	}

	int iCount = 0;
	while(!iStaticFile.eof() && iStaticFile.getline(cBuffer,MAX_NUM-1)) 
	{
		string sStr(cBuffer);
		CTestObjectDir::TrimString(sStr);
		if(sStr.empty())
			iCount++;
	}
	iStaticFile.close();
	
	return (void *)(iCount);
}

void *CTestStaticCFile::GetValidLine(void *pParam)
{
	CTestStaticCFile *pThis = (CTestStaticCFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;
	
	char cBuffer[MAX_NUM];
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticBase::GetValidLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return (void *)0; 
	}

	int iCount = 0;
	bool flag = false;		
	while(!iStaticFile.eof() && iStaticFile.getline(cBuffer,MAX_NUM-1)) 
	{	
		string sStr(cBuffer);
		CTestObjectDir::TrimString(sStr);
		if(!sStr.empty())
		{
			int index_s = sStr.find("//");
			if(0 == index_s)
				continue;
			
			int index_f = sStr.find("/*");
			if(0 == index_f)
			{
				flag = true;
			}
			else if(0 < index_f)
			{
				flag = true;
				iCount++;
				int index = sStr.find("*/");
				if(0 < index)
					flag = false;
				continue;
			}
			
			if(flag)
			{
				int index_l = sStr.find("*/");
				if(0 <= index_l)
				{
					if(index_l + 2 == sStr.length())
					{
						flag = false;
						continue;
					}
					else
					{
						flag = false;
						iCount++;
						continue;
					}
				}
				else
					continue;
			}
			else
			{
				iCount++;
				continue;
			}
		}
		
	}
	iStaticFile.close();
	
	return (void *)iCount;
}

void *CTestStaticCFile::GetNoteLine(void *pParam)
{
	CTestStaticCFile *pThis = (CTestStaticCFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;
	
	char buffer[MAX_NUM];	
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticBase::GetNoteLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return (void *)0; 
	}

	int iCount = 0;
	bool flag = false;
	
	while(!iStaticFile.eof() && iStaticFile.getline(buffer,MAX_NUM-1)) 
	{
		string sStr(buffer);
		CTestObjectDir::TrimString(sStr);
		if(!sStr.empty())
		{
			int index_s = sStr.find("//");
			if(0 <= index_s)
			{
				iCount++;
				continue;
			}
			
			int index_f = sStr.find("/*");
			if(0 <= index_f)
				flag = true;
			
			if(flag)
			{
				int index_l = sStr.find("*/");
				if(0 <= index_l)
				{
					flag = false;
					iCount++;
					continue;
				}
				else
				{
					iCount++;
					continue;
				}
			}
		}
	}
	iStaticFile.close();
	
	return (void *)iCount;
}


TestStaticHtmlFile.h

#ifndef TEST_STATIC_HTML_FILE_H
#define TEST_STATIC_HTML_FILE_H

#include <iostream>
#include <string>
#include <vector>
#include <map> 
#include <fstream>

#include <linux/types.h>
#include <dirent.h>
#include <stdlib.h>
#include <stdio.h>
#include <strings.h>
#include <string.h>


using namespace std;

class CTestStaticHtmlFile
{
	public:
		CTestStaticHtmlFile(const string& sFilePath):_sFilePath(sFilePath) {}
		~CTestStaticHtmlFile(){}

	public:
		void InitStaticCFile();
		static void *GetTotalLine(void *pParam);
		static void *GetBlankLine(void *pParam);
		static void *GetValidLine(void *pParam);
		static void *GetNoteLine(void *pParam);
		
		inline int GetTotalNum() {return _iTotalLine;}
		inline int GetBlankNum() {return _iBlankLine;}
		inline int GetValidNum() {return _iValidLine;}
		inline int GetNoteNum()  {return _iNoteLine;}
		inline string GetFilePath() {return _sFilePath;}

	private:
		void CreateThread();

	private:
		
		string						_sFilePath;

		long						_iTotalLine;
		long						_iBlankLine;
		long						_iValidLine;
		long						_iNoteLine;

};



#endif

TestStaticHtmlFile.cpp

#include "TestStaticHtmlFile.h"
#include "Test4.h"

using namespace std;

void CTestStaticHtmlFile::InitStaticCFile()
{
	this->_iTotalLine = -1;
	this->_iBlankLine = -1;
	this->_iValidLine = -1;
	this->_iNoteLine  = -1;

	this->CreateThread();
}

void CTestStaticHtmlFile::CreateThread()
{
	void *it,*ib,*iv,*in;
	pthread_t pidt,pidb,pidv,pidn;
	
	int iError_t = pthread_create(&pidt,NULL,GetTotalLine,this);  
	if(iError_t)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetTotalLine> is failed!"<<endl;
	    return;
    }

	int iError_b = pthread_create(&pidb,NULL,GetBlankLine,this);  
	if(iError_b)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetBlankLine> is failed!"<<endl;
	    return;
    }

	int iError_v = pthread_create(&pidv,NULL,GetValidLine,this);  
	if(iError_v)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetValidLine> is failed!"<<endl;
	    return;
    }

	int iError_n = pthread_create(&pidn,NULL,GetNoteLine,this);  
	if(iError_n)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Create pthread<GetNoteLine> is failed!"<<endl;
	    return;
    }
	
	int iError_rt = pthread_join(pidt,&it);
	if(iError_rt)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidt> is failed!"<<endl;
	    return;
    }

	int iError_rb = pthread_join(pidb,&ib);
	if(iError_rb)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidb> is failed!"<<endl;
	    return;
    }

	int iError_rv = pthread_join(pidv,&iv);
	if(iError_rv)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidv> is failed!"<<endl;
	    return;
    }

	int iError_rn = pthread_join(pidn,&in);
	if(iError_rn)
    {
	    cout<<"[CTestStaticCFile::CreateThread] Jion pthread<pidn> is failed!"<<endl;
	    return;
    }
	
	_iTotalLine = (long)it;
	_iBlankLine = (long)ib;
	_iValidLine = (long)iv;
	_iNoteLine = (long)in;
}


void *CTestStaticHtmlFile::GetTotalLine(void *pParam)
{
	CTestStaticHtmlFile *pThis = (CTestStaticHtmlFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;
	
	char cBuffer[MAX_NUM];	
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticBase::GetTotalLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return NULL; 
	}

	int iCount = 0;
	while(!iStaticFile.eof() && iStaticFile.getline(cBuffer,MAX_NUM-1)) 
		iCount++;

	iStaticFile.close();
	
	return (void *)iCount;
}

void *CTestStaticHtmlFile::GetBlankLine(void *pParam)
{
	CTestStaticHtmlFile *pThis = (CTestStaticHtmlFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;

	char cBuffer[MAX_NUM];
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticBase::GetBlankLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return 0; 
	}

	int iCount = 0;
	while(!iStaticFile.eof() && iStaticFile.getline(cBuffer,MAX_NUM-1)) 
	{
		string sStr(cBuffer);
		CTestObjectDir::TrimString(sStr);
		if(sStr.empty())
			iCount++;
	}
	iStaticFile.close();
	
	return (void *)(iCount);
}

void *CTestStaticHtmlFile::GetValidLine(void *pParam)
{
	CTestStaticHtmlFile *pThis = (CTestStaticHtmlFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;
	
	char cBuffer[MAX_NUM];
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticHtmlFile::GetValidLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return (void *)0; 
	}

	int iCount = 0;
	bool flag = false;
	int iCounter = 0;
	
	while(!iStaticFile.eof() && iStaticFile.getline(cBuffer,MAX_NUM-1)) 
	{	
		iCounter++;
		string sStr(cBuffer);
		CTestObjectDir::TrimString(sStr);
		if(!sStr.empty())
		{
	
			int index_f = sStr.find("<!--");
			if(0 == index_f)
			{
				flag = true;
			}
			else if(0 < index_f)
			{
				flag = true;
				iCount++;
				int index = sStr.find("-->");
				if(0 < index)
					flag = false;
				continue;
			}
			
			if(flag)
			{
				int index_l = sStr.find("-->");
				if(0 <= index_l)
				{
					if(index_l + 3 == sStr.length())
					{
						flag = false;
						continue;
					}
					else
					{
						flag = false;
						iCount++;
						continue;
					}
				}
				else
					continue;
			}
			else
			{
				iCount++;
				continue;
			}
		}
		
	}
	iStaticFile.close();
	
	return (void *)iCount;
}

void *CTestStaticHtmlFile::GetNoteLine(void *pParam)
{
	CTestStaticHtmlFile *pThis = (CTestStaticHtmlFile *)pParam;
	if(NULL == pThis && pThis->GetFilePath().empty())
		return NULL;
	
	char buffer[MAX_NUM];	
	ifstream iStaticFile;
	iStaticFile.open(pThis->GetFilePath().c_str());

	if(!iStaticFile.is_open())
	{ 
		cout<<"[CTestStaticBase::GetNoteLine] Opening file ["<<pThis->GetFilePath()<<"] is failed!"<<endl; 
		return (void *)0; 
	}

	int iCount = 0;
	bool flag = false;
	while(!iStaticFile.eof() && iStaticFile.getline(buffer,MAX_NUM-1)) 
	{
		string sStr(buffer);
		CTestObjectDir::TrimString(sStr);
		if(!sStr.empty())
		{		
			int index_f = sStr.find("<!--");
			if(0 <= index_f)
				flag = true;
			
			if(flag)
			{
				int index_l = sStr.find("-->");
				if(0 <= index_l)
				{
					flag = false;
					iCount++;
					continue;
				}
				else
				{
					iCount++;
					continue;
				}
			}
		}
	}
	iStaticFile.close();
	
	return (void *)iCount;
}





main.cpp

#include "Test4.h"
#include "TestStaticCFile.h"
#include "TestStaticPythonFile.h"
#include "TestStaticShellFile.h"
#include "TestStaticHtmlFile.h"


using namespace std;


void PrintResultC(const string& sFileName)
{
	if(sFileName.empty())
		return;

	CTestStaticCFile *cHandle = new CTestStaticCFile(sFileName);
	cHandle->InitStaticCFile();
	cout<<"["<<sFileName<<"]	Tatol num["<<cHandle->GetTotalNum()<<"]	Blank num["<<cHandle->GetBlankNum()<<"]	Valid num["
		<<cHandle->GetValidNum()<<"]	Note num["<<cHandle->GetNoteNum()<<"]"<<endl;
	delete cHandle;
}


void PrintResultPython(const string& sFileName)
{
	if(sFileName.empty())
		return;

	CTestStaticPythonFile *cHandle = new CTestStaticPythonFile(sFileName);
	cHandle->InitStaticCFile();
	cout<<"["<<sFileName<<"]	Tatol num["<<cHandle->GetTotalNum()<<"]	Blank num["<<cHandle->GetBlankNum()<<"]	Valid num["
		<<cHandle->GetValidNum()<<"]	Note num["<<cHandle->GetNoteNum()<<"]"<<endl;
	delete cHandle;
}


void PrintResultShell(const string& sFileName)
{
	if(sFileName.empty())
		return;

	CTestStaticShellFile *cHandle = new CTestStaticShellFile(sFileName);
	cHandle->InitStaticCFile();
	cout<<"["<<sFileName<<"]	Tatol num["<<cHandle->GetTotalNum()<<"]	Blank num["<<cHandle->GetBlankNum()<<"]	Valid num["
		<<cHandle->GetValidNum()<<"]	Note num["<<cHandle->GetNoteNum()<<"]"<<endl;
	delete cHandle;
}


void PrintResultHtml(const string& sFileName)
{
	if(sFileName.empty())
		return;

	CTestStaticHtmlFile *cHandle = new CTestStaticHtmlFile(sFileName);
	cHandle->InitStaticCFile();
	cout<<"["<<sFileName<<"]	Tatol num["<<cHandle->GetTotalNum()<<"]	Blank num["<<cHandle->GetBlankNum()<<"]	Valid num["
		<<cHandle->GetValidNum()<<"]	Note num["<<cHandle->GetNoteNum()<<"]"<<endl;
	delete cHandle;
}



void test4(string& str)
{
	CTestObjectDir *TOD = new CTestObjectDir(str);
	if(NULL == TOD)
	{
		cout<<"New TestObjectDir is failed!"<<endl;
		return;
	}
	TOD->InitObjectDir();
	map<string,string> mFileName = TOD->GetFileMap();
	
	delete TOD;

	map<string,string>::iterator iter;
	for(iter = mFileName.begin(); iter != mFileName.end(); iter++)  
    {  
		string sFirst = iter->first;
		string sSecond = iter->second; 
		if("C" == sSecond)
			PrintResultC(sFirst);
		else if("Python" == sSecond)
			PrintResultPython(sFirst);
		else if("Shell" == sSecond)
			PrintResultShell(sFirst);
		else if("Html" == sSecond)
			PrintResultHtml(sFirst);
		else 
			cout<<"Other file!	"<<sFirst<<" "<<sSecond<<endl;
    } 

	cout<<"Engineering has "<<mFileName.size()<<" files."<<endl;
}


int main()
{
	try
	{
		char buffer[MAX_LEN];

		loop:
		cout<<"Please input the absolute path of the project directory : "<<endl;
		cin.getline(buffer,MAX_LEN);
		string str(buffer);
		CTestObjectDir::TrimString(str);

		if(str.empty())
		{
			cout<<"Please input the absolute path of the project directory again!"<<endl;
			goto loop;
		}
		
		//string str("/mnt/hgfs/Ubuntu/Test/FLHS/makefile");
		test4(str);
	}
	catch(...)
	{
		cout<<"[test4] has a error!"<<endl;
	}

	return 0;
	
}

Makefile:

CC = g++

FLAGS = -g

LIBS = -lpthread

OBJS = main.o Test4.o TestStaticCFile.o TestStaticPythonFile.o \
		TestStaticShellFile.o TestStaticHtmlFile.o

main:$(OBJS)
	$(CC) $(FLAGS) -o main $(OBJS) $(LIBS)

clean:
	rm -f $(OBJS) main


其余几个模块(TestStaticPythonFile.cpp /TestStaticShellFile.h /TestStaticShellFile.cpp)与CTestStaticHtmlFile相似


希望大家多多提意见。。。

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
代码统计工具使用说明 代码统计工具用Java语言实现,可以对C\C++、Java的源代码进统计统计结果包括:总数、空数、注释数和代码数,统计结果可以以表格的形式显示给用户或另存为文件。 一 执环境: 操作系统:WindowsNT/2000; 执环境:JDK1.3。 二 安装: 1)将code.zip解压缩; 3)设置环境变量: 在 开始—〉设置—〉控制面板—〉系统—〉高级—〉环境变量 中设置环境变量: PATH=[code.zip的安装目录]\jre\bin CLASSPATH=[code.zip的安装目录]\class 三 运: 直接运(双击)在源程序[code.zip的安装目录]\class目录下的CodeStatistic.bat 批处理文件。 〈注〉:如果程序不能正常运,有可能是前面系统环境变量PATH、CLASSPATH设置的问,请检查添加的路径是否正确,确定正确还不能正常运,请注销当前用户后再启动程序。 四 使用方法: 1. 代码统计工具开始工作后,点击窗口下方的“添加”按钮会以对话框的形式浏览硬盘目录,现在可以添加要进统计的C\C++、Java源程序, 2. 选定文件后,会在代码统计工具主界面的列表框中显示已选定的文件名和路径; 3. 点击“添加”按钮继续添加需统计的文件或点击“删除”按钮删除已选文件; 4. 需统计的文件选定后,点击“统计”按钮开始统计,弹出“代码统计结果”窗口以表格形式显示统计结果; 5. 在“代码统计结果” 窗口中可选择将统计结果另存为文件形式; 6. 点击“关闭”按钮关闭“代码统计结果” 窗口,回到主窗口; 7. 继续其它统计活动或点击“退出”按钮退出代码统计工具。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值