From fed5df768cde153508199bdbfc0453b8df0d5d27 Mon Sep 17 00:00:00 2001 From: Seungbaek Hong Date: Wed, 23 Nov 2022 19:44:10 +0900 Subject: [PATCH] [WIN32] Replace realpath function with absolute function Since the "realpath" function is not available in the Windows operating system, it has been replaced by an "absolute" function available in both Windows and Linux operating systems. - Replace "std::realpath" function with "std::filesystem::absolute" function. **Self evaluation:** 1. Build test: [x]Passed []Failed []Skipped 2. Run test: [x]Passed []Failed []Skipped Signed-off-by: Seungbaek Hong --- nntrainer/app_context.cpp | 2 +- nntrainer/models/model_loader.cpp | 2 +- nntrainer/utils/util_func.cpp | 12 ++++++++++++ nntrainer/utils/util_func.h | 10 ++++++++++ 4 files changed, 24 insertions(+), 2 deletions(-) diff --git a/nntrainer/app_context.cpp b/nntrainer/app_context.cpp index 43eaaa0b93..fbf560f761 100644 --- a/nntrainer/app_context.cpp +++ b/nntrainer/app_context.cpp @@ -393,7 +393,7 @@ void AppContext::setWorkingDirectory(const std::string &base) { } closedir(dir); - char *ret = realpath(base.c_str(), nullptr); + char *ret = getRealpath(base.c_str(), nullptr); if (ret == nullptr) { std::stringstream ss; diff --git a/nntrainer/models/model_loader.cpp b/nntrainer/models/model_loader.cpp index 3000c9f020..0a22303fb3 100644 --- a/nntrainer/models/model_loader.cpp +++ b/nntrainer/models/model_loader.cpp @@ -470,7 +470,7 @@ int ModelLoader::loadFromConfig(std::string config, NeuralNetwork &model) { model_file_context = std::make_unique(); - auto config_realpath_char = realpath(config.c_str(), nullptr); + auto config_realpath_char = getRealpath(config.c_str(), nullptr); if (config_realpath_char == nullptr) { ml_loge("failed to resolve config path to absolute path, reason: %s", strerror(errno)); diff --git a/nntrainer/utils/util_func.cpp b/nntrainer/utils/util_func.cpp index 1bd1469663..5cf04368d5 100644 --- a/nntrainer/utils/util_func.cpp +++ b/nntrainer/utils/util_func.cpp @@ -20,6 +20,10 @@ * */ +#ifdef _WIN32 +#define MAX_PATH_LENGTH 1024 +#endif + #include #include #include @@ -197,4 +201,12 @@ bool istrequal(const std::string &a, const std::string &b) { }); } +char *getRealpath(const char *__restrict name, char *__restrict resolved) { +#ifdef _WIN32 + return _fullpath(resolved, name, MAX_PATH_LENGTH); +#else + return realpath(name, resolved); +#endif +} + } // namespace nntrainer diff --git a/nntrainer/utils/util_func.h b/nntrainer/utils/util_func.h index 5147accb9d..f51e796bba 100644 --- a/nntrainer/utils/util_func.h +++ b/nntrainer/utils/util_func.h @@ -271,6 +271,16 @@ template T enum_class_or(T e1, T e2) { return static_cast(i1 | i2); } +/** + * @brief Convert a relative path into an absolute path. + * + * @param name relative path + * @param resolved variable to store the result value. + * + * @return absolute path + */ +char *getRealpath(const char *__restrict name, char *__restrict resolved); + } /* namespace nntrainer */ #endif /* __cplusplus */