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 {
38 
41  : error_( false )
42 {}
43 
46 void YamlConfigWriter::writeFile( const Config& config, const QString& filename )
47 {
48  try
49  {
50  std::ofstream out( qPrintable( filename ));
51  if( out )
52  {
53  writeStream( config, out, filename );
54  }
55  else
56  {
57  error_ = true;
58  message_ = "Failed to open " + filename + " for writing.";
59  }
60  }
61  catch( std::exception ex )
62  {
63  error_ = true;
64  message_ = ex.what();
65  }
66 }
67 
71 QString YamlConfigWriter::writeString( const Config& config, const QString& filename )
72 {
73  std::stringstream out;
74  writeStream( config, out, filename );
75  if( !error_ )
76  {
77  return QString::fromStdString( out.str() );
78  }
79  else
80  {
81  return "";
82  }
83 }
84 
87 void YamlConfigWriter::writeStream( const Config& config, std::ostream& out, const QString& filename )
88 {
89  error_ = false;
90  message_ = "";
91  YAML::Emitter emitter;
92  writeConfigNode( config, emitter );
93  if( !error_ )
94  {
95  out << emitter.c_str() << std::endl;
96  }
97 }
98 
101 {
102  return error_;
103 }
104 
106 {
107  return message_;
108 }
109 
110 void YamlConfigWriter::writeConfigNode( const Config& config, YAML::Emitter& emitter )
111 {
112  switch( config.getType() )
113  {
114  case Config::List:
115  {
116  emitter << YAML::BeginSeq;
117  for( int i = 0; i < config.listLength(); i++ )
118  {
119  writeConfigNode( config.listChildAt( i ), emitter );
120  }
121  emitter << YAML::EndSeq;
122  break;
123  }
124  case Config::Map:
125  {
126  emitter << YAML::BeginMap;
127  Config::MapIterator map_iter = config.mapIterator();
128  while( map_iter.isValid() )
129  {
130  Config child = map_iter.currentChild();
131 
132  emitter << YAML::Key;
133  emitter << map_iter.currentKey().toStdString();
134  emitter << YAML::Value;
135  writeConfigNode( child, emitter );
136 
137  map_iter.advance();
138  }
139  emitter << YAML::EndMap;
140  break;
141  }
142  case Config::Value:
143  {
144  QString value = config.getValue().toString();
145  if( value.size() == 0 )
146  {
147  emitter << YAML::DoubleQuoted << "";
148  }
149  else
150  {
151  emitter << value.toStdString();
152  }
153  break;
154  }
155  default:
156  emitter << YAML::Null;
157  break;
158  }
159 }
160 
161 } // end namespace rviz
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:167
QString errorMessage()
Return an error message if the latest write call had an error, or the empty string if there was no er...
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:317
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:322
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...
bool isValid()
Return true if the iterator currently points to a valid entry, false if not.
Definition: config.cpp:385
bool error()
Return true if the latest write operation had an error.
Configuration data storage class.
Definition: config.h:125
QString currentKey()
Return the name of the current map entry.
Definition: config.cpp:413
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:312
Iterator class for looping over all entries in a Map type Config Node.
Definition: config.h:282
void writeFile(const Config &config, const QString &filename)
Write config data to a file. This potentially changes the return values of error() and statusMessage(...
MapIterator mapIterator() const
Return a new iterator for looping over key/value pairs.
Definition: config.cpp:344
YamlConfigWriter()
Constructor. Writer starts in a non-error state.
Config currentChild()
Return a Config reference to the current map entry.
Definition: config.cpp:423
void writeConfigNode(const Config &config, YAML::Emitter &emitter)
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:367


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Sat Apr 27 2019 02:33:42