diff options
Diffstat (limited to 'host/lib')
-rw-r--r-- | host/lib/include/uhdlib/utils/paths.hpp | 24 | ||||
-rw-r--r-- | host/lib/utils/CMakeLists.txt | 1 | ||||
-rw-r--r-- | host/lib/utils/paths.cpp | 2 | ||||
-rw-r--r-- | host/lib/utils/pathslib.cpp | 45 |
4 files changed, 72 insertions, 0 deletions
diff --git a/host/lib/include/uhdlib/utils/paths.hpp b/host/lib/include/uhdlib/utils/paths.hpp new file mode 100644 index 000000000..d74973301 --- /dev/null +++ b/host/lib/include/uhdlib/utils/paths.hpp @@ -0,0 +1,24 @@ +// +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0+ +// + +#ifndef INCLUDED_UHDLIB_UTILS_PATHS_HPP +#define INCLUDED_UHDLIB_UTILS_PATHS_HPP + +#include <string> + +namespace uhd { + + /*! Expand environment variables in paths, like Python's + * os.path.expandvars(). + * + * If expansion fails, will simply return the original path. + */ + std::string path_expandvars(const std::string& path); + +} /* namespace uhd */ + +#endif /* INCLUDED_UHDLIB_UTILS_PATHS_HPP */ + diff --git a/host/lib/utils/CMakeLists.txt b/host/lib/utils/CMakeLists.txt index 7af094e83..62d1ba090 100644 --- a/host/lib/utils/CMakeLists.txt +++ b/host/lib/utils/CMakeLists.txt @@ -168,6 +168,7 @@ LIBUHD_APPEND_SOURCES( ${CMAKE_CURRENT_SOURCE_DIR}/load_modules.cpp ${CMAKE_CURRENT_SOURCE_DIR}/log.cpp ${CMAKE_CURRENT_SOURCE_DIR}/paths.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/pathslib.cpp ${CMAKE_CURRENT_SOURCE_DIR}/platform.cpp ${CMAKE_CURRENT_SOURCE_DIR}/static.cpp ${CMAKE_CURRENT_SOURCE_DIR}/tasks.cpp diff --git a/host/lib/utils/paths.cpp b/host/lib/utils/paths.cpp index d609e254f..8a7ba001f 100644 --- a/host/lib/utils/paths.cpp +++ b/host/lib/utils/paths.cpp @@ -8,6 +8,7 @@ #include <uhd/config.hpp> #include <uhd/exception.hpp> #include <uhd/utils/paths.hpp> +#include <uhdlib/utils/paths.hpp> #include <boost/algorithm/string.hpp> #include <boost/bind.hpp> @@ -96,6 +97,7 @@ static std::vector<std::string> get_env_paths(const std::string &var_name){ } #ifndef UHD_PLATFORM_WIN32 +// NOTE: This could be replaced by path_expandvars() /*! Expand a tilde character to the $HOME path. * * The path passed to this function must start with the tilde character in order diff --git a/host/lib/utils/pathslib.cpp b/host/lib/utils/pathslib.cpp new file mode 100644 index 000000000..cbb0d0917 --- /dev/null +++ b/host/lib/utils/pathslib.cpp @@ -0,0 +1,45 @@ +// +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0+ +// + +#include <uhd/config.hpp> +#include <uhdlib/utils/paths.hpp> + +#ifdef BOOST_MSVC +# include <windows.h> +#else +# include <wordexp.h> +#endif + +std::string uhd::path_expandvars(const std::string& path) +{ + if (path.empty()) { + return path; + } +#ifdef BOOST_MSVC + constexpr size_t max_pathlen = 4096; + char result[max_pathlen]; + const size_t result_len = ExpandEnvironmentStrings( + path.c_str(), + &result[0], + max_pathlen + ); + if (result == 0) { + return path; + } + return std::string(result, result+result_len); +#else + wordexp_t p; + std::string return_value; + if (wordexp(path.c_str(), &p, 0) == 0 && p.we_wordc > 0) { + return_value = std::string(p.we_wordv[0]); + } else { + return_value = path; + } + wordfree(&p); + return return_value; +#endif +} + |