OS and compiler abstraction

Overview

Header: #include <mrpt/system/os.h>.

Library: [mrpt-system]

// enums

enum mrpt::system::ConsoleBackgroundColor;
enum mrpt::system::ConsoleForegroundColor;
enum mrpt::system::ConsoleTextStyle;

// global functions

void mrpt::system::pause(const std::string& msg = std::string("Press any key to continue..."));
void mrpt::system::clearConsole();
std::string mrpt::system::MRPT_getCompilationDate();
std::string mrpt::system::MRPT_getVersion();
const std::string& mrpt::system::getMRPTLicense();
std::string mrpt::system::find_mrpt_shared_dir();
void mrpt::system::consoleColorAndStyle(ConsoleForegroundColor fg, ConsoleBackgroundColor bg = ConsoleBackgroundColor::DEFAULT, ConsoleTextStyle style = ConsoleTextStyle::REGULAR, bool applyToStdErr = false);

int mrpt::system::executeCommand(
    const std::string& command,
    std::string* output = nullptr,
    const std::string& mode = "r"
    );

bool mrpt::system::launchProcess(const std::string& command);

bool mrpt::system::loadPluginModule(
    const std::string& moduleFileName,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    );

bool mrpt::system::unloadPluginModule(
    const std::string& moduleFileName,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    );

bool mrpt::system::loadPluginModules(
    const std::string& moduleFileNames,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    );

bool mrpt::system::unloadPluginModules(
    const std::string& moduleFileNames,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    );

int mrpt::system::os::sprintf(char* buf, size_t bufSize, const char* format, ...);
int int mrpt::system::os::vsprintf(char* buf, size_t bufSize, const char* format, va_list args);
int mrpt::system::os::vsnprintf(char* buf, size_t bufSize, const char* format, va_list args);
FILE* mrpt::system::os::fopen(const char* fileName, const char* mode);
FILE* mrpt::system::os::fopen(const std::string& fileName, const char* mode);
int mrpt::system::os::fprintf(FILE* fil, const char* format, ...);
int void mrpt::system::os::fclose(FILE* f);
char* mrpt::system::os::strcat(char* dest, size_t destSize, const char* source);
char* mrpt::system::os::strcpy(char* dest, size_t destSize, const char* source);
int mrpt::system::os::_strcmp(const char* str1, const char* str2);
int mrpt::system::os::_strcmpi(const char* str1, const char* str2);
int mrpt::system::os::_strncmp(const char* str, const char* subStr, size_t count);
int mrpt::system::os::_strnicmp(const char* str, const char* subStr, size_t count);
int64_t mrpt::system::os::_strtoll(const char* nptr, char** endptr, int base);
uint64_t mrpt::system::os::_strtoull(const char* nptr, char** endptr, int base);
void mrpt::system::os::memcpy(void* dest, size_t destSize, const void* src, size_t copyCount);
int mrpt::system::os::getch();
bool mrpt::system::os::kbhit();

Global Functions

void mrpt::system::pause(const std::string& msg = std::string("Press any key to continue..."))

Shows the message “Press any key to continue” (or other custom message) to the current standard output and returns when a key is pressed.

void mrpt::system::clearConsole()

Clears the console window.

std::string mrpt::system::MRPT_getCompilationDate()

Returns the MRPT source code timestamp, according to the Reproducible-Builds specifications: https://reproducible-builds.org/specs/source-date-epoch/

std::string mrpt::system::MRPT_getVersion()

Returns a string describing the MRPT version.

const std::string& mrpt::system::getMRPTLicense()

Returns a const ref to a text with the same text that appears at the beginning of each MRPT file (useful for displaying the License text in GUIs)

std::string mrpt::system::find_mrpt_shared_dir()

Finds the “[MRPT]/share/mrpt/” directory, if available in the system.

This searches in (1) source code tree, (2) install target paths.

void mrpt::system::consoleColorAndStyle(
    ConsoleForegroundColor fg,
    ConsoleBackgroundColor bg = ConsoleBackgroundColor::DEFAULT,
    ConsoleTextStyle style = ConsoleTextStyle::REGULAR,
    bool applyToStdErr = false
    )

Changes the text color and style in the console for the text written from now on.

See available colors in ConsoleForegroundColor and ConsoleBackgroundColor.

By default the color of “cout” is changed, unless changeStdErr=true, in which case “cerr” is changed.

GNU/Linux: If stdout/stderr is not a real terminal with color support, calling this function will have no effect (i.e. no escape characters will be emitted).

The current implementation only supports a subset of all colors for Windows terminals.

(New in MRPT 2.3.3)

int mrpt::system::executeCommand(
    const std::string& command,
    std::string* output = nullptr,
    const std::string& mode = "r"
    )

Execute Generic Shell Command.

Original code snippet found in http://stackoverflow.com/a/30357710

Parameters:

command

Command to execute

output

