FileSystemUtil/FileSystemUtil_Linux.cpp

59 lines
1.4 KiB
C++

#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