Initial Commit
commit
c21b9a9fec
|
@ -0,0 +1,10 @@
|
|||
#pragma once
|
||||
#include <string>
|
||||
#include <functional>
|
||||
|
||||
/// Declaration
|
||||
/// Set 'skiplevel' to <1 to stop skipping.
|
||||
/// Set 'maxlevel' to <1 to ignore max level.
|
||||
void FindFileRev(const std::string& dirname,
|
||||
const int skiplevel,const int maxlevel,
|
||||
const std::function<void(const std::string&)>& func);
|
|
@ -0,0 +1,58 @@
|
|||
#include "FileSystemUtil.h"
|
||||
|
||||
#ifndef _WIN32
|
||||
|
||||
#include <dirent.h>
|
||||
#include <sys/types.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
|
||||
#include <cstdio>
|
||||
#include <ctime>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
|
||||
void _FindFileRev(const std::string& dirname,
|
||||
const int skiplevel,const int maxlevel,int nowlevel,
|
||||
const std::function<void(const std::string&)>& func)
|
||||
{
|
||||
DIR* Dir = NULL;
|
||||
struct dirent* file = NULL;
|
||||
|
||||
if ((Dir = opendir(dirname.c_str())) == NULL)
|
||||
{
|
||||
return ;
|
||||
}
|
||||
while ((file = readdir(Dir)) != nullptr)
|
||||
{
|
||||
if (file->d_type == DT_REG)
|
||||
{
|
||||
if(nowlevel>skiplevel) func(dirname + file->d_name);
|
||||
}
|
||||
else if (file->d_type == DT_DIR && strcmp(file->d_name, ".") != 0 && strcmp(file->d_name, "..") != 0)
|
||||
{
|
||||
if(maxlevel<1 || nowlevel<maxlevel)
|
||||
{
|
||||
_FindFileRev(dirname + file->d_name + "/" ,skiplevel,maxlevel,nowlevel+1,func);
|
||||
}
|
||||
}
|
||||
}
|
||||
closedir(Dir);
|
||||
}
|
||||
|
||||
void FindFileRev(const std::string& dirname,
|
||||
const int skiplevel,const int maxlevel,
|
||||
const std::function<void(const std::string&)>& func)
|
||||
{
|
||||
if (dirname[dirname.size()-1] != '/')
|
||||
{
|
||||
string curDir=dirname+"/";
|
||||
_FindFileRev(curDir,skiplevel,maxlevel,1,func);
|
||||
}
|
||||
else
|
||||
{
|
||||
_FindFileRev(dirname,skiplevel,maxlevel,1,func);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
|
@ -0,0 +1,56 @@
|
|||
#include "FileSystemUtil.h"
|
||||
|
||||
#ifdef _WIN32
|
||||
|
||||
#include <windows.h>
|
||||
#include <cstdio>
|
||||
#include <cstdlib>
|
||||
#include <cstring>
|
||||
#include <string>
|
||||
#include <functional>
|
||||
void _FindFileRev(const std::string& dirname,
|
||||
const int skiplevel,const int maxlevel,int nowlevel,
|
||||
const std::function<void(const std::string&)>& func)
|
||||
{
|
||||
std::string patternString=dirname+"*";
|
||||
|
||||
WIN32_FIND_DATA fnd;
|
||||
HANDLE hand=FindFirstFile(patternString.c_str(),&fnd);
|
||||
if(hand!=INVALID_HANDLE_VALUE)
|
||||
{
|
||||
do
|
||||
{
|
||||
std::string fullname=dirname+fnd.cFileName;
|
||||
if(fnd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
|
||||
{
|
||||
if(maxlevel>0 && nowlevel<maxlevel)
|
||||
{
|
||||
fullname.append("\\");
|
||||
_FindFileRev(fullname,skiplevel,maxlevel,nowlevel+1,func);
|
||||
}
|
||||
}
|
||||
else
|
||||
{
|
||||
if(nowlevel>skiplevel) func(fullname);
|
||||
}
|
||||
}
|
||||
while(FindNextFile(hand,&fnd));
|
||||
FindClose(hand);
|
||||
}
|
||||
}
|
||||
void FindFileRev(const std::string& dirname,
|
||||
const int skiplevel,const int maxlevel,
|
||||
const std::function<void(const std::string&)>& func)
|
||||
{
|
||||
if(dirname[dirname.size()-1]!='\\')
|
||||
{
|
||||
std::string dirnamex=dirname+"\\";
|
||||
_FindFileRev(dirnamex,skiplevel,maxlevel,1,func);
|
||||
}
|
||||
else
|
||||
{
|
||||
_FindFileRev(dirname,skiplevel,maxlevel,1,func);
|
||||
}
|
||||
}
|
||||
|
||||
#endif // _WIN32
|
Loading…
Reference in New Issue