|
| 1 | +#ifndef CS61_HEXDUMP_HH |
| 2 | +#define CS61_HEXDUMP_HH |
| 3 | +#include <cstdio> |
| 4 | +#include <cassert> |
| 5 | +#include <cstdlib> |
| 6 | + |
| 7 | +// hexdump(ptr, size) |
| 8 | +// Print a hexdump of the `size` bytes of data starting at `ptr` |
| 9 | +// to the standard output. The hexdump format is: |
| 10 | +// XXXXXXXX BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB BB |CCCCCCCCCCCCCCCC| |
| 11 | +// where XXXXXXXX is the address of the first byte in the line, |
| 12 | +// the BBs are hexadecimal values of those bytes, and the Cs show |
| 13 | +// the printable ASCII characters corresponding to those bytes |
| 14 | +// (`.` is displayed for non-printable characters). |
| 15 | +void hexdump(const void* ptr, size_t size, const char* objname = nullptr); |
| 16 | + |
| 17 | +// hexdump_object(object) |
| 18 | +// Like hexdump(&object, sizeof(object)). |
| 19 | +#define hexdump_object(object) hexdump(&(object), sizeof((object))) |
| 20 | + |
| 21 | +// hexdump_named_object(object) |
| 22 | +// Like hexdump(&object, sizeof(object)), but also print a header with |
| 23 | +// the name of `object`. |
| 24 | +#define hexdump_named_object(object) hexdump(&(object), sizeof((object)), #object) |
| 25 | + |
| 26 | + |
| 27 | +// fhexdump(f, ptr, size) |
| 28 | +// Like `hexdump(ptr, size)`, but print to file `f` rather than standard |
| 29 | +// output. |
| 30 | +void fhexdump(FILE* f, const void* ptr, size_t size, const char* objname = nullptr); |
| 31 | + |
| 32 | +// fhexdump_object(f, object) |
| 33 | +// Like `fhexdump(f, &object, sizeof(object))`. |
| 34 | +#define fhexdump_object(f, object) fhexdump((f), &(object), sizeof((object))) |
| 35 | + |
| 36 | +// fhexdump_named_object(f, object) |
| 37 | +// Like `fhexdump(f, &object, sizeof(object))`. |
| 38 | +#define fhexdump_named_object(f, object) fhexdump((f), &(object), sizeof((object)), #object) |
| 39 | + |
| 40 | + |
| 41 | +// fhexdump_at(f, first_offset, ptr, size) |
| 42 | +// Like fhexdump, but start with offset `first_offset` instead of the |
| 43 | +// address of `ptr`. |
| 44 | +void fhexdump_at(FILE* f, size_t first_offset, const void* ptr, size_t size, |
| 45 | + const char* objname = nullptr); |
| 46 | + |
| 47 | +#endif |
0 commit comments