| Directory: | ./ |
|---|---|
| File: | include/pinocchio/parsers/utils.hpp |
| Date: | 2025-02-12 21:03:38 |
| Exec | Total | Coverage | |
|---|---|---|---|
| Lines: | 21 | 26 | 80.8% |
| Branches: | 26 | 64 | 40.6% |
| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // | ||
| 2 | // Copyright (c) 2015 - 2016 CNRS | ||
| 3 | // | ||
| 4 | |||
| 5 | #ifndef __pinocchio_parsers_utils_hpp__ | ||
| 6 | #define __pinocchio_parsers_utils_hpp__ | ||
| 7 | |||
| 8 | #include <limits> | ||
| 9 | #include <sstream> | ||
| 10 | // #include <stdexcept> | ||
| 11 | |||
| 12 | #include <boost/filesystem/fstream.hpp> | ||
| 13 | #include <boost/foreach.hpp> | ||
| 14 | #include <boost/format.hpp> | ||
| 15 | |||
| 16 | #include "pinocchio/utils/file-explorer.hpp" | ||
| 17 | #include <boost/filesystem.hpp> | ||
| 18 | |||
| 19 | #include <exception> | ||
| 20 | |||
| 21 | namespace pinocchio | ||
| 22 | { | ||
| 23 | /// | ||
| 24 | /// \brief Supported model file extensions | ||
| 25 | /// | ||
| 26 | enum ModelFileExtensionType | ||
| 27 | { | ||
| 28 | UNKNOWN = 0, | ||
| 29 | URDF | ||
| 30 | }; | ||
| 31 | |||
| 32 | /// | ||
| 33 | /// \brief Extract the type of the given model file according to its extension | ||
| 34 | /// | ||
| 35 | /// \param[in] filename The complete path to the model file. | ||
| 36 | /// | ||
| 37 | /// \return The type of the extension of the model file | ||
| 38 | /// | ||
| 39 | inline ModelFileExtensionType checkModelFileExtension(const std::string & filename) | ||
| 40 | { | ||
| 41 | const std::string extension = filename.substr(filename.find_last_of(".") + 1); | ||
| 42 | |||
| 43 | if (extension == "urdf") | ||
| 44 | return URDF; | ||
| 45 | |||
| 46 | return UNKNOWN; | ||
| 47 | } | ||
| 48 | |||
| 49 | /** | ||
| 50 | * @brief Retrieve the path of the file whose path is given in URL-format. | ||
| 51 | * Currently convert from the following patterns : package:// or file:// | ||
| 52 | * | ||
| 53 | * @param[in] string The path given in the url-format | ||
| 54 | * @param[in] package_dirs A list of packages directories where to search for files | ||
| 55 | * if its pattern starts with package:// | ||
| 56 | * | ||
| 57 | * @return The path to the file (can be a relative or absolute path) | ||
| 58 | */ | ||
| 59 | inline std::string | ||
| 60 | 1428 | retrieveResourcePath(const std::string & string, const std::vector<std::string> & package_dirs) | |
| 61 | { | ||
| 62 | |||
| 63 | namespace bf = boost::filesystem; | ||
| 64 | 1428 | std::string result_path; | |
| 65 | |||
| 66 |
1/2✓ Branch 2 taken 1428 times.
✗ Branch 3 not taken.
|
1428 | const std::string separator("://"); |
| 67 | 1428 | const std::size_t pos_separator = string.find(separator); | |
| 68 |
1/2✓ Branch 1 taken 1428 times.
✗ Branch 2 not taken.
|
1428 | bf::path string_path(string); |
| 69 | |||
| 70 |
2/2✓ Branch 0 taken 1427 times.
✓ Branch 1 taken 1 times.
|
1428 | if (pos_separator != std::string::npos) |
| 71 | { | ||
| 72 |
1/2✓ Branch 1 taken 1427 times.
✗ Branch 2 not taken.
|
1427 | std::string scheme = string.substr(0, pos_separator); |
| 73 |
1/2✓ Branch 1 taken 1427 times.
✗ Branch 2 not taken.
|
1427 | std::string path = string.substr(pos_separator + 3, std::string::npos); |
| 74 | |||
| 75 |
2/6✗ Branch 1 not taken.
✓ Branch 2 taken 1427 times.
✗ Branch 4 not taken.
✗ Branch 5 not taken.
✓ Branch 6 taken 1427 times.
✗ Branch 7 not taken.
|
1427 | if (scheme == "package" || scheme == "model") |
| 76 | { | ||
| 77 | // if exists p1/string, path = p1/string, | ||
| 78 | // else if exists p2/string, path = p2/string | ||
| 79 | // else return an empty string that may provoke an error in loadPolyhedronFromResource() | ||
| 80 | |||
| 81 | // concatenate package_path with filename | ||
| 82 |
1/2✓ Branch 1 taken 1427 times.
✗ Branch 2 not taken.
|
1427 | for (std::size_t i = 0; i < package_dirs.size(); ++i) |
| 83 | { | ||
| 84 |
5/10✓ Branch 2 taken 1427 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1427 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1427 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1427 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 1427 times.
✗ Branch 17 not taken.
|
1427 | if (bf::exists(bf::path(package_dirs[i] + "/" + path))) |
| 85 | { | ||
| 86 |
2/4✓ Branch 2 taken 1427 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1427 times.
✗ Branch 6 not taken.
|
1427 | result_path = std::string(package_dirs[i] + "/" + path); |
| 87 | 1427 | break; | |
| 88 | } | ||
| 89 | } | ||
| 90 | } | ||
| 91 | ✗ | else if (scheme == "file") | |
| 92 | { | ||
| 93 | ✗ | result_path = path; | |
| 94 | } | ||
| 95 | else | ||
| 96 | { | ||
| 97 | ✗ | const std::string exception_message("Schemes of form '" + scheme + "' are not handled"); | |
| 98 | ✗ | throw std::invalid_argument(exception_message); | |
| 99 | } | ||
| 100 | 1427 | } | |
| 101 |
2/4✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
|
1 | else if (string_path.is_relative()) |
| 102 | { | ||
| 103 | // handle the case where a relative mesh path is specified without using //package | ||
| 104 |
1/2✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
|
1 | for (std::size_t i = 0; i < package_dirs.size(); ++i) |
| 105 | { | ||
| 106 |
5/10✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
✓ Branch 8 taken 1 times.
✗ Branch 9 not taken.
✓ Branch 11 taken 1 times.
✗ Branch 12 not taken.
✓ Branch 16 taken 1 times.
✗ Branch 17 not taken.
|
1 | if (bf::exists(bf::path(package_dirs[i] + "/" + string))) |
| 107 | { | ||
| 108 |
2/4✓ Branch 2 taken 1 times.
✗ Branch 3 not taken.
✓ Branch 5 taken 1 times.
✗ Branch 6 not taken.
|
1 | result_path = std::string(package_dirs[i] + "/" + string); |
| 109 | 1 | break; | |
| 110 | } | ||
| 111 | } | ||
| 112 | } | ||
| 113 | else // return the entry string | ||
| 114 | { | ||
| 115 | ✗ | result_path = string; | |
| 116 | } | ||
| 117 | |||
| 118 | 2856 | return result_path; | |
| 119 | 1428 | } | |
| 120 | |||
| 121 | } // namespace pinocchio | ||
| 122 | |||
| 123 | #endif // __pinocchio_parsers_utils_hpp__ | ||
| 124 |