recursive_directory_iterator - Same as directory_iterator except for it recurses down into child directories.
| 1340 | /// recursive_directory_iterator - Same as directory_iterator except for it |
| 1341 | /// recurses down into child directories. |
| 1342 | class recursive_directory_iterator { |
| 1343 | std::shared_ptr<detail::RecDirIterState> State; |
| 1344 | bool Follow; |
| 1345 | |
| 1346 | public: |
| 1347 | recursive_directory_iterator() = default; |
| 1348 | explicit recursive_directory_iterator(const Twine &path, std::error_code &ec, |
| 1349 | bool follow_symlinks = true) |
| 1350 | : State(std::make_shared<detail::RecDirIterState>()), |
| 1351 | Follow(follow_symlinks) { |
| 1352 | State->Stack.push(directory_iterator(path, ec, Follow)); |
| 1353 | if (State->Stack.top() == directory_iterator()) |
| 1354 | State.reset(); |
| 1355 | } |
| 1356 | |
| 1357 | // No operator++ because we need error_code. |
| 1358 | recursive_directory_iterator &increment(std::error_code &ec) { |
| 1359 | const directory_iterator end_itr = {}; |
| 1360 | |
| 1361 | if (State->HasNoPushRequest) |
| 1362 | State->HasNoPushRequest = false; |
| 1363 | else { |
| 1364 | file_type type = State->Stack.top()->type(); |
| 1365 | if (type == file_type::symlink_file && Follow) { |
| 1366 | // Resolve the symlink: is it a directory to recurse into? |
| 1367 | ErrorOr<basic_file_status> status = State->Stack.top()->status(); |
| 1368 | if (status) |
| 1369 | type = status->type(); |
| 1370 | // Otherwise broken symlink, and we'll continue. |
| 1371 | } |
| 1372 | if (type == file_type::directory_file) { |
| 1373 | State->Stack.push(directory_iterator(*State->Stack.top(), ec, Follow)); |
| 1374 | if (State->Stack.top() != end_itr) { |
| 1375 | ++State->Level; |
| 1376 | return *this; |
| 1377 | } |
| 1378 | State->Stack.pop(); |
| 1379 | } |
| 1380 | } |
| 1381 | |
| 1382 | while (!State->Stack.empty() |
| 1383 | && State->Stack.top().increment(ec) == end_itr) { |
| 1384 | State->Stack.pop(); |
| 1385 | --State->Level; |
| 1386 | } |
| 1387 | |
| 1388 | // Check if we are done. If so, create an end iterator. |
| 1389 | if (State->Stack.empty()) |
| 1390 | State.reset(); |
| 1391 | |
| 1392 | return *this; |
| 1393 | } |
| 1394 | |
| 1395 | const directory_entry &operator*() const { return *State->Stack.top(); } |
| 1396 | const directory_entry *operator->() const { return &*State->Stack.top(); } |
| 1397 | |
| 1398 | // observers |
| 1399 | /// Gets the current level. Starting path is at level 0. |