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 void getPlugins(const std::string& package, const std::string& attribute, V_string& plugins)
00111 {
00112 M_string plugins_map;
00113 getPlugins(package, attribute, plugins_map);
00114 M_string::iterator it = plugins_map.begin();
00115 M_string::iterator end = plugins_map.end();
00116 for (; it != end; ++it)
00117 {
00118 plugins.push_back(it->second);
00119 }
00120 }
00121
00122 void getPlugins(const std::string& package, const std::string& attribute, M_string& plugins)
00123 {
00124 V_string lines;
00125 command("plugins --attrib=" + attribute + " " + package, lines);
00126
00127 V_string::iterator it = lines.begin();
00128 V_string::iterator end = lines.end();
00129 for (; it != end; ++it)
00130 {
00131 V_string tokens;
00132 boost::split(tokens, *it, boost::is_any_of(" "));
00133
00134 if (tokens.size() >= 2)
00135 {
00136 std::string package = tokens[0];
00137 std::string rest = boost::join(V_string(tokens.begin() + 1, tokens.end()), " ");
00138 plugins[package] = rest;
00139 }
00140 }
00141 }
00142
00143 }
00144 }