Main MRPT website > C++ reference for MRPT 1.5.7
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 #include <iosfwd>
16 
17 namespace mrpt
18 {
19  namespace utils
20  {
21  /** A class for parsing text files, returning each non-empty and non-comment 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 (comment lines),
24  * unless this behavior is explicitly disabled with \a enableCommentFilters
25  * \ingroup mrpt_base_grp
26  */
28  {
29  public:
30  /** Default constructor; should call \a open() at some moment later. */
31  CTextFileLinesParser() : m_curLineNum(0), m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true), m_in{ NULL }, m_in_ownership{ true } { }
32 
33  /** Constructor for opening a file \exception std::exception On error opening file */
34  explicit CTextFileLinesParser(const std::string &fil) : m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true), m_in{ NULL }, m_in_ownership{ true } {
35  open(fil);
36  }
37 
38  explicit CTextFileLinesParser(std::istream& in) : m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true), m_in{ NULL }, m_in_ownership{ true } {
39  open(in);
40  }
41 
42  /** Open a file (an alternative to the constructor with a file name) */
43  void open(const std::string &fil)
44  {
45  m_curLineNum = 0;
46  m_fileName = fil;
47  this->close();
48  std::ifstream *ifs = new std::ifstream;
49  m_in = ifs;
50  ifs->open(fil.c_str());
51  if (!ifs->is_open())
52  THROW_EXCEPTION_FMT("Error opening file '%s' for reading",fil.c_str());
53  }
54 
55  void open(std::istream& in)
56  {
57  m_curLineNum = 0;
58  m_fileName = "{std::istream}";
59  this->close();
60  m_in = &in;
61  m_in_ownership = false;
62  }
63 
64 
65  /** Close the file (no need to call it normally, the file is closed upon destruction) */
66  void close() {
67  if (!m_in) return;
68  if (m_in_ownership) delete m_in;
69  m_in = NULL;
70  }
71 
72  /** Reset the read pointer to the beginning of the file */
73  void rewind()
74  {
75  m_curLineNum = 0;
76  m_in->clear();
77  m_in->seekg(0);
78  }
79 
80  /** Reads from the file and return the next (non-comment) line, as a std::string
81  * \return false on EOF.
82  */
83  inline bool getNextLine(std::string &out_str)
84  {
85  std::istringstream buf;
86  if (getNextLine(buf))
87  {
88  out_str = buf.str();
89  return true;
90  }
91  else
92  {
93  out_str.clear();
94  return false;
95  }
96  }
97 
98  /** Reads from the file and stores the next (non-comment) line into the given stream buffer.
99  * \return false on EOF.
100  */
101  bool getNextLine( std::istringstream &buf )
102  {
103  ASSERT_(m_in != NULL);
104  while (!m_in->fail())
105  {
106  std::string lin;
107  std::getline(*m_in,lin);
108  m_curLineNum++;
109  lin = mrpt::system::trim(lin);
110  if (lin.empty()) continue; // Ignore empty lines.
111  // Ignore comments lines, starting with "#" or "//".
112  if ( (m_filter_SH_comments && mrpt::system::strStarts(lin,"#"))
113  || (m_filter_C_comments && mrpt::system::strStarts(lin,"//"))
114  || (m_filter_MATLAB_comments && mrpt::system::strStarts(lin,"%")) )
115  continue;
116  // Parse the line as a string stream:
117  buf.str(lin);
118  buf.clear();
119  return true;
120  };
121  return false;
122  }
123 
124  /** Return the line number of the last line returned with \a getNextLine */
125  inline size_t getCurrentLineNumber() const { return m_curLineNum; }
126 
127  /** Enable/disable filtering of lines starting with "%", "//" or "#", respectively. */
128  inline void enableCommentFilters(
129  bool filter_MATLAB_comments,
130  bool filter_C_comments,
131  bool filter_SH_comments
132  )
133  {
134  m_filter_MATLAB_comments = filter_MATLAB_comments;
135  m_filter_C_comments = filter_C_comments;
136  m_filter_SH_comments = filter_SH_comments;
137  }
138 
139  private:
141  size_t m_curLineNum;
145  std::istream *m_in;
147 
148  }; // end of CTextFileLinesParser
149  } // End of namespace
150 } // end of namespace
151 #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:3919
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 BASE_IMPEXP strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
GLuint in
Definition: glext.h:6301
#define ASSERT_(f)
size_t getCurrentLineNumber() const
Return the line number of the last line returned with getNextLine.
std::string BASE_IMPEXP 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.5.7 Git: 5902e14cc Wed Apr 24 15:04:01 2019 +0200 at lun oct 28 01:39:17 CET 2019