tue_filesystem
crawler.cpp
Go to the documentation of this file.
1 
4 
5 #include <console_bridge/console.h>
6 
7 namespace tue
8 {
9 namespace filesystem
10 {
11 
12 Crawler::Crawler() : recursive_(true), ignore_hidden_dirs_(true), ignore_hidden_files_(true),
13  list_dirs_(false), list_files_(true)
14 {
15 }
16 
17 Crawler::Crawler(const Path& root_path) : recursive_(true), ignore_hidden_dirs_(true),
18  ignore_hidden_files_(true), list_dirs_(false), list_files_(true)
19 {
20  setRootPath(root_path);
21 }
22 
24 {
25 }
26 
27 void Crawler::setRootPath(const Path& root_path)
28 {
29  boost::filesystem::path p(root_path.string());
30  it_dir_ = boost::filesystem::recursive_directory_iterator(p);
31 }
32 
34 {
35  boost::filesystem::recursive_directory_iterator end;
36 
37  while(it_dir_ != end)
38  {
39  // Compute whether the current name should be returned to the caller.
40  bool found = false;
41 
42  if (list_files_ && boost::filesystem::is_regular_file(*it_dir_))
43  {
44  if (!ignore_hidden_files_ || it_dir_->path().filename().string()[0] != '.')
45  {
46  path = it_dir_->path().string();
47  found = true;
48  }
49  }
50  else if (boost::filesystem::is_directory(*it_dir_))
51  {
52  if (!ignore_hidden_dirs_ || it_dir_->path().filename().string()[0] != '.')
53  {
54  if (list_dirs_)
55  {
56  path = it_dir_->path().string();
57  found = true;
58  }
59 
60  if (!recursive_)
61  it_dir_.no_push();
62  }
63  else
64  {
65  it_dir_.no_push();
66  }
67  }
68 
69  // Advance to the next file or directory.
70  try
71  {
72  ++it_dir_;
73  }
74  catch(std::exception& ex)
75  {
76  // We couldn't access the next item in the collection, so we assume it refers to a directory that we can't
77  // access and we ask the iterator class not to navigate in that directory but skip to the next element.
78 
79  CONSOLE_BRIDGE_logError(ex.what());
80  it_dir_.no_push();
81  }
82 
83  if (found)
84  return true;
85  }
86 
87  return false;
88 }
89 
90 } // end filesystem namespace
91 } // end tue namespace
std::exception
tue::filesystem::Crawler::ignore_hidden_dirs_
bool ignore_hidden_dirs_
If set, hidden directories are not expanded.
Definition: crawler.h:99
tue::filesystem::Path
Definition: path.h:18
tue::filesystem::Crawler::nextPath
bool nextPath(Path &path)
Definition: crawler.cpp:33
tue::filesystem::Crawler::it_dir_
boost::filesystem::recursive_directory_iterator it_dir_
Definition: crawler.h:105
tue::filesystem::Crawler::setRootPath
void setRootPath(const Path &root_path)
Definition: crawler.cpp:27
tue::filesystem::Crawler::list_files_
bool list_files_
If set, iterator returns found files.
Definition: crawler.h:102
tue::filesystem::Crawler::ignore_hidden_files_
bool ignore_hidden_files_
If set, hidden files are not returned in the iterator.
Definition: crawler.h:100
tue::filesystem::Crawler::list_dirs_
bool list_dirs_
If set, iterator returns found directories.
Definition: crawler.h:101
tue::filesystem::Crawler::recursive_
bool recursive_
If set, iterator also returns content of sub-directories.
Definition: crawler.h:98
tue::filesystem::Crawler::~Crawler
virtual ~Crawler()
Definition: crawler.cpp:23
crawler.h
tue::filesystem::Path::string
const std::string & string() const
Definition: path.h:45
tue::filesystem::Crawler::Crawler
Crawler()
Definition: crawler.cpp:12
tue
Definition: crawler.h:9
std::exception::what
T what(T... args)