Main MRPT website > C++ reference for MRPT 1.5.3
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 line, along its line number.
21  * Lines are strip out of leading and trailing whitespaces.
22  * By default, lines starting with either "#", "//" or "%" are skipped (comment lines),
23  * unless this behavior is explicitly disabled with \a enableCommentFilters
24  * \ingroup mrpt_base_grp
25  */
27  {
28  public:
29  /** Default constructor; should call \a open() at some moment later. */
30  CTextFileLinesParser() : m_curLineNum(0), m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) { }
31 
32  /** Constructor for opening a file \exception std::exception On error opening file */
33  CTextFileLinesParser(const std::string &fil) : m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) {
34  open(fil);
35  }
36 
37  /** Open a file (an alternative to the constructor with a file name) */
38  void open(const std::string &fil)
39  {
40  m_curLineNum = 0;
41  m_fileName = fil;
42  m_in.close();
43  m_in.clear();
44  m_in.open(fil.c_str());
45  if (!m_in.is_open())
46  THROW_EXCEPTION_FMT("Error opening file '%s' for reading",fil.c_str());
47  }
48 
49  /** Close the file (no need to call it normally, the file is closed upon destruction) */
50  void close() { m_in.close(); }
51 
52  /** Reset the read pointer to the beginning of the file */
53  void rewind()
54  {
55  m_curLineNum = 0;
56  m_in.clear();
57  m_in.seekg(0);
58  }
59 
60  /** Reads from the file and return the next (non-comment) line, as a std::string
61  * \return false on EOF.
62  */
63  inline bool getNextLine(std::string &out_str)
64  {
65  std::istringstream buf;
66  if (getNextLine(buf))
67  {
68  out_str = buf.str();
69  return true;
70  }
71  else
72  {
73  out_str.clear();
74  return false;
75  }
76  }
77 
78  /** Reads from the file and stores the next (non-comment) line into the given stream buffer.
79  * \return false on EOF.
80  */
81  bool getNextLine( std::istringstream &buf )
82  {
83  while (!m_in.fail())
84  {
85  std::string lin;
86  std::getline(m_in,lin);
87  m_curLineNum++;
88  lin = mrpt::system::trim(lin);
89  if (lin.empty()) continue; // Ignore empty lines.
90  // Ignore comments lines, starting with "#" or "//".
91  if ( (m_filter_SH_comments && mrpt::system::strStarts(lin,"#"))
92  || (m_filter_C_comments && mrpt::system::strStarts(lin,"//"))
93  || (m_filter_MATLAB_comments && mrpt::system::strStarts(lin,"%")) )
94  continue;
95  // Parse the line as a string stream:
96  buf.str(lin);
97  buf.clear();
98  return true;
99  };
100  return false;
101  }
102 
103  /** Return the line number of the last line returned with \a getNextLine */
104  inline size_t getCurrentLineNumber() const { return m_curLineNum; }
105 
106  /** Enable/disable filtering of lines starting with "%", "//" or "#", respectively. */
107  inline void enableCommentFilters(
108  bool filter_MATLAB_comments,
109  bool filter_C_comments,
110  bool filter_SH_comments
111  )
112  {
113  m_filter_MATLAB_comments = filter_MATLAB_comments;
114  m_filter_C_comments = filter_C_comments;
115  m_filter_SH_comments = filter_SH_comments;
116  }
117 
118  private:
119  std::string m_fileName;
120  std::ifstream m_in;
121  size_t m_curLineNum;
125 
126  }; // end of CTextFileLinesParser
127  } // End of namespace
128 } // end of namespace
129 #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,...)
std::string BASE_IMPEXP trim(const std::string &str)
Removes leading and trailing spaces.
bool BASE_IMPEXP strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
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...
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.
size_t getCurrentLineNumber() const
Return the line number of the last line returned with getNextLine.
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.13 for MRPT 1.5.3 at Tue Oct 31 07:27:35 UTC 2017