utils.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2008, Willow Garage, Inc.
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice,
7  * this list of conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright
9  * notice, this list of conditions and the following disclaimer in the
10  * documentation and/or other materials provided with the distribution.
11  * * Neither the names of Stanford University or Willow Garage, Inc. nor the names of its
12  * contributors may be used to endorse or promote products derived from
13  * this software without specific prior written permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
16  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
17  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
18  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
19  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
20  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
21  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
22  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
23  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
24  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
25  * POSSIBILITY OF SUCH DAMAGE.
26  */
27 
28 #include <string>
29 #include <vector>
30 #include <boost/algorithm/string.hpp>
31 #include <boost/unordered_set.hpp>
32 
33 #include "utils.h"
34 
35 namespace rospack
36 {
37 
38 void
39 deduplicate_tokens(const std::string& instring,
40  bool last,
41  std::string& outstring)
42 {
43  std::vector<std::string> vec;
44  boost::unordered_set<std::string> set;
45  boost::split(vec, instring,
46  boost::is_any_of("\t "),
47  boost::token_compress_on);
48  if(last)
49  std::reverse(vec.begin(), vec.end());
50  std::vector<std::string> vec_out;
51  for(std::vector<std::string>::const_iterator it = vec.begin();
52  it != vec.end();
53  ++it)
54  {
55  if(set.find(*it) == set.end())
56  {
57  vec_out.push_back(*it);
58  set.insert(*it);
59  }
60  }
61  if(last)
62  std::reverse(vec_out.begin(), vec_out.end());
63  for(std::vector<std::string>::const_iterator it = vec_out.begin();
64  it != vec_out.end();
65  ++it)
66  {
67  if(it == vec_out.begin())
68  outstring.append(*it);
69  else
70  outstring.append(std::string(" ") + *it);
71  }
72 }
73 
74 void
75 parse_compiler_flags(const std::string& instring,
76  const std::string& token,
77  bool select,
78  bool last,
79  std::string& outstring)
80 {
81  std::string intermediate;
82  std::vector<std::string> result_vec;
83  boost::split(result_vec, instring,
84  boost::is_any_of("\t "),
85  boost::token_compress_on);
86  for(std::vector<std::string>::const_iterator it = result_vec.begin();
87  it != result_vec.end();
88  ++it)
89  {
90  // Combined into one arg
91  if(it->size() > token.size() && it->substr(0,token.size()) == token)
92  {
93  if(select)
94  {
95  if(intermediate.size())
96  intermediate.append(" ");
97  intermediate.append(it->substr(token.size()));
98  }
99  }
100  // Space-separated
101  else if((*it) == token)
102  {
103  std::vector<std::string>::const_iterator iit = it;
104  if(++iit != result_vec.end())
105  {
106  if(it->size() >= token.size() && it->substr(0,token.size()) == token)
107  {
108  // skip it
109  }
110  else
111  {
112  if(select)
113  {
114  if(intermediate.size())
115  intermediate.append(" ");
116  intermediate.append((*iit));
117  }
118  it = iit;
119  }
120  }
121  }
122  // Special case: if we're told to look for -l, then also find *.a
123  else if(it->size() > 2 &&
124  (*it)[0] == '/' &&
125  it->substr(it->size()-2) == ".a")
126  {
127  if(select)
128  {
129  if(intermediate.size())
130  intermediate.append(" ");
131  intermediate.append((*it));
132  }
133  }
134  else if(!select)
135  {
136  if(intermediate.size())
137  intermediate.append(" ");
138  intermediate.append((*it));
139  }
140  }
141  if(select)
142  deduplicate_tokens(intermediate, last, outstring);
143  else
144  outstring = intermediate;
145 }
146 
147 }
148 
rospack::deduplicate_tokens
void deduplicate_tokens(const std::string &instring, bool last, std::string &outstring)
Definition: utils.cpp:39
rospack::parse_compiler_flags
void parse_compiler_flags(const std::string &instring, const std::string &token, bool select, bool last, std::string &outstring)
Definition: utils.cpp:75
utils.h
rospack
Definition: rospack.h:123


rospack
Author(s): Brian Gerkey, Morgan Quigley, Dirk Thomas
autogenerated on Wed Mar 2 2022 00:54:50