[mrpt-io]

Overview

C++ library for Input-Output (IO) to raw and compressed files.

Library mrpt-io

This library is part of MRPT and can be installed in Debian-based systems with:

sudo apt install libmrpt-io-dev

See: mrpt_from_cmake

Overview

mrpt-io defines mrpt::io::CStream, the byte-stream interface used by the whole of MRPT, and its implementations for files, memory buffers and pipes. Streams only move raw bytes: to read and write objects (observations, maps, poses…) wrap a stream in an mrpt::serialization::CArchive (see [mrpt-serialization]).

Use it to:

  • Read and write files, optionally gzip- or Zstandard-compressed, with transparent detection of the compression format on reading.

  • Serialize data into memory buffers, e.g. to send them over a network.

  • Parse text files line by line, skipping comments.

Key classes and functions

Example: compressed text file

#include <mrpt/io/CCompressedInputStream.h>
#include <mrpt/io/CCompressedOutputStream.h>

// Write a Zstandard-compressed text file:
{
  mrpt::io::CCompressedOutputStream out(
      "log.txt.zst", mrpt::io::OpenMode::TRUNCATE,
      mrpt::io::CompressionOptions(mrpt::io::CompressionType::Zstd, 3));
  out.printf("x=%.3f y=%.3f\n", 1.0, 2.0);
}  // the file is flushed and closed here

// Read it back: the compression format is detected from the file contents.
mrpt::io::CCompressedInputStream in("log.txt.zst");
std::string line;
while (in.getline(line))
{
  std::cout << line << "\n";
}

Examples

  • io_compress_example: gzip compression of files and memory blocks.

  • io_pipes_example: sending serialized objects between threads through an mrpt::io::CPipe.

  • Python: mrpt_io_example.py.

Library contents

// enums

enum mrpt::io::CompressionType;
enum mrpt::io::OpenMode;

// structs

struct mrpt::io::CompressionOptions;
struct mrpt::io::VectorTextFileOptions;

// classes

class mrpt::io::CCompressedInputStream;
class mrpt::io::CCompressedOutputStream;
class mrpt::io::CFileGZInputStream;
class mrpt::io::CFileGZOutputStream;
class mrpt::io::CFileInputStream;
class mrpt::io::CFileOutputStream;
class mrpt::io::CFileStream;
class mrpt::io::CMemoryStream;
class mrpt::io::CPipe;
class mrpt::io::CPipeBaseEndPoint;
class mrpt::io::CPipeReadEndPoint;
class mrpt::io::CStream;
class mrpt::io::CTextFileLinesParser;

// global functions

std::string mrpt::io::lazy_load_absolute_path(const std::string& relativeOrAbsolutePath);
const std::string& mrpt::io::getLazyLoadPathBase();
void mrpt::io::setLazyLoadPathBase(const std::string& path);
void mrpt::io::registerAllClasses_mrpt_io();

Global Functions

std::string mrpt::io::lazy_load_absolute_path(const std::string& relativeOrAbsolutePath)

Makes sure of building an absolute path for the given relative (or possibly absolute) lazy-load object.

const std::string& mrpt::io::getLazyLoadPathBase()

Gets the current path to be used to locate relative lazy-load externally stored objects via lazy_load_absolute_path().

Default is ".".

void mrpt::io::setLazyLoadPathBase(const std::string& path)

Changes the base path to be used to locate relative lazy-load externally stored objects via lazy_load_absolute_path().

void mrpt::io::registerAllClasses_mrpt_io()

Forces manual RTTI registration of all serializable classes in this namespace.

Should never be required to be explicitly called by users, except if building MRPT as a static library.