utils.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (C) 2008, 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 <string>
00029 #include <vector>
00030 #include <boost/algorithm/string.hpp>
00031 #include <boost/tr1/unordered_set.hpp>
00032 
00033 #include "utils.h"
00034 
00035 namespace rospack
00036 {
00037 
00038 void
00039 deduplicate_tokens(const std::string& instring,
00040                    bool last,
00041                    std::string& outstring)
00042 {
00043   std::vector<std::string> vec;
00044   std::tr1::unordered_set<std::string> set;
00045   boost::split(vec, instring,
00046                boost::is_any_of("\t "),
00047                boost::token_compress_on);
00048   if(last)
00049     std::reverse(vec.begin(), vec.end());
00050   std::vector<std::string> vec_out;
00051   for(std::vector<std::string>::const_iterator it = vec.begin();
00052       it != vec.end();
00053       ++it)
00054   {
00055     if(set.find(*it) == set.end())
00056     {
00057       vec_out.push_back(*it);
00058       set.insert(*it);
00059     }
00060   }
00061   if(last)
00062     std::reverse(vec_out.begin(), vec_out.end());
00063   for(std::vector<std::string>::const_iterator it = vec_out.begin();
00064       it != vec_out.end();
00065       ++it)
00066   {
00067     if(it == vec_out.begin())
00068       outstring.append(*it);
00069     else
00070       outstring.append(std::string(" ") + *it);
00071   }
00072 }
00073 
00074 void
00075 parse_compiler_flags(const std::string& instring,
00076                      const std::string& token,
00077                      bool select,
00078                      bool last,
00079                      std::string& outstring)
00080 {
00081   std::string intermediate;
00082   std::vector<std::string> result_vec;
00083   boost::split(result_vec, instring,
00084                boost::is_any_of("\t "),
00085                boost::token_compress_on);
00086   for(std::vector<std::string>::const_iterator it = result_vec.begin();
00087       it != result_vec.end();
00088       ++it)
00089   {
00090     // Combined into one arg
00091     if(it->size() > token.size() && it->substr(0,token.size()) == token)
00092     {
00093       if(select)
00094       {
00095         if(intermediate.size())
00096           intermediate.append(" ");
00097         intermediate.append(it->substr(token.size()));
00098       }
00099     }
00100     // Space-separated
00101     else if((*it) == token)
00102     {
00103       std::vector<std::string>::const_iterator iit = it;
00104       if(++iit != result_vec.end())
00105       {
00106         if(it->size() >= token.size() && it->substr(0,token.size()) == token)
00107         {
00108           // skip it
00109         }
00110         else
00111         {
00112           if(select)
00113           {
00114             if(intermediate.size())
00115               intermediate.append(" ");
00116             intermediate.append((*iit));
00117           }
00118           it = iit;
00119         }
00120       }
00121     }
00122     // Special case: if we're told to look for -l, then also find *.a
00123     else if(it->size() > 2 &&
00124             (*it)[0] == '/' &&
00125             it->substr(it->size()-2) == ".a")
00126     {
00127       if(select)
00128       {
00129         if(intermediate.size())
00130           intermediate.append(" ");
00131         intermediate.append((*it));
00132       }
00133     }
00134     else if(!select)
00135     {
00136       if(intermediate.size())
00137         intermediate.append(" ");
00138       intermediate.append((*it));
00139     }
00140   }
00141   if(select)
00142     deduplicate_tokens(intermediate, last, outstring);
00143   else
00144     outstring = intermediate;
00145 }
00146 
00147 }
00148 


rospack
Author(s): Brian Gerkey, Morgan Quigley, Dirk Thomas
autogenerated on Tue Mar 7 2017 03:26:33