90 constexpr char table[] =
"0123456789ABCDEF";
91 if (0x20 < ch && ch < 0x7F) {
92 return {
' ',
static_cast<char>(ch)};
94 return {table[ch >> 4], table[ch & 0x0F]};
119 size_t calc_required_capacity(
long pos,
size_t len)
const {
120 return buffer_.capacity()
122 + ((pos + len) / blk_size_) * blk_size_;
174 write(s.data(), s.size());
222 blk_size_ = src.blk_size_;
224 buffer_ = std::move(src.buffer_);
225 if (size_to_shrink > 0 && size_to_shrink <
size()) {
226 buffer_.resize(size_to_shrink);
235 const std::string&
str() const noexcept {
249 return &buffer_.at(0);
262 return &buffer_.at(0);
273 const char*
ptr(
long pos)
const {
274 return buffer_.data() + pos;
286 return buffer_.data() + pos;
305 return buffer_.size();
313 return buffer_.capacity();
320 constexpr long tell() const noexcept {
343 long seek(
long offset,
int whence) {
344 long origin = (whence == SEEK_CUR ? pos_ : (whence == SEEK_END ?
size() : 0));
345 size_t new_pos = origin + offset;
349 if( new_pos >
size() ) {
356 void resize(
size_t len) {
357 size_t new_size = pos_ + len;
358 if (new_size >
size()) {
359 if (new_size > buffer_.capacity()) {
361 size_t new_cap = calc_required_capacity(pos_, new_size);
363 buffer_.capacity(), new_cap,
365 buffer_.reserve(new_cap);
367 buffer_.resize(new_size);
382 size_t write(
const void* src,
size_t len) {
386 if (pos_ + len >
size()) {
389 ::memcpy(buffer_.data() + pos_, src, len);
404 size_t read(
void* dst,
size_t len) {
408 if( pos_ + len >
size() ) {
412 if (buffer_.empty()) {
415 ::memcpy(dst, buffer_.data() + pos_, len);
457 std::ostringstream os;
459 for (
unsigned char c: buffer_) {
461 os << ch.c0 << ch.c1;
A byte buffer class with stream-like read/write semantics.
Definition tec_memfile.hpp:50
size_t write(const void *src, size_t len)
Writes data into the buffer at the current position.
Definition tec_memfile.hpp:382
long seek(long offset, int whence)
Moves the read/write position relative to a reference point.
Definition tec_memfile.hpp:343
const char * ptr(long pos) const
Returns a const pointer to a specific position in the buffer.
Definition tec_memfile.hpp:273
void * data()
Returns a mutable pointer to the buffer's data.
Definition tec_memfile.hpp:260
char * ptr(long pos)
Returns a mutable pointer to a specific position in the buffer.
Definition tec_memfile.hpp:285
static constexpr _Char2 to_hex_chars(unsigned char ch) noexcept
Converts a byte to a 2-character representation suitable for hex dumps.
Definition tec_memfile.hpp:89
const void * data() const
Returns a const pointer to the buffer's data.
Definition tec_memfile.hpp:247
constexpr void rewind() noexcept
Resets the read/write position to the beginning of the buffer.
Definition tec_memfile.hpp:327
size_t capacity() const noexcept
Returns the current capacity of the underlying storage.
Definition tec_memfile.hpp:312
constexpr size_t block_size() const noexcept
Returns the block size used for buffer expansion.
Definition tec_memfile.hpp:293
MemFile(const void *src, size_t len)
Constructs a MemFile from a raw memory buffer.
Definition tec_memfile.hpp:186
size_t read(void *dst, size_t len)
Reads data from the buffer starting at the current position.
Definition tec_memfile.hpp:404
MemFile(size_t block_size)
Constructs a MemFile with a specified block size for preallocation.
Definition tec_memfile.hpp:152
constexpr long tell() const noexcept
Returns the current read/write position.
Definition tec_memfile.hpp:320
void move_from(MemFile &&src, size_t size_to_shrink=0)
Moves data from another MemFile instance.
Definition tec_memfile.hpp:221
std::string as_hex() const
Returns a human-readable hex+ASCII dump string.
Definition tec_memfile.hpp:456
size_t size() const noexcept
Returns the logical size of the data in the buffer.
Definition tec_memfile.hpp:304
MemFile(const std::string &s)
Constructs a MemFile from a std::string.
Definition tec_memfile.hpp:168
const std::string & str() const noexcept
Returns a const reference to the internal string buffer.
Definition tec_memfile.hpp:235
void copy_from(const MemFile &src)
Copies data from another MemFile instance.
Definition tec_memfile.hpp:206
MemFile()
Constructs a buffer with the specified block size.
Definition tec_memfile.hpp:135
static constexpr size_t kDefaultBlockSize
Default block size for buffer expansion, matches BUFSIZ from <stdio.h>. Usually 8192 bytes.
Definition tec_memfile.hpp:103
#define TEC_ENTER(name)
Logs an entry message for a named context (e.g., function).
Definition tec_trace.hpp:211
#define TEC_TRACE(...)
Logs a formatted trace message.
Definition tec_trace.hpp:222
Definition tec_memfile.hpp:52
Common definitions and utilities for the tec namespace.
Provides a thread-safe tracing utility for debugging in the tec namespace.