[utils] Add getRealpath function
authorSeungbaek Hong <sb92.hong@samsung.net>
Wed, 23 Nov 2022 10:44:10 +0000 (19:44 +0900)
committerJijoong Moon <jijoong.moon@samsung.com>
Thu, 1 Dec 2022 02:07:02 +0000 (11:07 +0900)
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 <sb92.hong@samsung.net>
nntrainer/app_context.cpp
nntrainer/models/model_loader.cpp
nntrainer/utils/util_func.cpp
nntrainer/utils/util_func.h

index 43eaaa0..fbf560f 100644 (file)
@@ -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;
index 3000c9f..0a22303 100644 (file)
@@ -470,7 +470,7 @@ int ModelLoader::loadFromConfig(std::string config, NeuralNetwork &model) {
 
   model_file_context = std::make_unique<AppContext>();
 
-  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));
index 1bd1469..18ca7be 100644 (file)
  *
  */
 
+#ifdef _WIN32
+#define MAX_PATH_LENGTH 1024
+#endif
+
 #include <cmath>
 #include <fstream>
 #include <random>
@@ -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
index 5147acc..8a3e405 100644 (file)
@@ -271,6 +271,16 @@ template <typename T, typename C = int> T enum_class_or(T e1, T e2) {
   return static_cast<T>(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 */