Main MRPT website > C++ reference
MRPT logo

CTextFileLinesParser.h

Go to the documentation of this file.
00001 /* +---------------------------------------------------------------------------+
00002    |          The Mobile Robot Programming Toolkit (MRPT) C++ library          |
00003    |                                                                           |
00004    |                   http://mrpt.sourceforge.net/                            |
00005    |                                                                           |
00006    |   Copyright (C) 2005-2011  University of Malaga                           |
00007    |                                                                           |
00008    |    This software was written by the Machine Perception and Intelligent    |
00009    |      Robotics Lab, University of Malaga (Spain).                          |
00010    |    Contact: Jose-Luis Blanco  <jlblanco@ctima.uma.es>                     |
00011    |                                                                           |
00012    |  This file is part of the MRPT project.                                   |
00013    |                                                                           |
00014    |     MRPT is free software: you can redistribute it and/or modify          |
00015    |     it under the terms of the GNU General Public License as published by  |
00016    |     the Free Software Foundation, either version 3 of the License, or     |
00017    |     (at your option) any later version.                                   |
00018    |                                                                           |
00019    |   MRPT is distributed in the hope that it will be useful,                 |
00020    |     but WITHOUT ANY WARRANTY; without even the implied warranty of        |
00021    |     MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the         |
00022    |     GNU General Public License for more details.                          |
00023    |                                                                           |
00024    |     You should have received a copy of the GNU General Public License     |
00025    |     along with MRPT.  If not, see <http://www.gnu.org/licenses/>.         |
00026    |                                                                           |
00027    +---------------------------------------------------------------------------+ */
00028 #ifndef CTextFileLinesParser_H
00029 #define CTextFileLinesParser_H
00030 
00031 #include <mrpt/utils/utils_defs.h>
00032 #include <mrpt/system/string_utils.h>
00033 
00034 namespace mrpt
00035 {
00036     namespace utils
00037     {
00038                         /** A class for parsing text files, returning each non-empty and non-comment line, along its line number.
00039                           *  Lines are strip out of leading and trailing whitespaces.
00040                           *  By default, lines starting with either "#", "//" or "%" are skipped (comment lines),
00041                           *   unless this behavior is explicitly disabled with  \a enableCommentFilters
00042                           */
00043                         class BASE_IMPEXP CTextFileLinesParser
00044                         {
00045                         public:
00046                                 /** Default constructor; should call \a open() at some moment later. */
00047                                 CTextFileLinesParser() : m_curLineNum(0), m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) { }
00048 
00049                                 /** Constructor for opening a file  \exception std::exception On error opening file */
00050                                 CTextFileLinesParser(const std::string &fil) : m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) {
00051                                         open(fil);
00052                                 }
00053 
00054                                 /** Open a file (an alternative to the constructor with a file name) */
00055                                 void open(const std::string &fil)
00056                                 {
00057                                         m_curLineNum = 0;
00058                                         m_fileName = fil;
00059                                         m_in.close();
00060                                         m_in.open(fil.c_str());
00061                                         if (!m_in.is_open())
00062                                                 THROW_EXCEPTION_CUSTOM_MSG1("Error opening file '%s' for reading",fil.c_str());
00063                                 }
00064 
00065                                 /** Close the file (no need to call it normally, the file is closed upon destruction) */
00066                                 void close() { m_in.close(); }
00067 
00068                                 /** Reset the read pointer to the beginning of the file */
00069                                 void rewind()
00070                                 {
00071                                         m_curLineNum = 0;
00072                                         m_in.clear();
00073                                         m_in.seekg(0);
00074                                 }
00075 
00076                                 /** Reads from the file and return the next (non-comment) line, as a std::string
00077                                   * \return false on EOF.
00078                                   */
00079                                 inline bool getNextLine(std::string &out_str)
00080                                 {
00081                                         std::istringstream buf;
00082                                         if (getNextLine(buf))
00083                                         {
00084                                                 out_str = buf.str();
00085                                                 return true;
00086                                         }
00087                                         else
00088                                         {
00089                                                 out_str.clear();
00090                                                 return false;
00091                                         }
00092                                 }
00093 
00094                                 /** Reads from the file and stores the next (non-comment) line into the given stream buffer.
00095                                   * \return false on EOF.
00096                                   */
00097                                 bool getNextLine( std::istringstream &buf )
00098                                 {
00099                                         while (!m_in.fail())
00100                                         {
00101                                                 std::string lin;
00102                                                 std::getline(m_in,lin);
00103                                                 m_curLineNum++;
00104                                                 lin = mrpt::system::trim(lin);
00105                                                 if (lin.empty()) continue; // Ignore empty lines.
00106                                                 // Ignore comments lines, starting with "#" or "//".
00107                                                 if ( (m_filter_SH_comments && mrpt::system::strStarts(lin,"#"))
00108                                                   || (m_filter_C_comments  && mrpt::system::strStarts(lin,"//"))
00109                                                   || (m_filter_MATLAB_comments && mrpt::system::strStarts(lin,"%")) )
00110                                                         continue;
00111                                                 // Parse the line as a string stream:
00112                                                 buf.str(lin);
00113                                                 buf.clear();
00114                                                 return true;
00115                                         };
00116                                         return false;
00117                                 }
00118 
00119                                 /** Return the line number of the last line returned with \a getNextLine */
00120                                 inline size_t getCurrentLineNumber() const { return m_curLineNum; }
00121 
00122                                 /** Enable/disable filtering of lines starting with "%", "//" or "#", respectively. */
00123                                 inline void enableCommentFilters(
00124                                         bool filter_MATLAB_comments,
00125                                         bool filter_C_comments,
00126                                         bool filter_SH_comments
00127                                         )
00128                                 {
00129                                         m_filter_MATLAB_comments = filter_MATLAB_comments;
00130                                         m_filter_C_comments = filter_C_comments;
00131                                         m_filter_SH_comments = filter_SH_comments;
00132                                 }
00133 
00134                         private:
00135                                 std::string   m_fileName;
00136                                 std::ifstream m_in;
00137                                 size_t        m_curLineNum;
00138                                 bool              m_filter_MATLAB_comments;
00139                                 bool              m_filter_C_comments;
00140                                 bool              m_filter_SH_comments;
00141 
00142                         };  // end of CTextFileLinesParser
00143         } // End of namespace
00144 } // end of namespace
00145 #endif



Page generated by Doxygen 1.7.3 for MRPT 0.9.4 SVN: at Sat Mar 26 06:40:17 UTC 2011