📄 dir.cpp
字号:
// Dir.cpp: implementation of the CDir class.
//
//////////////////////////////////////////////////////////////////////
#include "stdafx.h"
#include "check.h"
#include "Dir.h"
#ifdef _DEBUG
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#define new DEBUG_NEW
#endif
//////////////////////////////////////////////////////////////////////
// Construction/Destruction
//////////////////////////////////////////////////////////////////////
CDir::CDir()
{
}
CDir::~CDir()
{
}
//搜索整个文件夹下的匹配文件(递归)
bool CDir::search_file_r(const char* folder, const char* pattern, vector<std::string>& set_file)
{
if (folder == 0 || folder[0] == 0 ||
pattern == 0 || pattern[0] == 0)
return false;
//get all subfolder
vector<std::string> set_folder;
set_folder.push_back(folder);
if (!get_all_sub_folders(folder, set_folder))
return false;
//search file with pattern in each folder
int cnt = set_folder.size();
for (int i = 0; i < cnt; i++)
{
const char* folder = set_folder[i].c_str();
//dir
DIR_DATA dir;
dir.set_file.clear();
dir.set_folder.clear();
if (!dir_pattern(folder, pattern, dir))
continue;
//add filename/foldername
int file_cnt = dir.set_file.size();
int folder_cnt = dir.set_folder.size();
for (int j = 0; j < file_cnt; j++)
{
std::string s1 = folder;
s1 += "\\";
s1 += dir.set_file[j].c_str();
set_file.push_back(s1);
//TRACE1("%s\n", s1.c_str());
}
for (j = 0; j < folder_cnt; j++)
{
std::string s1 = folder;
s1 += "\\";
s1 += dir.set_folder[j].c_str();
set_folder.push_back(s1);
}
}
return true;
}
//获取整个文件夹下的所有文件夹
bool CDir::get_all_sub_folders(const char* folder, vector<std::string>& set_folder)
{
if (folder == 0 || folder[0] == 0)
return false;
//set folder and file
char curfolder[MAX_PATH] = {0};
strcpy( curfolder, folder );
char file[MAX_PATH] = {0};
sprintf( file, "%s\\*.*", curfolder );
//find file
WIN32_FIND_DATA wfd;
HANDLE hFind = ::FindFirstFile( file, &wfd );
if (hFind == INVALID_HANDLE_VALUE)
return true;
while (1)
{
char* name = wfd.cFileName;
if (strcmp(name, ".") == 0 || strcmp(name, "..") == 0)
{
if (!::FindNextFile(hFind, &wfd)) break;
continue;
}
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
{
//sub folder name
char subfolder[MAX_PATH] = {0};
sprintf( subfolder, "%s\\%s", curfolder, name );
std::string s1 = subfolder;
set_folder.push_back(s1);
//get folders recursively!
if (!get_all_sub_folders(subfolder, set_folder))
{
::FindClose(hFind);
return false;
}
}
if (!::FindNextFile(hFind, &wfd)) break;
}
::FindClose(hFind);
return true;
}
//查询文件夹(按模式)
bool CDir::dir_pattern(const char* folder, const char* pattern, DIR_DATA& dir)
{
if (folder == 0 || folder[0] == 0 ||
pattern == 0 || pattern[0] == 0)
return false;
//set pattern file
char ptnfile[MAX_PATH] = {0};
if (strlen(pattern) == 0)
strcpy(ptnfile, folder);
else
sprintf(ptnfile, "%s\\%s", folder, pattern);
//find file
HANDLE hFind = INVALID_HANDLE_VALUE;
WIN32_FIND_DATA wfd;
hFind = FindFirstFile( ptnfile, &wfd );
if (hFind == INVALID_HANDLE_VALUE)
return false;
//walk through each file/folder
while (1)
{
char* name = wfd.cFileName;
if (stricmp(name, ".") == 0 || stricmp(name, "..") == 0)
{
if (!FindNextFile(hFind, &wfd))
break;
continue;
}
string s1 = name;
if (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
dir.set_folder.push_back(s1);
else
dir.set_file.push_back(s1);
if (!FindNextFile(hFind, &wfd))
break;
}
FindClose(hFind);
return true;
}
bool CDir::search_src_file(const char* folder, vector<std::string>& set_file)
{
if (folder == 0 || folder[0] == 0)
return false;
set_file.clear();
bool b1 = this->search_file_r(folder, "*.h", set_file);
bool b2 = this->search_file_r(folder, "*.cpp", set_file);
return b1 && b2;
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -