Skip to content

Commit

Permalink
[WIN32] Replace realpath function with absolute function
Browse files Browse the repository at this point in the history
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 <sb92.hong@samsung.net>
  • Loading branch information
Seungbaek Hong committed Nov 30, 2022
1 parent b1b3939 commit fed5df7
Show file tree
Hide file tree
Showing 4 changed files with 24 additions and 2 deletions.
2 changes: 1 addition & 1 deletion nntrainer/app_context.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;
Expand Down
2 changes: 1 addition & 1 deletion nntrainer/models/model_loader.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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));
Expand Down
12 changes: 12 additions & 0 deletions nntrainer/utils/util_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,10 @@
*
*/

#ifdef _WIN32
#define MAX_PATH_LENGTH 1024
#endif

#include <cmath>
#include <fstream>
#include <random>
Expand Down Expand Up @@ -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
10 changes: 10 additions & 0 deletions nntrainer/utils/util_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 *__restrict name, char *__restrict resolved);

} /* namespace nntrainer */

#endif /* __cplusplus */
Expand Down

0 comments on commit fed5df7

Please sign in to comment.