Pointer to string containing the shell output

mode

read/write access

Returns:

0 for success, -1 otherwise.

bool mrpt::system::launchProcess(const std::string& command)

Executes the given command (which may contain a program + arguments), and waits until it finishes.

Returns:

false on any error, true otherwise

bool mrpt::system::loadPluginModule(
    const std::string& moduleFileName,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    )

Loads a dynamically-linked “plug-in” module (Windows: .dll, GNU/Linux: .so).

Useful to register mrpt-rtti -based classes defined in external user code.

Parameters:

moduleFileName

Absolute or relative path to the module file.

outErrorMsgs

If provided, error messages will be saved here. If not, errors will be dumped to std::cerr.

Returns:

true If modules could be loaded without errors. Upon mrpt-system library unloading, all loaded modules will be automatically unloaded too. Manual unload is possible with unloadPluginModule().

bool mrpt::system::unloadPluginModule(
    const std::string& moduleFileName,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    )

Unloads “plug-in” modules loaded with loadPluginModule().

Unloaded is done automatically before program exit even if this is not explicitly called.

Returns:

true if module could be unloaded without errors.

bool mrpt::system::loadPluginModules(
    const std::string& moduleFileNames,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    )

Like loadPluginModule(), but loads a comma (,) separated list of “plug-in” modules.

Returns:

true if all modules could be loaded without errors. Upon mrpt-system library unloading, all loaded modules will be automatically unloaded too. Manual unload is possible with unloadPluginModules().

bool mrpt::system::unloadPluginModules(
    const std::string& moduleFileNames,
    mrpt::optional_ref<std::string> outErrorMsgs = std::nullopt
    )

Unloads “plug-in” modules loaded with loadPluginModules().

Unloaded is done automatically before program exit even if this is not explicitly called.

Returns:

true if all modules could be unloaded without errors.

int mrpt::system::os::sprintf(
    char* buf,
    size_t bufSize,
    const char* format,
    ...
    )

An OS-independent version of sprintf (Notice the bufSize param, which may be ignored in some compilers)

See also:

mrpt::format

int int mrpt::system::os::vsprintf(
    char* buf,
    size_t bufSize,
    const char* format,
    va_list args
    )

An OS-independent version of vsprintf (Notice the bufSize param, which may be ignored in some compilers)

int mrpt::system::os::vsnprintf(
    char* buf,
    size_t bufSize,
    const char* format,
    va_list args
    )

An OS-independent version of vsnprintf (Notice the bufSize param, which may be ignored in some compilers)

FILE* mrpt::system::os::fopen(const char* fileName, const char* mode)

An OS-independent version of fopen.

Returns:

It will always return nullptr on any error.

FILE* mrpt::system::os::fopen(const std::string& fileName, const char* mode)

An OS-independent version of fopen (std::string version)

Returns:

It will always return nullptr on any error.

int mrpt::system::os::fprintf(FILE* fil, const char* format, ...)

An OS-independent version of fprintf.

int void mrpt::system::os::fclose(FILE* f)

An OS-independent version of fclose.

Parameters:

std::exception

On trying to close a nullptr file descriptor.

char* mrpt::system::os::strcat(char* dest, size_t destSize, const char* source)

An OS-independent version of strcat.

Returns:

It will always return the “dest” pointer.

char* mrpt::system::os::strcpy(char* dest, size_t destSize, const char* source)

An OS-independent version of strcpy.

Returns:

It will always return the “dest” pointer.

int mrpt::system::os::_strcmp(const char* str1, const char* str2)

An OS-independent version of strcmp.

Returns:

It will return 0 when both strings are equal, casi sensitive.

int mrpt::system::os::_strcmpi(const char* str1, const char* str2)

An OS-independent version of strcmpi.

Returns:

It will return 0 when both strings are equal, casi insensitive.

int mrpt::system::os::_strncmp(
    const char* str,
    const char* subStr,
    size_t count
    )

An OS-independent version of strncmp.

Returns:

It will return 0 when both strings are equal, casi sensitive.

int mrpt::system::os::_strnicmp(
    const char* str,
    const char* subStr,
    size_t count
    )

An OS-independent version of strnicmp.

Returns:

It will return 0 when both strings are equal, casi insensitive.

int64_t mrpt::system::os::_strtoll(const char* nptr, char** endptr, int base)

An OS-independent version of strtoll.

uint64_t mrpt::system::os::_strtoull(const char* nptr, char** endptr, int base)

An OS-independent version of strtoull.

void mrpt::system::os::memcpy(
    void* dest,
    size_t destSize,
    const void* src,
    size_t copyCount
    )

An OS and compiler independent version of “memcpy”.

int mrpt::system::os::getch()

An OS-independent version of getch, which waits until a key is pushed.

Returns:

The pushed key code

bool mrpt::system::os::kbhit()

An OS-independent version of kbhit, which returns true if a key has been pushed.