使用boost::filesystem实现目录遍…

下面的代码实现了深度优先和广度优先两种遍历方式,可以指定最大遍历深度,可以指定结果中是否包含子文件夹
======================================================================
#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));                                                     
                                 
                     
            while (!qu.empty())
            {
                  path_and_depth_t path_and_depth = (order == DEPTH_FIRST) ? qu.back() : qu.front();
                  string& file_str(path_and_depth.first);
                  int depth = path_and_depth.second;
                  if (order == DEPTH_FIRST)
                  {
                        qu.pop_back();
                  }
                  else
                  {
                        qu.pop_front();
                  }
                  fs::path file(file_str);
                  if (fs::exists(file))
                  {
                        if (fs::is_directory(file))
                        {
                              if (include_sub_dirs)
                              {
                                    files.push_back(file_str);                                   
                              }
                              if (depth < max_depth || max_depth == UNLIMITED_DEPTH)
                              {
                                    int next_depth = depth + 1;
                                    fs::directory_iterator end_iter;
                                    for (fs::directory_iterator file_itr(file); file_itr != end_iter; ++file_itr)
                                    {
                                          qu.push_back(path_and_depth_t(fs::system_complete(*file_itr).native_directory_string(), next_depth));                                                     
                                    }
                              }
                        }
                        else
                        {
                              files.push_back(file_str);                             
                        }
                                 
            }
            return true;
      }

};



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值