FazBrowse GitHub Viewer
|
Trending
|
URL:
|
Home
Tools:
[Download Repo ZIP]
[View Raw Code]
[Original HTTPS Page]
knowrob/src/URI.cpp at dev · daniel86/knowrob · GitHub
daniel86
/
knowrob
Public
forked from
knowrob/knowrob
Notifications
You must be signed in to change notification settings
Fork
5
Star
1
Code
Issues
0
Pull requests
2
Actions
Projects
Wiki
Security and quality
0
Insights
Additional navigation options
Code
Issues
Pull requests
Actions
Projects
Wiki
Security and quality
Insights
Expand file tree
Breadcrumbs
knowrob
/
src
/
URI.cpp
Copy path
More file actions
More file actions
Latest commit
History
History
History
101 lines (90 loc) · 2.63 KB
Breadcrumbs
knowrob
/
src
/
URI.cpp
Copy path
File metadata and controls
101 lines (90 loc) · 2.63 KB
Raw
Copy raw file
Download raw file
Open symbols panel
Edit and raw actions
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
/*
* This file is part of KnowRob, please consult
* https://github.com/knowrob/knowrob for license details.
*/
#
ifndef
_WIN32
#
include
<
pwd.h
>
#
endif
#
include
<
filesystem
>
#
include
"
knowrob/URI.h
"
#
include
"
knowrob/Logger.h
"
#
include
<
optional
>
using
namespace
knowrob
;
URI
::
URI
(std::string_view path)
: path_(path) {
updateURI
();
}
URI
::
URI
(std::string path, std::string protocol, std::string host,
int
port)
: protocol_(std::move(protocol)),
path_
(std::move(path)),
host_(std::move(host)),
port_(port) {
updateURI
();
}
URI
::
URI
(
const
boost::property_tree::ptree &property_tree) {
protocol_ = property_tree.
get_optional
<std::string>(
"
protocol
"
);
path_ = property_tree.
get
(
"
path
"
,
"
/
"
);
host_ = property_tree.
get_optional
<std::string>(
"
host
"
);
port_ = property_tree.
get_optional
<
int
>(
"
port
"
);
updateURI
();
}
void
URI::updateURI
() {
std::stringstream ss;
if
(protocol_ && protocol_.
value
() !=
"
file
"
) {
ss << protocol_ <<
"
://
"
;
}
if
(host_) {
ss << host_.
value
();
if
(port_) ss <<
'
:
'
<< port_.
value
();
}
ss << path_;
uri_ = ss.
str
();
}
std::optional<std::string>
getHomePath
(
void
) {
#
ifdef
_WIN32
auto
env_drive =
getenv
(
"
HOMEDRIVE
"
);
auto
env_path =
getenv
(
"
HOMEPATH
"
);
if
(env_drive && env_path) {
return
std::string
(env_drive) +
std::string
(env_path);
}
else
{
KB_WARN
(
"
Could not determine home directory
"
);
return
std::
nullopt
;
}
#
else
auto
env_home =
getenv
(
"
HOME
"
);
if
(env_home) {
return
std::string
(env_home);
}
else
{
struct
passwd
*pw =
getpwuid
(
getuid
());
if
(pw) {
return
std::string
(pw->
pw_dir
);
}
else
{
KB_WARN
(
"
Could not determine home directory
"
);
return
std::
nullopt
;
}
}
#
endif
}
std::string
URI::resolve
(
const
std::string_view &uriString) {
static
std::filesystem::path
projectPath
(
KNOWROB_SOURCE_DIR
);
static
std::filesystem::path
installPath
(
KNOWROB_INSTALL_PREFIX
);
static
std::optional<std::string>
homePath
(
getHomePath
());
std::filesystem::path
filePath
(uriString);
if
(!
exists
(filePath)) {
std::vector<std::filesystem::path> possiblePaths;
//
prefer loading from source directory
possiblePaths.
push_back
(projectPath / filePath);
possiblePaths.
push_back
(projectPath /
"
src
"
/ filePath);
//
next try loading from home directory
if
(homePath) {
possiblePaths.
push_back
(
std::filesystem::path
(homePath.
value
()) /
"
.knowrob
"
/ filePath);
}
//
lastly try loading from install directory
possiblePaths.
push_back
(installPath /
"
share
"
/
"
knowrob
"
/ filePath);
possiblePaths.
push_back
(installPath /
"
lib
"
/
"
knowrob
"
/ filePath);
for
(
const
auto
&p: possiblePaths) {
if
(
exists
(p))
return
p.
u8string
();
}
}
return
std::string
(uriString);
}
Back
|
FazBrowse Home
|
New Git URL