Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
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
00128
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