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://www.mrpt.org/                                |
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                  * \ingroup mrpt_base_grp
00043                           */
00044                         class BASE_IMPEXP CTextFileLinesParser
00045                         {
00046                         public:
00047                                 /** Default constructor; should call \a open() at some moment later. */
00048                                 CTextFileLinesParser() : m_curLineNum(0), m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) { }
00049 
00050                                 /** Constructor for opening a file  \exception std::exception On error opening file */
00051                                 CTextFileLinesParser(const std::string &fil) : m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) {
00052                                         open(fil);
00053                                 }
00054 
00055                                 /** Open a file (an alternative to the constructor with a file name) */
00056                                 void open(const std::string &fil)
00057                                 {
00058                                         m_curLineNum = 0;
00059                                         m_fileName = fil;
00060                                         m_in.close();
00061                                         m_in.clear(); 
00062                                         m_in.open(fil.c_str());
00063                                         if (!m_in.is_open())
00064                                                 THROW_EXCEPTION_CUSTOM_MSG1("Error opening file '%s' for reading",fil.c_str());
00065                                 }
00066 
00067                                 /** Close the file (no need to call it normally, the file is closed upon destruction) */
00068                                 void close() { m_in.close(); }
00069 
00070                                 /** Reset the read pointer to the beginning of the file */
00071                                 void rewind()
00072                                 {
00073                                         m_curLineNum = 0;
00074                                         m_in.clear();
00075                                         m_in.seekg(0);
00076                                 }
00077 
00078                                 /** Reads from the file and return the next (non-comment) line, as a std::string
00079                                   * \return false on EOF.
00080                                   */
00081                                 inline bool getNextLine(std::string &out_str)
00082                                 {
00083                                         std::istringstream buf;
00084                                         if (getNextLine(buf))
00085                                         {
00086                                                 out_str = buf.str();
00087                                                 return true;
00088                                         }
00089                                         else
00090                                         {
00091                                                 out_str.clear();
00092                                                 return false;
00093                                         }
00094                                 }
00095 
00096                                 /** Reads from the file and stores the next (non-comment) line into the given stream buffer.
00097                                   * \return false on EOF.
00098                                   */
00099                                 bool getNextLine( std::istringstream &buf )
00100                                 {
00101                                         while (!m_in.fail())
00102                                         {
00103                                                 std::string lin;
00104                                                 std::getline(m_in,lin);
00105                                                 m_curLineNum++;
00106                                                 lin = mrpt::system::trim(lin);
00107                                                 if (lin.empty()) continue; // Ignore empty lines.
00108                                                 // Ignore comments lines, starting with "#" or "//".
00109                                                 if ( (m_filter_SH_comments && mrpt::system::strStarts(lin,"#"))
00110                                                   || (m_filter_C_comments  && mrpt::system::strStarts(lin,"//"))
00111                                                   || (m_filter_MATLAB_comments && mrpt::system::strStarts(lin,"%")) )
00112                                                         continue;
00113                                                 // Parse the line as a string stream:
00114                                                 buf.str(lin);
00115                                                 buf.clear();
00116                                                 return true;
00117                                         };
00118                                         return false;
00119                                 }
00120 
00121                                 /** Return the line number of the last line returned with \a getNextLine */
00122                                 inline size_t getCurrentLineNumber() const { return m_curLineNum; }
00123 
00124                                 /** Enable/disable filtering of lines starting with "%", "//" or "#", respectively. */
00125                                 inline void enableCommentFilters(
00126                                         bool filter_MATLAB_comments,
00127                                         bool filter_C_comments,
00128                                         bool filter_SH_comments
00129                                         )
00130                                 {
00131                                         m_filter_MATLAB_comments = filter_MATLAB_comments;
00132                                         m_filter_C_comments = filter_C_comments;
00133                                         m_filter_SH_comments = filter_SH_comments;
00134                                 }
00135 
00136                         private:
00137                                 std::string   m_fileName;
00138                                 std::ifstream m_in;
00139                                 size_t        m_curLineNum;
00140                                 bool              m_filter_MATLAB_comments;
00141                                 bool              m_filter_C_comments;
00142                                 bool              m_filter_SH_comments;
00143 
00144                         };  // end of CTextFileLinesParser
00145         } // End of namespace
00146 } // end of namespace
00147 #endif



Page generated by Doxygen 1.7.5 for MRPT 0.9.5 SVN: at Thu Oct 13 21:25:36 UTC 2011