wxWidgets编写代码行数统计工具

 非递归遍历,参见代码

m_exts里应该写类似于"h;cpp;c;php"这样的

  
  
  
  
  1. void wxWidgetsTestDialog::Onm_browseClick(wxCommandEvent& event) 
  2.     wxDirDialog dirDlg(this
  3.                        "Select a folder contains source files"
  4.                        wxEmptyString 
  5.                        ); 
  6.     dirDlg.ShowModal(); 
  7.     wxString path = dirDlg.GetPath(); 
  8.     // the user did not select any folder 
  9.     if(path == wxEmptyString) 
  10.     { 
  11.         return
  12.     } 
  13.     wxString exts = this->m_exts->GetValue(); 
  14.     wxArrayString extArray = wxSplit(exts,';'); 
  15.     if(extArray.empty()) 
  16.     { 
  17.         wxMessageBox("file filter is empty."); 
  18.         return
  19.     } 
  20.     // get the selected folder 
  21.     this->m_targetFolder->SetLabel(path); 
  22.     this->m_resultMessage->SetLabel("parsing...\n"); 
  23.     wxSetWorkingDirectory(path); 
  24.     std::queue<wxString> openList;                                 // construct a openList 
  25.     std::vector<wxString> searchResults;                           // construct a search result vector 
  26.     openList.push(path); 
  27.     while(!openList.empty()) 
  28.     { 
  29.         wxString currentPath = openList.front(); 
  30.         wxSetWorkingDirectory(currentPath); 
  31.         openList.pop(); 
  32.         wxString findResult = wxFindFirstFile("*.*",0);             // search file & dir 
  33.         while(findResult != wxEmptyString) 
  34.         { 
  35.             wxString fullPath = wxGetCwd()+"\\"+findResult;         // get the file's full path 
  36.             // if the file is a directory , add to openList 
  37.             if(wxDirExists(fullPath)) 
  38.             { 
  39.                 openList.push(fullPath); 
  40.             } 
  41.             else 
  42.             { 
  43.                 wxString curExt; 
  44.                 wxSplitPath(fullPath,NULL,NULL,&curExt); 
  45.                 int len = extArray.size(); 
  46.                 for(int i = 0;i<len;i++) 
  47.                 { 
  48.                     if(curExt == extArray[i]) 
  49.                     { 
  50.                         searchResults.push_back(fullPath); 
  51.                     } 
  52.                 } 
  53.             } 
  54.             findResult = wxFindNextFile(); 
  55.         } 
  56.     } 
  57.  
  58.     unsigned int linesTotal = 0; 
  59.  
  60.     BOOST_FOREACH(wxString item,searchResults) 
  61.     { 
  62.         int ret = GetFileLineCount(item); 
  63.         this->m_resultMessage->AppendText(wxString::Format("file: %s , %d lines.\n",item.c_str(),ret)); 
  64.         linesTotal += ret; 
  65.     } 
  66.  
  67.     this->m_resultMessage->AppendText(wxString::Format("%d lines total.",linesTotal)); 

 

  
  
  
  
  1. unsigned int GetFileLineCount(wxString path) 
  2.     unsigned int count = 0; 
  3.     wxFile file; 
  4.     if(!file.Open(path)) 
  5.     { 
  6.         return 0; 
  7.     } 
  8.     wxFileInputStream fileInputStream(path); 
  9.     wxTextInputStream textStream(fileInputStream); 
  10.  
  11.     wxString temp =   textStream.ReadLine(); 
  12.     while(fileInputStream.CanRead()) 
  13.     { 
  14.         count ++; 
  15.         temp = textStream.ReadLine(); 
  16.     } 
  17.     return count; 

 

本文出自 “冰狐浪子的博客” 博客,请务必保留此出处http://bhlzlx.blog.51cto.com/3389283/1016647

你可能感兴趣的:(代码,遍历,行数,文件夹,wxwidgets)