Main MRPT website > C++ reference
MRPT logo
CTextFileLinesParser.h
Go to the documentation of this file.
1 /* +---------------------------------------------------------------------------+
2  | The Mobile Robot Programming Toolkit (MRPT) C++ library |
3  | |
4  | http://www.mrpt.org/ |
5  | |
6  | Copyright (C) 2005-2012 University of Malaga |
7  | |
8  | This software was written by the Machine Perception and Intelligent |
9  | Robotics Lab, University of Malaga (Spain). |
10  | Contact: Jose-Luis Blanco <jlblanco@ctima.uma.es> |
11  | |
12  | This file is part of the MRPT project. |
13  | |
14  | MRPT is free software: you can redistribute it and/or modify |
15  | it under the terms of the GNU General Public License as published by |
16  | the Free Software Foundation, either version 3 of the License, or |
17  | (at your option) any later version. |
18  | |
19  | MRPT is distributed in the hope that it will be useful, |
20  | but WITHOUT ANY WARRANTY; without even the implied warranty of |
21  | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
22  | GNU General Public License for more details. |
23  | |
24  | You should have received a copy of the GNU General Public License |
25  | along with MRPT. If not, see <http://www.gnu.org/licenses/>. |
26  | |
27  +---------------------------------------------------------------------------+ */
28 #ifndef CTextFileLinesParser_H
29 #define CTextFileLinesParser_H
30 
31 #include <mrpt/utils/utils_defs.h>
33 
34 namespace mrpt
35 {
36  namespace utils
37  {
38  /** A class for parsing text files, returning each non-empty and non-comment line, along its line number.
39  * Lines are strip out of leading and trailing whitespaces.
40  * By default, lines starting with either "#", "//" or "%" are skipped (comment lines),
41  * unless this behavior is explicitly disabled with \a enableCommentFilters
42  * \ingroup mrpt_base_grp
43  */
45  {
46  public:
47  /** Default constructor; should call \a open() at some moment later. */
48  CTextFileLinesParser() : m_curLineNum(0), m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) { }
49 
50  /** Constructor for opening a file \exception std::exception On error opening file */
51  CTextFileLinesParser(const std::string &fil) : m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) {
52  open(fil);
53  }
54 
55  /** Open a file (an alternative to the constructor with a file name) */
56  void open(const std::string &fil)
57  {
58  m_curLineNum = 0;
59  m_fileName = fil;
60  m_in.close();
61  m_in.clear();
62  m_in.open(fil.c_str());
63  if (!m_in.is_open())
64  THROW_EXCEPTION_CUSTOM_MSG1("Error opening file '%s' for reading",fil.c_str());
65  }
66 
67  /** Close the file (no need to call it normally, the file is closed upon destruction) */
68  void close() { m_in.close(); }
69 
70  /** Reset the read pointer to the beginning of the file */
71  void rewind()
72  {
73  m_curLineNum = 0;
74  m_in.clear();
75  m_in.seekg(0);
76  }
77 
78  /** Reads from the file and return the next (non-comment) line, as a std::string
79  * \return false on EOF.
80  */
81  inline bool getNextLine(std::string &out_str)
82  {
83  std::istringstream buf;
84  if (getNextLine(buf))
85  {
86  out_str = buf.str();
87  return true;
88  }
89  else
90  {
91  out_str.clear();
92  return false;
93  }
94  }
95 
96  /** Reads from the file and stores the next (non-comment) line into the given stream buffer.
97  * \return false on EOF.
98  */
99  bool getNextLine( std::istringstream &buf )
100  {
101  while (!m_in.fail())
102  {
103  std::string lin;
104  std::getline(m_in,lin);
105  m_curLineNum++;
106  lin = mrpt::system::trim(lin);
107  if (lin.empty()) continue; // Ignore empty lines.
108  // Ignore comments lines, starting with "#" or "//".
109  if ( (m_filter_SH_comments && mrpt::system::strStarts(lin,"#"))
110  || (m_filter_C_comments && mrpt::system::strStarts(lin,"//"))
111  || (m_filter_MATLAB_comments && mrpt::system::strStarts(lin,"%")) )
112  continue;
113  // Parse the line as a string stream:
114  buf.str(lin);
115  buf.clear();
116  return true;
117  };
118  return false;
119  }
120 
121  /** Return the line number of the last line returned with \a getNextLine */
122  inline size_t getCurrentLineNumber() const { return m_curLineNum; }
123 
124  /** Enable/disable filtering of lines starting with "%", "//" or "#", respectively. */
125  inline void enableCommentFilters(
126  bool filter_MATLAB_comments,
127  bool filter_C_comments,
128  bool filter_SH_comments
129  )
130  {
131  m_filter_MATLAB_comments = filter_MATLAB_comments;
132  m_filter_C_comments = filter_C_comments;
133  m_filter_SH_comments = filter_SH_comments;
134  }
135 
136  private:
137  std::string m_fileName;
138  std::ifstream m_in;
139  size_t m_curLineNum;
143 
144  }; // end of CTextFileLinesParser
145  } // End of namespace
146 } // end of namespace
147 #endif



Page generated by Doxygen 1.8.3 for MRPT 0.9.6 SVN: at Fri Feb 15 22:05:02 EST 2013