DDS  ver. 1.6
HexView.h
Go to the documentation of this file.
1 // Copyright 2014 GSI, Inc. All rights reserved.
2 //
3 // Helps to represent data as in a HEX viewer
4 //
5 #ifndef HEXVIEW_H_
6 #define HEXVIEW_H_
7 
8 // STD
9 #include <iomanip>
10 // MiscCommon
11 #include "def.h"
12 
13 namespace MiscCommon
14 {
46  template <class _T>
47  class CHexView
48  {
49  public:
50  CHexView(const _T& _Val, size_t _nElementsInRaw = 16)
51  : m_nElementsInRaw(_nElementsInRaw)
52  , m_Container(_Val)
53  {
54  }
55 
56  friend std::ostream& operator<<(std::ostream& _ostream, const CHexView<_T>& _this)
57  {
58  std::stringstream ssHex;
59  ssHex << std::hex << std::uppercase;
60  std::stringstream ssTxt;
61  size_t nCount = 0;
62  BYTEVector_t::const_iterator iter = _this.m_Container.begin();
63  BYTEVector_t::const_iterator iter_end = _this.m_Container.end();
64  for (; iter != iter_end; ++iter)
65  {
66  ssHex << std::setw(2) << std::setfill('0') << (static_cast<unsigned int>(*iter)) << ' ';
67  ssTxt << (isprint(*iter) ? static_cast<char>(*iter) : '.');
68  ++nCount;
69  if (0 == (nCount % _this.m_nElementsInRaw))
70  {
71  _this.Print(_ostream, ssHex, ssTxt, nCount);
72  ssHex.str("");
73  ssTxt.str("");
74  }
75  }
76  if (!ssHex.str().empty())
77  _this.Print(_ostream, ssHex, ssTxt, nCount);
78 
79  return _ostream;
80  }
81 
82  private:
83  void Print(std::ostream& _ostream,
84  const std::stringstream& _ssHex,
85  const std::stringstream& _ssTxt,
86  size_t _nCount) const
87  {
88  static size_t nRaw = 0;
89  if (_nCount <= m_nElementsInRaw)
90  nRaw = 0;
91  _ostream << "0x" << std::right << std::setw(8) << std::setfill('0') << std::hex << std::uppercase
92  << (nRaw * m_nElementsInRaw) << " | " << std::left << std::setw(m_nElementsInRaw * 3)
93  << std::setfill(' ') << _ssHex.str() << " | " << std::left << std::setw(m_nElementsInRaw)
94  << _ssTxt.str() << '\n';
95  ++nRaw;
96  }
97 
98  private:
99  const size_t m_nElementsInRaw;
100  const _T& m_Container;
101  };
102 
104 };
105 
106 #endif /*HEXVIEW_H_*/
CHexView(const _T &_Val, size_t _nElementsInRaw=16)
Definition: HexView.h:50
CHexView< BYTEVector_t > BYTEVectorHexView_t
Definition: HexView.h:103
This class helps to represent a given container&#39;s data as in a HEX viewer.
Definition: HexView.h:47
#define _T(s)
Use TCHAR instead of char or wchar_t. It will be appropriately translated.
Definition: def.h:85
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21