コード例 #1
0
ファイル: WC.cpp プロジェクト: linkaixing/WC
int main(int argc,char *argv[])
{
	InitInput initInput;
	initInput.init(argc,argv);
	bool useCWL=false;
	if(initInput.dir=="" && !initInput.x){
		cout<<"请输入参数!";
		return 0;
	}
	//输入-x时
	if(initInput.x){
		OPENFILENAME ofn;      // 公共对话框结构。     
		TCHAR szFile[MAX_PATH]; // 保存获取文件名称的缓冲区。               
		// 初始化选择文件对话框。     
		ZeroMemory(&ofn, sizeof(OPENFILENAME));    
		ofn.lStructSize = sizeof(OPENFILENAME);    
		ofn.hwndOwner = NULL;    
		ofn.lpstrFile = szFile;    
		ofn.lpstrFile[0] = '\0';   
		ofn.nMaxFile = sizeof(szFile);    
		ofn.lpstrFilter = "All(*.*)\0*.*\0Text(*.txt)\0*.TXT\0\0";    
		ofn.nFilterIndex = 1;    
		ofn.lpstrFileTitle = NULL;    
		ofn.nMaxFileTitle = 0;    
		ofn.lpstrInitialDir = NULL;    
		ofn.Flags = OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
		while(1){
			// 显示打开选择文件对话框。
			if ( GetOpenFileName(&ofn) ){ 
				CWL cwl;
				SA sa;
				ifstream file;
				file.open(szFile,ios::in);
				if(!file.good()){
					cout<<"文件打开失败!"<<endl;
					continue;
				}
				cwl.charCount(file);
				cwl.wordCount(file);
				cwl.lineCount(file);
				useCWL=true;
				sa.cal(file);
				file.close();
				if(useCWL)
					cout<<endl;
				cout<<"代码行数:"<<sa.codeRow<<endl;
				cout<<"空行数:"<<sa.nullRow<<endl;
				cout<<"注释行数:"<<sa.annoRow;
			}
			else
				break;
		}
	}
	//输入不是-x时
	else{
		//输入-c -w -l时
		if(!strstr(initInput.dir.c_str(),"*") && !strstr(initInput.dir.c_str(),"?") && (initInput.c || initInput.w || initInput.l)){
			CWL cwl;
			ifstream file;
			file.open(initInput.dir,ios::in);
			if(!file.good()){
				cout<<initInput.dir+"文件打开失败!";
				return 0;
			}
			if(initInput.c){
				cwl.charCount(file);
			}
			if(initInput.w){
				cwl.wordCount(file);
			}
			if(initInput.l){
				cwl.lineCount(file);
			}
			file.close();
			useCWL=true;
		}
		//输入-s -a时
		if(initInput.s || initInput.a){
			SA sa;
			if((initInput.dir[0]=='*' || initInput.dir[0]=='?') && initInput.dir[1]=='.'){
				if(initInput.s)
					sa.searchFile(sa.GetProgramDir().c_str(),initInput.dir,0);
				else
					sa.searchFile(sa.GetProgramDir().c_str(),initInput.dir,1);
			}
			else if(strstr(initInput.dir.c_str(),"*") || strstr(initInput.dir.c_str(),"?")){
				string str;
				if(strstr(initInput.dir.c_str(),"*"))
					str=strstr(initInput.dir.c_str(),"*");
				else
					str=strstr(initInput.dir.c_str(),"?");
				if(str[1]=='.'){
					string s=initInput.dir.substr(0,initInput.dir.length()-str.length());
					if(initInput.s)
						sa.searchFile(s.c_str(),str,0);
					else
						sa.searchFile(s.c_str(),str,1);
				}
				else
					sa.statfileurllist.push_back(initInput.dir);
			}
			else
				sa.statfileurllist.push_back(initInput.dir);
			for(int i=0;i<sa.statfileurllist.size();i++){
				ifstream file;
				file.open(sa.statfileurllist[i],ios::in);
				if(!file.good()){
					cout<<sa.statfileurllist[i]+"文件打开失败";
					return 0;
				}
				sa.cal(file);
				file.close();
			}
			if(useCWL)
				cout<<endl;
			cout<<"代码行数:"<<sa.codeRow<<endl;
			cout<<"空行数:"<<sa.nullRow<<endl;
			cout<<"注释行数:"<<sa.annoRow;
		}
		else{
			if(!useCWL)
				cout<<"请输入正确路径!";
		}
	}
	return 0;
}