Skip to content

Commit

Permalink
[utils] Add getRealpath function
Browse files Browse the repository at this point in the history
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>
  • Loading branch information
Seungbaek Hong committed Nov 24, 2022
1 parent b1b3939 commit cd27593
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
8 changes: 8 additions & 0 deletions nntrainer/utils/util_func.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -197,4 +197,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
14 changes: 14 additions & 0 deletions nntrainer/utils/util_func.h
Original file line number Diff line number Diff line change
Expand Up @@ -23,6 +23,10 @@
#ifndef __UTIL_FUNC_H__
#define __UTIL_FUNC_H__

#ifdef _WIN32
#define MAX_PATH_LENGTH 1024
#endif

#ifdef __cplusplus

#include <cstring>
Expand Down Expand Up @@ -271,6 +275,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 cd27593

Please sign in to comment.