array_parser.cpp
Go to the documentation of this file.
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 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  {
53  switch (input_ss.peek())
54  {
55  case EOF:
56  break;
57  case '[':
58  depth++;
59  if (depth > 2)
60  {
61  error_return = "Array depth greater than 2";
62  return result;
63  }
64  input_ss.get();
65  current_vector.clear();
66  break;
67  case ']':
68  depth--;
69  if (depth < 0)
70  {
71  error_return = "More close ] than open [";
72  return result;
73  }
74  input_ss.get();
75  if (depth == 1)
76  {
77  result.push_back(current_vector);
78  }
79  break;
80  case ',':
81  case ' ':
82  case '\t':
83  input_ss.get();
84  break;
85  default: // All other characters should be part of the numbers.
86  if (depth != 2)
87  {
88  std::stringstream err_ss;
89  err_ss << "Numbers at depth other than 2. Char was '" << char(input_ss.peek()) << "'.";
90  error_return = err_ss.str();
91  return result;
92  }
93  float value;
94  input_ss >> value;
95  if (!!input_ss)
96  {
97  current_vector.push_back(value);
98  }
99  break;
100  }
101  }
102 
103  if (depth != 0)
104  {
105  error_return = "Unterminated vector string.";
106  }
107  else
108  {
109  error_return = "";
110  }
111 
112  return result;
113 }
114 
115 } // end namespace 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.


costmap_2d
Author(s): Eitan Marder-Eppstein, David V. Lu!!, Dave Hershberger, contradict@gmail.com
autogenerated on Thu Jan 21 2021 04:05:42