package.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2009, Willow Garage, Inc.
00003  *
00004  * Redistribution and use in source and binary forms, with or without
00005  * modification, are permitted provided that the following conditions are met:
00006  *   * Redistributions of source code must retain the above copyright notice,
00007  *     this list of conditions and the following disclaimer.
00008  *   * Redistributions in binary form must reproduce the above copyright
00009  *     notice, this list of conditions and the following disclaimer in the
00010  *     documentation and/or other materials provided with the distribution.
00011  *   * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
00012  *     contributors may be used to endorse or promote products derived from
00013  *     this software without specific prior written permission.
00014  *
00015  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00016  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00017  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00018  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00019  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00020  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00021  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00022  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00023  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00024  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00025  * POSSIBILITY OF SUCH DAMAGE.
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 // Mutex used to lock calls into librospack, which is not thread-safe.
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   // strip empties
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   // scrape any newlines out of it
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 } // namespace package
00144 } // namespace ros


roslib_electric
Author(s): Ken Conley/kwc@willowgarage.com, Morgan Quigley/mquigley@cs.stanford.edu, Josh Faust/jfaust@willowgarage.com
autogenerated on Thu Jan 2 2014 11:54:25