ros_uri.hpp
Go to the documentation of this file.
1 
18 #ifndef ROS_URI_HPP_
19 #define ROS_URI_HPP_
20 
21 #include <vector>
22 #include <string>
23 #include <ros/ros.h>
24 #include <ros/package.h>
26 
27 namespace fs = boost::filesystem;
28 
39 class ros_uri {
40 private:
44  inline static const std::string getPackagePrefix() {
45  return "package://";
46  };
47 
51  inline static const std::string getFilePrefix() {
52  return "file://";
53  };
54 public:
60  static std::string absolute_path(std::string uriString) {
61  if (uriString.find(getPackagePrefix()) == 0) {
62  std::size_t pos = uriString.find("/", getPackagePrefix().size());
63  std::string packageName = uriString.substr(getPackagePrefix().size(), pos - getPackagePrefix().size());
64  std::string packagePathStr = ros::package::getPath(packageName);
65  return packagePathStr + uriString.substr(pos, uriString.size() - pos);
66  } else if (uriString.find(getFilePrefix()) == 0) {
67  std::string pathStr = uriString.substr(getFilePrefix().size(), uriString.size() - getFilePrefix().size());
68  return pathStr;
69  }
70  return "";
71  };
72 
79  static std::string package_uri(std::string pathStr, std::string packageName = std::string()) {
80  // Get the abs path
81  fs::path absPath = fs::path(pathStr);
82 
83  // if there is no package name given, start searching.
84  if (packageName == std::string()) {
85  // get all possible packaged
86  std::vector<std::string> packageVector;
87  ros::package::getAll(packageVector);
88 
89  // do a stable sort, so we can do the binary search
90  std::stable_sort(packageVector.begin(), packageVector.end());
91 
92  // start with the current path and then get step by step up the path
93  fs::path parentPath = absPath;
94 
95  // as long as there is a parent do the search process.
96  while (parentPath != fs::path()) {
97  // get the name of the file
98  fs::path filename = parentPath.filename();
99 
100  // do the binary search for the package, if it's found
101  if (std::binary_search(packageVector.begin(), packageVector.end(), filename)) {
102  // get the path to package, and if it equal to the rest of the path nothing to do anymore
103  fs::path possiblePath = fs::path(ros::package::getPath(filename.string()));
104  if (possiblePath == parentPath.string()) {
105  packageName = filename.string();
106  // stop looping
107  break;
108  }
109  }
110 
111  parentPath = parentPath.parent_path();
112  }
113  }
114 
115  // create the uri
116  fs::path baseFolder = ros::package::getPath(packageName);
117  fs::path relativePath = fs::pathext::relative_path(absPath, baseFolder);
118  return getPackagePrefix() + packageName + "/" + relativePath.string();
119  };
120 
124  static std::string file_uri(fs::path path) {
125  return getFilePrefix() + fs::absolute(path).string();
126  };
127 };
128 
129 
130 #endif /* ROS_URI_HPP_ */
ROSLIB_DECL bool getAll(V_string &packages)
static std::string file_uri(fs::path path)
Definition: ros_uri.hpp:124
static std::string package_uri(std::string pathStr, std::string packageName=std::string())
Definition: ros_uri.hpp:79
static path relative_path(path &subject, path base=current_path())
static const std::string getFilePrefix()
Definition: ros_uri.hpp:51
ROSLIB_DECL std::string getPath(const std::string &package_name)
static std::string absolute_path(std::string uriString)
Definition: ros_uri.hpp:60
static const std::string getPackagePrefix()
Definition: ros_uri.hpp:44


asr_ros_uri
Author(s): Schleicher Ralf
autogenerated on Fri Dec 6 2019 03:24:27