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