TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec_dump.hpp
Go to the documentation of this file.
1// Time-stamp: <Last changed 2026-02-20 15:57:00 by magnolia>
2/*----------------------------------------------------------------------
3------------------------------------------------------------------------
4Copyright (c) 2020-2026 The Emacs Cat (https://github.com/olddeuteronomy/tec).
5
6 Licensed under the Apache License, Version 2.0 (the "License");
7 you may not use this file except in compliance with the License.
8 You may obtain a copy of the License at
9
10 http://www.apache.org/licenses/LICENSE-2.0
11
12 Unless required by applicable law or agreed to in writing, software
13 distributed under the License is distributed on an "AS IS" BASIS,
14 WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15 See the License for the specific language governing permissions and
16 limitations under the License.
17------------------------------------------------------------------------
18----------------------------------------------------------------------*/
26#pragma once
27
28#include <cstddef>
29#include <iostream>
30#include <iomanip>
31#include <cctype>
32#include <ostream>
33#include <string>
34#include <string_view>
35
36
37namespace tec {
38
39namespace dump {
40
41inline std::string as_table(std::string_view s) {
42 constexpr size_t bytes_per_line = 32;
43 constexpr size_t chars_per_line = 2 * bytes_per_line;
44 std::ostringstream os;
45 // Header: decimal column numbers, 2 digits, padded with 0
46 os << "offset|";
47 for (size_t i = 0; i < bytes_per_line; i += 2) {
48 os << std::setw(2) << std::setfill('0') << i << " ";
49 }
50 os << '\n';
51 // Separator line.
52 os << "======|";
53 for (size_t i = 0; i < bytes_per_line / 2; ++i) {
54 os << "++--";
55 }
56 // Print bytes as 2 characters.
57 for(size_t n = 0 ; n < s.size() ; n += 2) {
58 if((n % chars_per_line) == 0) {
59 os << "|\n"
60 << std::dec << std::setw(6) << std::setfill('0')
61 << n / 2 << "|";
62 }
63 os << s[n] << s[n+1];
64 }
65 os << "|";
66 return os.str();
67}
68
69} // namespace dump
70
71} // namespace tec
72
73/*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
74*
75* USAGE
76*
77*~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
78
79int main() {
80 const char data[] =
81 "Hello world! This is a dump of the sequence of printable bytes. "
82 "Non-printable bytes are shown in hex: \x01\x02\x03\x04\x05\x06\x07"
83 "\x08\x09\x0A\x0B\x0C\x0D\x0E\x0F\x1A\x1B\x1C\x1D\x1E\x1F"
84 "\xA1\xA2\xA3\xA4\xA5\xF0\xFF"
85 "\x00"
86 ;
87
88 tec::Bytes b(data, strlen(data));
89 std::cout << tec::dump::as_table(b) << "\n";
90 return 0;
91}
92
93OUTPUT:
94
95offset|00 02 04 06 08 10 12 14 16 18 20 22 24 26 28 30
96======|++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--++--|
97000000| H e l l o20 w o r l d !20 T h i s20 i s20 a20 d u m p20 o f20 t|
98000032| h e20 s e q u e n c e20 o f20 p r i n t a b l e20 b y t e s .20|
99000064| N o n - p r i n t a b l e20 b y t e s20 a r e20 s h o w n20 i n|
100000096|20 h e x :200102030405060708090A0B0C0D0E0F1A1B1C1D1E1FA1A2A3A4A5|
101000128|F0FF00|
102
103 *~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~*/