MRPT  2.0.0
CTextFileLinesParser.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | https://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2020, Individual contributors, see AUTHORS file |
6  | See: https://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See: https://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
12 #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 
25 CTextFileLinesParser::CTextFileLinesParser(std::istream& in) { open(in); }
26 void CTextFileLinesParser::open(std::istream& in)
27 {
28  m_curLineNum = 0;
29  m_fileName = "{std::istream}";
30  this->close();
31  m_in = &in;
32 }
33 
34 void CTextFileLinesParser::open(const std::string& fil)
35 {
36  m_curLineNum = 0;
37  m_fileName = fil;
38  this->close();
39  auto ifs = std::make_shared<std::ifstream>();
40  ifs->clear();
41  ifs->open(fil.c_str());
42  if (!ifs->is_open())
43  THROW_EXCEPTION_FMT("Error opening file '%s' for reading", fil.c_str());
44  m_my_in = std::shared_ptr<std::istream>(ifs);
45  m_in = m_my_in.get();
46 }
47 
49 {
50  if (!m_in) return;
51  m_my_in.reset();
52  m_in = nullptr;
53 }
55 {
56  m_curLineNum = 0;
57  m_in->clear();
58  m_in->seekg(0);
59 }
60 
61 bool CTextFileLinesParser::getNextLine(std::string& out_str)
62 {
63  std::istringstream buf;
64  if (getNextLine(buf))
65  {
66  out_str = buf.str();
67  return true;
68  }
69  out_str.clear();
70  return false;
71 }
72 
73 bool CTextFileLinesParser::getNextLine(std::istringstream& buf)
74 {
75  ASSERT_(m_in != nullptr);
76  while (!m_in->fail())
77  {
78  std::string lin;
79  std::getline(*m_in, lin);
80  m_curLineNum++;
81  lin = mrpt::system::trim(lin);
82  if (lin.empty()) continue; // Ignore empty lines.
83  // Ignore comments lines, starting with "#" or "//".
84  if ((m_filter_SH_comments && mrpt::system::strStarts(lin, "#")) ||
85  (m_filter_C_comments && mrpt::system::strStarts(lin, "//")) ||
86  (m_filter_MATLAB_comments && mrpt::system::strStarts(lin, "%")))
87  continue;
88  // Parse the line as a string stream:
89  buf.str(lin);
90  buf.clear();
91  return true;
92  };
93  return false;
94 }
95 
97 {
98  return m_curLineNum;
99 }
100 
102  bool filter_MATLAB_comments, bool filter_C_comments,
103  bool filter_SH_comments)
104 {
105  m_filter_MATLAB_comments = filter_MATLAB_comments;
106  m_filter_C_comments = filter_C_comments;
107  m_filter_SH_comments = filter_SH_comments;
108 }
STL namespace.
void rewind()
Reset the read pointer to the beginning of the file.
#define ASSERT_(f)
Defines an assertion mechanism.
Definition: exceptions.h:120
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.
CTextFileLinesParser()=default
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)
std::string trim(const std::string &str)
Removes leading and trailing spaces.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
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 2.0.0 Git: b38439d21 Tue Mar 31 19:58:06 2020 +0200 at miƩ abr 1 00:50:30 CEST 2020