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 (const 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 // Workaround as YAML::Null is missing from Windows DLLs;
156 // see https://github.com/jbeder/yaml-cpp/issues/950
157 #ifndef _WIN32
158  emitter << YAML::Null;
159 #endif
160  break;
161  }
162 }
163 
164 } // end namespace rviz
rviz::Config::MapIterator::isValid
bool isValid()
Return true if the iterator currently points to a valid entry, false if not.
Definition: config.cpp:392
rviz::YamlConfigWriter::YamlConfigWriter
YamlConfigWriter()
Constructor. Writer starts in a non-error state.
Definition: yaml_config_writer.cpp:39
rviz::YamlConfigWriter::error_
bool error_
Definition: yaml_config_writer.h:73
rviz::Config::MapIterator::currentKey
QString currentKey()
Return the name of the current map entry.
Definition: config.cpp:420
rviz::YamlConfigWriter::writeStream
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...
Definition: yaml_config_writer.cpp:86
rviz::YamlConfigWriter::errorMessage
QString errorMessage()
Return an error message if the latest write call had an error, or the empty string if there was no er...
Definition: yaml_config_writer.cpp:104
yaml_config_writer.h
rviz::Config::List
@ List
Definition: config.h:151
rviz
Definition: add_display_dialog.cpp:54
rviz::YamlConfigWriter::error
bool error()
Return true if the latest write operation had an error.
Definition: yaml_config_writer.cpp:99
rviz::Config::Value
@ Value
Definition: config.h:152
rviz::Config::MapIterator::currentChild
Config currentChild()
Return a Config reference to the current map entry.
Definition: config.cpp:430
rviz::YamlConfigWriter::writeConfigNode
void writeConfigNode(const Config &config, YAML::Emitter &emitter)
Definition: yaml_config_writer.cpp:109
rviz::YamlConfigWriter::writeString
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...
Definition: yaml_config_writer.cpp:70
rviz::YamlConfigWriter::message_
QString message_
Definition: yaml_config_writer.h:72
rviz::Config::Map
@ Map
Definition: config.h:150
rviz::Config::MapIterator
Iterator class for looping over all entries in a Map type Config Node.
Definition: config.h:291
rviz::YamlConfigWriter::writeFile
void writeFile(const Config &config, const QString &filename)
Write config data to a file. This potentially changes the return values of error() and statusMessage(...
Definition: yaml_config_writer.cpp:45
config
config
rviz::Config::MapIterator::advance
void advance()
Advance iterator to next entry.
Definition: config.cpp:374
rviz::Config
Configuration data storage class.
Definition: config.h:124


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust, William Woodall
autogenerated on Fri Dec 13 2024 03:31:03