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 class BASE_IMPEXP CTextFileLinesParser
00044 {
00045 public:
00046
00047 CTextFileLinesParser() : m_curLineNum(0), m_filter_MATLAB_comments(true), m_filter_C_comments(true), m_filter_SH_comments(true) { }
00048
00049
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
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
00066 void close() { m_in.close(); }
00067
00068
00069 void rewind()
00070 {
00071 m_curLineNum = 0;
00072 m_in.clear();
00073 m_in.seekg(0);
00074 }
00075
00076
00077
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
00095
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;
00106
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
00112 buf.str(lin);
00113 buf.clear();
00114 return true;
00115 };
00116 return false;
00117 }
00118
00119
00120 inline size_t getCurrentLineNumber() const { return m_curLineNum; }
00121
00122
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 };
00143 }
00144 }
00145 #endif