class mrpt::io::CTextFileLinesParser

A class for parsing text files, returning each non-empty and non-comment line, along its line number.

Lines are strip out of leading and trailing whitespaces. By default, lines starting with either “#”, “//” or “%” are skipped as comment lines, unless this behavior is explicitly disabled with enableCommentFilters.

#include <mrpt/io/CTextFileLinesParser.h>

class CTextFileLinesParser
{
public:
    // construction

    CTextFileLinesParser();
    CTextFileLinesParser(const std::string& filename);
    CTextFileLinesParser(std::istream& in);

    //
methods

    void open(const std::string& fil);
    void open(std::istream& in);
    void close();
    void rewind();
    bool getNextLine(std::string& out_str);
    bool getNextLine(std::istringstream& buf);
    size_t getCurrentLineNumber() const;

    void enableCommentFilters(
        bool filter_MATLAB_comments,
        bool filter_C_comments,
        bool filter_SH_comments
        );
};

Construction

CTextFileLinesParser()

Default constructor; should call open() at some moment later.

CTextFileLinesParser(const std::string& filename)

Constructor for opening a file.

Parameters:

std::exception

On error opening file

CTextFileLinesParser(std::istream& in)

Constructor for reading from a generic std::istream.

Note that a reference to the stream is stored in the object, so it’s the user responsibility to make sure the stream is not destroyed before than this object.

Methods

void open(const std::string& fil)

Open a file (an alternative to the constructor with a file name)

void open(std::istream& in)

Opens for reading a generic std::istream.

Note that a reference to the stream is stored in the object, so it’s the user responsibility to make sure the stream is not destroyed before than this object.

void close()

Close the file (no need to call it normally, the file is closed upon destruction)

void rewind()

Reset the read pointer to the beginning of the file.

bool getNextLine(std::string& out_str)

Reads from the file and return the next (non-comment) line, as a std::string.

Returns:

false on EOF.

bool getNextLine(std::istringstream& buf)

Reads from the file and stores the next (non-comment) line into the given stream buffer.

Returns:

false on EOF.

size_t getCurrentLineNumber() const

Return the line number of the last line returned with getNextLine.

void enableCommentFilters(
    bool filter_MATLAB_comments,
    bool filter_C_comments,
    bool filter_SH_comments
    )

Enable/disable filtering of lines starting with “%”, “//” or “#”, respectively.