|
TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
|
A byte buffer class with stream-like read/write semantics. More...
#include <tec_memfile.hpp>
Classes | |
| struct | _Char2 |
Public Member Functions | |
| MemFile () | |
| Constructs a buffer with the specified block size. | |
| MemFile (size_t block_size) | |
| Constructs a MemFile with a specified block size for preallocation. | |
| MemFile (const std::string &s) | |
| Constructs a MemFile from a std::string. | |
| MemFile (const void *src, size_t len) | |
| Constructs a MemFile from a raw memory buffer. | |
| void | copy_from (const MemFile &src) |
| Copies data from another MemFile instance. | |
| void | move_from (MemFile &&src, size_t size_to_shrink=0) |
| Moves data from another MemFile instance. | |
| const std::string & | str () const noexcept |
| Returns a const reference to the internal string buffer. | |
| const void * | data () const |
| Returns a const pointer to the buffer's data. | |
| void * | data () |
| Returns a mutable pointer to the buffer's data. | |
| const char * | ptr (long pos) const |
| Returns a const pointer to a specific position in the buffer. | |
| char * | ptr (long pos) |
| Returns a mutable pointer to a specific position in the buffer. | |
| constexpr size_t | block_size () const noexcept |
| Returns the block size used for buffer expansion. | |
| size_t | size () const noexcept |
| Returns the logical size of the data in the buffer. | |
| size_t | capacity () const noexcept |
| Returns the current capacity of the underlying storage. | |
| constexpr long | tell () const noexcept |
| Returns the current read/write position. | |
| constexpr void | rewind () noexcept |
| Resets the read/write position to the beginning of the buffer. | |
| long | seek (long offset, int whence) |
| Moves the read/write position relative to a reference point. | |
| void | resize (size_t len) |
| size_t | write (const void *src, size_t len) |
| Writes data into the buffer at the current position. | |
| size_t | read (void *dst, size_t len) |
| Reads data from the buffer starting at the current position. | |
| std::string | as_hex () const |
| Returns a human-readable hex+ASCII dump string. | |
Static Public Member Functions | |
| static constexpr _Char2 | to_hex_chars (unsigned char ch) noexcept |
| Converts a byte to a 2-character representation suitable for hex dumps. | |
Static Public Attributes | |
| static constexpr size_t | kDefaultBlockSize {BUFSIZ} |
Default block size for buffer expansion, matches BUFSIZ from <stdio.h>. Usually 8192 bytes. | |
A byte buffer class with stream-like read/write semantics.
The MemFile class provides a dynamically resizing buffer for sequential reading and writing of elements of type char. It mimics the behavior of file streams using seek, tell, read, and write operations. The buffer grows in blocks of a configurable size to minimize reallocations.
|
inline |
Constructs a buffer with the specified block size.
The initial capacity is set to one block. The buffer starts empty with position at 0.
| block_size | Size of each growth block. Must be greater than 0. Defaults to kDefaultBlockSize. |
|
inlineexplicit |
Constructs a MemFile with a specified block size for preallocation.
Initializes the internal buffer with the given block size as capacity, but sets the initial size to 0. This allows for efficient appending without frequent reallocations.
| block_size | The initial capacity and block size for allocation. |
|
inlineexplicit |
Constructs a MemFile from a std::string.
Uses a default block size for allocation and writes the contents of the provided string into the buffer.
| s | The string to initialize the buffer with. |
Constructs a MemFile from a raw memory buffer.
Uses a default block size for allocation and writes the specified length of data from the source pointer into the buffer.
| src | Pointer to the source data. |
| len | Length of the data to copy. |
|
inline |
Returns a human-readable hex+ASCII dump string.
Each byte is represented by exactly two characters:
! to ~) are shown as space + the character itself.This format is extremely useful for debugging binary data, network packets, or memory contents, because it makes text-like regions immediately recognizable while still showing exact byte values.
2 * size(), containing the mixed hex/ASCII representation.Returns the block size used for buffer expansion.
|
inlinenoexcept |
Returns the current capacity of the underlying storage.
|
inline |
Returns a mutable pointer to the buffer's data.
Assumes the buffer is non-empty; uses at(0) to access, which may throw if empty. This avoids certain compiler warnings related to empty strings.
Returns a const pointer to the buffer's data.
Assumes the buffer is non-empty; uses at(0) to access, which may throw if empty. This avoids certain compiler warnings related to empty strings.
Moves data from another MemFile instance.
Transfers ownership of the buffer and state from the source, optionally shrinking the buffer to a specified size if smaller than the current size.
| src | The rvalue reference to the source MemFile. |
| size_to_shrink | Optional size to resize the buffer to (default 0, no shrink). |
Returns a mutable pointer to a specific position in the buffer.
No bounds checking is performed; caller must ensure pos is valid.
| pos | The position offset. |
Returns a const pointer to a specific position in the buffer.
No bounds checking is performed; caller must ensure pos is valid.
| pos | The position offset. |
Reads data from the buffer starting at the current position.
Does not read past the logical end of data (size_).
| dst | Pointer to the destination buffer. |
| len | Maximum number of elements to read. |
len == 0 or no data. Moves the read/write position relative to a reference point.
Behaves like fseek. Supports SEEK_SET, SEEK_CUR, and SEEK_END.
| offset | Number of elements to move (can be negative). |
| whence | Reference point:
|
|
inlinenoexcept |
Returns the logical size of the data in the buffer.
This is the amount of data that has been written and is readable.
|
inlinenoexcept |
Returns a const reference to the internal string buffer.
Returns the current read/write position.
|
inlinestaticconstexprnoexcept |
Converts a byte to a 2-character representation suitable for hex dumps.
Returns a 2-element struct containing either:
! to ~). Space (0x20) is deliberately treated as non-printable and rendered in hex.This format is commonly used in classic hex dump tools (e.g. hexdump -C, xxd, debug output) where printable characters are shown directly and everything else in hex.
| value | The byte to convert. |
For all other values (including space, control chars, and high bytes):{ '0'..'F', '0'..'F' }`constexpr and inline, making it usable in both runtime and compile-time contexts with zero overhead.Writes data into the buffer at the current position.
The buffer automatically expands if necessary. Expansion occurs in multiples of blk_size_ to reduce reallocations.
| src | Pointer to the source data. |
| len | Number of elements to write. |
len == 0).