MRPT  2.0.4
List of all members | Public Member Functions | Private Attributes
mrpt::io::CTextFileLinesParser Class Reference

Detailed Description

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.

Definition at line 25 of file CTextFileLinesParser.h.

#include <mrpt/io/CTextFileLinesParser.h>

Public Member Functions

 CTextFileLinesParser ()=default
 Default constructor; should call open() at some moment later. More...
 
 CTextFileLinesParser (const std::string &filename)
 Constructor for opening a file. More...
 
 CTextFileLinesParser (std::istream &in)
 Constructor for reading from a generic std::istream. More...
 
void open (const std::string &fil)
 Open a file (an alternative to the constructor with a file name) More...
 
void open (std::istream &in)
 Opens for reading a generic std::istream. More...
 
void close ()
 Close the file (no need to call it normally, the file is closed upon destruction) More...
 
void rewind ()
 Reset the read pointer to the beginning of the file. More...
 
bool getNextLine (std::string &out_str)
 Reads from the file and return the next (non-comment) line, as a std::string. More...
 
bool getNextLine (std::istringstream &buf)
 Reads from the file and stores the next (non-comment) line into the given stream buffer. More...
 
size_t getCurrentLineNumber () const
 Return the line number of the last line returned with getNextLine. More...
 
void enableCommentFilters (bool filter_MATLAB_comments, bool filter_C_comments, bool filter_SH_comments)
 Enable/disable filtering of lines starting with "%", "//" or "#", respectively. More...
 

Private Attributes

std::string m_fileName
 
std::istream * m_in {nullptr}
 Points to either a user-owned object, or to m_my_in. More...
 
std::shared_ptr< std::istream > m_my_in
 
size_t m_curLineNum {0}
 
bool m_filter_MATLAB_comments {true}
 
bool m_filter_C_comments {true}
 
bool m_filter_SH_comments {true}
 

Constructor & Destructor Documentation

◆ CTextFileLinesParser() [1/3]

mrpt::io::CTextFileLinesParser::CTextFileLinesParser ( )
default

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

◆ CTextFileLinesParser() [2/3]

CTextFileLinesParser::CTextFileLinesParser ( const std::string &  filename)
explicit

Constructor for opening a file.

Exceptions
std::exceptionOn error opening file

Definition at line 20 of file CTextFileLinesParser.cpp.

◆ CTextFileLinesParser() [3/3]

CTextFileLinesParser::CTextFileLinesParser ( std::istream &  in)
explicit

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.

Definition at line 25 of file CTextFileLinesParser.cpp.

Member Function Documentation

◆ close()

void CTextFileLinesParser::close ( )

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

Definition at line 48 of file CTextFileLinesParser.cpp.

◆ enableCommentFilters()

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

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

Definition at line 101 of file CTextFileLinesParser.cpp.

Referenced by TEST().

Here is the caller graph for this function:

◆ getCurrentLineNumber()

size_t CTextFileLinesParser::getCurrentLineNumber ( ) const

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

Definition at line 96 of file CTextFileLinesParser.cpp.

Referenced by mrpt::graphs::detail::graph_ops< graph_t >::load_graph_of_poses_from_text_stream(), and TEST().

Here is the caller graph for this function:

◆ getNextLine() [1/2]

bool CTextFileLinesParser::getNextLine ( std::string &  out_str)

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

Returns
false on EOF.

Definition at line 61 of file CTextFileLinesParser.cpp.

Referenced by mrpt::graphs::detail::graph_ops< graph_t >::load_graph_of_poses_from_text_stream(), and TEST().

Here is the caller graph for this function:

◆ getNextLine() [2/2]

bool CTextFileLinesParser::getNextLine ( std::istringstream &  buf)

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

Returns
false on EOF.

Definition at line 73 of file CTextFileLinesParser.cpp.

References ASSERT_, mrpt::system::strStarts(), and mrpt::system::trim().

Here is the call graph for this function:

◆ open() [1/2]

void CTextFileLinesParser::open ( const std::string &  fil)

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

Definition at line 34 of file CTextFileLinesParser.cpp.

References THROW_EXCEPTION_FMT.

◆ open() [2/2]

void CTextFileLinesParser::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.

Definition at line 26 of file CTextFileLinesParser.cpp.

◆ rewind()

void CTextFileLinesParser::rewind ( )

Reset the read pointer to the beginning of the file.

Definition at line 54 of file CTextFileLinesParser.cpp.

Referenced by mrpt::graphs::detail::graph_ops< graph_t >::load_graph_of_poses_from_text_stream().

Here is the caller graph for this function:

Member Data Documentation

◆ m_curLineNum

size_t mrpt::io::CTextFileLinesParser::m_curLineNum {0}
private

Definition at line 83 of file CTextFileLinesParser.h.

◆ m_fileName

std::string mrpt::io::CTextFileLinesParser::m_fileName
private

Definition at line 79 of file CTextFileLinesParser.h.

◆ m_filter_C_comments

bool mrpt::io::CTextFileLinesParser::m_filter_C_comments {true}
private

Definition at line 85 of file CTextFileLinesParser.h.

◆ m_filter_MATLAB_comments

bool mrpt::io::CTextFileLinesParser::m_filter_MATLAB_comments {true}
private

Definition at line 84 of file CTextFileLinesParser.h.

◆ m_filter_SH_comments

bool mrpt::io::CTextFileLinesParser::m_filter_SH_comments {true}
private

Definition at line 86 of file CTextFileLinesParser.h.

◆ m_in

std::istream* mrpt::io::CTextFileLinesParser::m_in {nullptr}
private

Points to either a user-owned object, or to m_my_in.

Definition at line 81 of file CTextFileLinesParser.h.

◆ m_my_in

std::shared_ptr<std::istream> mrpt::io::CTextFileLinesParser::m_my_in
private

Definition at line 82 of file CTextFileLinesParser.h.




Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020