TEC
A lightweight C++ library enabling safe, efficient execution in multithreaded and concurrent systems.
Loading...
Searching...
No Matches
tec::MemFile Class Reference

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 voiddata () const
 Returns a const pointer to the buffer's data.
 
voiddata ()
 Returns a mutable pointer to the buffer's data.
 
const charptr (long pos) const
 Returns a const pointer to a specific position in the buffer.
 
charptr (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.
 

Detailed Description

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.

Constructor & Destructor Documentation

◆ MemFile() [1/4]

tec::MemFile::MemFile ( )
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.

Parameters
block_sizeSize of each growth block. Must be greater than 0. Defaults to kDefaultBlockSize.

◆ MemFile() [2/4]

tec::MemFile::MemFile ( size_t  block_size)
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.

Parameters
block_sizeThe initial capacity and block size for allocation.

◆ MemFile() [3/4]

tec::MemFile::MemFile ( const std::string &  s)
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.

Parameters
sThe string to initialize the buffer with.

◆ MemFile() [4/4]

tec::MemFile::MemFile ( const void src,
size_t  len 
)
inline

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.

Parameters
srcPointer to the source data.
lenLength of the data to copy.

Member Function Documentation

◆ as_hex()

std::string tec::MemFile::as_hex ( ) const
inline

Returns a human-readable hex+ASCII dump string.

Each byte is represented by exactly two characters:

  • Printable ASCII characters in the range 0x21–0x7E (! to ~) are shown as space + the character itself.
  • All other bytes (including space 0x20, control characters, DEL, and values ≥ 0x7F) are shown as two uppercase hexadecimal digits.

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.

Returns
std::string A string of length exactly 2 * size(), containing the mixed hex/ASCII representation.
Complexity
O(N) where N = size(), with a very tight constant factor.
Example
const char hello[] = "Hello\x01\x02World\xFF\0";
MemFile data(hello, strlen(hello));
// data contains: 48 65 6C 6C 6F 01 02 57 6F 72 6C 64 FF
std::cout << data.as_hex() << '\n';
// Output: " H e l l o010203 W o r l dFF"
A byte buffer class with stream-like read/write semantics.
Definition tec_memfile.hpp:50
const void * data() const
Returns a const pointer to the buffer's data.
Definition tec_memfile.hpp:247
Note
Space (0x20) is deliberately rendered as hex "20", not as ' ' + ' ', which matches classic hex dump tools and avoids ambiguity.
See also
to_hex_chars()

◆ block_size()

constexpr size_t tec::MemFile::block_size ( ) const
inlineconstexprnoexcept

Returns the block size used for buffer expansion.

Returns
The current block size in number of elements.

◆ capacity()

size_t tec::MemFile::capacity ( ) const
inlinenoexcept

Returns the current capacity of the underlying storage.

Returns
Total number of bytes the buffer can hold without reallocation.

◆ copy_from()

void tec::MemFile::copy_from ( const MemFile src)
inline

Copies data from another MemFile instance.

Rewinds the current position and writes the entire contents of the source MemFile into this one.

Parameters
srcThe source MemFile to copy from.

◆ data() [1/2]

void * tec::MemFile::data ( )
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
Mutable pointer to the data.

◆ data() [2/2]

const void * tec::MemFile::data ( ) const
inline

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.

Returns
Const pointer to the data.

◆ move_from()

void tec::MemFile::move_from ( MemFile &&  src,
size_t  size_to_shrink = 0 
)
inline

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.

Parameters
srcThe rvalue reference to the source MemFile.
size_to_shrinkOptional size to resize the buffer to (default 0, no shrink).

◆ ptr() [1/2]

char * tec::MemFile::ptr ( long  pos)
inline

Returns a mutable pointer to a specific position in the buffer.

No bounds checking is performed; caller must ensure pos is valid.

Parameters
posThe position offset.
Returns
Mutable pointer to the position.

◆ ptr() [2/2]

const char * tec::MemFile::ptr ( long  pos) const
inline

Returns a const pointer to a specific position in the buffer.

No bounds checking is performed; caller must ensure pos is valid.

Parameters
posThe position offset.
Returns
Const pointer to the position.

◆ read()

size_t tec::MemFile::read ( void dst,
size_t  len 
)
inline

Reads data from the buffer starting at the current position.

Does not read past the logical end of data (size_).

Parameters
dstPointer to the destination buffer.
lenMaximum number of elements to read.
Returns
Number of elements actually read. Returns 0 if at end of data or if len == 0 or no data.

◆ seek()

long tec::MemFile::seek ( long  offset,
int  whence 
)
inline

Moves the read/write position relative to a reference point.

Behaves like fseek. Supports SEEK_SET, SEEK_CUR, and SEEK_END.

Parameters
offsetNumber of elements to move (can be negative).
whenceReference point:
  • SEEK_SET: from beginning of buffer
  • SEEK_CUR: from current position
  • SEEK_END: from end of data
Returns
A new read/write position, -1 if seeking before start, -2 if seeking beyond end.

◆ size()

size_t tec::MemFile::size ( ) const
inlinenoexcept

Returns the logical size of the data in the buffer.

This is the amount of data that has been written and is readable.

Returns
Number of bytes currently stored.

◆ str()

const std::string & tec::MemFile::str ( ) const
inlinenoexcept

Returns a const reference to the internal string buffer.

Returns
Const reference to the buffer.

◆ tell()

constexpr long tec::MemFile::tell ( ) const
inlineconstexprnoexcept

Returns the current read/write position.

Returns
The current position as a signed offset from the start.

◆ to_hex_chars()

static constexpr _Char2 tec::MemFile::to_hex_chars ( unsigned char  ch)
inlinestaticconstexprnoexcept

Converts a byte to a 2-character representation suitable for hex dumps.

Returns a 2-element struct containing either:

  • The two hexadecimal digits (uppercase) representing the byte value, or
  • The literal ASCII character preceded by a space, but only for printable ASCII characters in the range 0x21–0x7E (! 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.

Parameters
valueThe byte to convert.
Returns
struct _Char2 A struct with two characters:
  • For printable ASCII (except space): ‘{ ’ ', actual_char }
  • For all other values (including space, control chars, and high bytes):{ '0'..'F', '0'..'F' }`
Examples
to_hex_chars(65); // 'A' → {' ', 'A'}
to_hex_chars(32); // ' ' → {'2', '0'} (space is forced to hex)
to_hex_chars(10); // '\n' → {'0', 'A'}
to_hex_chars(255); // 0xFF → {'F', 'F'}
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
Note
The function is constexpr and inline, making it usable in both runtime and compile-time contexts with zero overhead.
See also
as_hex()

◆ write()

size_t tec::MemFile::write ( const void src,
size_t  len 
)
inline

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.

Parameters
srcPointer to the source data.
lenNumber of elements to write.
Returns
Number of elements actually written. Returns 0 on failure (e.g., out of memory or len == 0).

The documentation for this class was generated from the following file: