44 char operator()(
unsigned int n ) {
return "0123456789abcdef"[n] ; }
49 char operator()(
char c )
52 const unsigned int n =
static_cast<unsigned char>(c) ;
53 return n >= 32U && n < 127U ? c :
'.' ;
57 template <
unsigned N,
typename Tin,
typename Temit,
typename Ttohex,
typename Ttopr
intable>
58 void hexdump_imp( Tin begin , Tin end , Temit & emitter ,
unsigned int width , Ttohex tohex , Ttoprintable toprintable )
60 width = width < N ? width : N ;
62 char hexbuffer[N*3+2] ;
63 hexbuffer[N*3+1] =
'\0' ;
64 char *
const hexbuffer_end = hexbuffer+width*3 ;
65 *hexbuffer_end =
'\0' ;
66 char * hex = hexbuffer ;
68 char printablebuffer[N+1] ;
69 char *
const printablebuffer_end = printablebuffer + width ;
70 *printablebuffer_end =
'\0' ;
71 char * printable = printablebuffer ;
73 std::size_t line_number = 0U ;
74 std::size_t address = 0U ;
75 for( Tin p = begin ; p != end ; ++p )
77 const unsigned int c =
static_cast<unsigned char>(*p) ;
79 *hex++ = tohex((c>>4)&0xf) ;
80 *hex++ = tohex(c&0xf) ;
81 *printable++ = toprintable(*p) ;
82 if( printable == printablebuffer_end )
84 emitter.emit( address , line_number , width , hexbuffer+1 , hexbuffer_end , printablebuffer ) ;
87 printable = printablebuffer ;
91 if( printable != printablebuffer )
94 for(
char * p = hex ; p != hexbuffer_end ; ++p ) *p =
' ' ;
95 *hexbuffer_end =
' ' ;
97 emitter.emit( address , line_number , printable-printablebuffer , hexbuffer+1 , hex , printablebuffer ) ;
103 explicit hexdump_ostream_emitter( std::ostream & out ,
unsigned int address_width ,
const char * prefix ,
const char * space ,
const char * bar ) :
105 m_address_width(address_width) ,
111 void emit( std::size_t address , std::size_t line_number , std::size_t ,
const char * hex ,
const char * padding ,
const char * printable )
114 << (line_number?
"\n":
"")
116 << std::setfill(
'0') << std::setw(static_cast<int>(m_address_width)) << std::hex << address << std::dec
120 << (line_number?padding:
"")
121 << m_bar << printable ;
123 std::ostream & m_out ;
124 unsigned int m_address_width ;
125 const char * m_prefix ;
126 const char * m_space ;
130 template <
unsigned N,
typename T,
typename Ttohex,
typename Ttopr
intable>
134 const char * prefix ,
const char * space ,
const char * bar ,
136 Ttohex tohex , Ttoprintable toprintable ) :
145 m_toprintable(toprintable)
151 const char * m_prefix ;
152 const char * m_space ;
154 unsigned int m_width ;
156 Ttoprintable m_toprintable ;
159 template <
unsigned N,
typename T,
typename Ttohex,
typename Ttopr
intable>
160 std::ostream & operator<<( std::ostream & stream , const hexdump_streamable<N,T,Ttohex,Ttoprintable> & hd )
163 hexdump_imp<N>( hd.m_begin , hd.m_end , emitter , hd.m_width , hd.m_tohex , hd.m_toprintable ) ;
168 namespace imp = HexdumpImp ;
176 template <
int N,
typename T,
typename Ttohex,
typename Ttopr
intable>
179 unsigned int address_width ,
180 const char * prefix ,
const char * space ,
const char * bar ,
182 Ttohex tohex , Ttoprintable toprintable )
185 imp::hexdump_imp<N>( begin , end , emitter , width , tohex , toprintable ) ;
191 template <
int N,
typename T>
void
192 hex_dump( std::ostream & out , T begin , T end ,
193 unsigned int address_width ,
194 const char * prefix ,
const char * space ,
const char * bar ,
197 hex_dump<N,T,typename imp::hexdump_tohex,typename imp::hexdump_toprintable>( out , begin , end ,
198 address_width , prefix , space , bar , width ,
199 imp::hexdump_tohex() , imp::hexdump_toprintable() ) ;
202 template <
int N,
typename T>
void
203 hex_dump( std::ostream & out , T begin , T end ,
204 unsigned int address_width ,
205 const char * prefix ,
const char * space ,
const char * bar )
207 hex_dump<N,T>( out , begin , end ,
208 address_width , prefix , space , bar ,
212 template <
int N,
typename T>
void
213 hex_dump( std::ostream & out , T begin , T end ,
214 unsigned int address_width )
216 hex_dump<N,T>( out , begin , end , address_width ,
"" ,
" " ,
" | " ) ;
219 template <
int N,
typename T>
void
222 hex_dump<N,T>( out , begin , end , 6 ) ;
227 template <
int N,
typename T>
void
228 hex_dump( std::ostream & out ,
const T & str )
230 hex_dump<N,typename T::const_iterator>( out , str.begin() , str.end() ) ;
239 template <
int N,
typename T,
typename Ttohex,
typename Ttopr
intable> imp::hexdump_streamable<N,T,Ttohex,Ttoprintable>
241 unsigned int address_width ,
242 const char * prefix ,
const char * space ,
const char * bar ,
244 Ttohex tohex , Ttoprintable toprintable )
246 return imp::hexdump_streamable<N,T,Ttohex,Ttoprintable>( begin , end , address_width , prefix , space , bar , width , tohex , toprintable ) ;
251 template <
int N,
typename T> imp::hexdump_streamable<N,T,typename imp::hexdump_tohex,typename imp::hexdump_toprintable>
253 unsigned int address_width ,
254 const char * prefix ,
const char * space ,
const char * bar ,
257 return hexdump<N>( begin , end , address_width , prefix , space , bar , width ,
258 imp::hexdump_tohex() , imp::hexdump_toprintable() ) ;
261 template <
int N,
typename T> imp::hexdump_streamable<N,T,typename imp::hexdump_tohex,typename imp::hexdump_toprintable>
263 unsigned int address_width ,
264 const char * prefix ,
const char * space ,
const char * bar )
266 return hexdump<N>( begin , end , address_width , prefix , space , bar , N ) ;
269 template <
int N,
typename T> imp::hexdump_streamable<N,T,typename imp::hexdump_tohex,typename imp::hexdump_toprintable>
271 unsigned int address_width )
273 return hexdump<N>( begin , end , address_width ,
"" ,
" " ,
" | " ) ;
276 template <
int N,
typename T> imp::hexdump_streamable<N,T,typename imp::hexdump_tohex,typename imp::hexdump_toprintable>
279 return hexdump<N>( begin , end , 6 ) ;
284 template <
int N,
typename T> imp::hexdump_streamable<N,typename T::const_iterator,typename imp::hexdump_tohex, typename imp::hexdump_toprintable>
287 return hexdump<N,typename T::const_iterator>( seq.begin() , seq.end() ) ;
An output adaptor to write to std::ostream.
Nibble-to-hex-digit functor.
Char-to-printable-ascii functor.
void hex_dump(std::ostream &out, T begin, T end, unsigned int address_width, const char *prefix, const char *space, const char *bar, unsigned int width, Ttohex tohex, Ttoprintable toprintable)
Performs a hex dump to the given stream.
A streamable class used by G::hexdump().
imp::hexdump_streamable< N, T, Ttohex, Ttoprintable > hexdump(T begin, T end, unsigned int address_width, const char *prefix, const char *space, const char *bar, unsigned int width, Ttohex tohex, Ttoprintable toprintable)
Returns a streamable object that does a hex dump.