diff options
Diffstat (limited to 'host')
| -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 | ||||
| -rw-r--r-- | host/tests/CMakeLists.txt | 23 | ||||
| -rw-r--r-- | host/tests/paths_test.cpp | 72 | 
6 files changed, 167 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 +} + diff --git a/host/tests/CMakeLists.txt b/host/tests/CMakeLists.txt index 437a697d0..7d53b50c4 100644 --- a/host/tests/CMakeLists.txt +++ b/host/tests/CMakeLists.txt @@ -125,6 +125,29 @@ UHD_INSTALL(TARGETS      COMPONENT tests  ) +ADD_EXECUTABLE(paths_test +    paths_test.cpp +    ${CMAKE_SOURCE_DIR}/lib/utils/pathslib.cpp +) +# Careful: This is to satisfy the out-of-library build of paths.cpp. This is +# duplicate code from lib/utils/CMakeLists.txt, and it's been simplified. +SET(UHD_LIB_DIR "lib") +FILE(TO_NATIVE_PATH "${CMAKE_INSTALL_PREFIX}" UHD_PKG_PATH) +STRING(REPLACE "\\" "\\\\" UHD_PKG_PATH "${UHD_PKG_PATH}") +SET_SOURCE_FILES_PROPERTIES( +    ${CMAKE_SOURCE_DIR}/lib/utils/paths.cpp +    PROPERTIES COMPILE_DEFINITIONS +    "UHD_PKG_PATH=\"${UHD_PKG_PATH}\";UHD_LIB_DIR=\"${UHD_LIB_DIR}\"" +) +TARGET_LINK_LIBRARIES(paths_test uhd ${Boost_LIBRARIES}) +UHD_ADD_TEST(paths_test paths_test) +UHD_INSTALL(TARGETS +    paths_test +    RUNTIME +    DESTINATION ${PKG_LIB_DIR}/tests +    COMPONENT tests +) +  ########################################################################  # demo of a loadable module  ######################################################################## diff --git a/host/tests/paths_test.cpp b/host/tests/paths_test.cpp new file mode 100644 index 000000000..0ccebd333 --- /dev/null +++ b/host/tests/paths_test.cpp @@ -0,0 +1,72 @@ +// +// Copyright 2018 Ettus Research, a National Instruments Company +// +// SPDX-License-Identifier: GPL-3.0-or-later +// + +#include <uhd/config.hpp> +#include <uhd/exception.hpp> +#include <uhd/utils/paths.hpp> +#include <uhdlib/utils/paths.hpp> +#include <boost/test/unit_test.hpp> +#include <boost/filesystem/operations.hpp> +#include <vector> +#include <iostream> + +BOOST_AUTO_TEST_CASE(test_paths_expandvars) { +#ifdef UHD_PLATFORM_WIN32 +    const std::string path_to_expand("\%programdata%/uhd/uhd.conf"); +#else +    const std::string path_to_expand("$HOME/.uhd/uhd.conf"); +#endif +    const std::string expanded_path = uhd::path_expandvars(path_to_expand); + +    std::cout << "Expanded path: " << path_to_expand << " -> " +              << expanded_path << std::endl; +    BOOST_CHECK(path_to_expand != expanded_path); + +#ifdef UHD_PLATFORM_WIN32 +    const std::string var_identifier = "%"; +#else +    const std::string var_identifier = "$"; +#endif + +    BOOST_CHECK(expanded_path.find(var_identifier) == std::string::npos); +} + + +BOOST_AUTO_TEST_CASE(test_get_paths) { +    using namespace uhd; + +    const std::string tmp_path = get_tmp_path(); +    const std::string app_path = get_app_path(); +    const std::string pkg_path = get_pkg_path(); +    const auto module_paths = get_module_paths(); + +    std::cout << "tmp_path: " << tmp_path << std::endl; +    std::cout << "app_path: " << app_path << std::endl; +    std::cout << "pkg_path: " << pkg_path << std::endl; +    for (const auto& module_path : module_paths) { +        std::cout << "module path: " << module_path << std::endl; +    } + +    const std::string images_dir_search_path = ""; +    const std::string images_dir = get_images_dir(images_dir_search_path); +    BOOST_REQUIRE_THROW( +        find_image_path("this_device_does_not_exist.bit", ""), +        uhd::io_error +    ); + +    const std::string utility_path = find_utility( +            "uhd_images_downloader" +    ); +    std::cout << "utility_path: " << utility_path << std::endl; + +    const std::string utility_error = print_utility_error( +            "uhd_images_downloader", +            "--help" +    ); +    std::cout << "utility_error: " << tmp_path << std::endl; + +} + | 
