Bitcoin Core  25.99.0
P2P Digital Currency
Public Member Functions | Private Member Functions | Private Attributes | List of all members
CBufferedFile Class Reference

Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from. More...

#include <streams.h>

Public Member Functions

 CBufferedFile (FILE *fileIn, uint64_t nBufSize, uint64_t nRewindIn, int nTypeIn, int nVersionIn)
 
 ~CBufferedFile ()
 
 CBufferedFile (const CBufferedFile &)=delete
 
CBufferedFileoperator= (const CBufferedFile &)=delete
 
int GetVersion () const
 
int GetType () const
 
void fclose ()
 
bool eof () const
 check whether we're at the end of the source file More...
 
void read (Span< std::byte > dst)
 read a number of bytes More...
 
void SkipTo (const uint64_t file_pos)
 Move the read position ahead in the stream to the given position. More...
 
uint64_t GetPos () const
 return the current reading position More...
 
bool SetPos (uint64_t nPos)
 rewind to a given reading position More...
 
bool SetLimit (uint64_t nPos=std::numeric_limits< uint64_t >::max())
 prevent reading beyond a certain position no argument removes the limit More...
 
template<typename T >
CBufferedFileoperator>> (T &&obj)
 
void FindByte (std::byte byte)
 search for a given byte in the stream, and remain positioned on it More...
 

Private Member Functions

bool Fill ()
 read data from the source to fill the buffer More...
 
std::pair< std::byte *, size_t > AdvanceStream (size_t length)
 Advance the stream's read pointer (m_read_pos) by up to 'length' bytes, filling the buffer from the file so that at least one byte is available. More...
 

Private Attributes

const int nType
 
const int nVersion
 
FILE * src
 source file More...
 
uint64_t nSrcPos {0}
 how many bytes have been read from source More...
 
uint64_t m_read_pos {0}
 how many bytes have been read from this More...
 
uint64_t nReadLimit
 up to which position we're allowed to read More...
 
uint64_t nRewind
 how many bytes we guarantee to rewind More...
 
std::vector< std::byte > vchBuf
 the buffer More...
 

Detailed Description

Non-refcounted RAII wrapper around a FILE* that implements a ring buffer to deserialize from.

It guarantees the ability to rewind a given number of bytes.

Will automatically close the file when it goes out of scope if not null. If you need to close the file early, use file.fclose() instead of fclose(file).

Definition at line 582 of file streams.h.

Constructor & Destructor Documentation

◆ CBufferedFile() [1/2]

CBufferedFile::CBufferedFile ( FILE *  fileIn,
uint64_t  nBufSize,
uint64_t  nRewindIn,
int  nTypeIn,
int  nVersionIn 
)
inline

Definition at line 635 of file streams.h.

◆ ~CBufferedFile()

CBufferedFile::~CBufferedFile ( )
inline

Definition at line 643 of file streams.h.

Here is the call graph for this function:

◆ CBufferedFile() [2/2]

CBufferedFile::CBufferedFile ( const CBufferedFile )
delete

Member Function Documentation

◆ AdvanceStream()

std::pair<std::byte*, size_t> CBufferedFile::AdvanceStream ( size_t  length)
inlineprivate

Advance the stream's read pointer (m_read_pos) by up to 'length' bytes, filling the buffer from the file so that at least one byte is available.

Return a pointer to the available buffer data and the number of bytes (which may be less than the requested length) that may be accessed beginning at that pointer.

Definition at line 617 of file streams.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ eof()

bool CBufferedFile::eof ( ) const
inline

check whether we're at the end of the source file

Definition at line 664 of file streams.h.

Here is the caller graph for this function:

◆ fclose()

void CBufferedFile::fclose ( )
inline

Definition at line 655 of file streams.h.

Here is the caller graph for this function:

◆ Fill()

bool CBufferedFile::Fill ( )
inlineprivate

read data from the source to fill the buffer

Definition at line 596 of file streams.h.

Here is the caller graph for this function:

◆ FindByte()

void CBufferedFile::FindByte ( std::byte  byte)
inline

search for a given byte in the stream, and remain positioned on it

Definition at line 724 of file streams.h.

Here is the call graph for this function:
Here is the caller graph for this function:

◆ GetPos()

uint64_t CBufferedFile::GetPos ( ) const
inline

return the current reading position

Definition at line 687 of file streams.h.

Here is the caller graph for this function:

◆ GetType()

int CBufferedFile::GetType ( ) const
inline

Definition at line 653 of file streams.h.

Here is the caller graph for this function:

◆ GetVersion()

int CBufferedFile::GetVersion ( ) const
inline

Definition at line 652 of file streams.h.

Here is the caller graph for this function:

◆ operator=()

CBufferedFile& CBufferedFile::operator= ( const CBufferedFile )
delete

◆ operator>>()

template<typename T >
CBufferedFile& CBufferedFile::operator>> ( T &&  obj)
inline

Definition at line 718 of file streams.h.

Here is the call graph for this function:

◆ read()

void CBufferedFile::read ( Span< std::byte >  dst)
inline

read a number of bytes

Definition at line 669 of file streams.h.

Here is the call graph for this function:

◆ SetLimit()

bool CBufferedFile::SetLimit ( uint64_t  nPos = std::numeric_limits<uint64_t>::max())
inline

prevent reading beyond a certain position no argument removes the limit

Definition at line 710 of file streams.h.

Here is the caller graph for this function:

◆ SetPos()

bool CBufferedFile::SetPos ( uint64_t  nPos)
inline

rewind to a given reading position

Definition at line 692 of file streams.h.

Here is the caller graph for this function:

◆ SkipTo()

void CBufferedFile::SkipTo ( const uint64_t  file_pos)
inline

Move the read position ahead in the stream to the given position.

Use SetPos() to back up in the stream, not SkipTo().

Definition at line 680 of file streams.h.

Here is the call graph for this function:
Here is the caller graph for this function:

Member Data Documentation

◆ m_read_pos

uint64_t CBufferedFile::m_read_pos {0}
private

how many bytes have been read from this

Definition at line 590 of file streams.h.

◆ nReadLimit

uint64_t CBufferedFile::nReadLimit
private

up to which position we're allowed to read

Definition at line 591 of file streams.h.

◆ nRewind

uint64_t CBufferedFile::nRewind
private

how many bytes we guarantee to rewind

Definition at line 592 of file streams.h.

◆ nSrcPos

uint64_t CBufferedFile::nSrcPos {0}
private

how many bytes have been read from source

Definition at line 589 of file streams.h.

◆ nType

const int CBufferedFile::nType
private

Definition at line 585 of file streams.h.

◆ nVersion

const int CBufferedFile::nVersion
private

Definition at line 586 of file streams.h.

◆ src

FILE* CBufferedFile::src
private

source file

Definition at line 588 of file streams.h.

◆ vchBuf

std::vector<std::byte> CBufferedFile::vchBuf
private

the buffer

Definition at line 593 of file streams.h.


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