helper.cpp
Go to the documentation of this file.
1 // this is for emacs file handling -*- mode: c++; indent-tabs-mode: nil -*-
2 // -- BEGIN LICENSE BLOCK ----------------------------------------------
3 // This file is part of the SCHUNK Canopen Driver suite.
4 //
5 // This program is free software licensed under the LGPL
6 // (GNU LESSER GENERAL PUBLIC LICENSE Version 3).
7 // You can find a copy of this license in LICENSE folder in the top
8 // directory of the source code.
9 //
10 // © Copyright 2016 SCHUNK GmbH, Lauffen/Neckar Germany
11 // © Copyright 2016 FZI Forschungszentrum Informatik, Karlsruhe, Germany
12 // -- END LICENSE BLOCK ------------------------------------------------
13 //----------------------------------------------------------------------
21 //----------------------------------------------------------------------
22 
23 #include "helper.h"
24 #include "Logging.h"
25 
26 #include <boost/foreach.hpp>
27 #include <boost/property_tree/ptree.hpp>
28 #include <boost/property_tree/ini_parser.hpp>
29 
30 namespace icl_hardware{
31 namespace canopen_schunk{
32 
33 std::string hexToString(const uint64_t num)
34 {
35  std::stringstream ss;
36  ss << "0x"
37  << std::hex << std::setw(2) << std::setfill('0')
38  << static_cast<int>( num );
39  return ss.str();
40 }
41 
42 std::string hexArrayToString( const unsigned char* msg, const uint8_t length)
43 {
44  std::stringstream ss;
45  for (size_t i=0; i < length; ++i)
46  {
47  ss << hexToString (msg[i]) << " ";
48  }
49 
50  return ss.str();
51 }
52 
53 std::string binaryString (const uint64_t num)
54 {
55  std::bitset<64> bs(num);
56  std::stringstream ss;
57  ss << "0b" << bs;
58  return ss.str();
59 }
60 
61 
62 std::string sanitizeString(const std::string& text)
63 {
64  std::ostringstream ss;
65  for (std::string::const_iterator it = text.begin(); it != text.end(); ++it)
66  {
67  if (std::isgraph(*it) && *it != '\r' && *it != '\n')
68  {
69  ss << *it;
70  }
71  }
72  return ss.str();
73 }
74 
75 std::map< uint32_t, std::string > getErrorMapFromConfigFile(const std::string& filename,
76  const std::string& category)
77 {
78  std::map< uint32_t, std::string > ret_map;
79 
80  try
81  {
82  boost::property_tree::ptree tree;
83  boost::property_tree::read_ini(filename, tree);
84 
85  BOOST_FOREACH(boost::property_tree::ptree::value_type &v,
86  tree.get_child(category))
87  {
88 
89  // The data function is used to access the data stored in a node.
90  uint32_t key = 0x0;
91  std::stringstream ss;
92  ss << std::hex << v.first.data();
93  ss >> key;
94  ret_map[key] = v.second.data();
95  }
96  }
97  catch ( const boost::property_tree::ptree_error& e )
98  {
99  LOGGING_ERROR(CanOpen, "Error parsing error codes " << filename << ": "
100  << e.what()
101  << ". These error codes will not be available." << endl);
102  }
103 
104  return ret_map;
105 }
106 
107 }} // End of NS
unsigned int uint32_t
std::string hexArrayToString(const unsigned char *msg, const uint8_t length)
Transforms an array of unsigned chars into a string of Hex representations of those chars...
Definition: helper.cpp:42
#define LOGGING_ERROR(streamname, arg)
unsigned __int64 uint64_t
unsigned char uint8_t
ThreadStream & endl(ThreadStream &stream)
tree
std::string binaryString(const uint64_t num)
Definition: helper.cpp:53
std::map< uint32_t, std::string > getErrorMapFromConfigFile(const std::string &filename, const std::string &category)
Creates an error map from a given INI file.
Definition: helper.cpp:75
std::string sanitizeString(const std::string &text)
This function removes all non-graphical characters from the given string.
Definition: helper.cpp:62
std::string hexToString(const uint64_t num)
Converts a hexadecimal number into its string representation 0xXX.
Definition: helper.cpp:33


schunk_canopen_driver
Author(s): Felix Mauch , Georg Heppner
autogenerated on Mon Jun 10 2019 15:07:49