C/C++ API Reference
Loading...
Searching...
No Matches
hex_dump.h
1// Copyright 2020 The Pigweed Authors
2//
3// Licensed under the Apache License, Version 2.0 (the "License"); you may not
4// use this file except in compliance with the License. You may obtain a copy of
5// the License at
6//
7// https://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS, WITHOUT
11// WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. See the
12// License for the specific language governing permissions and limitations under
13// the License.
14
15#pragma once
16
17#include <cstdint>
18
19#include "pw_bytes/span.h"
20#include "pw_span/span.h"
21#include "pw_status/status.h"
22
24namespace pw::dump {
25
27
36inline constexpr const size_t kHexAddrStringSize = sizeof(uintptr_t) * 2 + 2;
37
85 public:
86 enum AddressMode {
87 kDisabled = 0,
88 kOffset = 1,
89 kAbsolute = 2,
90 };
91
92 struct Flags {
94 uint8_t bytes_per_line : 8;
95
103 uint8_t group_every : 8;
104
106 bool show_ascii : 1;
107
109 bool show_header : 1;
110
112 AddressMode prefix_mode : 2;
113 };
114
115 Flags flags = {.bytes_per_line = 16,
116 .group_every = 1,
117 .show_ascii = true,
118 .show_header = true,
119 .prefix_mode = AddressMode::kOffset};
120
121 FormattedHexDumper() = default;
123 SetLineBuffer(dest)
124 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
125 }
126 FormattedHexDumper(span<char> dest, Flags config_flags)
127 : flags(config_flags) {
128 SetLineBuffer(dest)
129 .IgnoreError(); // TODO: b/242598609 - Handle Status properly
130 }
131
132 // TODO: b/234892215 - Add iterator support.
133
148
165
192
193 private:
194 Status ValidateBufferSize();
195 Status PrintFormatHeader();
196
197 size_t current_offset_;
198 span<char> dest_;
199 ConstByteSpan source_data_;
200};
201
229Status DumpAddr(span<char> dest, uintptr_t addr);
230inline Status DumpAddr(span<char> dest, const void* ptr) {
231 uintptr_t addr = reinterpret_cast<uintptr_t>(ptr);
232 return DumpAddr(dest, addr);
233}
234
235} // namespace pw::dump
Definition: status.h:86
constexpr void IgnoreError() const
Definition: status.h:223
Definition: hex_dump.h:84
Definition: span_impl.h:235
Status DumpAddr(span< char > dest, uintptr_t addr)
bool show_ascii
Show or hide ascii interpretation of binary data.
Definition: hex_dump.h:106
uint8_t bytes_per_line
Sets the number of source data bytes to print in each formatted line.
Definition: hex_dump.h:94
Status BeginDump(ConstByteSpan data)
constexpr const size_t kHexAddrStringSize
Definition: hex_dump.h:36
Status SetLineBuffer(span< char > dest)
bool show_header
Show descriptive column headers.
Definition: hex_dump.h:109
AddressMode prefix_mode
Prefix each line of the dump with an offset or absolute address.
Definition: hex_dump.h:112
uint8_t group_every
Definition: hex_dump.h:103
Hexdump utilities.
Definition: hex_dump.h:24
Definition: hex_dump.h:92