Initial Commit

master
Kirigaya Kazuto 2017-08-07 18:16:55 +08:00
commit c21b9a9fec
4 changed files with 131 additions and 0 deletions

10
FileSystemUtil.h Normal file
View File

@ -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);

58
FileSystemUtil_Linux.cpp Normal file
View File

@ -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

56
FileSystemUtil_Win.cpp Normal file
View File

@ -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

7
Readme.md Normal file
View File

@ -0,0 +1,7 @@
# File System Util
Some useful class and functions to operate file system.
All Rights Reserved. Not For Public.
**HC TECH 2017**