Nav2 Navigation Stack - humble  humble
ROS 2 Navigation Stack
array_parser.cpp
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  *
29  * author: Dave Hershberger
30  */
31 
32 #include <cstdio> // for EOF
33 #include <string>
34 #include <sstream>
35 #include <vector>
36 
37 namespace nav2_costmap_2d
38 {
39 
44 std::vector<std::vector<float>> parseVVF(const std::string & input, std::string & error_return)
45 {
46  std::vector<std::vector<float>> result;
47 
48  std::stringstream input_ss(input);
49  int depth = 0;
50  std::vector<float> current_vector;
51  while (!!input_ss && !input_ss.eof()) {
52  switch (input_ss.peek()) {
53  case EOF:
54  break;
55  case '[':
56  depth++;
57  if (depth > 2) {
58  error_return = "Array depth greater than 2";
59  return result;
60  }
61  input_ss.get();
62  current_vector.clear();
63  break;
64  case ']':
65  depth--;
66  if (depth < 0) {
67  error_return = "More close ] than open [";
68  return result;
69  }
70  input_ss.get();
71  if (depth == 1) {
72  result.push_back(current_vector);
73  }
74  break;
75  case ',':
76  case ' ':
77  case '\t':
78  input_ss.get();
79  break;
80  default: // All other characters should be part of the numbers.
81  if (depth != 2) {
82  std::stringstream err_ss;
83  err_ss << "Numbers at depth other than 2. Char was '" << char(input_ss.peek()) << "'.";
84  error_return = err_ss.str();
85  return result;
86  }
87  float value;
88  input_ss >> value;
89  if (!!input_ss) {
90  current_vector.push_back(value);
91  }
92  break;
93  }
94  }
95 
96  if (depth != 0) {
97  error_return = "Unterminated vector string.";
98  } else {
99  error_return = "";
100  }
101 
102  return result;
103 }
104 
105 } // end namespace nav2_costmap_2d
std::vector< std::vector< float > > parseVVF(const std::string &input, std::string &error_return)
Parse a vector of vectors of floats from a string.