mirror of
https://github.com/ggerganov/llama.cpp.git
synced 2025-01-13 04:00:16 +00:00
tool-call
: add fs_list_files to common, w/ win32 impl for msys2 build
This commit is contained in:
parent
cb7912ee74
commit
9ac4b04aa2
@ -44,6 +44,7 @@
|
||||
#include <fcntl.h>
|
||||
#include <io.h>
|
||||
#else
|
||||
#include <dirent.h>
|
||||
#include <sys/ioctl.h>
|
||||
#include <sys/stat.h>
|
||||
#include <unistd.h>
|
||||
@ -777,6 +778,43 @@ bool fs_create_directory_with_parents(const std::string & path) {
|
||||
#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 cache_directory = "";
|
||||
auto ensure_trailing_slash = [](std::string p) {
|
||||
|
@ -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_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_file(const std::string & filename);
|
||||
|
@ -13,7 +13,6 @@
|
||||
#include <iostream>
|
||||
#include <string>
|
||||
#include <json.hpp>
|
||||
#include <dirent.h>
|
||||
|
||||
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) {
|
||||
auto do_find = [&](const std::string & folder) {
|
||||
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);
|
||||
auto files = fs_list_files(folder, ext);
|
||||
if (files.empty()) {
|
||||
files = do_find("../" + folder);
|
||||
files = fs_list_files("../" + folder, ext);
|
||||
}
|
||||
return files;
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user