array_parser.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  * author: Dave Hershberger
00030  */
00031 
00032 #include <cstdio>  // for EOF
00033 #include <string>
00034 #include <sstream>
00035 #include <vector>
00036 
00037 namespace costmap_2d
00038 {
00039 
00044 std::vector<std::vector<float> > parseVVF(const std::string& input, std::string& error_return)
00045 {
00046   std::vector<std::vector<float> > result;
00047 
00048   std::stringstream input_ss(input);
00049   int depth = 0;
00050   std::vector<float> current_vector;
00051   while (!!input_ss && !input_ss.eof())
00052   {
00053     switch (input_ss.peek())
00054     {
00055     case EOF:
00056       break;
00057     case '[':
00058       depth++;
00059       if (depth > 2)
00060       {
00061         error_return = "Array depth greater than 2";
00062         return result;
00063       }
00064       input_ss.get();
00065       current_vector.clear();
00066       break;
00067     case ']':
00068       depth--;
00069       if (depth < 0)
00070       {
00071         error_return = "More close ] than open [";
00072         return result;
00073       }
00074       input_ss.get();
00075       if (depth == 1)
00076       {
00077         result.push_back(current_vector);
00078       }
00079       break;
00080     case ',':
00081     case ' ':
00082     case '\t':
00083       input_ss.get();
00084       break;
00085     default:  // All other characters should be part of the numbers.
00086       if (depth != 2)
00087       {
00088         std::stringstream err_ss;
00089         err_ss << "Numbers at depth other than 2. Char was '" << char(input_ss.peek()) << "'.";
00090         error_return = err_ss.str();
00091         return result;
00092       }
00093       float value;
00094       input_ss >> value;
00095       if (!!input_ss)
00096       {
00097         current_vector.push_back(value);
00098       }
00099       break;
00100     }
00101   }
00102 
00103   if (depth != 0)
00104   {
00105     error_return = "Unterminated vector string.";
00106   }
00107   else
00108   {
00109     error_return = "";
00110   }
00111 
00112   return result;
00113 }
00114 
00115 }  // end namespace costmap_2d


costmap_2d
Author(s): Eitan Marder-Eppstein, David V. Lu!!, Dave Hershberger, contradict@gmail.com
autogenerated on Sun Mar 3 2019 03:46:15