Main MRPT website > C++ reference for MRPT 1.9.9
List of all members | Public Types | Public Member Functions | Private Attributes
mrpt::io::CFileStream Class Reference

Detailed Description

This CStream derived class allow using a file as a read/write binary stream, creating it if the file didn't exist.

The default behavior can be change to open as read, write, read and write,... in the constructor.

See also
CStream, CFileInputStream, CFileOutputStrea, CFileGZInputStream

Definition at line 39 of file CFileStream.h.

#include <mrpt/io/CFileStream.h>

Inheritance diagram for mrpt::io::CFileStream:
Inheritance graph

Public Types

enum  TSeekOrigin { sFromBeginning = 0, sFromCurrent = 1, sFromEnd = 2 }
 Used in CStream::Seek. More...
 

Public Member Functions

 CFileStream (const std::string &fileName, TFileOpenModes mode=fomRead|fomWrite)
 Constructor and open a file. More...
 
 CFileStream ()
 Constructor. More...
 
 CFileStream (const CFileStream &)=delete
 
CFileStreamoperator= (const CFileStream &)=delete
 
virtual ~CFileStream ()
 Destructor. More...
 
size_t Read (void *Buffer, size_t Count) override
 Introduces a pure virtual method responsible for reading from the stream. More...
 
size_t Write (const void *Buffer, size_t Count) override
 Introduces a pure virtual method responsible for writing to the stream. More...
 
bool open (const std::string &fileName, TFileOpenModes mode=fomRead|fomWrite)
 Opens the file, returning true on success. More...
 
void close ()
 Closes the file. More...
 
bool fileOpenCorrectly () const
 Returns true if the file was open without errors. More...
 
bool is_open ()
 Returns true if the file was open without errors. More...
 
bool checkEOF ()
 Will be true if EOF has been already reached. More...
 
void clearError ()
 Resets stream error status bits (e.g. More...
 
uint64_t Seek (int64_t off, CStream::TSeekOrigin org=sFromBeginning) override
 Introduces a pure virtual method for moving to a specified position in the streamed resource. More...
 
uint64_t getTotalBytesCount () const override
 Returns the total amount of bytes in the stream. More...
 
uint64_t getPosition () const override
 Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one. More...
 
uint64_t getPositionI ()
 The current Input cursor position, where 0 is the first byte. More...
 
uint64_t getPositionO ()
 The current Input cursor position, where 0 is the first byte. More...
 
bool readLine (std::string &str)
 Reads one string line from the file (until a new-line character) More...
 
virtual size_t ReadBufferImmediate (void *Buffer, size_t Count)
 Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream). More...
 
