commit c21b9a9fecf3f39218f6325e421870c75d1cd575 Author: kiritow <1362050620@qq.com> Date: Mon Aug 7 18:16:55 2017 +0800 Initial Commit diff --git a/FileSystemUtil.h b/FileSystemUtil.h new file mode 100644 index 0000000..545f1dc --- /dev/null +++ b/FileSystemUtil.h @@ -0,0 +1,10 @@ +#pragma once +#include +#include + +/// 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& func); diff --git a/FileSystemUtil_Linux.cpp b/FileSystemUtil_Linux.cpp new file mode 100644 index 0000000..213f3da --- /dev/null +++ b/FileSystemUtil_Linux.cpp @@ -0,0 +1,58 @@ +#include "FileSystemUtil.h" + +#ifndef _WIN32 + +#include +#include +#include +#include + +#include +#include +#include +#include + +void _FindFileRev(const std::string& dirname, + const int skiplevel,const int maxlevel,int nowlevel, + const std::function& 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 || nowleveld_name + "/" ,skiplevel,maxlevel,nowlevel+1,func); + } + } + } + closedir(Dir); +} + +void FindFileRev(const std::string& dirname, + const int skiplevel,const int maxlevel, + const std::function& func) +{ + if (dirname[dirname.size()-1] != '/') + { + string curDir=dirname+"/"; + _FindFileRev(curDir,skiplevel,maxlevel,1,func); + } + else + { + _FindFileRev(dirname,skiplevel,maxlevel,1,func); + } +} + +#endif // _WIN32 diff --git a/FileSystemUtil_Win.cpp b/FileSystemUtil_Win.cpp new file mode 100644 index 0000000..4072f5e --- /dev/null +++ b/FileSystemUtil_Win.cpp @@ -0,0 +1,56 @@ +#include "FileSystemUtil.h" + +#ifdef _WIN32 + +#include +#include +#include +#include +#include +#include +void _FindFileRev(const std::string& dirname, + const int skiplevel,const int maxlevel,int nowlevel, + const std::function& 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 && nowlevelskiplevel) func(fullname); + } + } + while(FindNextFile(hand,&fnd)); + FindClose(hand); + } +} +void FindFileRev(const std::string& dirname, + const int skiplevel,const int maxlevel, + const std::function& 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 diff --git a/Readme.md b/Readme.md new file mode 100644 index 0000000..9595395 --- /dev/null +++ b/Readme.md @@ -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**