GCC Code Coverage Report


Directory: ./
File: src/utils/file-explorer.cpp
Date: 2024-08-27 18:20:05
Exec Total Coverage
Lines: 37 42 88.1%
Branches: 31 62 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 98 void extractPathFromEnvVar(
14 const std::string & env_var_name,
15 std::vector<std::string> & list_of_paths,
16 const std::string & delimiter)
17 {
18 98 const char * env_var_value = std::getenv(env_var_name.c_str());
19
20
1/2
✓ Branch 0 taken 98 times.
✗ Branch 1 not taken.
98 if (env_var_value != NULL)
21 {
22
1/2
✓ Branch 2 taken 98 times.
✗ Branch 3 not taken.
98 std::string policyStr(env_var_value);
23 // Add a separator at the end so that last path is also retrieved
24
1/2
✓ Branch 1 taken 98 times.
✗ Branch 2 not taken.
98 policyStr += delimiter;
25 98 size_t lastOffset = 0;
26 while (true)
27 {
28 245 size_t offset = policyStr.find_first_of(delimiter, lastOffset);
29
2/2
✓ Branch 1 taken 147 times.
✓ Branch 2 taken 98 times.
245 if (offset < policyStr.size())
30 {
31
2/4
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 147 times.
✗ Branch 5 not taken.
147 list_of_paths.push_back(policyStr.substr(lastOffset, offset - lastOffset));
32 // Support for devel spaces: We also need to look one package above:
33 // Work-around for https://github.com/stack-of-tasks/pinocchio/issues/1463
34
3/6
✓ Branch 1 taken 147 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 147 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 147 times.
✗ Branch 8 not taken.
147 list_of_paths.push_back(policyStr.substr(lastOffset, offset - lastOffset) + "/..");
35 }
36
2/2
✓ Branch 0 taken 98 times.
✓ Branch 1 taken 147 times.
245 if (offset == std::string::npos)
37 98 break;
38 else
39 147 lastOffset = offset + 1; // add one to skip the delimiter
40 147 }
41 98 }
42 98 }
43
44 std::vector<std::string>
45 extractPathFromEnvVar(const std::string & env_var_name, const std::string & delimiter)
46 {
47 std::vector<std::string> list_of_paths;
48 extractPathFromEnvVar(env_var_name, list_of_paths, delimiter);
49 return list_of_paths;
50 }
51
52 49 void appendSuffixToPaths(std::vector<std::string> & list_of_paths, const std::string & suffix)
53 {
54
2/2
✓ Branch 1 taken 98 times.
✓ Branch 2 taken 49 times.
147 for (size_t i = 0; i < list_of_paths.size(); ++i)
55 {
56 98 list_of_paths[i] += suffix;
57 }
58 49 }
59
60 49 std::vector<std::string> rosPaths()
61 {
62 49 std::vector<std::string> raw_list_of_paths;
63 49 std::vector<std::string> raw_list_of_prefixes;
64
3/6
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 49 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 49 times.
✗ Branch 10 not taken.
49 extractPathFromEnvVar("ROS_PACKAGE_PATH", raw_list_of_paths);
65
3/6
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 49 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 49 times.
✗ Branch 10 not taken.
49 extractPathFromEnvVar("AMENT_PREFIX_PATH", raw_list_of_prefixes);
66
67
2/4
✓ Branch 2 taken 49 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 49 times.
✗ Branch 6 not taken.
49 appendSuffixToPaths(raw_list_of_prefixes, "/share");
68
1/2
✓ Branch 4 taken 49 times.
✗ Branch 5 not taken.
98 raw_list_of_paths.insert(
69 49 raw_list_of_paths.end(), raw_list_of_prefixes.begin(), raw_list_of_prefixes.end());
70
71 // Work-around for https://github.com/stack-of-tasks/pinocchio/issues/1463
72 // To support ROS devel/isolated spaces, we also need to look one package above the package.xml:
73 49 fs::path path;
74 49 std::vector<std::string> list_of_paths;
75 49 for (std::vector<std::string>::iterator it = raw_list_of_paths.begin();
76
2/2
✓ Branch 3 taken 294 times.
✓ Branch 4 taken 49 times.
343 it != raw_list_of_paths.end(); ++it)
77 {
78
1/2
✓ Branch 2 taken 294 times.
✗ Branch 3 not taken.
294 list_of_paths.push_back(*it);
79
1/2
✓ Branch 2 taken 294 times.
✗ Branch 3 not taken.
294 path = fs::path(*it);
80
4/8
✓ Branch 1 taken 294 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 294 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 294 times.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 294 times.
294 if (fs::exists(path / "package.xml"))
81 {
82 list_of_paths.push_back(fs::path(path / "..").string());
83 }
84 }
85 98 return list_of_paths;
86 49 }
87
88 } // namespace pinocchio
89