yaml_config_reader.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 #include <sstream>
00032 
00033 #ifdef RVIZ_HAVE_YAMLCPP_05
00034 #include <yaml-cpp/yaml.h>
00035 #else
00036 #include <yaml-cpp/node.h>
00037 #include <yaml-cpp/parser.h>
00038 #endif
00039 
00040 #include "rviz/yaml_config_reader.h"
00041 
00042 namespace rviz
00043 {
00044 
00045 YamlConfigReader::YamlConfigReader()
00046   : error_( false )
00047 {}
00048 
00049 void YamlConfigReader::readFile( Config& config, const QString& filename )
00050 {
00051   std::ifstream in( qPrintable( filename ));
00052   readStream( config, in, filename );
00053 }
00054 
00055 void YamlConfigReader::readString( Config& config, const QString& data, const QString& filename )
00056 {
00057   std::stringstream ss( data.toStdString() );
00058   readStream( config, ss, filename );
00059 }
00060 
00061 void YamlConfigReader::readStream( Config& config, std::istream& in, const QString& filename )
00062 {
00063   try
00064   {
00065     YAML::Node yaml_node;
00066 #ifdef RVIZ_HAVE_YAMLCPP_05
00067     yaml_node = YAML::Load(in);
00068 #else
00069     YAML::Parser parser( in );
00070     parser.GetNextDocument( yaml_node );
00071 #endif
00072     error_ = false;
00073     message_ = "";
00074     readYamlNode( config, yaml_node );
00075   }
00076   catch( YAML::ParserException& ex )
00077   {
00078     message_ = ex.what();
00079     error_ = true;
00080   }
00081 }
00082 
00083 void YamlConfigReader::readYamlNode( Config& config, const YAML::Node& yaml_node )
00084 {
00085   switch( yaml_node.Type() )
00086   {
00087   case YAML::NodeType::Map:
00088   {
00089 #ifdef RVIZ_HAVE_YAMLCPP_05
00090     for( YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
00091 #else
00092     for( YAML::Iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
00093 #endif
00094     {
00095       std::string key;
00096 #ifdef RVIZ_HAVE_YAMLCPP_05
00097       key = it->first.as<std::string>();
00098 #else
00099       it.first() >> key;
00100 #endif
00101       Config child = config.mapMakeChild( QString::fromStdString( key ));
00102 #ifdef RVIZ_HAVE_YAMLCPP_05
00103       readYamlNode( child, it->second );
00104 #else
00105       readYamlNode( child, it.second() );
00106 #endif
00107     }
00108     break;
00109   }
00110   case YAML::NodeType::Sequence:
00111   {
00112 #ifdef RVIZ_HAVE_YAMLCPP_05
00113     for( YAML::const_iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
00114 #else
00115     for( YAML::Iterator it = yaml_node.begin(); it != yaml_node.end(); ++it )
00116 #endif
00117     {
00118       Config child = config.listAppendNew();
00119       readYamlNode( child, *it );
00120     }
00121     break;
00122   }
00123   case YAML::NodeType::Scalar:
00124   {
00125     std::string s;
00126 #ifdef RVIZ_HAVE_YAMLCPP_05
00127     s = yaml_node.as<std::string>();
00128 #else
00129     yaml_node >> s;
00130 #endif
00131     config.setValue( QString::fromStdString( s ));
00132     break;
00133   }
00134   case YAML::NodeType::Null:
00135   default:
00136     break;
00137   }
00138 }
00139 
00140 bool YamlConfigReader::error()
00141 {
00142   return error_;
00143 }
00144 
00145 QString YamlConfigReader::errorMessage()
00146 {
00147   return message_;
00148 }
00149 
00150 } // end namespace rviz


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