Main MRPT website > C++ reference for 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-2017, 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 #ifndef CTextFileLinesParser_H
10 #define CTextFileLinesParser_H
11 
12 #include <mrpt/utils/utils_defs.h>
14 #include <fstream>
15 
16 namespace mrpt
17 {
18 namespace utils
19 {
20 /** A class for parsing text files, returning each non-empty and non-comment
21 * line, along its line number.
22  * Lines are strip out of leading and trailing whitespaces.
23  * By default, lines starting with either "#", "//" or "%" are skipped
24 * (comment lines),
25  * unless this behavior is explicitly disabled with \a enableCommentFilters
26 * \ingroup mrpt_base_grp
27  */
29 {
30  public:
31  /** Default constructor; should call \a open() at some moment later. */
33  : m_curLineNum(0),
35  m_filter_C_comments(true),
37  {
38  }
39 
40  /** Constructor for opening a file \exception std::exception On error
41  * opening file */
44  m_filter_C_comments(true),
46  {
47  open(fil);
48  }
49 
50  /** Open a file (an alternative to the constructor with a file name) */
51  void open(const std::string& fil)
52  {
53  m_curLineNum = 0;
54  m_fileName = fil;
55  m_in.close();
56  m_in.clear();
57  m_in.open(fil.c_str());
58  if (!m_in.is_open())
60  "Error opening file '%s' for reading", fil.c_str());
61  }
62 
63  /** Close the file (no need to call it normally, the file is closed upon
64  * destruction) */
65  void close() { m_in.close(); }
66  /** Reset the read pointer to the beginning of the file */
67  void rewind()
68  {
69  m_curLineNum = 0;
70  m_in.clear();
71  m_in.seekg(0);
72  }
73 
74  /** Reads from the file and return the next (non-comment) line, as a
75  * std::string
76  * \return false on EOF.
77  */
78  inline bool getNextLine(std::string& out_str)
79  {
80  std::istringstream buf;
81  if (getNextLine(buf))
82  {
83  out_str = buf.str();
84  return true;
85  }
86  else
87  {
88  out_str.clear();
89  return false;
90  }
91  }
92 
93  /** Reads from the file and stores the next (non-comment) line into the
94  * given stream buffer.
95  * \return false on EOF.
96  */
97  bool getNextLine(std::istringstream& buf)
98  {
99  while (!m_in.fail())
100  {
101  std::string lin;
102  std::getline(m_in, lin);
103  m_curLineNum++;
104  lin = mrpt::system::trim(lin);
105  if (lin.empty()) continue; // Ignore empty lines.
106  // Ignore comments lines, starting with "#" or "//".
107  if ((m_filter_SH_comments && mrpt::system::strStarts(lin, "#")) ||
110  continue;
111  // Parse the line as a string stream:
112  buf.str(lin);
113  buf.clear();
114  return true;
115  };
116  return false;
117  }
118 
119  /** Return the line number of the last line returned with \a getNextLine */
120  inline size_t getCurrentLineNumber() const { return m_curLineNum; }
121  /** Enable/disable filtering of lines starting with "%", "//" or "#",
122  * respectively. */
123  inline void enableCommentFilters(
124  bool filter_MATLAB_comments, bool filter_C_comments,
125  bool filter_SH_comments)
126  {
127  m_filter_MATLAB_comments = filter_MATLAB_comments;
128  m_filter_C_comments = filter_C_comments;
129  m_filter_SH_comments = filter_SH_comments;
130  }
131 
132  private:
134  std::ifstream m_in;
135  size_t m_curLineNum;
139 
140 }; // end of CTextFileLinesParser
141 } // End of namespace
142 } // end of namespace
143 #endif
CTextFileLinesParser()
Default constructor; should call open() at some moment later.
bool getNextLine(std::istringstream &buf)
Reads from the file and stores the next (non-comment) line into the given stream buffer.
bool getNextLine(std::string &out_str)
Reads from the file and return the next (non-comment) line, as a std::string.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
void rewind()
Reset the read pointer to the beginning of the file.
CTextFileLinesParser(const std::string &fil)
Constructor for opening a file.
A class for parsing text files, returning each non-empty and non-comment line, along its line number...
GLsizei const GLchar ** string
Definition: glext.h:4101
void open(const std::string &fil)
Open a file (an alternative to the constructor with a file name)
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
bool strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
size_t getCurrentLineNumber() const
Return the line number of the last line returned with getNextLine.
std::string trim(const std::string &str)
Removes leading and trailing spaces.
void enableCommentFilters(bool filter_MATLAB_comments, bool filter_C_comments, bool filter_SH_comments)
Enable/disable filtering of lines starting with "%", "//" or "#", respectively.
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: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019