utils.h
Go to the documentation of this file.
1 /******************************************************************************
2  * Copyright (C) 2013-2014 by Alexander Rykovanov *
3  * rykovanov.as@gmail.com *
4  * *
5  * This library is free software; you can redistribute it and/or modify *
6  * it under the terms of the GNU Lesser General Public License as *
7  * published by the Free Software Foundation; version 3 of the License. *
8  * *
9  * This library is distributed in the hope that it will be useful, *
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of *
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the *
12  * GNU Lesser General Public License for more details. *
13  * *
14  * You should have received a copy of the GNU Lesser General Public License *
15  * along with this library; if not, write to the *
16  * Free Software Foundation, Inc., *
17  * 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. *
18  ******************************************************************************/
19 
20 #pragma once
21 
22 #include <iomanip>
23 #include <sstream>
24 #include <vector>
25 
26 namespace OpcUa
27 {
28 
29 inline std::string ToHexDump(const char * buf, std::size_t size)
30 {
31  std::stringstream result;
32  std::stringstream lineEnd;
33  unsigned pos = 0;
34  result << "size: " << size << "(0x" << std::hex << size << ")";
35 
36  while (pos < size)
37  {
38  if ((pos % 16) == 0)
39  {
40  result << std::endl << std::setfill('0') << std::setw(4) << (pos & 0xFFFF);
41  lineEnd.str(std::string());
42  }
43  if ((pos % 8) == 0)
44  {
45  result << " ";
46  lineEnd << " ";
47  }
48 
49  const char c = buf[pos];
50  result << " " << std::setw(2) << (c & 0xFF);
51  lineEnd << ((c > ' ' && c < 0x7f) ? c : '.');
52 
53  if ((pos % 16) == 15)
54  {
55  result << " " << lineEnd.str();
56  }
57  ++pos;
58  }
59 
60  result << std::endl << std::flush;
61  return result.str();
62 }
63 
64 template <typename T> inline std::ostream & ToHexDump(std::ostream & os, const std::vector<T> & buf, std::size_t size)
65 {
66  std::stringstream lineEnd;
67  size = std::min(size, buf.size());
68  unsigned pos = 0;
69  os << "size: " << size << "(0x" << std::hex << size << ")";
70 
71  while (pos < size)
72  {
73  if ((pos % 16) == 0)
74  {
75  os << std::endl << std::setfill('0') << std::setw(4) << (pos & 0xFFFF);
76  lineEnd.str(std::string());
77  }
78  if ((pos % 8) == 0)
79  {
80  os << " ";
81  lineEnd << " ";
82  }
83 
84  const char c = buf[pos];
85  os << " " << std::setw(2) << (c & 0xFF);
86  lineEnd << ((c > ' ' && c < 0x7f) ? c : '.');
87 
88  if ((pos % 16) == 15)
89  {
90  os << " " << lineEnd.str();
91  }
92  ++pos;
93  }
94 
95  os << std::endl;
96  return os;
97 }
98 
99 template <typename T> inline std::ostream & ToHexDump(std::ostream & os, const std::vector<T> & buf)
100 {
101  return ToHexDump(os, buf, buf.size());
102 }
103 
104 inline std::string ToHexDump(const std::vector<char> & buf, std::size_t size)
105 {
106  std::stringstream result;
107  ToHexDump(result, buf, size);
108  return result.str();
109 }
110 
111 inline std::string ToHexDump(const std::vector<char> & buf)
112 {
113  return ToHexDump(buf, buf.size());
114 }
115 
116 
117 }
IntFormatSpec< int, TypeSpec<'x'> > hex(int value)
OStream< ChannelType > & flush(OStream< ChannelType > &os)
Definition: stream.h:147
OPC UA Address space part. GNU LGPL.
std::string ToHexDump(const char *buf, std::size_t size)
Definition: utils.h:29


ros_opcua_impl_freeopcua
Author(s): Denis Štogl
autogenerated on Tue Jan 19 2021 03:12:08