RSC  0.17.1
Utility.cpp
Go to the documentation of this file.
1 /* ============================================================
2  *
3  * This file is part of the RSC project.
4  *
5  * Copyright (C) 2016 Jan Moringen <jmoringe@techfak.uni-bielefeld.de>
6  *
7  * This file may be licensed under the terms of the
8  * GNU Lesser General Public License Version 3 (the ``LGPL''),
9  * or (at your option) any later version.
10  *
11  * Software distributed under the License is distributed
12  * on an ``AS IS'' basis, WITHOUT WARRANTY OF ANY KIND, either
13  * express or implied. See the LGPL for the specific language
14  * governing rights and limitations.
15  *
16  * You should have received a copy of the LGPL along with this
17  * program. If not, go to http://www.gnu.org/licenses/lgpl.html
18  * or write to the Free Software Foundation, Inc.,
19  * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  * The development of this software was supported by:
22  * CoR-Lab, Research Institute for Cognition and Robotics
23  * Bielefeld University
24  *
25  * ============================================================ */
26 
27 #include "Utility.h"
28 
29 #include <boost/format.hpp>
30 #include <boost/tokenizer.hpp>
31 
32 #include "../runtime/ContainerIO.h"
33 
34 using namespace std;
35 
36 namespace rsc {
37 namespace config {
38 
39 vector<string> splitSequenceValue(const string& value) {
40  vector<string> result;
41  boost::escaped_list_separator<char> sep('\\', ':');
42  boost::tokenizer< boost::escaped_list_separator<char> > tok(value, sep);
43  copy(tok.begin(), tok.end(), back_inserter(result));
44  return result;
45 }
46 
47 vector<string> mergeSequenceValue(const string& description,
48  const vector<string>& key,
49  const string& raw,
50  const vector<string>& previous) {
51  // Split value at ":" tokens. Each substring is an option value.
52  vector<string> values;
53  try {
54  values = splitSequenceValue(raw);
55  } catch (const std::exception& e) {
56  throw invalid_argument(
57  boost::str(boost::format("Invalid %1% (option %2%) value `%3%': %4%")
58  % description
59  % boost::io::group(std::container_none,
60  std::element_sequence(".", ""),
61  key)
62  % raw
63  % e.what()));
64  }
65 
66  // Add all specified values. The empty string, produced by "::"
67  // means that the previous value should be spliced in.
68  vector<string> result;
69  for (vector<string>::const_iterator it = values.begin();
70  it != values.end(); ++it) {
71  const string value = *it;
72  if (value.empty()) {
73  copy(previous.begin(), previous.end(),
74  inserter(result, result.end()));
75  } else {
76  result.push_back(value);
77  }
78  }
79 
80  return result;
81 }
82 
83 }
84 }
detail::set_element_sequence_style< detail::element_sequence_style > element_sequence(const string &separator, const string &first_separator, const string &last_separator)
Definition: ContainerIO.cpp:98
vector< string > splitSequenceValue(const string &value)
Definition: Utility.cpp:39
STL namespace.
const detail::set_container_style< detail::container_style > container_none
vector< string > mergeSequenceValue(const string &description, const vector< string > &key, const string &raw, const vector< string > &previous)
Definition: Utility.cpp:47