virtual int printf (const char *fmt,...) MRPT_printf_format_check(2
 Writes a string to the stream in a textual form. More...
 
template<typename CONTAINER_TYPE >
virtual int void printf_vector (const char *fmt, const CONTAINER_TYPE &V, char separator=',')
 Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T. More...
 
bool getline (std::string &out_str)
 Reads from the stream until a '
' character is found ('' characters are ignored). More...
 

Private Attributes

std::fstream m_f
 The actual input file stream. More...
 

Member Enumeration Documentation

◆ TSeekOrigin

Used in CStream::Seek.

Enumerator
sFromBeginning 
sFromCurrent 
sFromEnd 

Definition at line 34 of file io/CStream.h.

Constructor & Destructor Documentation

◆ CFileStream() [1/3]

CFileStream::CFileStream ( const std::string fileName,
TFileOpenModes  mode = fomRead | fomWrite 
)

Constructor and open a file.

Parameters
fileNameThe file to be open in this stream
modeThe open mode: can be an or'd conbination of different values.
Exceptions
std::exceptionOn error creating or accessing the file. By default the file is opened for open and write and created if not found.

Definition at line 32 of file CFileStream.cpp.

References mrpt::io::fomAppend, mrpt::io::fomRead, mrpt::io::fomWrite, and m_f.

◆ CFileStream() [2/3]

CFileStream::CFileStream ( )

Constructor.

Definition at line 28 of file CFileStream.cpp.

◆ CFileStream() [3/3]

mrpt::io::CFileStream::CFileStream ( const CFileStream )
delete

◆ ~CFileStream()

CFileStream::~CFileStream ( )
virtual

Destructor.

Definition at line 83 of file CFileStream.cpp.

References m_f.

Member Function Documentation

◆ checkEOF()

bool CFileStream::checkEOF ( )

Will be true if EOF has been already reached.

Definition at line 204 of file CFileStream.cpp.

References m_f.

◆ clearError()

void CFileStream::clearError ( )

Resets stream error status bits (e.g.

after an EOF)

Definition at line 210 of file CFileStream.cpp.

References m_f.

◆ close()

void CFileStream::close ( )

Closes the file.

Definition at line 79 of file CFileStream.cpp.

References m_f.

◆ fileOpenCorrectly()

bool CFileStream::fileOpenCorrectly ( ) const

Returns true if the file was open without errors.

Definition at line 188 of file CFileStream.cpp.

References m_f.

Referenced by getTotalBytesCount(), and is_open().

◆ getline()

bool CStream::getline ( std::string out_str)
inherited

Reads from the stream until a '
' character is found ('' characters are ignored).

Returns
false on EOF or any other read error.

Definition at line 69 of file CStream.cpp.

◆ getPosition()

uint64_t CFileStream::getPosition ( ) const
overridevirtual

Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the last one.

Implements mrpt::io::CStream.

Definition at line 154 of file CFileStream.cpp.

References m_f.

Referenced by Seek().

◆ getPositionI()

uint64_t CFileStream::getPositionI ( )

The current Input cursor position, where 0 is the first byte.

Definition at line 166 of file CFileStream.cpp.

References m_f.

◆ getPositionO()

uint64_t CFileStream::getPositionO ( )

The current Input cursor position, where 0 is the first byte.

Definition at line 177 of file CFileStream.cpp.

References m_f.

◆ getTotalBytesCount()

uint64_t CFileStream::getTotalBytesCount ( ) const
overridevirtual

Returns the total amount of bytes in the stream.

Implements mrpt::io::CStream.

Definition at line 141 of file CFileStream.cpp.

References fileOpenCorrectly(), and m_f.

◆ is_open()

bool mrpt::io::CFileStream::is_open ( )
inline

Returns true if the file was open without errors.

Definition at line 82 of file CFileStream.h.

References fileOpenCorrectly().

◆ open()

bool CFileStream::open ( const std::string fileName,
TFileOpenModes  mode = fomRead | fomWrite 
)

Opens the file, returning true on success.

Parameters
fileNameThe file to be open in this stream
modeThe open mode: can be an or'd conbination of different values. By default the file is opened for open and write and created if not found.

Definition at line 56 of file CFileStream.cpp.

References mrpt::io::fomAppend, mrpt::io::fomRead, mrpt::io::fomWrite, and m_f.

◆ operator=()

CFileStream& mrpt::io::CFileStream::operator= ( const CFileStream )
delete

◆ printf()

int CStream::printf ( const char *  fmt,
  ... 
)
virtualinherited

Writes a string to the stream in a textual form.

See also
CStdOutStream

Definition at line 30 of file CStream.cpp.

References MRPT_END, MRPT_START, and vsnprintf.

Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel(), and mrpt::io::CStream::printf_vector().

◆ printf_vector()

template<typename CONTAINER_TYPE >
virtual int void mrpt::io::CStream::printf_vector ( const char *  fmt,
const CONTAINER_TYPE &  V,
char  separator = ',' 
)
inlineinherited

Prints a vector in the format [A,B,C,...] using CStream::printf, and the fmt string for each vector element T.

Template Parameters
CONTAINER_TYPEcan be any vector<T>, deque<T> or alike.

Definition at line 104 of file io/CStream.h.

References mrpt::io::CStream::printf().

Referenced by mrpt::hmtslam::CTopLCDetector_GridMatching::computeTopologicalObservationModel().

◆ Read()

size_t CFileStream::Read ( void Buffer,
size_t  Count 
)
overridevirtual

Introduces a pure virtual method responsible for reading from the stream.

Implements mrpt::io::CStream.

Definition at line 88 of file CFileStream.cpp.

References m_f.

◆ ReadBufferImmediate()

virtual size_t mrpt::io::CStream::ReadBufferImmediate ( void Buffer,
size_t  Count 
)
inlinevirtualinherited

Reads a block of bytes from the stream into Buffer, and returns the amound of bytes actually read, without waiting for more extra bytes to arrive (just those already enqued in the stream).

Note that this method will fallback to ReadBuffer() in most CStream classes but in some hardware-related classes.

Exceptions
std::exceptionOn any error, or if ZERO bytes are read.

Reimplemented in mrpt::comms::CInterfaceFTDI.

Definition at line 64 of file io/CStream.h.

References mrpt::io::CStream::Read().

◆ readLine()

bool CFileStream::readLine ( std::string str)

Reads one string line from the file (until a new-line character)

Returns
true if a line has been read, false on EOF or error

Definition at line 192 of file CFileStream.cpp.

References m_f.

◆ Seek()

uint64_t CFileStream::Seek ( int64_t  Offset,
CStream::TSeekOrigin  Origin = sFromBeginning 
)
overridevirtual

Introduces a pure virtual method for moving to a specified position in the streamed resource.

he Origin parameter indicates how to interpret the Offset parameter. Origin should be one of the following values:

  • sFromBeginning (Default) Offset is from the beginning of the resource. Seek moves to the position Offset. Offset must be >= 0.
  • sFromCurrent Offset is from the current position in the resource. Seek moves to Position + Offset.
  • sFromEnd Offset is from the end of the resource. Offset must be <= 0 to indicate a number of bytes before the end of the file.
    Returns
    Seek returns the new value of the Position property.

Implements mrpt::io::CStream.

Definition at line 112 of file CFileStream.cpp.

References getPosition(), m_f, mrpt::io::CStream::sFromBeginning, mrpt::io::CStream::sFromCurrent, and mrpt::io::CStream::sFromEnd.

◆ Write()

size_t CFileStream::Write ( const void Buffer,
size_t  Count 
)
overridevirtual

Introduces a pure virtual method responsible for writing to the stream.

Write attempts to write up to Count bytes to Buffer, and returns the number of bytes actually written.

Implements mrpt::io::CStream.

Definition at line 99 of file CFileStream.cpp.

References m_f.

Member Data Documentation

◆ m_f

std::fstream mrpt::io::CFileStream::m_f
private



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019