aboutsummaryrefslogtreecommitdiffstats
path: root/host/lib/utils/pathslib.cpp
diff options
context:
space:
mode:
authorMartin Braun <martin.braun@ettus.com>2018-02-16 18:21:44 -0800
committerMartin Braun <martin.braun@ettus.com>2018-02-20 16:23:46 -0800
commita99e89a689490aa03e2bbd8153b61e394cd55a6e (patch)
treedb28c00c4d5b44bba918eb4fc52a6683e52fa181 /host/lib/utils/pathslib.cpp
parent2ff296c99760628c4b0042cf6d2b32a0e3986364 (diff)
downloaduhd-a99e89a689490aa03e2bbd8153b61e394cd55a6e.tar.gz
uhd-a99e89a689490aa03e2bbd8153b61e394cd55a6e.tar.bz2
uhd-a99e89a689490aa03e2bbd8153b61e394cd55a6e.zip
lib: Add path_expandvars() internal API call
Diffstat (limited to 'host/lib/utils/pathslib.cpp')
-rw-r--r--host/lib/utils/pathslib.cpp45
1 files changed, 45 insertions, 0 deletions
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
+}
+