stdr_yaml_parser.cpp
Go to the documentation of this file.
00001 /******************************************************************************
00002    STDR Simulator - Simple Two DImensional Robot Simulator
00003    Copyright (C) 2013 STDR Simulator
00004    This program is free software; you can redistribute it and/or modify
00005    it under the terms of the GNU General Public License as published by
00006    the Free Software Foundation; either version 3 of the License, or
00007    (at your option) any later version.
00008    This program is distributed in the hope that it will be useful,
00009    but WITHOUT ANY WARRANTY; without even the implied warranty of
00010    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00011    GNU General Public License for more details.
00012    You should have received a copy of the GNU General Public License
00013    along with this program; if not, write to the Free Software Foundation,
00014    Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301  USA
00015    
00016    Authors : 
00017    * Manos Tsardoulias, etsardou@gmail.com
00018    * Aris Thallas, aris.thallas@gmail.com
00019    * Chris Zalidis, zalidis@gmail.com 
00020 ******************************************************************************/
00021 
00022 #include "stdr_parser/stdr_yaml_parser.h"
00023 
00024 namespace stdr_parser
00025 {
00030   YamlParser::YamlParser(void)
00031   {
00032 
00033   }
00034   
00040   void YamlParser::parse(std::string file_name, Node* base_node)
00041   {
00042     std::string path = file_name;
00043     std::ifstream fin(path.c_str());
00044     
00045     if (!fin.good()) {
00046       throw ParserException("Failed to load '"+ file_name +"', no such file!");
00047     }
00048     
00049     #ifdef HAVE_NEW_YAMLCPP
00050       YAML::Node doc = YAML::Load(fin);
00051     #else
00052       YAML::Parser parser(fin);
00053       YAML::Node doc;
00054       parser.GetNextDocument(doc);
00055     #endif
00056     
00057     base_node->file_origin = file_name;
00058 #ifndef HAVE_NEW_YAMLCPP
00059     base_node->file_row = doc.GetMark().line;
00060 #endif
00061     
00062     parseLow(doc,base_node);
00063   }
00064   
00071   void YamlParser::parseLow(const YAML::Node& node,Node* n)
00072   {
00073     if(node.Type() == YAML::NodeType::Scalar)
00074     {
00075       Node* new_node = new Node();
00076       std::string s;
00077       node >> s;
00078       new_node->value = s;
00079       new_node->file_origin = n->file_origin;
00080 #ifndef HAVE_NEW_YAMLCPP
00081       new_node->file_row = node.GetMark().line;
00082 #endif
00083       n->elements.push_back(new_node);
00084     }
00085     else if(node.Type() == YAML::NodeType::Sequence)
00086     {
00087       for(unsigned int i = 0 ; i < node.size() ; i++) 
00088       {
00089         parseLow(node[i],n);
00090       }
00091     }
00092     else if(node.Type() == YAML::NodeType::Map)
00093     {
00094       std::string s;
00095 #ifdef HAVE_NEW_YAMLCPP
00096       for(YAML::const_iterator it = node.begin() ; it != node.end() ; it++) 
00097 #else
00098       for(YAML::Iterator it = node.begin() ; it != node.end() ; it++) 
00099 #endif
00100       {
00101         Node* new_node = new Node();
00102 #ifdef HAVE_NEW_YAMLCPP
00103         it->first >> s;
00104 #else
00105         it.first() >> s;
00106 #endif
00107         new_node->tag = s;
00108         new_node->file_origin = n->file_origin;
00109 #ifndef HAVE_NEW_YAMLCPP
00110         new_node->file_row = node.GetMark().line;
00111 #endif
00112         n->elements.push_back(new_node);
00113         if(s == "filename")
00114         {
00115           std::string file_name;
00116 #ifdef HAVE_NEW_YAMLCPP
00117           it->second >> file_name;
00118 #else
00119           it.second() >> file_name;
00120 #endif
00121           std::string path = ros::package::getPath("stdr_resources") + 
00122               std::string("/resources/") + file_name;
00123           std::ifstream fin(path.c_str());
00124           if (!fin.good()) {
00125             throw ParserException("Failed to load '"+ file_name +
00126                                     "', no such file!");
00127           }
00128           #ifdef HAVE_NEW_YAMLCPP
00129             YAML::Node doc = YAML::Load(fin);
00130           #else
00131             YAML::Parser parser(fin);
00132             YAML::Node doc;
00133             parser.GetNextDocument(doc);
00134           #endif
00135           new_node->file_origin = file_name;
00136 #ifndef HAVE_NEW_YAMLCPP
00137           new_node->file_row = doc.GetMark().line;
00138 #endif
00139           
00140           parseLow(doc,new_node);
00141         }
00142         else
00143         {
00144 #ifdef HAVE_NEW_YAMLCPP
00145           parseLow(it->second,new_node);
00146 #else
00147           parseLow(it.second(),new_node);
00148 #endif
00149         }
00150       }
00151     }
00152   }
00153 }
00154 


stdr_parser
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Wed Sep 2 2015 03:36:18