BufferPrint.hh

Go to the documentation of this file.
00001 
00019 #ifndef avro_BufferPrint_hh__
00020 #define avro_BufferPrint_hh__
00021 
00022 #include <ctype.h>
00023 #include <iostream>
00024 #include <iomanip>
00025 #include "BufferReader.hh"
00026 
00033 namespace avro {
00034 
00035 namespace detail {
00036 
00045 inline void
00046 hexPrint(std::ostream &os, BufferReader &reader)
00047 {
00048     std::ios_base::fmtflags savedFlags = os.flags();
00049 
00050     char sixteenBytes[16];
00051     int offset = 0;
00052 
00053     os << std::setfill('0');
00054     os << std::hex;
00055 
00056     while(reader.bytesRemaining()) {
00057 
00058         os << std::setw(8) << offset << "  ";
00059 
00060         size_t inBuffer = reader.read(sixteenBytes, sizeof(sixteenBytes));
00061         offset += inBuffer;
00062 
00063         // traverse 8 bytes or inBuffer, whatever is less
00064         size_t cnt = std::min(inBuffer, static_cast<size_t>(8));
00065 
00066         size_t i = 0;
00067         for (; i < cnt; ++i) {
00068             os << std::setw(2);
00069             os << (static_cast<int>(sixteenBytes[i]) & 0xff) << ' ';
00070         }
00071         for (; i < 8; ++i) {
00072             os << "   ";
00073         }
00074         os << ' ';
00075 
00076         // traverse 16 bytes or inBuffer, whatever is less
00077         cnt = std::min(inBuffer, static_cast<size_t>(16));
00078 
00079         for (; i < cnt; ++i) {
00080             os << std::setw(2);
00081             os << (static_cast<int>(sixteenBytes[i]) & 0xff) << ' ';
00082         }
00083         for (; i < 16; ++i) {
00084             os << "   ";
00085         }
00086         os << " |";
00087         for(i = 0; i < inBuffer; ++i) {
00088             os.put(isprint(sixteenBytes[i]) ? sixteenBytes[i] : '.' );
00089         }
00090         os << "|\n";
00091 
00092     }
00093 
00094     // restore flags
00095     os.flags( savedFlags);
00096 }
00097 
00098 } // namespace detail
00099 
00100 } // namespace
00101 
00102 inline
00103 std::ostream& operator<<(std::ostream& os, const avro::OutputBuffer& buffer)
00104 {
00105     avro::BufferReader reader(buffer);
00106     avro::detail::hexPrint(os, reader);
00107     return os;
00108 }
00109 
00110 inline
00111 std::ostream& operator<<(std::ostream& os, const avro::InputBuffer& buffer)
00112 {
00113     avro::BufferReader reader(buffer);
00114     avro::detail::hexPrint(os, reader);
00115     return os;
00116 }
00117 
00118 #endif 
Generated on Thu Sep 2 18:40:54 2010 for Avro C++ by  doxygen 1.6.3