Go to the documentation of this file.00001
00018 #ifndef ROS_URI_HPP_
00019 #define ROS_URI_HPP_
00020
00021 #include <vector>
00022 #include <string>
00023 #include <ros/ros.h>
00024 #include <ros/package.h>
00025 #include "ros_uri/filesystem_ext.hpp"
00026
00027 namespace fs = boost::filesystem;
00028
00039 class ros_uri {
00040 private:
00044 inline static const std::string getPackagePrefix() {
00045 return "package://";
00046 };
00047
00051 inline static const std::string getFilePrefix() {
00052 return "file://";
00053 };
00054 public:
00060 static std::string absolute_path(std::string uriString) {
00061 if (uriString.find(getPackagePrefix()) == 0) {
00062 std::size_t pos = uriString.find("/", getPackagePrefix().size());
00063 std::string packageName = uriString.substr(getPackagePrefix().size(), pos - getPackagePrefix().size());
00064 std::string packagePathStr = ros::package::getPath(packageName);
00065 return packagePathStr + uriString.substr(pos, uriString.size() - pos);
00066 } else if (uriString.find(getFilePrefix()) == 0) {
00067 std::string pathStr = uriString.substr(getFilePrefix().size(), uriString.size() - getFilePrefix().size());
00068 return pathStr;
00069 }
00070 return "";
00071 };
00072
00079 static std::string package_uri(std::string pathStr, std::string packageName = std::string()) {
00080
00081 fs::path absPath = fs::path(pathStr);
00082
00083
00084 if (packageName == std::string()) {
00085
00086 std::vector<std::string> packageVector;
00087 ros::package::getAll(packageVector);
00088
00089
00090 std::stable_sort(packageVector.begin(), packageVector.end());
00091
00092
00093 fs::path parentPath = absPath;
00094
00095
00096 while (parentPath != fs::path()) {
00097
00098 fs::path filename = parentPath.filename();
00099
00100
00101 if (std::binary_search(packageVector.begin(), packageVector.end(), filename)) {
00102
00103 fs::path possiblePath = fs::path(ros::package::getPath(filename.string()));
00104 if (possiblePath == parentPath.string()) {
00105 packageName = filename.string();
00106
00107 break;
00108 }
00109 }
00110
00111 parentPath = parentPath.parent_path();
00112 }
00113 }
00114
00115
00116 fs::path baseFolder = ros::package::getPath(packageName);
00117 fs::path relativePath = fs::pathext::relative_path(absPath, baseFolder);
00118 return getPackagePrefix() + packageName + "/" + relativePath.string();
00119 };
00120
00124 static std::string file_uri(fs::path path) {
00125 return getFilePrefix() + fs::absolute(path).string();
00126 };
00127 };
00128
00129
00130 #endif