pinocchio  UNKNOWN
utils.hpp
1 //
2 // Copyright (c) 2015 - 2016 CNRS
3 //
4 // This file is part of Pinocchio
5 // Pinocchio is free software: you can redistribute it
6 // and/or modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation, either version
8 // 3 of the License, or (at your option) any later version.
9 //
10 // Pinocchio is distributed in the hope that it will be
11 // useful, but WITHOUT ANY WARRANTY; without even the implied warranty
12 // of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 // General Lesser Public License for more details. You should have
14 // received a copy of the GNU Lesser General Public License along with
15 // Pinocchio If not, see
16 // <http://www.gnu.org/licenses/>.
17 
18 #ifndef __se3_parsers_utils_hpp__
19 #define __se3_parsers_utils_hpp__
20 
21 #include <iostream>
22 #include <limits>
23 #include <sstream>
24 // #include <stdexcept>
25 
26 #include <boost/filesystem/fstream.hpp>
27 #include <boost/foreach.hpp>
28 #include <boost/format.hpp>
29 
30 #include "pinocchio/utils/file-explorer.hpp"
31 #include <boost/filesystem.hpp>
32 
33 #include <exception>
34 
35 namespace se3
36 {
41  UNKNOWN = 0,
42  URDF,
43  LUA
44  };
45 
53  inline ModelFileExtensionType checkModelFileExtension (const std::string & filename)
54  {
55  const std::string extension = filename.substr(filename.find_last_of(".") + 1);
56 
57  if (extension == "urdf")
58  return URDF;
59  else if (extension == "lua")
60  return LUA;
61 
62  return UNKNOWN;
63  }
64 
65 
66 
77  inline std::string retrieveResourcePath(const std::string & string,
78  const std::vector<std::string> & package_dirs) throw (std::invalid_argument)
79  {
80 
81  namespace bf = boost::filesystem;
82  std::string result_path;
83 
84  const std::string separator("://");
85  const std::size_t pos_separator = string.find(separator);
86 
87  if (pos_separator != std::string::npos)
88  {
89  std::string scheme = string.substr(0, pos_separator);
90  std::string path = string.substr(pos_separator+3, std::string::npos);
91 
92  if(scheme == "package")
93  {
94  // if exists p1/string, path = p1/string,
95  // else if exists p2/string, path = p2/string
96  // else return an empty string that may provoke an error in loadPolyhedronFromResource()
97 
98  // concatenate package_path with filename
99  for (std::size_t i = 0; i < package_dirs.size(); ++i)
100  {
101  if ( bf::exists( bf::path(package_dirs[i] + "/" + path)))
102  {
103  result_path = std::string( package_dirs[i] + "/" + path );
104  break;
105  }
106  }
107  }
108  else if (scheme == "file")
109  {
110  result_path = path;
111  }
112  else
113  {
114  const std::string exception_message ("Schemes of form" + scheme + "are not handled");
115  throw std::invalid_argument(exception_message);
116  }
117  }
118  else // return the entry string
119  {
120  result_path = string;
121  }
122 
123  return result_path;
124  }
125 
126 } // namespace se3
127 
128 #endif // __se3_parsers_utils_hpp__
ModelFileExtensionType checkModelFileExtension(const std::string &filename)
Extract the type of the given model file according to its extension.
Definition: utils.hpp:53
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:77
ModelFileExtensionType
Supported model file extensions.
Definition: utils.hpp:40