25'ten fazla konu seçemezsiniz Konular bir harf veya rakamla başlamalı, kısa çizgiler ('-') içerebilir ve en fazla 35 karakter uzunluğunda olabilir.
 
 
 

90 satır
1.9 KiB

  1. #pragma once
  2. #include <iomanip>
  3. #include "hexdump.h"
  4. namespace cppcore
  5. {
  6. /* hexdump */
  7. hexdump::hexdump(
  8. const void * p_data,
  9. size_t p_len,
  10. size_t p_offset,
  11. size_t p_split,
  12. size_t p_newline)
  13. : data (p_data)
  14. , len (p_len)
  15. , offset (p_offset)
  16. , split (p_split)
  17. , newline (p_newline)
  18. { }
  19. void hexdump::print(std::ostream& os) const
  20. {
  21. using namespace ::std;
  22. const uint8_t * p = static_cast<const uint8_t*>(data);
  23. size_t i = 0;
  24. size_t l = 0;
  25. std::string s;
  26. while (i < len)
  27. {
  28. if (i % newline == 0)
  29. {
  30. os << hex << setw(8) << setfill('0') << (offset + i);
  31. }
  32. if (l % split == 0)
  33. {
  34. os.put(' ');
  35. s.push_back('|');
  36. }
  37. s.push_back(std::isprint(p[i])
  38. ? static_cast<char>(p[i])
  39. : '.');
  40. os << " " << hex << setw(2) << setfill('0') << static_cast<int>(p[i]);
  41. ++l;
  42. ++i;
  43. if (i % newline == 0)
  44. {
  45. os << " " << s << "|" << endl;
  46. s.clear();
  47. l = 0;
  48. }
  49. }
  50. if (l > 0)
  51. {
  52. auto rest = (newline - l);
  53. auto fill = (rest / split);
  54. os << std::string(3 * rest + fill, ' ')
  55. << " " << s << std::string(rest + fill, ' ') << "|";
  56. }
  57. }
  58. }
  59. namespace std
  60. {
  61. /**
  62. * @brief Write the helper class to stream using the << operator.
  63. */
  64. template<typename T_char, typename T_traits>
  65. inline basic_ostream<T_char, T_traits>& operator<< (
  66. basic_ostream<T_char, T_traits>& os,
  67. const cppcore::hexdump& d)
  68. {
  69. d.print(os);
  70. return os;
  71. }
  72. }