yaml_config_writer.cpp
Go to the documentation of this file.
00001 /*
00002  * Copyright (c) 2012, Willow Garage, Inc.
00003  * All rights reserved.
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  *     * Redistributions of source code must retain the above copyright
00009  *       notice, this list of conditions and the following disclaimer.
00010  *     * Redistributions in binary form must reproduce the above copyright
00011  *       notice, this list of conditions and the following disclaimer in the
00012  *       documentation and/or other materials provided with the distribution.
00013  *     * Neither the name of the Willow Garage, Inc. nor the names of its
00014  *       contributors may be used to endorse or promote products derived from
00015  *       this software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  */
00029 
00030 #include <fstream>
00031 
00032 #include <yaml-cpp/emitter.h>
00033 
00034 #include "rviz/yaml_config_writer.h"
00035 
00036 namespace rviz
00037 {
00038 
00040 YamlConfigWriter::YamlConfigWriter()
00041   : error_( false )
00042 {}
00043 
00046 void YamlConfigWriter::writeFile( const Config& config, const QString& filename )
00047 {
00048   try
00049   {
00050     std::ofstream out( qPrintable( filename ));
00051     if( out )
00052     {
00053       writeStream( config, out, filename );
00054     }
00055     else
00056     {
00057       error_ = true;
00058       message_ = "Failed to open " + filename + " for writing.";
00059     }
00060   }
00061   catch( std::exception ex )
00062   {
00063     error_ = true;
00064     message_ = ex.what();
00065   }
00066 }
00067 
00071 QString YamlConfigWriter::writeString( const Config& config, const QString& filename )
00072 {
00073   std::stringstream out;
00074   writeStream( config, out, filename );
00075   if( !error_ )
00076   {
00077     return QString::fromStdString( out.str() );
00078   }
00079   else
00080   {
00081     return "";
00082   }
00083 }
00084 
00087 void YamlConfigWriter::writeStream( const Config& config, std::ostream& out, const QString& filename )
00088 {
00089   error_ = false;
00090   message_ = "";
00091   YAML::Emitter emitter;
00092   writeConfigNode( config, emitter );
00093   if( !error_ )
00094   {
00095     out << emitter.c_str() << std::endl;
00096   }
00097 }
00098 
00100 bool YamlConfigWriter::error()
00101 {
00102   return error_;
00103 }
00104 
00105 QString YamlConfigWriter::errorMessage()
00106 {
00107   return message_;
00108 }
00109 
00110 void YamlConfigWriter::writeConfigNode( const Config& config, YAML::Emitter& emitter )
00111 {
00112   switch( config.getType() )
00113   {
00114   case Config::List:
00115   {
00116     emitter << YAML::BeginSeq;
00117     for( int i = 0; i < config.listLength(); i++ )
00118     {
00119       writeConfigNode( config.listChildAt( i ), emitter );
00120     }
00121     emitter << YAML::EndSeq;
00122     break;
00123   }
00124   case Config::Map:
00125   {
00126     emitter << YAML::BeginMap;
00127     Config::MapIterator map_iter = config.mapIterator();
00128     while( map_iter.isValid() )
00129     {
00130       Config child = map_iter.currentChild();
00131 
00132       emitter << YAML::Key;
00133       emitter << map_iter.currentKey().toStdString();
00134       emitter << YAML::Value;
00135       writeConfigNode( child, emitter );
00136 
00137       map_iter.advance();
00138     }
00139     emitter << YAML::EndMap;
00140     break;
00141   }
00142   case Config::Value:
00143   {
00144     QString value = config.getValue().toString();
00145     if( value.size() == 0 )
00146     {
00147       emitter << YAML::DoubleQuoted << "";
00148     }
00149     else
00150     {
00151       emitter << value.toStdString();
00152     }
00153     break;
00154   }
00155   default:
00156     emitter << YAML::Null;
00157     break;
00158   }
00159 }
00160 
00161 } // end namespace rviz


rviz
Author(s): Dave Hershberger, David Gossow, Josh Faust
autogenerated on Thu Aug 27 2015 15:02:28