yaml_config_writer.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 
32 #include <yaml-cpp/emitter.h>
33 
35 
36 namespace rviz
37 {
40 {
41 }
42 
45 void YamlConfigWriter::writeFile(const Config& config, const QString& filename)
46 {
47  try
48  {
49  std::ofstream out(qPrintable(filename));
50  if (out)
51  {
52  writeStream(config, out, filename);
53  }
54  else
55  {
56  error_ = true;
57  message_ = "Failed to open " + filename + " for writing.";
58  }
59  }
60  catch (std::exception ex)
61  {
62  error_ = true;
63  message_ = ex.what();
64  }
65 }
66 
70 QString YamlConfigWriter::writeString(const Config& config, const QString& filename)
71 {
72  std::stringstream out;
73  writeStream(config, out, filename);
74  if (!error_)
75  {
76  return QString::fromStdString(out.str());
77  }
78  else
79  {
80  return "";
81  }
82 }
83 
86 void YamlConfigWriter::writeStream(const Config& config, std::ostream& out, const QString& /*filename*/)
87 {
88  error_ = false;
89  message_ = "";
90  YAML::Emitter emitter;
91  writeConfigNode(config, emitter);
92  if (!error_)
93  {
94  out << emitter.c_str() << std::endl;
95  }
96 }
97 
100 {
101  return error_;
102 }
103 
105 {
106  return message_;
107 }
108 
109 void YamlConfigWriter::writeConfigNode(const Config& config, YAML::Emitter& emitter)
110 {
111  switch (config.getType())
112  {
113  case Config::List:
114  {
115  emitter << YAML::BeginSeq;
116  for (int i = 0; i < config.listLength(); i++)
117  {
118  writeConfigNode(config.listChildAt(i), emitter);
119  }
120  emitter << YAML::EndSeq;
121  break;
122  }
123  case Config::Map:
124  {
125  emitter << YAML::BeginMap;
126  Config::MapIterator map_iter = config.mapIterator();
127  while (map_iter.isValid())
128  {
129  Config child = map_iter.currentChild();
130 
131  emitter << YAML::Key;
132  emitter << map_iter.currentKey().toStdString();
133  emitter << YAML::Value;
134  writeConfigNode(child, emitter);
135 
136  map_iter.advance();
137  }
138  emitter << YAML::EndMap;
139  break;
140  }
141  case Config::Value:
142  {
143  QString value = config.getValue().toString();
144  if (value.size() == 0)
145  {
146  emitter << YAML::DoubleQuoted << "";
147  }
148  else
149  {
150  emitter << value.toStdString();
151  }
152  break;
153  }
154  default:
155  emitter << YAML::Null;
156  break;
157  }
158 }
159 
160 } // end namespace rviz
QVariant getValue() const
If this config object is valid and is a Value type, this returns its value. Otherwise it returns an i...
Definition: config.cpp:324
QString errorMessage()
Return an error message if the latest write call had an error, or the empty string if there was no er...
QString writeString(const Config &config, const QString &filename="data string")
Write config data to a string, and return it. This potentially changes the return values of error() a...
int listLength() const
Returns the length of the List in this Node, or 0 if this Node does not have type List...
Definition: config.cpp:329
bool isValid()
Return true if the iterator currently points to a valid entry, false if not.
Definition: config.cpp:397
bool error()
Return true if the latest write operation had an error.
Configuration data storage class.
Definition: config.h:124
QString currentKey()
Return the name of the current map entry.
Definition: config.cpp:425
Iterator class for looping over all entries in a Map type Config Node.
Definition: config.h:288
Type getType() const
Return the Type of the referenced Node, or Invalid if this Config does not refer to a Node at all...
Definition: config.cpp:178
void writeFile(const Config &config, const QString &filename)
Write config data to a file. This potentially changes the return values of error() and statusMessage(...
YamlConfigWriter()
Constructor. Writer starts in a non-error state.
Config listChildAt(int i) const
Return the i&#39;th child in the list, if the referenced Node has type List. Returns an Invalid Config if...
Definition: config.cpp:334
Config currentChild()
Return a Config reference to the current map entry.
Definition: config.cpp:435
void writeConfigNode(const Config &config, YAML::Emitter &emitter)
MapIterator mapIterator() const
Return a new iterator for looping over key/value pairs.
Definition: config.cpp:356
void writeStream(const Config &config, std::ostream &out, const QString &filename="data stream")
Write config data to a std::ostream. This potentially changes the return values of error() and status...
void advance()
Advance iterator to next entry.
Definition: config.cpp:379


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat May 27 2023 02:06:25