| Directory: | ./ |
|---|---|
| File: | src/utils/file-explorer.cpp |
| Date: | 2025-02-12 21:03:38 |
| 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 | 152 | void extractPathFromEnvVar( | |
| 14 | const std::string & env_var_name, | ||
| 15 | std::vector<std::string> & list_of_paths, | ||
| 16 | const std::string & delimiter) | ||
| 17 | { | ||
| 18 | 152 | const char * env_var_value = std::getenv(env_var_name.c_str()); | |
| 19 | |||
| 20 |
1/2✓ Branch 0 taken 152 times.
✗ Branch 1 not taken.
|
152 | if (env_var_value != NULL) |
| 21 | { | ||
| 22 |
1/2✓ Branch 2 taken 152 times.
✗ Branch 3 not taken.
|
152 | 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 152 times.
✗ Branch 2 not taken.
|
152 | policyStr += delimiter; |
| 25 | 152 | size_t lastOffset = 0; | |
| 26 | while (true) | ||
| 27 | { | ||
| 28 | 380 | size_t offset = policyStr.find_first_of(delimiter, lastOffset); | |
| 29 |
2/2✓ Branch 1 taken 228 times.
✓ Branch 2 taken 152 times.
|
380 | if (offset < policyStr.size()) |
| 30 | { | ||
| 31 |
2/4✓ Branch 1 taken 228 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 228 times.
✗ Branch 5 not taken.
|
228 | 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 228 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 228 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 228 times.
✗ Branch 8 not taken.
|
228 | list_of_paths.push_back(policyStr.substr(lastOffset, offset - lastOffset) + "/.."); |
| 35 | } | ||
| 36 |
2/2✓ Branch 0 taken 152 times.
✓ Branch 1 taken 228 times.
|
380 | if (offset == std::string::npos) |
| 37 | 152 | break; | |
| 38 | else | ||
| 39 | 228 | lastOffset = offset + 1; // add one to skip the delimiter | |
| 40 | 228 | } | |
| 41 | 152 | } | |
| 42 | 152 | } | |
| 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 | 76 | void appendSuffixToPaths(std::vector<std::string> & list_of_paths, const std::string & suffix) | |
| 53 | { | ||
| 54 |
2/2✓ Branch 1 taken 152 times.
✓ Branch 2 taken 76 times.
|
228 | for (size_t i = 0; i < list_of_paths.size(); ++i) |
| 55 | { | ||
| 56 | 152 | list_of_paths[i] += suffix; | |
| 57 | } | ||
| 58 | 76 | } | |
| 59 | |||
| 60 | 76 | std::vector<std::string> rosPaths() | |
| 61 | { | ||
| 62 | 76 | std::vector<std::string> raw_list_of_paths; | |
| 63 | 76 | std::vector<std::string> raw_list_of_prefixes; | |
| 64 |
3/6✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 76 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 76 times.
✗ Branch 10 not taken.
|
76 | extractPathFromEnvVar("ROS_PACKAGE_PATH", raw_list_of_paths); |
| 65 |
3/6✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
✓ Branch 6 taken 76 times.
✗ Branch 7 not taken.
✓ Branch 9 taken 76 times.
✗ Branch 10 not taken.
|
76 | extractPathFromEnvVar("AMENT_PREFIX_PATH", raw_list_of_prefixes); |
| 66 | |||
| 67 |
2/4✓ Branch 2 taken 76 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 76 times.
✗ Branch 6 not taken.
|
76 | appendSuffixToPaths(raw_list_of_prefixes, "/share"); |
| 68 |
1/2✓ Branch 4 taken 76 times.
✗ Branch 5 not taken.
|
152 | raw_list_of_paths.insert( |
| 69 | 76 | 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 | 76 | fs::path path; | |
| 74 | 76 | std::vector<std::string> list_of_paths; | |
| 75 | 76 | for (std::vector<std::string>::iterator it = raw_list_of_paths.begin(); | |
| 76 |
2/2✓ Branch 3 taken 456 times.
✓ Branch 4 taken 76 times.
|
532 | it != raw_list_of_paths.end(); ++it) |
| 77 | { | ||
| 78 |
1/2✓ Branch 2 taken 456 times.
✗ Branch 3 not taken.
|
456 | list_of_paths.push_back(*it); |
| 79 |
1/2✓ Branch 2 taken 456 times.
✗ Branch 3 not taken.
|
456 | path = fs::path(*it); |
| 80 |
4/8✓ Branch 1 taken 456 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 456 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 456 times.
✗ Branch 8 not taken.
✗ Branch 11 not taken.
✓ Branch 12 taken 456 times.
|
456 | if (fs::exists(path / "package.xml")) |
| 81 | { | ||
| 82 | ✗ | list_of_paths.push_back(fs::path(path / "..").string()); | |
| 83 | } | ||
| 84 | } | ||
| 85 | 152 | return list_of_paths; | |
| 86 | 76 | } | |
| 87 | |||
| 88 | } // namespace pinocchio | ||
| 89 |