From d1015b683f1cdd1e7e9679849aa052f6b96863a5 Mon Sep 17 00:00:00 2001 From: Zygo Blaxell Date: Sun, 17 Oct 2021 02:08:22 -0400 Subject: [PATCH] bytevector: add ostream output with hexdump There is a hexdump template in fs. Move hexdump to its own header, then ByteVector can use it too. Signed-off-by: Zygo Blaxell --- include/crucible/bytevector.h | 2 ++ include/crucible/hexdump.h | 36 +++++++++++++++++++++++++++++++++++ lib/bytevector.cc | 8 ++++++++ lib/fs.cc | 25 +----------------------- 4 files changed, 47 insertions(+), 24 deletions(-) create mode 100644 include/crucible/hexdump.h diff --git a/include/crucible/bytevector.h b/include/crucible/bytevector.h index 2e1ac4c..e04788a 100644 --- a/include/crucible/bytevector.h +++ b/include/crucible/bytevector.h @@ -2,6 +2,7 @@ #define _CRUCIBLE_BYTEVECTOR_H_ #include +#include #include #include @@ -66,6 +67,7 @@ namespace crucible { return reinterpret_cast(data()); } + ostream & operator<<(ostream &os, const ByteVector &bv); } #endif // _CRUCIBLE_BYTEVECTOR_H_ diff --git a/include/crucible/hexdump.h b/include/crucible/hexdump.h new file mode 100644 index 0000000..7e32591 --- /dev/null +++ b/include/crucible/hexdump.h @@ -0,0 +1,36 @@ +#ifndef CRUCIBLE_HEXDUMP_H +#define CRUCIBLE_HEXDUMP_H + +#include "crucible/string.h" + +#include + +namespace crucible { + using namespace std; + + template + ostream & + hexdump(ostream &os, const V &v) + { + os << "V { size = " << v.size() << ", data:\n"; + for (size_t i = 0; i < v.size(); i += 8) { + string hex, ascii; + for (size_t j = i; j < i + 8; ++j) { + if (j < v.size()) { + uint8_t c = v[j]; + char buf[8]; + sprintf(buf, "%02x ", c); + hex += buf; + ascii += (c < 32 || c > 126) ? '.' : c; + } else { + hex += " "; + ascii += ' '; + } + } + os << astringprintf("\t%08x %s %s\n", i, hex.c_str(), ascii.c_str()); + } + return os << "}"; + } +}; + +#endif // CRUCIBLE_HEXDUMP_H diff --git a/lib/bytevector.cc b/lib/bytevector.cc index a4482f2..344a2cb 100644 --- a/lib/bytevector.cc +++ b/lib/bytevector.cc @@ -1,6 +1,8 @@ #include "crucible/bytevector.h" #include "crucible/error.h" +#include "crucible/hexdump.h" +#include "crucible/string.h" namespace crucible { using namespace std; @@ -144,4 +146,10 @@ namespace crucible { { return m_ptr.get(); } + + ostream& + operator<<(ostream &os, const ByteVector &bv) { + hexdump(os, bv); + return os; + } } diff --git a/lib/fs.cc b/lib/fs.cc index ab7e46d..cd20c77 100644 --- a/lib/fs.cc +++ b/lib/fs.cc @@ -2,6 +2,7 @@ #include "crucible/error.h" #include "crucible/fd.h" +#include "crucible/hexdump.h" #include "crucible/limits.h" #include "crucible/ntoa.h" #include "crucible/string.h" @@ -866,30 +867,6 @@ namespace crucible { } } - template - ostream & - hexdump(ostream &os, const V &v) - { - os << "V { size = " << v.size() << ", data:\n"; - for (size_t i = 0; i < v.size(); i += 8) { - string hex, ascii; - for (size_t j = i; j < i + 8; ++j) { - if (j < v.size()) { - uint8_t c = v[j]; - char buf[8]; - sprintf(buf, "%02x ", c); - hex += buf; - ascii += (c < 32 || c > 126) ? '.' : c; - } else { - hex += " "; - ascii += ' '; - } - } - os << astringprintf("\t%08x %s %s\n", i, hex.c_str(), ascii.c_str()); - } - return os << "}"; - } - string btrfs_search_type_ntoa(unsigned type) {