DDS  ver. 3.4
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  std::ios_base::fmtflags flags(_ostream.flags());
92  _ostream << "0x" << std::right << std::setw(8) << std::setfill('0') << std::hex << std::uppercase
93  << (nRaw * m_nElementsInRaw) << " | " << std::left << std::setw(m_nElementsInRaw * 3)
94  << std::setfill(' ') << _ssHex.str() << " | " << std::left << std::setw(m_nElementsInRaw)
95  << _ssTxt.str() << '\n';
96  ++nRaw;
97  _ostream.flags(flags);
98  }
99 
100  private:
101  const size_t m_nElementsInRaw;
102  const _T& m_Container;
103  };
104 
106 }; // namespace MiscCommon
107 
108 #endif /*HEXVIEW_H_*/
CHexView(const _T &_Val, size_t _nElementsInRaw=16)
Definition: HexView.h:50
CHexView< BYTEVector_t > BYTEVectorHexView_t
Definition: HexView.h:105
This class helps to represent a given container'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
friend std::ostream & operator<<(std::ostream &_ostream, const CHexView< _T > &_this)
Definition: HexView.h:56
Miscellaneous functions and helpers are located here.
Definition: BOOST_FILESYSTEM.h:21