stdr_yaml_parser.cpp
Go to the documentation of this file.
1 /******************************************************************************
2  STDR Simulator - Simple Two DImensional Robot Simulator
3  Copyright (C) 2013 STDR Simulator
4  This program is free software; you can redistribute it and/or modify
5  it under the terms of the GNU General Public License as published by
6  the Free Software Foundation; either version 3 of the License, or
7  (at your option) any later version.
8  This program is distributed in the hope that it will be useful,
9  but WITHOUT ANY WARRANTY; without even the implied warranty of
10  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
11  GNU General Public License for more details.
12  You should have received a copy of the GNU General Public License
13  along with this program; if not, write to the Free Software Foundation,
14  Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
15 
16  Authors :
17  * Manos Tsardoulias, etsardou@gmail.com
18  * Aris Thallas, aris.thallas@gmail.com
19  * Chris Zalidis, zalidis@gmail.com
20 ******************************************************************************/
21 
23 
24 namespace stdr_parser
25 {
31  {
32 
33  }
34 
40  void YamlParser::parse(std::string file_name, Node* base_node)
41  {
42  std::string path = file_name;
43  std::ifstream fin(path.c_str());
44 
45  if (!fin.good()) {
46  throw ParserException("Failed to load '"+ file_name +"', no such file!");
47  }
48 
49 #ifdef HAVE_NEW_YAMLCPP
50  YAML::Node doc = YAML::Load(fin);
51 #else
52  YAML::Parser parser(fin);
53  YAML::Node doc;
54  parser.GetNextDocument(doc);
55 #endif
56 
57  base_node->file_origin = file_name;
58 #ifndef HAVE_NEW_YAMLCPP
59  base_node->file_row = doc.GetMark().line;
60 #endif
61 
62  parseLow(doc,base_node);
63  }
64 
71  void YamlParser::parseLow(const YAML::Node& node,Node* n)
72  {
73  if(node.Type() == YAML::NodeType::Scalar)
74  {
75  Node* new_node = new Node();
76  std::string s;
77  node >> s;
78  new_node->value = s;
79  new_node->file_origin = n->file_origin;
80 
81 #ifndef HAVE_NEW_YAMLCPP
82  new_node->file_row = node.GetMark().line;
83 #endif
84 
85  n->elements.push_back(new_node);
86  }
87  else if(node.Type() == YAML::NodeType::Sequence)
88  {
89  for(unsigned int i = 0 ; i < node.size() ; i++)
90  {
91  parseLow(node[i],n);
92  }
93  }
94  else if(node.Type() == YAML::NodeType::Map)
95  {
96  std::string s;
97 #ifdef HAVE_NEW_YAMLCPP
98  for(YAML::const_iterator it = node.begin() ; it != node.end() ; it++)
99 #else
100  for(YAML::Iterator it = node.begin() ; it != node.end() ; it++)
101 #endif
102  {
103  Node* new_node = new Node();
104 #ifdef HAVE_NEW_YAMLCPP
105  it->first >> s;
106 #else
107  it.first() >> s;
108 #endif
109  new_node->tag = s;
110  new_node->file_origin = n->file_origin;
111 #ifndef HAVE_NEW_YAMLCPP
112  new_node->file_row = node.GetMark().line;
113 #endif
114  n->elements.push_back(new_node);
115  if(s == "filename")
116  {
117  std::string file_name;
118 #ifdef HAVE_NEW_YAMLCPP
119  it->second >> file_name;
120 #else
121  it.second() >> file_name;
122 #endif
123  std::string path = ros::package::getPath("stdr_resources") +
124  std::string("/resources/") + file_name;
125  std::ifstream fin(path.c_str());
126  if (!fin.good()) {
127  // If not found on stdr_resources/resources,
128  // search on the directory containing parent file
129  path = extractDirname(new_node->file_origin) +
130  std::string("/") + file_name;
131  fin.open(path.c_str());
132  if (!fin.good()) {
133  throw ParserException("Failed to load '"+ file_name +
134  "', no such file!");
135  }
136  }
137 #ifdef HAVE_NEW_YAMLCPP
138  YAML::Node doc = YAML::Load(fin);
139 #else
140  YAML::Parser parser(fin);
141  YAML::Node doc;
142  parser.GetNextDocument(doc);
143 #endif
144  new_node->file_origin = file_name;
145 #ifndef HAVE_NEW_YAMLCPP
146  new_node->file_row = doc.GetMark().line;
147 #endif
148 
149  parseLow(doc,new_node);
150  }
151  else
152  {
153 #ifdef HAVE_NEW_YAMLCPP
154  parseLow(it->second,new_node);
155 #else
156  parseLow(it.second(),new_node);
157 #endif
158  }
159  }
160  }
161  }
162 }
163 
std::string tag
The node value (if it not a tag node)
The main namespace for STDR GUI XML parser.
XmlRpcServer s
std::string value
The node children.
YamlParser(void)
Default constructor.
ROSLIB_DECL std::string getPath(const std::string &package_name)
static void parse(std::string file_name, Node *n)
Private function that initiates the parsing of an xml file.
std::string extractDirname(std::string s)
Extracts the directory from an absolute path. It does the same functionality as libgen&#39;s dirname but ...
Implements the main functionalities of the stdr parser tree.
std::string file_origin
Row in the original file.
static void parseLow(const YAML::Node &node, Node *n)
Low-level recursive function for parsing the yaml file.
std::vector< Node * > elements
File it was into.
Provides a parser exception. Publicly inherits from std::runtime_error. Used in robot handler...


stdr_parser
Author(s): Manos Tsardoulias, Chris Zalidis, Aris Thallas
autogenerated on Mon Jun 10 2019 15:14:54