From 6f82ee6b781f824a3c399fbd5dc8abd27384432b Mon Sep 17 00:00:00 2001 From: Seungbaek Hong Date: Wed, 23 Nov 2022 19:44:10 +0900 Subject: [PATCH] [utils] Add getRealpath function Since the "realpath" function is not available in the Windows operating system, the "getRealpath" function has been added and replaced so that it can be used in the Windows operating system. - Add "getRealpath" function to utils. **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 43eaaa0..fbf560f 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 3000c9f..0a22303 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 1bd1469..18ca7be 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 *name, char *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 5147acc..8a3e405 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 *name, char *resolved); + } /* namespace nntrainer */ #endif /* __cplusplus */ -- 2.7.4