pinocchio  2.1.3
utils.hpp
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 {
28  UNKNOWN = 0,
29  URDF,
30  LUA
31  };
32 
40  inline ModelFileExtensionType checkModelFileExtension (const std::string & filename)
41  {
42  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
43 
44  if (extension == "urdf")
45  return URDF;
46  else if (extension == "lua")
47  return LUA;
48 
49  return UNKNOWN;
50  }
51 
52 
53 
64  inline std::string retrieveResourcePath(const std::string & string,
65  const std::vector<std::string> & package_dirs)
66  {
67 
68  namespace bf = boost::filesystem;
69  std::string result_path;
70 
71  const std::string separator("://");
72  const std::size_t pos_separator = string.find(separator);
73 
74  if (pos_separator != std::string::npos)
75  {
76  std::string scheme = string.substr(0, pos_separator);
77  std::string path = string.substr(pos_separator+3, std::string::npos);
78 
79  if(scheme == "package")
80  {
81  // if exists p1/string, path = p1/string,
82  // else if exists p2/string, path = p2/string
83  // else return an empty string that may provoke an error in loadPolyhedronFromResource()
84 
85  // concatenate package_path with filename
86  for (std::size_t i = 0; i < package_dirs.size(); ++i)
87  {
88  if ( bf::exists( bf::path(package_dirs[i] + "/" + path)))
89  {
90  result_path = std::string( package_dirs[i] + "/" + path );
91  break;
92  }
93  }
94  }
95  else if (scheme == "file")
96  {
97  result_path = path;
98  }
99  else
100  {
101  const std::string exception_message ("Schemes of form" + scheme + "are not handled");
102  throw std::invalid_argument(exception_message);
103  }
104  }
105  else // return the entry string
106  {
107  result_path = string;
108  }
109 
110  return result_path;
111  }
112 
113 } // namespace pinocchio
114 
115 #endif // __pinocchio_parsers_utils_hpp__
ModelFileExtensionType checkModelFileExtension(const std::string &filename)
Extract the type of the given model file according to its extension.
Definition: utils.hpp:40
Main pinocchio namespace.
Definition: treeview.dox:24
std::string retrieveResourcePath(const std::string &string, const std::vector< std::string > &package_dirs)
Retrieve the path of the file whose path is given in an url-format. Currently convert from the folliw...
Definition: utils.hpp:64
ModelFileExtensionType
Supported model file extensions.
Definition: utils.hpp:27