libostd/examples/listdir.cc

32 lines
687 B
C++
Raw Normal View History

2017-05-03 02:14:27 +02:00
/** @example listdir.cc
*
* A simple example of integration of std::filesystem with ranges.
*/
2016-01-31 23:19:23 +01:00
#include <ostd/io.hh>
#include <ostd/filesystem.hh>
#include <ostd/range.hh>
2016-01-31 23:19:23 +01:00
using namespace ostd;
void list_dirs(filesystem::path const &path, int off = 0) {
filesystem::directory_iterator ds{path};
for (auto &v: ds) {
auto p = filesystem::path{v};
if (!filesystem::is_directory(p)) {
2016-01-31 23:19:23 +01:00
continue;
}
for_each(range(off), [](int) { write(' '); });
writeln(p.filename());
list_dirs(p, off + 1);
2016-01-31 23:19:23 +01:00
}
}
int main(int argc, char **argv) {
if (argc < 1) {
return 1;
}
2016-01-31 23:19:23 +01:00
list_dirs(argv[1]);
return 0;
2016-02-07 22:17:15 +01:00
}