Main MRPT website > C++ reference for MRPT 1.9.9
CTextFileLinesParser.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
14 #include <mrpt/core/exceptions.h>
15 #include <sstream>
16 
17 using namespace mrpt::io;
18 using namespace std;
19 
21 {
22  open(fil);
23 }
24 
26 {
27  m_curLineNum = 0;
28  m_fileName = fil;
29  m_in.close();
30  m_in.clear();
31  m_in.open(fil.c_str());
32  if (!m_in.is_open())
33  THROW_EXCEPTION_FMT("Error opening file '%s' for reading", fil.c_str());
34 }
35 
36 void CTextFileLinesParser::close() { m_in.close(); }
38 {
39  m_curLineNum = 0;
40  m_in.clear();
41  m_in.seekg(0);
42 }
43 
45 {
46  std::istringstream buf;
47  if (getNextLine(buf))
48  {
49  out_str = buf.str();
50  return true;
51  }
52 
53  out_str.clear();
54  return false;
55 }
56 
57 bool CTextFileLinesParser::getNextLine(std::istringstream& buf)
58 {
59  while (!m_in.fail())
60  {
61  std::string lin;
62  std::getline(m_in, lin);
63  m_curLineNum++;
64  lin = mrpt::system::trim(lin);
65  if (lin.empty()) continue; // Ignore empty lines.
66  // Ignore comments lines, starting with "#" or "//".
67  if ((m_filter_SH_comments && mrpt::system::strStarts(lin, "#")) ||
68  (m_filter_C_comments && mrpt::system::strStarts(lin, "//")) ||
69  (m_filter_MATLAB_comments && mrpt::system::strStarts(lin, "%")))
70  continue;
71  // Parse the line as a string stream:
72  buf.str(lin);
73  buf.clear();
74  return true;
75  };
76  return false;
77 }
78 
80 {
81  return m_curLineNum;
82 }
83 
85  bool filter_MATLAB_comments, bool filter_C_comments,
86  bool filter_SH_comments)
87 {
88  m_filter_MATLAB_comments = filter_MATLAB_comments;
89  m_filter_C_comments = filter_C_comments;
90  m_filter_SH_comments = filter_SH_comments;
91 }
mrpt::io::CTextFileLinesParser::open
void open(const std::string &fil)
Open a file (an alternative to the constructor with a file name)
Definition: CTextFileLinesParser.cpp:25
exceptions.h
mrpt::io
Definition: img/CImage.h:22
string_utils.h
CTextFileLinesParser.h
mrpt::io::CTextFileLinesParser::enableCommentFilters
void enableCommentFilters(bool filter_MATLAB_comments, bool filter_C_comments, bool filter_SH_comments)
Enable/disable filtering of lines starting with "%", "//" or "#", respectively.
Definition: CTextFileLinesParser.cpp:84
THROW_EXCEPTION_FMT
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
mrpt::io::CTextFileLinesParser::close
void close()
Close the file (no need to call it normally, the file is closed upon destruction)
Definition: CTextFileLinesParser.cpp:36
mrpt::io::CTextFileLinesParser::getCurrentLineNumber
size_t getCurrentLineNumber() const
Return the line number of the last line returned with getNextLine.
Definition: CTextFileLinesParser.cpp:79
mrpt::io::CTextFileLinesParser::getNextLine
bool getNextLine(std::string &out_str)
Reads from the file and return the next (non-comment) line, as a std::string.
Definition: CTextFileLinesParser.cpp:44
mrpt::io::CTextFileLinesParser::rewind
void rewind()
Reset the read pointer to the beginning of the file.
Definition: CTextFileLinesParser.cpp:37
mrpt::io::CTextFileLinesParser::CTextFileLinesParser
CTextFileLinesParser()
Default constructor; should call open() at some moment later.
Definition: CTextFileLinesParser.h:30
mrpt::system::strStarts
bool strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
Definition: string_utils.cpp:312
string
GLsizei const GLchar ** string
Definition: glext.h:4101
mrpt::system::trim
std::string trim(const std::string &str)
Removes leading and trailing spaces.
Definition: string_utils.cpp:270
io-precomp.h



Page generated by Doxygen 1.8.17 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at miƩ 12 jul 2023 10:03:34 CEST