helper.cpp revision 37dc0245523708194fd1a57b410bc35bde73f5fc
1#include "helper.h" 2 3#include <iostream> 4#include <iomanip> 5 6#include <stdlib.h> 7 8#include "term.h" 9 10using namespace std; 11using namespace term::color; 12 13void dump_hex(unsigned char const *data, 14 size_t size, size_t begin, size_t end) { 15 if (end <= begin) { 16 // Nothing to print now. Return directly. 17 return; 18 } 19 20 std::ios_base::fmtflags prev_flags = cout.flags(); 21 22 size_t lower = begin & (~0xfUL); 23 size_t upper = (end & (~0xfUL)) ? end : ((end + 16UL) & (~0xfUL)); 24 25 for (size_t i = lower; i < upper; i += 16) { 26 cout << hex << setfill('0') << setw(8) << right << i << ':'; 27 28 if (i < begin) { 29 cout << dark::magenta(); 30 } 31 32 for (size_t j = i, k = i + 16; j < k; ++j) { 33 if (j == begin) { 34 cout << normal(); 35 } 36 37 if (j == end) { 38 cout << dark::magenta(); 39 } 40 41 if (j < size) { 42 cout << ' ' << hex << setfill('0') << setw(2) << (unsigned)data[j]; 43 } 44 } 45 46 // Only setw affects the next out, other will affect all outputs. So we 47 // should recover the flags. 48 cout.flags( prev_flags ); 49 50 cout << normal() << endl; 51 } 52} 53