file-explorer.cpp 2.08 KB
Newer Older
1
//
Justin Carpentier's avatar
Justin Carpentier committed
2
// Copyright (c) 2016-2021 CNRS INRIA
3
4
//

Justin Carpentier's avatar
Justin Carpentier committed
5
#include <cstdlib>
6
#include <boost/filesystem.hpp>
7
8
#include "pinocchio/utils/file-explorer.hpp"

9
namespace fs = boost::filesystem;
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
namespace pinocchio
{

  void extractPathFromEnvVar(const std::string & env_var_name,
                             std::vector<std::string> & list_of_paths,
                             const std::string & delimiter)
  {
    const char * env_var_value = std::getenv(env_var_name.c_str());
    
    if(env_var_value != NULL)
    {
      std::string policyStr(env_var_value);
      // Add a separator at the end so that last path is also retrieved
      policyStr += std::string(":");
      size_t lastOffset = 0;
25
26
27
      std::string path_string;
      path_string.reserve(500);
      fs::path path;
28
29
30
31
      
      while(true)
      {
        size_t offset = policyStr.find_first_of(delimiter, lastOffset);
Wolfgang Merkt's avatar
Wolfgang Merkt committed
32
        if (offset < policyStr.size()) {
33
34
35
          path_string = policyStr.substr(lastOffset, offset - lastOffset);
          path = fs::path(path_string);
          list_of_paths.push_back(path_string);
Wolfgang Merkt's avatar
Wolfgang Merkt committed
36
          // Work-around for https://github.com/stack-of-tasks/pinocchio/issues/1463
37
38
39
40
          // To support ROS devel/isolated spaces, we also need to look one package above the package.xml:
          if (fs::exists(path / "package.xml")) {
            list_of_paths.push_back(fs::path(path / "..").string());
          }
Wolfgang Merkt's avatar
Wolfgang Merkt committed
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
        if (offset == std::string::npos)
          break;
        else
          lastOffset = offset + 1; // add one to skip the delimiter
      }
    }
  }

  std::vector<std::string> extractPathFromEnvVar(const std::string & env_var_name,
                                                 const std::string & delimiter)
  {
    std::vector<std::string> list_of_paths;
    extractPathFromEnvVar(env_var_name,list_of_paths,delimiter);
    return list_of_paths;
  }

  std::vector<std::string> rosPaths()
  {
    std::vector<std::string> list_of_paths;
    extractPathFromEnvVar("ROS_PACKAGE_PATH",list_of_paths);
    extractPathFromEnvVar("AMENT_PREFIX_PATH",list_of_paths);
    
    return list_of_paths;
  }

}