|
- #pragma once
-
- #include <iomanip>
-
- #include "hexdump.h"
-
- namespace cppcore
- {
-
- /* hexdump */
-
- hexdump::hexdump(
- const void * p_data,
- size_t p_len,
- size_t p_offset,
- size_t p_split,
- size_t p_newline)
- : data (p_data)
- , len (p_len)
- , offset (p_offset)
- , split (p_split)
- , newline (p_newline)
- { }
-
- void hexdump::print(std::ostream& os) const
- {
- using namespace ::std;
-
- const uint8_t * p = static_cast<const uint8_t*>(data);
- size_t i = 0;
- size_t l = 0;
- std::string s;
-
- while (i < len)
- {
- if (i % newline == 0)
- {
- if (i > 0)
- os << endl;
- os << hex << setw(8) << setfill('0') << (offset + i);
- }
-
- if (l % split == 0)
- {
- os.put(' ');
- s.push_back('|');
- }
-
- s.push_back(std::isprint(p[i])
- ? static_cast<char>(p[i])
- : '.');
-
- os << " " << hex << setw(2) << setfill('0') << static_cast<int>(p[i]);
- ++l;
- ++i;
-
- if (i % newline == 0)
- {
- os << " " << s << "|";
- s.clear();
- l = 0;
- }
- }
-
- if (l > 0)
- {
- auto rest = (newline - l);
- auto fill = (rest / split);
- os << std::string(3 * rest + fill, ' ')
- << " " << s << std::string(rest + fill, ' ') << "|";
- }
- }
-
- }
-
- namespace std
- {
-
- /**
- * @brief Write the helper class to stream using the << operator.
- */
- template<typename T_char, typename T_traits>
- inline basic_ostream<T_char, T_traits>& operator<< (
- basic_ostream<T_char, T_traits>& os,
- const cppcore::hexdump& d)
- {
- d.print(os);
- return os;
- }
-
- }
|