使用boost::filesystem实现目录遍…
2015-10-10 09:20
344 查看
下面的代码实现了深度优先和广度优先两种遍历方式,可以指定最大遍历深度,可以指定结果中是否包含子文件夹
======================================================================
#include <string>
#include <vector>
#include <deque>
#include <utility>
#include
<boost/filesystem/operations.hpp>
#include
<boost/filesystem/path.hpp>
class file_tool
{
public:
enum traverse_order_t
{
DEPTH_FIRST = 1,
BREADTH_FIRST =
2,
};
enum { UNLIMITED_DEPTH =
-1};
static bool get_sub_files(const
std::string& path,
std::vector<std::string>&
files, int max_depth = UNLIMITED_DEPTH, bool include_sub_dirs =
false, traverse_order_t order = BREADTH_FIRST)
{
using namespace std;
namespace fs =
boost::filesystem;
typedef
std::pair<string,
int> path_and_depth_t;
deque<path_and_depth_t> qu;
{
fs::path root(path);
if
(!fs::exists(root) ||
!fs::is_directory(root))
{
return false;
}
if
(max_depth <= 0 &&
max_depth != UNLIMITED_DEPTH)
{
return true;
}
fs::directory_iterator
end_iter;
for
(fs::directory_iterator
file_itr(root); file_itr != end_iter; ++file_itr)
{
qu.push_back(path_and_depth_t(fs::system_complete(*file_itr).native_directory_string(),
1));
}
======================================================================
#include <string>
#include <vector>
#include <deque>
#include <utility>
#include
<boost/filesystem/operations.hpp>
#include
<boost/filesystem/path.hpp>
class file_tool
{
public:
enum traverse_order_t
{
DEPTH_FIRST = 1,
BREADTH_FIRST =
2,
};
enum { UNLIMITED_DEPTH =
-1};
static bool get_sub_files(const
std::string& path,
std::vector<std::string>&
files, int max_depth = UNLIMITED_DEPTH, bool include_sub_dirs =
false, traverse_order_t order = BREADTH_FIRST)
{
using namespace std;
namespace fs =
boost::filesystem;
typedef
std::pair<string,
int> path_and_depth_t;
deque<path_and_depth_t> qu;
{
fs::path root(path);
if
(!fs::exists(root) ||
!fs::is_directory(root))
{
return false;
}
if
(max_depth <= 0 &&
max_depth != UNLIMITED_DEPTH)
{
return true;
}
fs::directory_iterator
end_iter;
for
(fs::directory_iterator
file_itr(root); file_itr != end_iter; ++file_itr)
{
qu.push_back(path_and_depth_t(fs::system_complete(*file_itr).native_directory_string(),
1));
}
相关文章推荐
- perl输出环境变量
- python 面向对象编程案例01
- 深入理解Java Proxy
- php中new一个类对象的两种写法
- 使用用户自定义类型作为std::map的…
- 为何编译器无法自动生成拷贝函数
- 史上最简单的c++四种cast使用教程
- 项目管理之进度悖论
- 使用boost::asio::write时慎用Comp…
- 使用boost条件变量实现消息队列
- 并行计算:近来语言发展的趋势和我…
- 转载:C, Erlang, Java and Go Web…
- c++中实现类似java printStackTrac…
- ASCII字符随机混淆字典的生成
- 端口复用(SO_REUSEADDR)是干啥用的…
- 正确实现和使用assert
- 《C陷阱与缺陷》书评兼感想
- RAII、异常、构造函数是一家人
- 一个笑话和其对开发的启示
- 并发环境下的单态——应用程序级单态…