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 #if BOOST_VERSION < 106500
32 #include <boost/tr1/unordered_set.hpp>
33 #else
34 #include <boost/unordered_set.hpp>
35 #endif
36 
37 #include "utils.h"
38 
39 namespace rospack
40 {
41 
42 void
43 deduplicate_tokens(const std::string& instring,
44  bool last,
45  std::string& outstring)
46 {
47  std::vector<std::string> vec;
48 #if BOOST_VERSION < 106500
49  std::tr1::unordered_set<std::string> set;
50 #else
51  boost::unordered_set<std::string> set;
52 #endif
53  boost::split(vec, instring,
54  boost::is_any_of("\t "),
55  boost::token_compress_on);
56  if(last)
57  std::reverse(vec.begin(), vec.end());
58  std::vector<std::string> vec_out;
59  for(std::vector<std::string>::const_iterator it = vec.begin();
60  it != vec.end();
61  ++it)
62  {
63  if(set.find(*it) == set.end())
64  {
65  vec_out.push_back(*it);
66  set.insert(*it);
67  }
68  }
69  if(last)
70  std::reverse(vec_out.begin(), vec_out.end());
71  for(std::vector<std::string>::const_iterator it = vec_out.begin();
72  it != vec_out.end();
73  ++it)
74  {
75  if(it == vec_out.begin())
76  outstring.append(*it);
77  else
78  outstring.append(std::string(" ") + *it);
79  }
80 }
81 
82 void
83 parse_compiler_flags(const std::string& instring,
84  const std::string& token,
85  bool select,
86  bool last,
87  std::string& outstring)
88 {
89  std::string intermediate;
90  std::vector<std::string> result_vec;
91  boost::split(result_vec, instring,
92  boost::is_any_of("\t "),
93  boost::token_compress_on);
94  for(std::vector<std::string>::const_iterator it = result_vec.begin();
95  it != result_vec.end();
96  ++it)
97  {
98  // Combined into one arg
99  if(it->size() > token.size() && it->substr(0,token.size()) == token)
100  {
101  if(select)
102  {
103  if(intermediate.size())
104  intermediate.append(" ");
105  intermediate.append(it->substr(token.size()));
106  }
107  }
108  // Space-separated
109  else if((*it) == token)
110  {
111  std::vector<std::string>::const_iterator iit = it;
112  if(++iit != result_vec.end())
113  {
114  if(it->size() >= token.size() && it->substr(0,token.size()) == token)
115  {
116  // skip it
117  }
118  else
119  {
120  if(select)
121  {
122  if(intermediate.size())
123  intermediate.append(" ");
124  intermediate.append((*iit));
125  }
126  it = iit;
127  }
128  }
129  }
130  // Special case: if we're told to look for -l, then also find *.a
131  else if(it->size() > 2 &&
132  (*it)[0] == '/' &&
133  it->substr(it->size()-2) == ".a")
134  {
135  if(select)
136  {
137  if(intermediate.size())
138  intermediate.append(" ");
139  intermediate.append((*it));
140  }
141  }
142  else if(!select)
143  {
144  if(intermediate.size())
145  intermediate.append(" ");
146  intermediate.append((*it));
147  }
148  }
149  if(select)
150  deduplicate_tokens(intermediate, last, outstring);
151  else
152  outstring = intermediate;
153 }
154 
155 }
156 
void parse_compiler_flags(const std::string &instring, const std::string &token, bool select, bool last, std::string &outstring)
Definition: utils.cpp:83
void deduplicate_tokens(const std::string &instring, bool last, std::string &outstring)
Definition: utils.cpp:43


rospack
Author(s): Brian Gerkey, Morgan Quigley, Dirk Thomas
autogenerated on Thu Dec 10 2020 03:42:11