I’m having some trouble with the following function. It is supposed to be given a path and a set of allowed file extensions, then find all files in that path with any of those extensions. Instead it finds nothing and returns an empty set.
std::set<boostfs::path> scan_directory(const boostfs::path& p,
const bool recurse,
const std::set<std::string>& allowed) {
std::string ext ;
std::set<boostfs::path> incs, incs2 ;
boostfs::path::iterator itr ;
// Extract directory and filename
boostfs::path file = p.filename() ;
boostfs::path dir = p.parent_path() ;
std::cout << "path: " << p.string() << std::endl ;
for (itr = dir.begin(); itr != dir.end(); ++itr) {
if (boostfs::is_directory(*itr)) {
if (recurse) {
std::cout << "dir: " << itr->string() << std::endl ;
incs2 = scan_directory(*itr, true, allowed) ;
incs.insert(incs2.begin(), incs2.end()) ;
}
} else {
// Don't include the original source
if (*itr != p) {
// Include only allowed file types
ext = itr->extension().string() ;
std::cout << "file: " << itr->string() << std::endl ;
std::cout << "ext: " << ext << std::endl ;
if (allowed.find(ext) != allowed.end()) {
incs.insert(*itr) ;
}
}
}
}
return incs ;
}
The prints to cout are just for debugging. I’m testing it with the following directory structure:
./test/cpp/
foo.cpp
foo.h
test.cpp
./test/cpp/bar/
baz.cpp
baz.h
I invoke the function with the path “test/cpp/test.cpp”, recurse true and a set containing one string “.cpp”. I get the following output from the prints,
path: test/cpp/test.cpp
dir: test
path: test
file: cpp
ext:
Then the function ends and the rest of the program continues, only it’s given an empty set of files so not much to work on. Given the test directory it should return a set containing “test/cpp/foo.cpp” and “test/cpp/bar/baz.cpp”.
I’m fairly sure it worked not long ago, but I’m not sure when it broke or what I did that made it do so. I’m sure it’s some small, annoying technicality.
I found my problem. I was using
path::iteratorinstead ofdirectory_iterator(orrecursive_directory_iterator) so I was looping through the components of the path instead of the contents of the directory. I could have sworn it worked earlier, but that might just have been luck maybe.Here’s my working code
I’ll let it be known that the examples of iterating through directories in Boost’s documentation are AWFUL