GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: src/utils/file-explorer.cpp Lines: 32 38 84.2 %
Date: 2024-01-23 21:41:47 Branches: 28 56 50.0 %

Line Branch Exec Source
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
82
  void extractPathFromEnvVar(const std::string & env_var_name,
14
                             std::vector<std::string> & list_of_paths,
15
                             const std::string & delimiter)
16
  {
17
82
    const char * env_var_value = std::getenv(env_var_name.c_str());
18
19
82
    if(env_var_value != NULL)
20
    {
21
82
      std::string policyStr(env_var_value);
22
      // Add a separator at the end so that last path is also retrieved
23
41
      policyStr += delimiter;
24
41
      size_t lastOffset = 0;
25
      while(true)
26
      {
27
123
        size_t offset = policyStr.find_first_of(delimiter, lastOffset);
28
123
        if (offset < policyStr.size())
29

82
          list_of_paths.push_back(policyStr.substr(lastOffset, offset - lastOffset));
30
123
        if (offset == std::string::npos)
31
41
          break;
32
        else
33
82
          lastOffset = offset + 1; // add one to skip the delimiter
34
82
      }
35
    }
36
82
  }
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
41
  void appendSuffixToPaths(std::vector<std::string> & list_of_paths,
47
                           const std::string & suffix)
48
  {
49
41
    for (size_t i = 0; i < list_of_paths.size(); ++i)
50
    {
51
      list_of_paths[i] += suffix;
52
    }
53
41
  }
54
55
41
  std::vector<std::string> rosPaths()
56
  {
57
82
    std::vector<std::string> raw_list_of_paths;
58
82
    std::vector<std::string> raw_list_of_prefixes;
59

41
    extractPathFromEnvVar("ROS_PACKAGE_PATH", raw_list_of_paths);
60

41
    extractPathFromEnvVar("AMENT_PREFIX_PATH", raw_list_of_prefixes);
61
62

41
    appendSuffixToPaths(raw_list_of_prefixes, "/share");
63
41
    raw_list_of_paths.insert(raw_list_of_paths.end(), raw_list_of_prefixes.begin(),
64
82
                             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
82
    fs::path path;
69
41
    std::vector<std::string> list_of_paths;
70
123
    for (std::vector<std::string>::iterator it = raw_list_of_paths.begin(); it != raw_list_of_paths.end(); ++it) {
71
82
      list_of_paths.push_back(*it);
72
82
      path = fs::path(*it);
73


82
      if (fs::exists(path / "package.xml")) {
74
        list_of_paths.push_back(fs::path(path / "..").string());
75
      }
76
    }
77
82
    return list_of_paths;
78
  }
79
80
}