Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
00026
00027
00028 #include "ros/package.h"
00029 #include "rospack/rospack.h"
00030
00031 #include <cstdio>
00032 #include <iostream>
00033
00034 #include <boost/algorithm/string/split.hpp>
00035 #include <boost/algorithm/string/join.hpp>
00036 #include <boost/algorithm/string/classification.hpp>
00037 #include <boost/thread/mutex.hpp>
00038
00039 namespace ros
00040 {
00041 namespace package
00042 {
00043
00044
00045 static boost::mutex librospack_mutex;
00046
00047 std::string command(const std::string& _cmd)
00048 {
00049 boost::mutex::scoped_lock lock(librospack_mutex);
00050
00051 rospack::ROSPack rp;
00052 int ret;
00053 try
00054 {
00055 ret = rp.run(_cmd);
00056 if(ret == 0)
00057 return rp.getOutput();
00058 else {
00059 if ( !rp.is_quiet() )
00060 std::cerr << "ROSPack::run returned non-zero." << std::endl;
00061 }
00062 }
00063 catch(std::runtime_error &e)
00064 {
00065 if ( !rp.is_quiet() )
00066 std::cerr << "[rospack] " << e.what() << std::endl;
00067 }
00068 return std::string("");
00069 }
00070
00071 void command(const std::string& cmd, V_string& output)
00072 {
00073 std::string out_string = command(cmd);
00074 V_string full_list;
00075 boost::split(full_list, out_string, boost::is_any_of("\r\n"));
00076
00077
00078 V_string::iterator it = full_list.begin();
00079 V_string::iterator end = full_list.end();
00080 for (; it != end; ++it)
00081 {
00082 if (!it->empty())
00083 {
00084 output.push_back(*it);
00085 }
00086 }
00087 }
00088
00089 std::string getPath(const std::string& package_name)
00090 {
00091 std::string path = command("find " + package_name);
00092
00093
00094 for (size_t newline = path.find('\n'); newline != std::string::npos;
00095 newline = path.find('\n'))
00096 {
00097 path.erase(newline, 1);
00098 }
00099
00100 return path;
00101 }
00102
00103 bool getAll(V_string& packages)
00104 {
00105 command("list-names", packages);
00106
00107 return true;
00108 }
00109
00110 static void getPlugins(const std::string& package, const std::string& attribute, V_string& packages, V_string& plugins)
00111 {
00112 V_string lines;
00113 command("plugins --attrib=" + attribute + " " + package, lines);
00114
00115 V_string::iterator it = lines.begin();
00116 V_string::iterator end = lines.end();
00117 for (; it != end; ++it)
00118 {
00119 V_string tokens;
00120 boost::split(tokens, *it, boost::is_any_of(" "));
00121
00122 if (tokens.size() >= 2)
00123 {
00124 std::string package = tokens[0];
00125 std::string rest = boost::join(V_string(tokens.begin() + 1, tokens.end()), " ");
00126 packages.push_back(package);
00127 plugins.push_back(rest);
00128 }
00129 }
00130 }
00131
00132 void getPlugins(const std::string& package, const std::string& attribute, V_string& plugins)
00133 {
00134 V_string packages;
00135 getPlugins(package, attribute, packages, plugins);
00136 }
00137
00138 void getPlugins(const std::string& package, const std::string& attribute, M_string& plugins)
00139 {
00140 V_string packages, plugins_v;
00141 getPlugins(package, attribute, packages, plugins_v);
00142 for (std::size_t i = 0 ; i < packages.size() ; ++i)
00143 plugins[packages[i]] = plugins_v[i];
00144 }
00145
00146 }
00147 }