chat-template: enumerate files w/ C API rather than private using std::__fs::filesystem

This commit is contained in:
ochafik 2024-09-28 18:47:11 +01:00
parent c657857e21
commit 6e0053a81b

View File

@ -13,6 +13,7 @@
#include <iostream>
#include <string>
#include <json.hpp>
#include <dirent.h>
using json = nlohmann::ordered_json;
@ -39,9 +40,22 @@ static void assert_equals(const T & expected, const T & actual) {
static std::vector<std::string> find_files(const std::string & folder, const std::string & ext) {
std::vector<std::string> files;
for (const auto & entry : std::__fs::filesystem::directory_iterator(folder)) {
if (entry.path().extension() == ext)
files.push_back(entry.path().string());
// Note: once we can use C++17 this becomes:
// for (const auto & entry : std::filesystem::directory_iterator(folder))
// if (entry.path().extension() == ext) files.push_back(entry.path().string());
DIR* dir = opendir(folder.c_str());
if (dir != nullptr) {
struct dirent* entry;
while ((entry = readdir(dir)) != nullptr) {
if (entry->d_type == DT_REG) { // If it's a regular file
std::string filename = entry->d_name;
if (filename.length() >= ext.length() &&
filename.compare(filename.length() - ext.length(), ext.length(), ext) == 0) {
files.push_back(folder + "/" + filename);
}
}
}
closedir(dir);
}
return files;
}