MRPT  1.9.9
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-2019, 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 
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  m_in_ownership = false;
33 }
34 
36 {
37  m_curLineNum = 0;
38  m_fileName = fil;
39  this->close();
40  auto ifs = new std::ifstream;
41  m_in = ifs;
42  m_in_ownership = true;
43  ifs->clear();
44  ifs->open(fil.c_str());
45  if (!ifs->is_open())
46  THROW_EXCEPTION_FMT("Error opening file '%s' for reading", fil.c_str());
47 }
48 
50 {
51  if (!m_in) return;
52  if (m_in_ownership) delete m_in;
53  m_in = nullptr;
54 }
56 {
57  m_curLineNum = 0;
58  m_in->clear();
59  m_in->seekg(0);
60 }
61 
63 {
64  std::istringstream buf;
65  if (getNextLine(buf))
66  {
67  out_str = buf.str();
68  return true;
69  }
70  out_str.clear();
71  return false;
72 }
73 
74 bool CTextFileLinesParser::getNextLine(std::istringstream& buf)
75 {
76  ASSERT_(m_in != nullptr);
77  while (!m_in->fail())
78  {
79  std::string lin;
80  std::getline(*m_in, lin);
81  m_curLineNum++;
82  lin = mrpt::system::trim(lin);
83  if (lin.empty()) continue; // Ignore empty lines.
84  // Ignore comments lines, starting with "#" or "//".
85  if ((m_filter_SH_comments && mrpt::system::strStarts(lin, "#")) ||
86  (m_filter_C_comments && mrpt::system::strStarts(lin, "//")) ||
87  (m_filter_MATLAB_comments && mrpt::system::strStarts(lin, "%")))
88  continue;
89  // Parse the line as a string stream:
90  buf.str(lin);
91  buf.clear();
92  return true;
93  };
94  return false;
95 }
96 
98 {
99  return m_curLineNum;
100 }
101 
103  bool filter_MATLAB_comments, bool filter_C_comments,
104  bool filter_SH_comments)
105 {
106  m_filter_MATLAB_comments = filter_MATLAB_comments;
107  m_filter_C_comments = filter_C_comments;
108  m_filter_SH_comments = filter_SH_comments;
109 }
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.
GLsizei const GLchar ** string
Definition: glext.h:4116
bool strStarts(const std::string &str, const std::string &subStr)
Return true if "str" starts with "subStr" (case sensitive)
GLuint in
Definition: glext.h:7391
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 1.9.9 Git: abb8b1a1e Fri Oct 18 14:19:12 2019 +0200 at vie oct 18 14:20:13 CEST 2019