Loading...
Searching...
No Matches
stdex::stream::memory_file Class Reference

In-memory file. More...

`#include <stdex/stream.hpp>`

Inheritance diagram for stdex::stream::memory_file:

## Public Member Functions

| memory_file (state_t state=state_t::ok)
| memory_file (size_t size, state_t state=state_t::ok)
| Creates an empty file of reserved size.
| memory_file (void *data, size_t size, size_t reserved, bool manage=false, state_t state=state_t::ok)
| Creates a file based on available data.
| memory_file (void *data, size_t size, bool manage=false, state_t state=state_t::ok)
| Creates a file based on available data.
| memory_file (const schar_t *filename, int mode)
| Loads content from file-system file.
| memory_file (const stdex::sstring &filename, int mode)
| Loads content from file-system file.
void | reserve (size_t required, bool tight=false) noexcept
| Reallocates memory.
void | load (const schar_t *filename, int mode)
| Loads content from a file-system file.
void | load (const stdex::sstring &filename, int mode)
| Loads content from a file-system file.
void | save (const schar_t *filename, int mode)
| Saves content to a file-system file.
void | save (const stdex::sstring &filename, int mode)
| Saves content to a file-system file.
const void * | data () const
| Returns pointer to data.
virtual size_t | read (_Out_writes_bytes_to_opt_(length, return) void *data, size_t length)
| Reads block of data from the stream.
template<class T >
memory_file & | read_data (T &data)
| Reads one primitive data type.
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
memory_file & | read_str (std::basic_string< _Elem, _Traits, _Ax > &data)
| Reads length-prefixed string from the stream.
virtual size_t | write (_In_reads_bytes_opt_(length) const void *data, size_t length)
| Writes block of data to the stream.
void | write_byte (uint8_t byte, size_t amount=1)
| Writes a byte of data.
template<class T >
memory_file & | write_data (const T data)
| Writes one primitive data type.
template<class T >
memory_file & | write_str (const T *data)
| Writes string to the stream length-prefixed.
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
memory_file & | write_str (const std::basic_string< _Elem, _Traits, _Ax > &data)
| Writes string to the stream length-prefixed.
size_t | write_stream (basic &stream, size_t amount=SIZE_MAX)
| Writes content of another stream.
virtual void | close ()
| Closes the stream.
virtual fpos_t | seek (foff_t offset, seek_t how=seek_t::beg)
| Seeks to specified relative file position.
virtual fpos_t | tell () const
| Returns absolute file position in file or fpos_max if fails. This method does not update stream state.
virtual fsize_t | size ()
| Returns file size Should the file size cannot be determined, the method returns fsize_max and it does not reset the state to failed.
virtual void | truncate ()
| Sets file size - truncates the remainder of file content from the current file position to the end of file.
void | set (fpos_t offset, const int8_t data)
void | set (fpos_t offset, const int16_t data)
void | set (fpos_t offset, const int32_t data)
void | set (fpos_t offset, const int64_t data)
void | set (fpos_t offset, const uint8_t data)
void | set (fpos_t offset, const uint16_t data)
void | set (fpos_t offset, const uint32_t data)
void | set (fpos_t offset, const uint64_t data)
void | set (fpos_t offset, const float data)
void | set (fpos_t offset, const double data)
void | set (fpos_t offset, const char data)
void | get (fpos_t offset, int8_t &data)
void | get (fpos_t offset, int16_t &data)
void | get (fpos_t offset, int32_t &data)
void | get (fpos_t offset, int64_t &data)
void | get (fpos_t offset, uint8_t &data)
void | get (fpos_t offset, uint16_t &data)
void | get (fpos_t offset, uint32_t &data)
void | get (fpos_t offset, uint64_t &data)
void | get (fpos_t offset, float &data)
void | get (fpos_t offset, double &data)
void | get (fpos_t offset, char &data)
memory_file & | operator<< (const int8_t data)
memory_file & | operator>> (int8_t &data)
memory_file & | operator<< (const int16_t data)
memory_file & | operator>> (int16_t &data)
memory_file & | operator<< (const int32_t data)
memory_file & | operator>> (int32_t &data)
memory_file & | operator<< (const int64_t data)
memory_file & | operator>> (int64_t &data)
memory_file & | operator<< (const uint8_t data)
memory_file & | operator>> (uint8_t &data)
memory_file & | operator<< (const uint16_t data)
memory_file & | operator>> (uint16_t &data)
memory_file & | operator<< (const uint32_t data)
memory_file & | operator>> (uint32_t &data)
memory_file & | operator<< (const uint64_t data)
memory_file & | operator>> (uint64_t &data)
memory_file & | operator<< (const float data)
memory_file & | operator>> (float &data)
memory_file & | operator<< (const double data)
memory_file & | operator>> (double &data)
memory_file & | operator<< (const char data)
memory_file & | operator>> (char &data)
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
memory_file & | operator>> (std::basic_string< _Elem, _Traits, _Ax > &data)
template<class T >
memory_file & | operator<< (const T *data)
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
memory_file & | operator<< (const std::basic_string< _Elem, _Traits, _Ax > &data)
Public Member Functions inherited from stdex::stream::basic_file
virtual std::vector< uint8_t > | read_remainder (size_t max_length=SIZE_MAX)
| Reads and returns remainder of the stream.
fpos_t | seekbeg (fpos_t offset)
| Seeks to absolute file position.
fpos_t | seekcur (foff_t offset)
| Seeks to relative from current file position.
fpos_t | seekend (foff_t offset)
| Seeks to relative from end file position.
virtual void | skip (fsize_t amount)
| Skips given amount of bytes of data on the stream.
virtual void | lock (fpos_t offset, fsize_t length)
| Locks file section for exclusive access.
virtual void | unlock (fpos_t offset, fsize_t length)
| Unlocks file section for exclusive access.
virtual time_point | ctime () const
| Returns file creation time.
virtual time_point | atime () const
| Returns file access time.
virtual time_point | mtime () const
| Returns file modification time.
virtual void | set_ctime (time_point date)
| Sets file create time.
virtual void | set_atime (time_point date)
| Sets file access time.
virtual void | set_mtime (time_point date)
| Sets file modification time.
charset_id | read_charset (charset_id default_charset=charset_id::system)
| Attempts to detect textfile charset based on UTF-32, UTF-16 or UTF-8 BOM.
Public Member Functions inherited from stdex::stream::basic
| basic (state_t state=state_t::ok)
virtual void | flush ()
| Persists volatile element data.
state_t | state () const
| Returns stream state after last operation.
bool | ok () const
| Returns true if the stream state is clean i.e. previous operation was succesful.
uint8_t | read_byte ()
| Reads one byte of data.
void | write_byte (uint8_t byte, fsize_t amount=1)
| Writes a byte of data.
template<class T >
basic & | read_data (T &data)
| Reads one primitive data type.
template<class T >
basic & | write_data (const T data)
| Writes one primitive data type.
template<class _Traits = std::char_traits<char>, class _Ax = std::allocator<char>>
size_t | readln (std::basic_string< char, _Traits, _Ax > &str)
| Reads stream to the end-of-line or end-of-file.
template<class _Traits = std::char_traits<wchar_t>, class _Ax = std::allocator<wchar_t>>
size_t | readln (std::basic_string< wchar_t, _Traits, _Ax > &wstr)
| Reads stream to the end-of-line or end-of-file.
template<class T_from , class T_to , class _Traits = std::char_traits<T_to>, class _Ax = std::allocator<T_to>>
size_t | readln (std::basic_string< T_to, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
| Reads stream to the end-of-line or end-of-file.
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
size_t | readln_and_attach (std::basic_string< _Elem, _Traits, _Ax > &str)
| Reads stream to the end-of-line or end-of-file and append to str.
template<class T_from , class T_to , class _Traits = std::char_traits<T_to>, class _Ax = std::allocator<T_to>>
size_t | readln_and_attach (std::basic_string< T_to, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
| Reads stream to the end-of-line or end-of-file and append to str.
size_t | read_array (_Out_writes_bytes_(size *count) void *array, size_t size, size_t count)
| Reads an array of data from the stream.
size_t | write_array (_In_reads_bytes_opt_(size *count) const void *array, size_t size, size_t count)
| Writes an array of data to the stream.
template<class T_from , class T_to >
size_t | write_array (const T_from *wstr, charset_encoder< T_from, T_to > &encoder)
| Writes array of characters to the stream.
template<class T_from , class T_to >
size_t | write_array (_In_reads_or_z_opt_(num_chars) const T_from *wstr, size_t num_chars, charset_encoder< T_from, T_to > &encoder)
| Writes array of characters to the stream.
template<class T_from , class T_to , class _Traits = std::char_traits<T_from>, class _Ax = std::allocator<T_from>>
size_t | write_array (const std::basic_string< T_from, _Traits, _Ax > &wstr, charset_encoder< T_from, T_to > &encoder)
| Writes array of characters to the stream.
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
basic & | read_str (std::basic_string< _Elem, _Traits, _Ax > &data)
| Reads length-prefixed string from the stream.
template<class T >
basic & | write_str (const T *data)
| Writes string to the stream length-prefixed.
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
basic & | write_str (const std::basic_string< _Elem, _Traits, _Ax > &data)
| Writes string to the stream length-prefixed.
fsize_t | write_stream (basic &stream, fsize_t amount=fsize_max)
| Writes content of another stream.
void | write_charset (charset_id charset)
| Writes UTF8, UTF-16 or UTF-32 byte-order-mark.
size_t | write_sprintf (_Printf_format_string_params_(2) const char *format, locale_t locale,...)
| Writes formatted string to the stream.
size_t | write_sprintf (_Printf_format_string_params_(2) const wchar_t *format, locale_t locale,...)
| Writes formatted string to the stream.
size_t | write_vsprintf (_Printf_format_string_params_(2) const char *format, locale_t locale, va_list params)
| Writes formatted string to the stream.
size_t | write_vsprintf (_Printf_format_string_params_(2) const wchar_t *format, locale_t locale, va_list params)
| Writes formatted string to the stream.
basic & | operator>> (int8_t &data)
basic & | operator<< (const int8_t data)
basic & | operator>> (int16_t &data)
basic & | operator<< (const int16_t data)
basic & | operator>> (int32_t &data)
basic & | operator<< (const int32_t data)
basic & | operator>> (int64_t &data)
basic & | operator<< (const int64_t data)
basic & | operator>> (uint8_t &data)
basic & | operator<< (const uint8_t data)
basic & | operator>> (uint16_t &data)
basic & | operator<< (const uint16_t data)
basic & | operator>> (uint32_t &data)
basic & | operator<< (const uint32_t data)
basic & | operator>> (uint64_t &data)
basic & | operator<< (const uint64_t data)
basic & | operator>> (float &data)
basic & | operator<< (const float data)
basic & | operator>> (double &data)
basic & | operator<< (const double data)
basic & | operator>> (char &data)
basic & | operator<< (const char data)
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
basic & | operator>> (std::basic_string< _Elem, _Traits, _Ax > &data)
template<class T >
basic & | operator<< (const T *data)
template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>
basic & | operator<< (const std::basic_string< _Elem, _Traits, _Ax > &data)
template<class _Ty , class _Alloc = std::allocator<_Ty>>
basic & | operator<< (const std::vector< _Ty, _Alloc > &data)
template<class _Ty , class _Alloc = std::allocator<_Ty>>
basic & | operator>> (std::vector< _Ty, _Alloc > &data)
template<class _Kty , class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
basic & | operator<< (const std::set< _Kty, _Pr, _Alloc > &data)
template<class _Kty , class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
basic & | operator>> (std::set< _Kty, _Pr, _Alloc > &data)
template<class _Kty , class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
basic & | operator<< (const std::multiset< _Kty, _Pr, _Alloc > &data)
template<class _Kty , class _Pr = std::less<_Kty>, class _Alloc = std::allocator<_Kty>>
basic & | operator>> (std::multiset< _Kty, _Pr, _Alloc > &data)

## Protected Member Functions

template<class T >
void | set (fpos_t offset, const T data)
| Writes data to specified file location This does not move file pointer nor update file size. It checks for reserved space Assume-only (in Debug builds). Use with caution!
template<class T >
void | get (fpos_t offset, T &data)
| Reads data from specified file location This does not move file pointer. It checks for data size Assume-only (in Debug builds). Use with caution!

## Protected Attributes

uint8_t * | m_data
| file data
bool | m_manage
| may reallocate m_data?
size_t | m_offset
| file pointer
size_t | m_size
| file size
size_t | m_reserved
| reserved file size
Protected Attributes inherited from stdex::stream::basic
state_t | m_state

## Detailed Description

In-memory file.

## Constructor & Destructor Documentation

## memory_file() [1/5]

stdex::stream::memory_file::memory_file | ( | size_t | size,
|  | state_t | state = `state_t::ok`
| ) |  |

inline

Creates an empty file of reserved size.

Parameters

  [in] | size | Reserved size
  [in] | state | Initial stream state

## memory_file() [2/5]

stdex::stream::memory_file::memory_file | ( | void * | data,
|  | size_t | size,
|  | size_t | reserved,
|  | bool | manage = `false`,
|  | state_t | state = `state_t::ok`
| ) |  |

inline

Creates a file based on available data.

Parameters

  [in] | data | Pointer to data
  [in] | size | Valid data size
  [in] | reserved | Reserved data size
  [in] | manage | Is input data allocated using malloc() and this class may reallocate data?
  [in] | state | Initial stream state

## memory_file() [3/5]

stdex::stream::memory_file::memory_file | ( | void * | data,
|  | size_t | size,
|  | bool | manage = `false`,
|  | state_t | state = `state_t::ok`
| ) |  |

inline

Creates a file based on available data.

Parameters

  [in] | data | Pointer to data
  [in] | size | Valid and reserved data size
  [in] | manage | Is input data allocated using malloc() and this class may reallocate data?
  [in] | state | Initial stream state

## memory_file() [4/5]

stdex::stream::memory_file::memory_file | ( | const schar_t * | filename,
|  | int | mode
| ) |  |

inline

Loads content from file-system file.

Parameters

  [in] | filename | Filename
  [in] | mode | Bitwise combination of mode_t flags

## memory_file() [5/5]

stdex::stream::memory_file::memory_file | ( | const stdex::sstring & | filename,
|  | int | mode
| ) |  |

inline

Loads content from file-system file.

Parameters

  [in] | filename | Filename
  [in] | mode | Bitwise combination of mode_t flags

## Member Function Documentation

## close()

virtual void stdex::stream::memory_file::close | ( |  | ) |

inlinevirtual

Closes the stream.

Reimplemented from stdex::stream::basic.

## get()

template<class T >

void stdex::stream::memory_file::get | ( | fpos_t | offset,
|  | T & | data
| ) |  |

inlineprotected

Reads data from specified file location This does not move file pointer. It checks for data size Assume-only (in Debug builds). Use with caution!

Parameters

  [in] | offset | Offset in file where to write data
  [in] | data | Data to write

## load() [1/2]

void stdex::stream::memory_file::load | ( | const schar_t * | filename,
|  | int | mode
| ) |  |

inline

Loads content from a file-system file.

Parameters

  [in] | filename | Filename
  [in] | mode | Bitwise combination of mode_t flags

## load() [2/2]

void stdex::stream::memory_file::load | ( | const stdex::sstring & | filename,
|  | int | mode
| ) |  |

inline

Loads content from a file-system file.

Parameters

  [in] | filename | Filename
  [in] | mode | Bitwise combination of mode_t flags

## read()

virtual size_t stdex::stream::memory_file::read | ( | _Out_writes_bytes_to_opt_(length, return) void * | data,
|  | size_t | length
| ) |  |

inlinevirtual

Reads block of data from the stream.

Parameters

  [out] | data | Buffer to store read data
  [in] | length | Byte limit of data to read

Returns
  Number of bytes succesfully read. On EOF, 0 is returned and stream state is set to state_t::eof. On error, 0 is returned and stream state is set to state_t::fail. On null reads (length == 0), 0 is returned and stream state is set to state_t::ok.

Reimplemented from stdex::stream::basic.

## read_data()

template<class T >

memory_file & stdex::stream::memory_file::read_data | ( | T & | data | ) |

inline

Reads one primitive data type.

This method is intended for chaining: e.g. stream.read_str(a).read_str(b).read_str(c)... Since it would make it impossible to detect if any of the read_str(a) or read_str(b) failed should read_str(c) succeed, the method skips reading if stream state is not ok.

As memory read rarely fails, a #define CHECK_STREAM_STATE 0 turns this checking off when performance is paramount.

Parameters

  [in] | data | Where to store read data

Returns
  This stream

## read_str()

template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>

memory_file & stdex::stream::memory_file::read_str | ( | std::basic_string< _Elem, _Traits, _Ax > & | data | ) |

inline

Reads length-prefixed string from the stream.

This method is intended for chaining: e.g. stream.read_str(a).read_str(b).read_str(c)... Since it would make it impossible to detect if any of the read_str(a) or read_str(b) failed should read_str(c) succeed, the method skips reading if stream state is not ok.

As memory read rarely fails, a #define CHECK_STREAM_STATE 0 turns this checking off when performance is paramount.

Parameters

  [in] | data | String to read to

Returns
  This stream

## reserve()

void stdex::stream::memory_file::reserve | ( | size_t | required,
|  | bool | tight = `false`
| ) |  |

inlinenoexcept

Reallocates memory.

Parameters

  [in] | required | Demanded memory size
  [in] | tight | Don't overallocate on grow, release excessive on decrease.

## save() [1/2]

void stdex::stream::memory_file::save | ( | const schar_t * | filename,
|  | int | mode
| ) |  |

inline

Saves content to a file-system file.

Parameters

  [in] | filename | Filename
  [in] | mode | Bitwise combination of mode_t flags

## save() [2/2]

void stdex::stream::memory_file::save | ( | const stdex::sstring & | filename,
|  | int | mode
| ) |  |

inline

Saves content to a file-system file.

Parameters

  [in] | filename | Filename
  [in] | mode | Bitwise combination of mode_t flags

## seek()

virtual fpos_t stdex::stream::memory_file::seek | ( | foff_t | offset,
|  | seek_t | how = `seek_t::beg`
| ) |  |

inlinevirtual

Seeks to specified relative file position.

Returns
  Absolute file position after seek, or fpos_max if seek failed.

Implements stdex::stream::basic_file.

## set()

template<class T >

void stdex::stream::memory_file::set | ( | fpos_t | offset,
|  | const T | data
| ) |  |

inlineprotected

Writes data to specified file location This does not move file pointer nor update file size. It checks for reserved space Assume-only (in Debug builds). Use with caution!

Parameters

  [in] | offset | Offset in file where to write data
  [in] | data | Data to write

## size()

virtual fsize_t stdex::stream::memory_file::size | ( |  | ) |

inlinevirtual

Returns file size Should the file size cannot be determined, the method returns fsize_max and it does not reset the state to failed.

Implements stdex::stream::basic_file.

## tell()

virtual fpos_t stdex::stream::memory_file::tell | ( |  | ) | const

inlinevirtual

Returns absolute file position in file or fpos_max if fails. This method does not update stream state.

Returns
  Absolute file position or fpos_max if position cannot be determined.

Implements stdex::stream::basic_file.

## truncate()

virtual void stdex::stream::memory_file::truncate | ( |  | ) |

inlinevirtual

Sets file size - truncates the remainder of file content from the current file position to the end of file.

Implements stdex::stream::basic_file.

## write()

virtual size_t stdex::stream::memory_file::write | ( | _In_reads_bytes_opt_(length) const void * | data,
|  | size_t | length
| ) |  |

inlinevirtual

Writes block of data to the stream.

Parameters

  [in] | data | Buffer to write data from
  [in] | length | Number of bytes to write

Returns
  Number of bytes succesfully written. On error, stream state is set to state_t::fail.

Reimplemented from stdex::stream::basic.

## write_data()

template<class T >

memory_file & stdex::stream::memory_file::write_data | ( | const T | data | ) |

inline

Writes one primitive data type.

This method is intended for chaining: e.g. stream.write_data(a).write_data(b).write_data(c)... Since it would make it impossible to detect if any of the write_data(a) or write_data(b) failed should write_data(c) succeed, the method skips writing if stream state is not ok.

As memory write rarely fails, a #define CHECK_STREAM_STATE 0 turns this checking off when performance is paramount.

Parameters

  [in] | data | Data to write

Returns
  This stream

## write_str() [1/2]

template<class _Elem , class _Traits = std::char_traits<_Elem>, class _Ax = std::allocator<_Elem>>

memory_file & stdex::stream::memory_file::write_str | ( | const std::basic_string< _Elem, _Traits, _Ax > & | data | ) |

inline

Writes string to the stream length-prefixed.

This method is intended for chaining: e.g. stream.write_str(a).write_str(b).write_str(c)... Since it would make it impossible to detect if any of the write_str(a) or write_str(b) failed should write_str(c) succeed, the method skips writing if stream state is not ok.

As memory write rarely fails, a #define CHECK_STREAM_STATE 0 turns this checking off when performance is paramount.

Parameters

  [in] | data | String to write

Returns
  This stream

## write_str() [2/2]

template<class T >

memory_file & stdex::stream::memory_file::write_str | ( | const T * | data | ) |

inline

Writes string to the stream length-prefixed.

This method is intended for chaining: e.g. stream.write_str(a).write_str(b).write_str(c)... Since it would make it impossible to detect if any of the write_str(a) or write_str(b) failed should write_str(c) succeed, the method skips writing if stream state is not ok.

As memory write rarely fails, a #define CHECK_STREAM_STATE 0 turns this checking off when performance is paramount.

Parameters

  [in] | data | String to write

Returns
  This stream

## write_stream()

size_t stdex::stream::memory_file::write_stream | ( | basic & | stream,
|  | size_t | amount = `SIZE_MAX`
| ) |  |

inline

Writes content of another stream.

Returns
  Number of bytes written

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

- include/stdex/stream.hpp