ros_uri.hpp
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                 // Get the abs path
00081                 fs::path absPath = fs::path(pathStr);
00082 
00083                 // if there is no package name given, start searching.
00084                 if (packageName == std::string()) {
00085                         // get all possible packaged
00086                         std::vector<std::string> packageVector;
00087                         ros::package::getAll(packageVector);
00088 
00089                         // do a stable sort, so we can do the binary search
00090                         std::stable_sort(packageVector.begin(), packageVector.end());
00091 
00092                         // start with the current path and then get step by step up the path
00093                         fs::path parentPath = absPath;
00094 
00095                         // as long as there is a parent do the search process.
00096                         while (parentPath != fs::path()) {
00097                                 // get the name of the file
00098                                 fs::path filename = parentPath.filename();
00099 
00100                                 // do the binary search for the package, if it's found
00101                                 if (std::binary_search(packageVector.begin(), packageVector.end(), filename)) {
00102                                         // get the path to package, and if it equal to the rest of the path nothing to do anymore
00103                                         fs::path possiblePath = fs::path(ros::package::getPath(filename.string()));
00104                                         if (possiblePath == parentPath.string()) {
00105                                                 packageName = filename.string();
00106                                                 // stop looping
00107                                                 break;
00108                                         }
00109                                 }
00110 
00111                                 parentPath = parentPath.parent_path();
00112                         }
00113                 }
00114 
00115                 // create the uri
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 /* ROS_URI_HPP_ */


asr_ros_uri
Author(s): Schleicher Ralf
autogenerated on Sat Jun 8 2019 20:46:59