From 675997e56cd8f63bacb8da26a77693fa07f517c7 Mon Sep 17 00:00:00 2001 From: Grzegorz Kisala Date: Fri, 17 Jan 2025 09:35:35 +0100 Subject: [PATCH] This PR start series of changes which should make possible compilation of nntrainer on windows platform. Header dirent.h is not available on windows so to make code more portable I've replaced this functionality with std::filesystem Self-evaluation: Build test: [X]Passed [ ]Failed [ ]Skipped Run test: [X]Passed [ ]Failed [ ]Skipped --- nntrainer/app_context.cpp | 32 ++++++++++++-------------------- 1 file changed, 12 insertions(+), 20 deletions(-) diff --git a/nntrainer/app_context.cpp b/nntrainer/app_context.cpp index 61890fc9d8..6c700d88ea 100644 --- a/nntrainer/app_context.cpp +++ b/nntrainer/app_context.cpp @@ -11,8 +11,8 @@ * @bug No known bugs except for NYI items * */ -#include #include +#include #include #include #include @@ -418,14 +418,11 @@ AppContext &AppContext::Global() { } void AppContext::setWorkingDirectory(const std::string &base) { - DIR *dir = opendir(base.c_str()); - - if (!dir) { + if (!std::filesystem::is_directory(base)) { std::stringstream ss; ss << func_tag << "path is not directory or has no permission: " << base; throw std::invalid_argument(ss.str().c_str()); } - closedir(dir); char *ret = getRealpath(base.c_str(), nullptr); @@ -571,40 +568,35 @@ int AppContext::registerOptimizer(const std::string &library_path, std::vector AppContext::registerPluggableFromDirectory(const std::string &base_path) { - DIR *dir = opendir(base_path.c_str()); + const auto directory_exist = std::filesystem::is_directory(base_path); - NNTR_THROW_IF(dir == nullptr, std::invalid_argument) + NNTR_THROW_IF(!directory_exist, std::invalid_argument) << func_tag << "failed to open the directory: " << base_path; - struct dirent *entry; + std::vector keys = {}; - std::vector keys; + for (const auto &entry : std::filesystem::directory_iterator(base_path)) { + const auto &entry_name = entry.path().string(); - while ((entry = readdir(dir)) != NULL) { - if (endswith(entry->d_name, solib_suffix)) { - if (endswith(entry->d_name, layerlib_suffix)) { + if (endswith(entry_name, solib_suffix)) { + if (endswith(entry_name, layerlib_suffix)) { try { - int key = registerLayer(entry->d_name, base_path); + int key = registerLayer(entry_name, base_path); keys.emplace_back(key); } catch (std::exception &e) { - closedir(dir); throw; } - } else if (endswith(entry->d_name, optimizerlib_suffix)) { + } else if (endswith(entry_name, optimizerlib_suffix)) { try { - int key = registerOptimizer(entry->d_name, base_path); + int key = registerOptimizer(entry_name, base_path); keys.emplace_back(key); } catch (std::exception &e) { - closedir(dir); throw; } } } } - if (dir != NULL) - closedir(dir); - return keys; }