tool-call: add fs_list_files to common, w/ win32 impl for msys2 build

This commit is contained in:
ochafik 2024-09-29 00:34:07 +01:00
parent cb7912ee74
commit 9ac4b04aa2
3 changed files with 41 additions and 24 deletions

View File

@ -44,6 +44,7 @@
#include <fcntl.h> #include <fcntl.h>
#include <io.h> #include <io.h>
#else #else
#include <dirent.h>
#include <sys/ioctl.h> #include <sys/ioctl.h>
#include <sys/stat.h> #include <sys/stat.h>
#include <unistd.h> #include <unistd.h>
@ -777,6 +778,43 @@ bool fs_create_directory_with_parents(const std::string & path) {
#endif // _WIN32 #endif // _WIN32
} }
std::vector<std::string> fs_list_files(const std::string & folder, const std::string & ext) {
std::vector<std::string> files;
// 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());
#ifdef _WIN32
std::string search_path = folder + "\\*" + ext;
WIN32_FIND_DATA fd;
HANDLE hFind = ::FindFirstFile(search_path.c_str(), &fd);
if (hFind != INVALID_HANDLE_VALUE) {
do {
if (!(fd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)) {
files.push_back(folder + "\\" + fd.cFileName);
}
} while (::FindNextFile(hFind, &fd));
::FindClose(hFind);
}
#else
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);
}
#endif
return files;
}
std::string fs_get_cache_directory() { std::string fs_get_cache_directory() {
std::string cache_directory = ""; std::string cache_directory = "";
auto ensure_trailing_slash = [](std::string p) { auto ensure_trailing_slash = [](std::string p) {

View File

@ -397,6 +397,7 @@ std::string string_from(const struct llama_context * ctx, const struct llama_bat
bool fs_validate_filename(const std::string & filename); bool fs_validate_filename(const std::string & filename);
bool fs_create_directory_with_parents(const std::string & path); bool fs_create_directory_with_parents(const std::string & path);
std::vector<std::string> fs_list_files(const std::string & path, const std::string & ext);
std::string fs_get_cache_directory(); std::string fs_get_cache_directory();
std::string fs_get_cache_file(const std::string & filename); std::string fs_get_cache_file(const std::string & filename);

View File

@ -13,7 +13,6 @@
#include <iostream> #include <iostream>
#include <string> #include <string>
#include <json.hpp> #include <json.hpp>
#include <dirent.h>
using json = nlohmann::ordered_json; using json = nlohmann::ordered_json;
@ -39,30 +38,9 @@ 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) { static std::vector<std::string> find_files(const std::string & folder, const std::string & ext) {
auto do_find = [&](const std::string & folder) { auto files = fs_list_files(folder, ext);
std::vector<std::string> files;
// 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;
};
auto files = do_find(folder);
if (files.empty()) { if (files.empty()) {
files = do_find("../" + folder); files = fs_list_files("../" + folder, ext);
} }
return files; return files;
} }