Fork of Tangara with customizations
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
tangara-fw/src/database/include/file_gatherer.hpp

60 lines
1.5 KiB

2 years ago
#pragma once
#include <deque>
#include <functional>
#include <sstream>
#include <string>
#include "ff.h"
namespace database {
static_assert(sizeof(TCHAR) == sizeof(char), "TCHAR must be CHAR");
template <typename Callback>
auto FindFiles(const std::string& root, Callback cb) -> void {
2 years ago
std::deque<std::string> to_explore;
to_explore.push_back(root);
while (!to_explore.empty()) {
std::string next_path_str = to_explore.front();
const TCHAR* next_path = static_cast<const TCHAR*>(next_path_str.c_str());
FF_DIR dir;
2 years ago
FRESULT res = f_opendir(&dir, next_path);
if (res != FR_OK) {
// TODO: log.
continue;
}
for (;;) {
FILINFO info;
res = f_readdir(&dir, &info);
if (res != FR_OK || info.fname[0] == 0) {
2 years ago
// No more files in the directory.
break;
} else if (info.fattrib & (AM_HID | AM_SYS) || info.fname[0] == '.') {
2 years ago
// System or hidden file. Ignore it and move on.
continue;
} else {
std::stringstream full_path;
full_path << next_path_str << "/" << info.fname;
if (info.fattrib & AM_DIR) {
// This is a directory. Add it to the explore queue.
to_explore.push_back(full_path.str());
} else {
// This is a file! Let the callback know about it.
// std::invoke(cb, full_path.str(), info);
std::invoke(cb, full_path.str());
2 years ago
}
}
}
f_closedir(&dir);
to_explore.pop_front();
}
}
} // namespace database