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 
00081 #ifndef HAVE_NEW_YAMLCPP
00082       new_node->file_row = node.GetMark().line;
00083 #endif
00084       
00085       n->elements.push_back(new_node);
00086     }
00087     else if(node.Type() == YAML::NodeType::Sequence)
00088     {
00089       for(unsigned int i = 0 ; i < node.size() ; i++) 
00090       {
00091         parseLow(node[i],n);
00092       }
00093     }
00094     else if(node.Type() == YAML::NodeType::Map)
00095     {
00096       std::string s;
00097 #ifdef HAVE_NEW_YAMLCPP
00098       for(YAML::const_iterator it = node.begin() ; it != node.end() ; it++) 
00099 #else
00100         for(YAML::Iterator it = node.begin() ; it != node.end() ; it++) 
00101 #endif
00102         {
00103           Node* new_node = new Node();
00104 #ifdef HAVE_NEW_YAMLCPP
00105           it->first >> s;
00106 #else
00107           it.first() >> s;
00108 #endif
00109           new_node->tag = s;
00110           new_node->file_origin = n->file_origin;
00111 #ifndef HAVE_NEW_YAMLCPP
00112           new_node->file_row = node.GetMark().line;
00113 #endif
00114           n->elements.push_back(new_node);
00115           if(s == "filename")
00116           {
00117             std::string file_name;
00118 #ifdef HAVE_NEW_YAMLCPP
00119             it->second >> file_name;
00120 #else
00121             it.second() >> file_name;
00122 #endif
00123             std::string path = ros::package::getPath("stdr_resources") + 
00124               std::string("/resources/") + file_name;
00125             std::ifstream fin(path.c_str());
00126             if (!fin.good()) {
00127               // If not found on stdr_resources/resources,
00128               // search on the directory containing parent file
00129               path = extractDirname(new_node->file_origin) +
00130                 std::string("/") + file_name;
00131               fin.open(path.c_str());
00132               if (!fin.good()) {
00133                 throw ParserException("Failed to load '"+ file_name +
00134                   "', no such file!");
00135               }
00136             }
00137 #ifdef HAVE_NEW_YAMLCPP
00138             YAML::Node doc = YAML::Load(fin);
00139 #else
00140             YAML::Parser parser(fin);
00141             YAML::Node doc;
00142             parser.GetNextDocument(doc);
00143 #endif
00144             new_node->file_origin = file_name;
00145 #ifndef HAVE_NEW_YAMLCPP
00146             new_node->file_row = doc.GetMark().line;
00147 #endif
00148 
00149             parseLow(doc,new_node);
00150           }
00151           else
00152           {
00153 #ifdef HAVE_NEW_YAMLCPP
00154             parseLow(it->second,new_node);
00155 #else
00156             parseLow(it.second(),new_node);
00157 #endif
00158           }
00159         }
00160     }
00161   }
00162 }
00163 


stdr_parser
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Thu Jun 6 2019 18:57:14