yaml_util.cpp
Go to the documentation of this file.
00001 // *****************************************************************************
00002 //
00003 // Copyright (c) 2014, Southwest Research Institute® (SwRI®)
00004 // All rights reserved.
00005 //
00006 // Redistribution and use in source and binary forms, with or without
00007 // modification, are permitted provided that the following conditions are met:
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 Southwest Research Institute® (SwRI®) nor the
00014 //       names of its contributors may be used to endorse or promote products
00015 //       derived from 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 <COPYRIGHT HOLDER> BE LIABLE FOR ANY
00021 // DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
00022 // (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES;
00023 // LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND
00024 // ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
00025 // (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
00026 // SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
00027 //
00028 // *****************************************************************************
00029 
00030 #include <swri_yaml_util/yaml_util.h>
00031 
00032 #include <stdio.h>
00033 // C++ standard libraries
00034 #include <sstream>
00035 #include <iomanip>
00036 #ifdef YAMLCPP_OLD_API
00037 #include <fstream>
00038 #endif  // YAMLCPP_OLD_API
00039 
00040 #ifndef YAMLCPP_OLD_API
00041 namespace YAML
00042 {
00043   void operator >> (const YAML::Node& node, float& value)
00044   {
00045     value = node.as<float>();
00046   }
00047 
00048   void operator >> (const YAML::Node& node, double& value)
00049   {
00050     value = node.as<double>();
00051   }
00052 
00053   void operator >> (const YAML::Node& node, bool& value)
00054   {
00055     value = node.as<bool>();
00056   }
00057 
00058   void operator >> (const YAML::Node& node, int16_t& value)
00059   {
00060     value = node.as<int16_t>();
00061   }
00062 
00063   void operator >> (const YAML::Node& node, uint16_t& value)
00064   {
00065     value = node.as<uint16_t>();
00066   }
00067 
00068   void operator >> (const YAML::Node& node, int32_t& value)
00069   {
00070     value = node.as<int32_t>();
00071   }
00072 
00073   void operator >> (const YAML::Node& node, uint32_t& value)
00074   {
00075     value = node.as<uint32_t>();
00076   }
00077 
00078   void operator >> (const YAML::Node& node, int64_t& value)
00079   {
00080     value = node.as<int64_t>();
00081   }
00082   
00083   void operator >> (const YAML::Node& node, uint64_t& value)
00084   {
00085     value = node.as<uint64_t>();
00086   }
00087 
00088   void operator >> (const YAML::Node& node, std::string& value)
00089   {
00090     value = node.as<std::string>();
00091   }
00092 }
00093 #endif  // YAMLCPP_OLD_API
00094 
00095 namespace swri_yaml_util
00096 {
00097   bool LoadFile(const std::string& path, YAML::Node& yaml)
00098   {
00099     try
00100     {
00101       #ifndef YAMLCPP_OLD_API
00102       yaml = YAML::LoadFile(path);
00103       #else
00104       std::ifstream fin(path.c_str());
00105       if (fin.fail())
00106       {
00107         return false;
00108       }
00109       
00110       YAML::Parser parser(fin);
00111       parser.GetNextDocument(yaml);
00112       #endif  // YAMLCPP_OLD_API
00113     }
00114     catch (...)
00115     {
00116       return false;
00117     }
00118     
00119     return true;
00120   }
00121 
00122   bool LoadString(const std::string& input, YAML::Node& yaml)
00123   {
00124     try
00125     {
00126 #ifndef YAMLCPP_OLD_API
00127       yaml = YAML::Load(input);
00128 #else
00129       std::stringstream stream(input);
00130       YAML::Parser parser(stream);
00131       parser.GetNextDocument(yaml);
00132 #endif  // YAMLCPP_OLD_API
00133     }
00134     catch (...)
00135     {
00136       return false;
00137     }
00138     
00139     return true;
00140   }
00141 
00142   bool LoadMap(const std::map<std::string,std::string>& dict, YAML::Node& yaml)
00143   {
00144     std::vector<std::string> items;
00145     
00146     for (std::map<std::string,std::string>::const_iterator iter = dict.begin();
00147          iter != dict.end();
00148          ++iter)
00149     {
00150       if (!iter->first.empty()) {
00151         items.push_back("\"" + iter->first + "\": \"" + iter->second + "\"");
00152       }
00153     }
00154 
00155     std::string input = "{ ";
00156     for (size_t i = 0; i < items.size(); ++i) {
00157       input += items[i];
00158       if (i+1 < items.size()) {
00159         input += ", ";
00160       }
00161     }
00162     input += "}";
00163 
00164     printf("stringified: %s", input.c_str());
00165     
00166     return LoadString(input, yaml);
00167   }
00168   
00169   bool FindValue(const YAML::Node& node, const std::string& name)
00170   {
00171     #ifndef YAMLCPP_OLD_API
00172       return node[name];
00173     #else
00174       return node.FindValue(name);
00175     #endif  // YAMLCPP_OLD_API
00176   }
00177   
00178   std::auto_ptr<YAML::Node> Clone(const YAML::Node& node)
00179   {
00180     #ifndef YAMLCPP_OLD_API
00181       return std::auto_ptr<YAML::Node>(new YAML::Node(YAML::Clone(node)));
00182     #else
00183       return node.Clone();
00184     #endif  // YAMLCPP_OLD_API
00185   }
00186   
00187   std::string ToString(double value, int32_t precision)
00188   {
00189     std::stringstream ss;
00190     ss << std::setprecision(precision) << value;
00191 
00192     return ss.str();
00193   }
00194 }


swri_yaml_util
Author(s): Marc Alban
autogenerated on Tue Oct 3 2017 03:19:46