yaml_config_reader.cpp
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2012, Willow Garage, Inc.
3  * All rights reserved.
4  *
5  * Redistribution and use in source and binary forms, with or without
6  * modification, are permitted provided that the following conditions are met:
7  *
8  * * Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  * * Redistributions in binary form must reproduce the above copyright
11  * notice, this list of conditions and the following disclaimer in the
12  * documentation and/or other materials provided with the distribution.
13  * * Neither the name of the Willow Garage, Inc. nor the names of its
14  * contributors may be used to endorse or promote products derived from
15  * this software without specific prior written permission.
16  *
17  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
18  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
19  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
20  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
21  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
22  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
23  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
24  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
25  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
27  * POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #include <fstream>
31 #include <sstream>
32 
33 #ifdef RVIZ_HAVE_YAMLCPP_05
34 #include <yaml-cpp/yaml.h>
35 #else
36 #include <yaml-cpp/node.h>
37 #include <yaml-cpp/parser.h>
38 #endif
39 
41 
42 namespace rviz
43 {
44 
46  : error_( false )
47 {}
48 
49 void YamlConfigReader::readFile( Config& config, const QString& filename )
50 {
51  std::ifstream in( qPrintable( filename ));
52  readStream( config, in, filename );
53 }
54 
55 void YamlConfigReader::readString( Config& config, const QString& data, const QString& filename )
56 {
57  std::stringstream ss( data.toStdString() );
58  readStream( config, ss, filename );
59 }
60 
61 void YamlConfigReader::readStream( Config& config, std::istream& in, const QString& filename )
62 {
63  try
64  {
65  YAML::Node yaml_node;
66 #ifdef RVIZ_HAVE_YAMLCPP_05
67  yaml_node = YAML::Load(in);
68 #else
69  YAML::Parser parser( in );
70  parser.GetNextDocument( yaml_node );
71 #endif
72  error_ = false;
73  message_ = "";
74  readYamlNode( config, yaml_node );
75  }
76  catch( YAML::ParserException& ex )
77  {
78  message_ = ex.what();
79  error_ = true;
80  }
81 }
82 
83 void YamlConfigReader::readYamlNode( Config& config, const YAML::Node& yaml_node )
84 {
85  switch( yaml_node.Type() )
86  {
87  case YAML::NodeType::Map:
88  {
89 #ifdef RVIZ_HAVE_YAMLCPP_05
90  for( YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
91 #else
92  for( YAML::Iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
93 #endif
94  {
95  std::string key;
96 #ifdef RVIZ_HAVE_YAMLCPP_05
97  key = it->first.as<std::string>();
98 #else
99  it.first() >> key;
100 #endif
101  Config child = config.mapMakeChild( QString::fromStdString( key ));
102 #ifdef RVIZ_HAVE_YAMLCPP_05
103  readYamlNode( child, it->second );
104 #else
105  readYamlNode( child, it.second() );
106 #endif
107  }
108  break;
109  }
110  case YAML::NodeType::Sequence:
111  {
112 #ifdef RVIZ_HAVE_YAMLCPP_05
113  for( YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
114 #else
115  for( YAML::Iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
116 #endif
117  {
118  Config child = config.listAppendNew();
119  readYamlNode( child, *it );
120  }
121  break;
122  }
123  case YAML::NodeType::Scalar:
124  {
125  std::string s;
126 #ifdef RVIZ_HAVE_YAMLCPP_05
127  s = yaml_node.as<std::string>();
128 #else
129  yaml_node >> s;
130 #endif
131  config.setValue( QString::fromStdString( s ));
132  break;
133  }
134  case YAML::NodeType::Null:
135  default:
136  break;
137  }
138 }
139 
141 {
142  return error_;
143 }
144 
146 {
147  return message_;
148 }
149 
150 } // end namespace rviz
void readYamlNode(Config &config, const YAML::Node &yaml_node)
void setValue(const QVariant &value)
Ensures this is a valid Config object, sets the type to Value then sets the value.
Definition: config.cpp:305
QString errorMessage()
Return an error message if the latest read call had an error, or the empty string if not...
XmlRpcServer s
void readStream(Config &config, std::istream &in, const QString &filename="data stream")
Read config data from a std::istream. This potentially changes the return value sof error()...
YamlConfigReader()
Constructor. Object begins in a no-error state.
Configuration data storage class.
Definition: config.h:125
void readFile(Config &config, const QString &filename)
Read config data from a file. This potentially changes the return value sof error(), statusMessage(), and config().
Config mapMakeChild(const QString &key)
Create a child node stored with the given key, and return the child.
Definition: config.cpp:190
bool error()
Return true if the latest readFile() or readString() call had an error.
Config listAppendNew()
Ensure the referenced Node is of type List, append a new Empty Node to the end of the list...
Definition: config.cpp:334
void readString(Config &config, const QString &data, const QString &filename="data string")
Read config data from a string. This potentially changes the return value sof error(), statusMessage(), and config().
parser


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Wed Aug 28 2019 04:01:52