Go to the documentation of this file.00001
00002
00003
00004
00005
00006
00007
00008
00009
00010
00011
00012
00013
00014
00015
00016
00017
00018
00019
00020
00021
00022
00023
00024
00025
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
00039
00040
00041
00042
00043
00044 class BASE_IMPEXP CTextFileLinesParser
00045 {
00046 public:
00047
00048 CTextFileLinesParser() : m_curLineNum(0), m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) { }
00049
00050
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
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
00068 void close() { m_in.close(); }
00069
00070
00071 void rewind()
00072 {
00073 m_curLineNum = 0;
00074 m_in.clear();
00075 m_in.seekg(0);
00076 }
00077
00078
00079
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
00097
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;
00108
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
00114 buf.str(lin);
00115 buf.clear();
00116 return true;
00117 };
00118 return false;
00119 }
00120
00121
00122 inline size_t getCurrentLineNumber() const { return m_curLineNum; }
00123
00124
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 };
00145 }
00146 }
00147 #endif