MRPT  2.0.1
CTextFileLinesParser.h
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 #pragma once
10 
11 #include <fstream>
12 #include <iosfwd>
13 #include <memory>
14 #include <string>
15 
16 namespace mrpt::io
17 {
18 /** A class for parsing text files, returning each non-empty and non-comment
19  * line, along its line number. Lines are strip out of leading and trailing
20  * whitespaces. By default, lines starting with either "#", "//" or "%" are
21  * skipped as comment lines, unless this behavior is explicitly disabled with
22  * \a enableCommentFilters.
23  * \ingroup mrpt_io_grp
24  */
26 {
27  public:
28  /** Default constructor; should call \a open() at some moment later. */
29  CTextFileLinesParser() = default;
30  /** Constructor for opening a file \exception std::exception On error
31  * opening file */
32  explicit CTextFileLinesParser(const std::string& filename);
33 
34  /** Constructor for reading from a generic std::istream. Note that a
35  * reference to the stream is stored in the object, so it's the user
36  * responsibility to make sure the stream is not destroyed before than
37  * this object.
38  */
39  explicit CTextFileLinesParser(std::istream& in);
40 
41  /** Open a file (an alternative to the constructor with a file name) */
42  void open(const std::string& fil);
43 
44  /** Opens for reading a generic std::istream. Note that a
45  * reference to the stream is stored in the object, so it's the user
46  * responsibility to make sure the stream is not destroyed before than
47  * this object.
48  */
49  void open(std::istream& in);
50 
51  /** Close the file (no need to call it normally, the file is closed upon
52  * destruction) */
53  void close();
54  /** Reset the read pointer to the beginning of the file */
55  void rewind();
56 
57  /** Reads from the file and return the next (non-comment) line, as a
58  * std::string
59  * \return false on EOF.
60  */
61  bool getNextLine(std::string& out_str);
62 
63  /** Reads from the file and stores the next (non-comment) line into the
64  * given stream buffer.
65  * \return false on EOF.
66  */
67  bool getNextLine(std::istringstream& buf);
68 
69  /** Return the line number of the last line returned with \a getNextLine */
70  size_t getCurrentLineNumber() const;
71 
72  /** Enable/disable filtering of lines starting with "%", "//" or "#",
73  * respectively. */
75  bool filter_MATLAB_comments, bool filter_C_comments,
76  bool filter_SH_comments);
77 
78  private:
79  std::string m_fileName;
80  /** Points to either a user-owned object, or to m_my_in */
81  std::istream* m_in{nullptr};
83  size_t m_curLineNum{0};
85  bool m_filter_C_comments{true};
87 
88 }; // end of CTextFileLinesParser
89 } // namespace mrpt::io
std::istream * m_in
Points to either a user-owned object, or to m_my_in.
A class for parsing text files, returning each non-empty and non-comment line, along its line number...
std::shared_ptr< std::istream > m_my_in
void rewind()
Reset the read pointer to the beginning of the file.
size_t getCurrentLineNumber() const
Return the line number of the last line returned with getNextLine.
void open(const std::string &fil)
Open a file (an alternative to the constructor with a file name)
bool getNextLine(std::string &out_str)
Reads from the file and return the next (non-comment) line, as a std::string.
void enableCommentFilters(bool filter_MATLAB_comments, bool filter_C_comments, bool filter_SH_comments)
Enable/disable filtering of lines starting with "%", "//" or "#", respectively.
CTextFileLinesParser()=default
Default constructor; should call open() at some moment later.
void close()
Close the file (no need to call it normally, the file is closed upon destruction) ...



Page generated by Doxygen 1.8.14 for MRPT 2.0.1 Git: 0fef1a6d7 Fri Apr 3 23:00:21 2020 +0200 at vie abr 3 23:20:28 CEST 2020