GCC Code Coverage Report
Directory: ./ Exec Total Coverage
File: include/pinocchio/parsers/utils.hpp Lines: 19 24 79.2 %
Date: 2024-04-26 13:14:21 Branches: 25 60 41.7 %

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 <iostream>
9
#include <limits>
10
#include <sstream>
11
// #include <stdexcept>
12
13
#include <boost/filesystem/fstream.hpp>
14
#include <boost/foreach.hpp>
15
#include <boost/format.hpp>
16
17
#include "pinocchio/utils/file-explorer.hpp"
18
#include <boost/filesystem.hpp>
19
20
#include <exception>
21
22
namespace pinocchio
23
{
24
  ///
25
  /// \brief Supported model file extensions
26
  ///
27
  enum ModelFileExtensionType{
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
51
  /**
52
   * @brief      Retrieve the path of the file whose path is given in URL-format.
53
   *             Currently convert from the following patterns : package:// or file://
54
   *
55
   * @param[in]  string          The path given in the url-format
56
   * @param[in]  package_dirs    A list of packages directories where to search for files
57
   *                             if its pattern starts with package://
58
   *
59
   * @return     The path to the file (can be a relative or absolute path)
60
   */
61
728
   inline std::string retrieveResourcePath(const std::string & string,
62
                                           const std::vector<std::string> & package_dirs)
63
   {
64
65
    namespace bf = boost::filesystem;
66
728
    std::string result_path;
67
68
1456
    const std::string separator("://");
69
728
    const std::size_t pos_separator = string.find(separator);
70
1456
    bf::path string_path(string);
71
72
728
    if (pos_separator != std::string::npos)
73
    {
74
1454
      std::string scheme = string.substr(0, pos_separator);
75
1454
      std::string path = string.substr(pos_separator+3, std::string::npos);
76
77
727
      if(scheme == "package")
78
      {
79
        // if exists p1/string, path = p1/string,
80
        // else if exists p2/string, path = p2/string
81
        // else return an empty string that may provoke an error in loadPolyhedronFromResource()
82
83
        // concatenate package_path with filename
84
727
        for (std::size_t i = 0; i < package_dirs.size(); ++i)
85
        {
86


727
          if ( bf::exists( bf::path(package_dirs[i] + "/" + path)))
87
          {
88

727
            result_path = std::string( package_dirs[i] + "/" + path );
89
727
            break;
90
          }
91
        }
92
      }
93
      else if (scheme == "file")
94
      {
95
        result_path = path;
96
      }
97
      else
98
      {
99
        const std::string exception_message ("Schemes of form" + scheme + "are not handled");
100
        throw std::invalid_argument(exception_message);
101
      }
102
    }
103

1
    else if (string_path.is_relative())
104
    {
105
      // handle the case where a relative mesh path is specified without using //package
106
1
      for (std::size_t i = 0; i < package_dirs.size(); ++i)
107
        {
108


1
          if ( bf::exists( bf::path(package_dirs[i] + "/" + string)))
109
          {
110

1
            result_path = std::string( package_dirs[i] + "/" + string);
111
1
            break;
112
          }
113
        }
114
    }
115
    else // return the entry string
116
    {
117
      result_path = string;
118
    }
119
120
1456
    return result_path;
121
   }
122
123
} // namespace pinocchio
124
125
#endif // __pinocchio_parsers_utils_hpp__