knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
URI.cpp
Go to the documentation of this file.
1 /*
2  * This file is part of KnowRob, please consult
3  * https://github.com/knowrob/knowrob for license details.
4  */
5 
6 #ifndef _WIN32
7 
8 #include <pwd.h>
9 
10 #endif
11 
12 #include <filesystem>
13 #include "knowrob/URI.h"
14 #include "knowrob/Logger.h"
15 #include <optional>
16 
17 using namespace knowrob;
18 
19 URI::URI(std::string_view path)
20  : path_(path) {
21  updateURI();
22 }
23 
24 URI::URI(std::string path, std::string protocol, std::string host, int port)
25  : path_(std::move(path)),
26  protocol_(std::move(protocol)),
27  host_(std::move(host)),
28  port_(port) {
29  updateURI();
30 }
31 
32 URI::URI(const boost::property_tree::ptree &property_tree) {
33  protocol_ = property_tree.get_optional<std::string>("protocol");
34  path_ = property_tree.get("path", "/");
35  host_ = property_tree.get_optional<std::string>("host");
36  port_ = property_tree.get_optional<int>("port");
37  updateURI();
38 }
39 
41  std::stringstream ss;
42  if (protocol_ && protocol_.value() != "file") {
43  ss << protocol_ << "://";
44  }
45  if (host_) {
46  ss << host_.value();
47  if (port_) ss << ':' << port_.value();
48  }
49  ss << path_;
50  uri_ = ss.str();
51 }
52 
53 std::optional<std::string> URI::getHomePath(void) {
54 #ifdef _WIN32
55  auto env_drive = getenv("HOMEDRIVE");
56  auto env_path = getenv("HOMEPATH");
57  if (env_drive && env_path) {
58  return std::string(env_drive) + std::string(env_path);
59  } else {
60  KB_WARN("Could not determine home directory");
61  return std::nullopt;
62  }
63 #else
64  auto env_home = getenv("HOME");
65  if (env_home) {
66  return std::string(env_home);
67  } else {
68  struct passwd *pw = getpwuid(getuid());
69  if (pw) {
70  return std::string(pw->pw_dir);
71  } else {
72  KB_WARN("Could not determine home directory");
73  return std::nullopt;
74  }
75  }
76 #endif
77 }
78 
79 std::string URI::resolve(const std::string_view &uriString) {
80  static std::filesystem::path projectPath(KNOWROB_SOURCE_DIR);
81  static std::filesystem::path installPath(KNOWROB_INSTALL_PREFIX);
82  static std::optional<std::string> homePath(getHomePath());
83 
84  std::filesystem::path filePath(uriString);
85  if (!exists(filePath)) {
86  std::vector<std::filesystem::path> possiblePaths;
87  // prefer loading from source directory
88  possiblePaths.push_back(projectPath / filePath);
89  possiblePaths.push_back(projectPath / "src" / filePath);
90  // next try loading from home directory
91  if (homePath) {
92  possiblePaths.push_back(std::filesystem::path(homePath.value()) / ".knowrob" / filePath);
93  }
94  // lastly try loading from install directory
95  possiblePaths.push_back(installPath / "share" / "knowrob" / filePath);
96  possiblePaths.push_back(installPath / "lib" / "knowrob" / filePath);
97 
98  for (const auto &p: possiblePaths) {
99  if (exists(p)) return p.u8string();
100  }
101  }
102  return std::string(uriString);
103 }
#define KB_WARN
Definition: Logger.h:27
boost::optional< int > port_
Definition: URI.h:64
static std::optional< std::string > getHomePath(void)
Definition: URI.cpp:53
boost::optional< std::string > host_
Definition: URI.h:63
void updateURI()
Definition: URI.cpp:40
boost::optional< std::string > protocol_
Definition: URI.h:62
static std::string resolve(const std::string_view &uriString)
Definition: URI.cpp:79
std::string uri_
Definition: URI.h:60
std::string path_
Definition: URI.h:61
URI(std::string_view path)
Definition: URI.cpp:19
TermRule & string()
Definition: terms.cpp:63