knowrob  2.1.0
A Knowledge Base System for Cognition-enabled Robots
knowrob::URI Class Reference

#include <URI.h>

Public Member Functions

 URI (std::string_view path)
 
 URI (const boost::property_tree::ptree &property_tree)
 
 URI (std::string path, std::string protocol, std::string host, int port)
 
const auto & operator() () const
 
const std::string & path () const
 
 URI (std::string_view path)
 
 URI (const boost::property_tree::ptree &property_tree)
 
 URI (std::string path, std::string protocol, std::string host, int port)
 
const auto & operator() () const
 
const std::string & path () const
 

Static Public Member Functions

static std::string resolve (const std::string_view &uriString)
 
static std::optional< std::string > getHomePath (void)
 
static std::string resolve (const std::string_view &uriString)
 
static std::optional< std::string > getHomePath (void)
 

Protected Member Functions

void updateURI ()
 
void updateURI ()
 

Protected Attributes

std::string uri_
 
std::string path_
 
boost::optional< std::string > protocol_
 
boost::optional< std::string > host_
 
boost::optional< int > port_
 

Detailed Description

A URI is a uniform resource identifier.

Definition at line 17 of file URI.h.

Constructor & Destructor Documentation

◆ URI() [1/6]

URI::URI ( std::string_view  path)
explicit
Parameters
paththe path of the URI.

Definition at line 19 of file URI.cpp.

20  : path_(path) {
21  updateURI();
22 }
const std::string & path() const
Definition: URI.h:57
void updateURI()
Definition: URI.cpp:40
std::string path_
Definition: URI.h:61

◆ URI() [2/6]

URI::URI ( const boost::property_tree::ptree &  property_tree)
explicit
Parameters
property_treethe property tree to construct the URI from.

Definition at line 32 of file URI.cpp.

32  {
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 }
boost::optional< int > port_
Definition: URI.h:64
boost::optional< std::string > host_
Definition: URI.h:63
boost::optional< std::string > protocol_
Definition: URI.h:62
TermRule & string()
Definition: terms.cpp:63

◆ URI() [3/6]

URI::URI ( std::string  path,
std::string  protocol,
std::string  host,
int  port 
)
Parameters
paththe path of the URI.
protocolthe protocol of the URI.
hostthe host of the URI.
portthe port of the URI.

Definition at line 24 of file URI.cpp.

25  : path_(std::move(path)),
26  protocol_(std::move(protocol)),
27  host_(std::move(host)),
28  port_(port) {
29  updateURI();
30 }

◆ URI() [4/6]

knowrob::URI::URI ( std::string_view  path)
explicit
Parameters
paththe path of the URI.

◆ URI() [5/6]

knowrob::URI::URI ( const boost::property_tree::ptree &  property_tree)
explicit
Parameters
property_treethe property tree to construct the URI from.

◆ URI() [6/6]

knowrob::URI::URI ( std::string  path,
std::string  protocol,
std::string  host,
int  port 
)
Parameters
paththe path of the URI.
protocolthe protocol of the URI.
hostthe host of the URI.
portthe port of the URI.

Member Function Documentation

◆ getHomePath() [1/2]

std::optional< std::string > URI::getHomePath ( void  )
static
Returns
the home path of the current user.

Definition at line 53 of file URI.cpp.

53  {
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 }
#define KB_WARN
Definition: Logger.h:27

◆ getHomePath() [2/2]

static std::optional<std::string> knowrob::URI::getHomePath ( void  )
static
Returns
the home path of the current user.

◆ operator()() [1/2]

const auto& knowrob::URI::operator() ( ) const
inline
Returns
URI string of this data source.

Definition at line 52 of file URI.h.

52 { return uri_; }
std::string uri_
Definition: URI.h:60

◆ operator()() [2/2]

const auto& knowrob::URI::operator() ( ) const
inline
Returns
URI string of this data source.

Definition at line 52 of file URI.h.

52 { return uri_; }

◆ path() [1/2]

const std::string& knowrob::URI::path ( ) const
inline
Returns
string identifier of the data format.

Definition at line 57 of file URI.h.

57 { return path_; }

◆ path() [2/2]

const std::string& knowrob::URI::path ( ) const
inline
Returns
string identifier of the data format.

Definition at line 57 of file URI.h.

57 { return path_; }

◆ resolve() [1/2]

std::string URI::resolve ( const std::string_view &  uriString)
static

Resolves a URI string.

Parameters
uriStringthe URI string to resolve.
Returns
the resolved URI string.

Definition at line 79 of file URI.cpp.

79  {
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 }
static std::optional< std::string > getHomePath(void)
Definition: URI.cpp:53

◆ resolve() [2/2]

static std::string knowrob::URI::resolve ( const std::string_view &  uriString)
static

Resolves a URI string.

Parameters
uriStringthe URI string to resolve.
Returns
the resolved URI string.

◆ updateURI() [1/2]

void URI::updateURI ( )
protected

Definition at line 40 of file URI.cpp.

40  {
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 }

◆ updateURI() [2/2]

void knowrob::URI::updateURI ( )
protected

Member Data Documentation

◆ host_

boost::optional< std::string > knowrob::URI::host_
protected

Definition at line 63 of file URI.h.

◆ path_

std::string knowrob::URI::path_
protected

Definition at line 61 of file URI.h.

◆ port_

boost::optional< int > knowrob::URI::port_
protected

Definition at line 64 of file URI.h.

◆ protocol_

boost::optional< std::string > knowrob::URI::protocol_
protected

Definition at line 62 of file URI.h.

◆ uri_

std::string knowrob::URI::uri_
protected

Definition at line 60 of file URI.h.


The documentation for this class was generated from the following files: