helper.cpp
Go to the documentation of this file.
00001 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
00002 // -- BEGIN LICENSE BLOCK ----------------------------------------------
00003 // This file is part of the SCHUNK Canopen Driver suite.
00004 //
00005 // This program is free software licensed under the LGPL
00006 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
00007 // You can find a copy of this license in LICENSE folder in the top
00008 // directory of the source code.
00009 //
00010 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
00011 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
00012 // -- END LICENSE BLOCK ------------------------------------------------
00013 //----------------------------------------------------------------------
00021 //----------------------------------------------------------------------
00022 
00023 #include "helper.h"
00024 #include "Logging.h"
00025 
00026 #include <boost/foreach.hpp>
00027 #include <boost/property_tree/ptree.hpp>
00028 #include <boost/property_tree/ini_parser.hpp>
00029 
00030 namespace icl_hardware{
00031 namespace canopen_schunk{
00032 
00033 std::string hexToString(const uint64_t num)
00034 {
00035   std::stringstream ss;
00036   ss << "0x"
00037      << std::hex << std::setw(2) << std::setfill('0')
00038      << static_cast<int>( num );
00039   return ss.str();
00040 }
00041 
00042 std::string hexArrayToString( const unsigned char* msg, const uint8_t length)
00043 {
00044   std::stringstream ss;
00045   for (size_t i=0; i < length; ++i)
00046   {
00047     ss << hexToString (msg[i]) << " ";
00048   }
00049 
00050   return ss.str();
00051 }
00052 
00053 std::string binaryString (const uint64_t num)
00054 {
00055   std::bitset<64> bs(num);
00056   std::stringstream ss;
00057   ss << "0b" << bs;
00058      return ss.str();
00059 }
00060 
00061 
00062 std::string sanitizeString(const std::string& text)
00063 {
00064   std::ostringstream ss;
00065   for (std::string::const_iterator it = text.begin(); it != text.end(); ++it)
00066   {
00067     if (std::isgraph(*it) && *it != '\r' && *it != '\n')
00068     {
00069       ss << *it;
00070     }
00071   }
00072   return ss.str();
00073 }
00074 
00075 std::map< uint32_t, std::string > getErrorMapFromConfigFile(const std::string& filename,
00076                                                             const std::string& category)
00077 {
00078   std::map< uint32_t, std::string > ret_map;
00079 
00080   try
00081   {
00082     boost::property_tree::ptree tree;
00083     boost::property_tree::read_ini(filename, tree);
00084 
00085     BOOST_FOREACH(boost::property_tree::ptree::value_type &v,
00086                   tree.get_child(category))
00087     {
00088 
00089       // The data function is used to access the data stored in a node.
00090       uint32_t key = 0x0;
00091       std::stringstream ss;
00092       ss << std::hex << v.first.data();
00093       ss >> key;
00094       ret_map[key] = v.second.data();
00095     }
00096   }
00097   catch ( const boost::property_tree::ptree_error& e )
00098   {
00099     LOGGING_ERROR(CanOpen, "Error parsing error codes " << filename << ": "
00100                            << e.what()
00101                            << ". These error codes will not be available." << endl);
00102   }
00103 
00104   return ret_map;
00105 }
00106 
00107 }} // End of NS


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Sun May 22 2016 03:30:56