class mrpt::io::CStream

Python API: mrpt.io.CStream

Overview

This base class is used to provide a unified interface to files,memory buffers,..Please see the derived classes.

This class is largely inspired by Borland VCL “TStream” class.

Apart of the “VCL like” methods, operators “>>” and “<<” have been defined so that simple types (int,bool,char,float,char *,std::string,…) can be directly written and read to and from any CStream easily. Please, it is recomendable to read CSerializable documentation also.

See also:

CFileStream, CMemoryStream,CSerializable

#include <mrpt/io/CStream.h>

class CStream
{
public:
    // enums

    enum TSeekOrigin;

    // construction

    CStream();

    // methods

    virtual size_t Read(void* Buffer, size_t Count) = 0;
    virtual size_t Write(const void* Buffer, size_t Count) = 0;
    virtual size_t ReadBufferImmediate(void* Buffer, size_t Count);
    virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) = 0;
    virtual uint64_t getTotalBytesCount() const = 0;
    virtual uint64_t getPosition() const = 0;
    virtual int printf(const char* fmt, ...);

    template <typename CONTAINER_TYPE>
    int void printf_vector(
        const char* fmt,
        const CONTAINER_TYPE& V,
        char separator = ','
        );

    bool getline(std::string& out_str);
    virtual std::string getStreamDescription() const;
};

// direct descendants

class CClientTCPSocket;
class CInterfaceFTDI;
class CSerialPort;
class MockStream;
class CCompressedInputStream;
class CCompressedOutputStream;
class CFileGZInputStream;
class CFileGZOutputStream;
class CFileInputStream;
class CFileOutputStream;
class CFileStream;
class CMemoryStream;
class CPipeBaseEndPoint;

Construction

CStream()

Constructor.

Methods

virtual size_t Read(void* Buffer, size_t Count) = 0

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

virtual size_t Write(const void* Buffer, size_t Count) = 0

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.

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).

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

Parameters:

std::exception

On any error, or if ZERO bytes are read.

virtual uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin = sFromBeginning) = 0

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.

virtual uint64_t getTotalBytesCount() const = 0

Returns the total amount of bytes in the stream.

virtual uint64_t getPosition() const = 0

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

virtual int printf(const char* fmt, ...)

Writes a string to the stream in a textual form.

See also:

CStdOutStream

template <typename CONTAINER_TYPE>
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.

Parameters:

CONTAINER_TYPE

can be any vector<T>, deque<T> or alike.

bool getline(std::string& out_str)

Reads from the stream until a \n character is found (\r characters are ignored).

Returns:

false on EOF or any other read error.

virtual std::string getStreamDescription() const

Returns a human-friendly description of the stream, e.g.

a filename.