pinocchio  2.6.20
A fast and flexible implementation of Rigid Body Dynamics algorithms and their analytical derivatives
file-explorer.cpp
1 //
2 // Copyright (c) 2016-2021 CNRS INRIA
3 //
4 
5 #include <cstdlib>
6 #include <boost/filesystem.hpp>
7 #include "pinocchio/utils/file-explorer.hpp"
8 
9 namespace fs = boost::filesystem;
10 namespace pinocchio
11 {
12 
13  void extractPathFromEnvVar(const std::string & env_var_name,
14  std::vector<std::string> & list_of_paths,
15  const std::string & delimiter)
16  {
17  const char * env_var_value = std::getenv(env_var_name.c_str());
18 
19  if(env_var_value != NULL)
20  {
21  std::string policyStr(env_var_value);
22  // Add a separator at the end so that last path is also retrieved
23  policyStr += delimiter;
24  size_t lastOffset = 0;
25  while(true)
26  {
27  size_t offset = policyStr.find_first_of(delimiter, lastOffset);
28  if (offset < policyStr.size())
29  list_of_paths.push_back(policyStr.substr(lastOffset, offset - lastOffset));
30  if (offset == std::string::npos)
31  break;
32  else
33  lastOffset = offset + 1; // add one to skip the delimiter
34  }
35  }
36  }
37 
38  std::vector<std::string> extractPathFromEnvVar(const std::string & env_var_name,
39  const std::string & delimiter)
40  {
41  std::vector<std::string> list_of_paths;
42  extractPathFromEnvVar(env_var_name,list_of_paths,delimiter);
43  return list_of_paths;
44  }
45 
46  void appendSuffixToPaths(std::vector<std::string> & list_of_paths,
47  const std::string & suffix)
48  {
49  for (size_t i = 0; i < list_of_paths.size(); ++i)
50  {
51  list_of_paths[i] += suffix;
52  }
53  }
54 
55  std::vector<std::string> rosPaths()
56  {
57  std::vector<std::string> raw_list_of_paths;
58  std::vector<std::string> raw_list_of_prefixes;
59  extractPathFromEnvVar("ROS_PACKAGE_PATH", raw_list_of_paths);
60  extractPathFromEnvVar("AMENT_PREFIX_PATH", raw_list_of_prefixes);
61 
62  appendSuffixToPaths(raw_list_of_prefixes, "/share");
63  raw_list_of_paths.insert(raw_list_of_paths.end(), raw_list_of_prefixes.begin(),
64  raw_list_of_prefixes.end());
65 
66  // Work-around for https://github.com/stack-of-tasks/pinocchio/issues/1463
67  // To support ROS devel/isolated spaces, we also need to look one package above the package.xml:
68  fs::path path;
69  std::vector<std::string> list_of_paths;
70  for (std::vector<std::string>::iterator it = raw_list_of_paths.begin(); it != raw_list_of_paths.end(); ++it) {
71  list_of_paths.push_back(*it);
72  path = fs::path(*it);
73  if (fs::exists(path / "package.xml")) {
74  list_of_paths.push_back(fs::path(path / "..").string());
75  }
76  }
77  return list_of_paths;
78  }
79 
80 }
pinocchio::appendSuffixToPaths
void appendSuffixToPaths(std::vector< std::string > &list_of_paths, const std::string &suffix)
For a given vector of paths, add a suffix inplace to each path and return the vector inplace.
Definition: file-explorer.cpp:46
pinocchio::extractPathFromEnvVar
void extractPathFromEnvVar(const std::string &env_var_name, std::vector< std::string > &list_of_paths, const std::string &delimiter)
Parse an environment variable if exists and extract paths according to the delimiter.
Definition: file-explorer.cpp:13
pinocchio::rosPaths
std::vector< std::string > rosPaths()
Parse the environment variables ROS_PACKAGE_PATH / AMENT_PREFIX_PATH and extract paths.
Definition: file-explorer.cpp:55
pinocchio
Main pinocchio namespace.
Definition: treeview.dox:11