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 void CTextFileLinesParser::open(std::istream& in)
28 {
29  m_curLineNum = 0;
30  m_fileName = "{std::istream}";
31  this->close();
32  m_in = &in;
33  m_in_ownership = false;
34 }
35 
37 {
38  m_curLineNum = 0;
39  m_fileName = fil;
40  this->close();
41  auto ifs = new std::ifstream;
42  m_in = ifs;
43  m_in_ownership = true;
44  ifs->clear();
45  ifs->open(fil.c_str());
46  if (!ifs->is_open())
47  THROW_EXCEPTION_FMT("Error opening file '%s' for reading", fil.c_str());
48 }
49 
51 {
52  if (!m_in) return;
53  if (m_in_ownership) delete m_in;
54  m_in = nullptr;
55 }
57 {
58  m_curLineNum = 0;
59  m_in->clear();
60  m_in->seekg(0);
61 }
62 
64 {
65  std::istringstream buf;
66  if (getNextLine(buf))
67  {
68  out_str = buf.str();
69  return true;
70  }
71  out_str.clear();
72  return false;
73 }
74 
75 bool CTextFileLinesParser::getNextLine(std::istringstream& buf)
76 {
77  ASSERT_(m_in != nullptr);
78  while (!m_in->fail())
79  {
80  std::string lin;
81  std::getline(*m_in, lin);
82  m_curLineNum++;
83  lin = mrpt::system::trim(lin);
84  if (lin.empty()) continue; // Ignore empty lines.
85  // Ignore comments lines, starting with "#" or "//".
86  if ((m_filter_SH_comments && mrpt::system::strStarts(lin, "#")) ||
87  (m_filter_C_comments && mrpt::system::strStarts(lin, "//")) ||
88  (m_filter_MATLAB_comments && mrpt::system::strStarts(lin, "%")))
89  continue;
90  // Parse the line as a string stream:
91  buf.str(lin);
92  buf.clear();
93  return true;
94  };
95  return false;
96 }
97 
99 {
100  return m_curLineNum;
101 }
102 
104  bool filter_MATLAB_comments, bool filter_C_comments,
105  bool filter_SH_comments)
106 {
107  m_filter_MATLAB_comments = filter_MATLAB_comments;
108  m_filter_C_comments = filter_C_comments;
109  m_filter_SH_comments = filter_SH_comments;
110 }
STL namespace.
void rewind()
Reset the read pointer to the beginning of the file.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:113
size_t getCurrentLineNumber() const
Return the line number of the last line returned with getNextLine.
void open(const std::string &fil)
Open a file (an alternative to the constructor with a file name)
bool getNextLine(std::string &out_str)
Reads from the file and return the next (non-comment) line, as a std::string.
void enableCommentFilters(bool filter_MATLAB_comments, bool filter_C_comments, bool filter_SH_comments)
Enable/disable filtering of lines starting with "%", "//" or "#", respectively.
GLsizei const GLchar ** string
Definition: glext.h:4101
CTextFileLinesParser()
Default constructor; should call open() at some moment later.
bool strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
GLuint in
Definition: glext.h:7274
std::string trim(const std::string &str)
Removes leading and trailing spaces.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
void close()
Close the file (no need to call it normally, the file is closed upon destruction) ...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 7d5e6d718 Fri Aug 24 01:51:28 2018 +0200 at lun nov 2 08:35:50 CET 2020