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 #include <yaml-cpp/yaml.h>
35 
36 namespace rviz
37 {
39 {
40 }
41 
42 void YamlConfigReader::readFile(Config& config, const QString& filename)
43 {
44  std::ifstream in(qPrintable(filename));
45  readStream(config, in, filename);
46 }
47 
48 void YamlConfigReader::readString(Config& config, const QString& data, const QString& filename)
49 {
50  std::stringstream ss(data.toStdString());
51  readStream(config, ss, filename);
52 }
53 
54 void YamlConfigReader::readStream(Config& config, std::istream& in, const QString& /*filename*/)
55 {
56  try
57  {
58  YAML::Node yaml_node;
59  yaml_node = YAML::Load(in);
60  error_ = false;
61  message_ = "";
62  readYamlNode(config, yaml_node);
63  }
64  catch (YAML::ParserException& ex)
65  {
66  message_ = ex.what();
67  error_ = true;
68  }
69 }
70 
71 void YamlConfigReader::readYamlNode(Config& config, const YAML::Node& yaml_node)
72 {
73  switch (yaml_node.Type())
74  {
75  case YAML::NodeType::Map:
76  {
77  for (YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it)
78  {
79  std::string key;
80  key = it->first.as<std::string>();
81  Config child = config.mapMakeChild(QString::fromStdString(key));
82  readYamlNode(child, it->second);
83  }
84  break;
85  }
86  case YAML::NodeType::Sequence:
87  {
88  for (YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it)
89  {
90  Config child = config.listAppendNew();
91  readYamlNode(child, *it);
92  }
93  break;
94  }
95  case YAML::NodeType::Scalar:
96  {
97  std::string s;
98  s = yaml_node.as<std::string>();
99  config.setValue(QString::fromStdString(s));
100  break;
101  }
102  case YAML::NodeType::Null:
103  default:
104  break;
105  }
106 }
107 
109 {
110  return error_;
111 }
112 
114 {
115  return message_;
116 }
117 
118 } // end namespace rviz
rviz::YamlConfigReader::error
bool error()
Return true if the latest readFile() or readString() call had an error.
Definition: yaml_config_reader.cpp:108
s
XmlRpcServer s
rviz::YamlConfigReader::error_
bool error_
Definition: yaml_config_reader.h:72
rviz::YamlConfigReader::readString
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(),...
Definition: yaml_config_reader.cpp:48
rviz::YamlConfigReader::errorMessage
QString errorMessage()
Return an error message if the latest read call had an error, or the empty string if not.
Definition: yaml_config_reader.cpp:113
rviz::YamlConfigReader::readStream
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(),...
Definition: yaml_config_reader.cpp:54
yaml_config_reader.h
rviz
Definition: add_display_dialog.cpp:54
rviz::YamlConfigReader::readFile
void readFile(Config &config, const QString &filename)
Read config data from a file. This potentially changes the return value sof error(),...
Definition: yaml_config_reader.cpp:42
rviz::YamlConfigReader::YamlConfigReader
YamlConfigReader()
Constructor. Object begins in a no-error state.
Definition: yaml_config_reader.cpp:38
rviz::YamlConfigReader::readYamlNode
void readYamlNode(Config &config, const YAML::Node &yaml_node)
Definition: yaml_config_reader.cpp:71
rviz::YamlConfigReader::message_
QString message_
Definition: yaml_config_reader.h:71
config
config
rviz::Config
Configuration data storage class.
Definition: config.h:124


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Aug 2 2024 08:43:10