This is Unofficial EPICS BASE Doxygen Site
hexDump.cpp
Go to the documentation of this file.
1 
7 #include <iostream>
8 #include <sstream>
9 #include <string>
10 #include <iomanip>
11 #include <algorithm>
12 
13 #include <pv/byteBuffer.h>
14 
15 #define epicsExportSharedSymbols
16 #include <pv/hexDump.h>
17 
18 namespace epics {
19 namespace pvAccess {
20 
21 HexDump::HexDump(const char* buf, size_t len)
22  :buf(buf)
23  ,buflen(len)
24  ,_limit(1024u)
25  ,_groupBy(4u)
26  ,_perLine(16u)
27 {}
28 
30  size_t size, size_t offset)
31  :buf(bb.getBuffer() + bb.getPosition())
32  ,buflen(bb.getRemaining())
33  ,_limit((size_t)-1)
34  ,_groupBy(4u)
35  ,_perLine(16u)
36 {
37  if(offset > buflen)
38  offset = buflen;
39  buf += offset;
40  buflen -= offset;
41  if(buflen > size)
42  buflen = size;
43 }
44 
46 
47 static
48 size_t ilog2(size_t val)
49 {
50  size_t ret = 0;
51  while(val >>= 1)
52  ret++;
53  return ret;
54 }
55 
56 static
57 size_t bits2bytes(size_t val)
58 {
59  // round up to next multiple of 8
60  val -= 1u;
61  val |= 7u;
62  val += 1u;
63  // bits -> bytes
64  val /= 8u;
65  return val;
66 }
67 
69 std::ostream& operator<<(std::ostream& strm, const HexDump& hex)
70 {
71  const size_t len = std::min(hex.buflen, hex._limit);
72  // find address width in hex chars
73  // find bit width, rounded up to 8 bits, divide down to bytes
74  const size_t addrwidth = bits2bytes(ilog2(len))*2u;
75  size_t nlines = len/hex._perLine;
76 
77  if(len%hex._perLine)
78  nlines++;
79 
80  for(size_t l=0; l<nlines; l++)
81  {
82  size_t start = l*hex._perLine;
83  strm<<"0x"<<std::hex<<std::setw(addrwidth)<<std::setfill('0')<<start;
84 
85  // print hex chars
86  for(size_t col=0; col<hex._perLine; col++)
87  {
88  if(col%hex._groupBy == 0) {
89  strm<<' ';
90  }
91  if(start+col < len) {
92  strm<<std::hex<<std::setw(2)<<std::setfill('0')<<unsigned(hex.buf[start+col]&0xff);
93  } else {
94  strm<<" ";
95  }
96  }
97 
98  strm<<' ';
99 
100  // printable ascii
101  for(size_t col=0; col<hex._perLine && start+col<len; col++)
102  {
103  if(col%hex._groupBy == 0) {
104  strm<<' ';
105  }
106  char val = hex.buf[start+col]&0xff;
107  if(val>=' ' && val<='~') {
108  strm<<val;
109  } else {
110  strm<<'.';
111  }
112  }
113 
114  strm<<'\n';
115  }
116 
117  return strm;
118 }
119 
120 }
121 }
epicsShareFunc friend std::ostream & operator<<(std::ostream &strm, const HexDump &hex)
Definition: hexDump.cpp:69
HexDump(const char *buf, size_t len)
Definition: hexDump.cpp:21
#define min(x, y)
Definition: flexdef.h:78
#define epicsShareFunc
Definition: shareLib.h:209
TODO only here because of the Lockable.
Definition: ntaggregate.cpp:16
This class implements a Bytebuffer that is like the java.nio.ByteBuffer.
Definition: byteBuffer.h:233
Definition: tool_lib.h:64