pinocchio  2.6.3
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  std::vector<std::string> rosPaths()
47  {
48  std::vector<std::string> raw_list_of_paths;
49  extractPathFromEnvVar("ROS_PACKAGE_PATH", raw_list_of_paths);
50  extractPathFromEnvVar("AMENT_PREFIX_PATH", raw_list_of_paths);
51 
52  // Work-around for https://github.com/stack-of-tasks/pinocchio/issues/1463
53  // To support ROS devel/isolated spaces, we also need to look one package above the package.xml:
54  fs::path path;
55  std::vector<std::string> list_of_paths;
56  for (std::vector<std::string>::iterator it = raw_list_of_paths.begin(); it != raw_list_of_paths.end(); ++it) {
57  list_of_paths.push_back(*it);
58  path = fs::path(*it);
59  if (fs::exists(path / "package.xml")) {
60  list_of_paths.push_back(fs::path(path / "..").string());
61  }
62  }
63  return list_of_paths;
64  }
65 
66 }
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.
std::vector< std::string > rosPaths()
Parse the environment variables ROS_PACKAGE_PATH / AMENT_PREFIX_PATH and extract paths.
Main pinocchio namespace.
Definition: treeview.dox:24