非递归遍历,参见代码
m_exts里应该写类似于"h;cpp;c;php"这样的
- void wxWidgetsTestDialog::Onm_browseClick(wxCommandEvent& event)
- {
- wxDirDialog dirDlg(this,
- "Select a folder contains source files",
- wxEmptyString
- );
- dirDlg.ShowModal();
- wxString path = dirDlg.GetPath();
- // the user did not select any folder
- if(path == wxEmptyString)
- {
- return;
- }
- wxString exts = this->m_exts->GetValue();
- wxArrayString extArray = wxSplit(exts,';');
- if(extArray.empty())
- {
- wxMessageBox("file filter is empty.");
- return;
- }
- // get the selected folder
- this->m_targetFolder->SetLabel(path);
- this->m_resultMessage->SetLabel("parsing...\n");
- wxSetWorkingDirectory(path);
- std::queue<wxString> openList; // construct a openList
- std::vector<wxString> searchResults; // construct a search result vector
- openList.push(path);
- while(!openList.empty())
- {
- wxString currentPath = openList.front();
- wxSetWorkingDirectory(currentPath);
- openList.pop();
- wxString findResult = wxFindFirstFile("*.*",0); // search file & dir
- while(findResult != wxEmptyString)
- {
- wxString fullPath = wxGetCwd()+"\\"+findResult; // get the file's full path
- // if the file is a directory , add to openList
- if(wxDirExists(fullPath))
- {
- openList.push(fullPath);
- }
- else
- {
- wxString curExt;
- wxSplitPath(fullPath,NULL,NULL,&curExt);
- int len = extArray.size();
- for(int i = 0;i<len;i++)
- {
- if(curExt == extArray[i])
- {
- searchResults.push_back(fullPath);
- }
- }
- }
- findResult = wxFindNextFile();
- }
- }
- unsigned int linesTotal = 0;
- BOOST_FOREACH(wxString item,searchResults)
- {
- int ret = GetFileLineCount(item);
- this->m_resultMessage->AppendText(wxString::Format("file: %s , %d lines.\n",item.c_str(),ret));
- linesTotal += ret;
- }
- this->m_resultMessage->AppendText(wxString::Format("%d lines total.",linesTotal));
- }
- unsigned int GetFileLineCount(wxString path)
- {
- unsigned int count = 0;
- wxFile file;
- if(!file.Open(path))
- {
- return 0;
- }
- wxFileInputStream fileInputStream(path);
- wxTextInputStream textStream(fileInputStream);
- wxString temp = textStream.ReadLine();
- while(fileInputStream.CanRead())
- {
- count ++;
- temp = textStream.ReadLine();
- }
- return count;
- }
转载于:https://blog.51cto.com/bhlzlx/1016647