MRPT  2.0.4
CFileInputStream.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>
14 
15 using namespace mrpt::io;
16 using namespace std;
17 
18 static_assert(
19  !std::is_copy_constructible_v<CFileInputStream> &&
20  !std::is_copy_assignable_v<CFileInputStream>,
21  "Copy Check");
22 
23 CFileInputStream::CFileInputStream(const string& fileName) : m_if()
24 {
26 
27  // Try to open the file:
28  // Open for input:
29  if (!open(fileName))
31  "Error trying to open file: '%s'", fileName.c_str());
32 
33  MRPT_END
34 }
35 
36 /*---------------------------------------------------------------
37  Constructor
38  ---------------------------------------------------------------*/
40 /*---------------------------------------------------------------
41  open
42  ---------------------------------------------------------------*/
43 bool CFileInputStream::open(const string& fileName)
44 {
45  // Try to open the file:
46  // Open for input:
47  m_if.open(fileName.c_str(), ios_base::binary | ios_base::in);
48  return m_if.is_open();
49 }
50 
51 /*---------------------------------------------------------------
52  close
53  ---------------------------------------------------------------*/
55 {
56  if (m_if.is_open()) m_if.close();
57 }
58 
59 /*---------------------------------------------------------------
60  Destructor
61  ---------------------------------------------------------------*/
63 /*---------------------------------------------------------------
64  Read
65  Reads bytes from the stream into Buffer
66  ---------------------------------------------------------------*/
67 size_t CFileInputStream::Read(void* Buffer, size_t Count)
68 {
69  if (!m_if.is_open()) return 0;
70 
71  m_if.read(static_cast<char*>(Buffer), Count);
72  return m_if.fail() ? 0 : Count;
73 }
74 
75 /*---------------------------------------------------------------
76  Write
77  Writes a block of bytes to the stream.
78  ---------------------------------------------------------------*/
80  [[maybe_unused]] const void* Buffer, [[maybe_unused]] size_t Count)
81 {
82  THROW_EXCEPTION("Trying to write to a read file stream.");
83 }
84 
85 /*---------------------------------------------------------------
86  Seek
87  Method for moving to a specified position in the streamed resource.
88  See documentation of CStream::Seek
89  ---------------------------------------------------------------*/
90 uint64_t CFileInputStream::Seek(int64_t Offset, CStream::TSeekOrigin Origin)
91 {
92  if (!m_if.is_open()) return 0;
93 
94  ifstream::off_type offset = Offset;
95  ifstream::seekdir way;
96 
97  switch (Origin)
98  {
99  case sFromBeginning:
100  way = ios_base::beg;
101  break;
102  case sFromCurrent:
103  way = ios_base::cur;
104  break;
105  case sFromEnd:
106  way = ios_base::end;
107  break;
108  default:
109  THROW_EXCEPTION("Invalid value for 'Origin'");
110  }
111 
112  m_if.seekg(offset, way);
113 
114  return getPosition();
115 }
116 
117 /*---------------------------------------------------------------
118  getTotalBytesCount
119  ---------------------------------------------------------------*/
121 {
122  if (!fileOpenCorrectly()) return 0;
123 
124  auto& f = const_cast<std::ifstream&>(m_if);
125 
126  const uint64_t previousPos = f.tellg();
127  f.seekg(0, ios_base::end);
128  uint64_t fileSize = f.tellg();
129  f.seekg(previousPos, ios_base::beg);
130  return fileSize;
131 }
132 
133 /*---------------------------------------------------------------
134  getPosition
135  ---------------------------------------------------------------*/
137 {
138  auto& f = const_cast<std::ifstream&>(m_if);
139  if (m_if.is_open())
140  return f.tellg();
141  else
142  return 0;
143 }
144 
145 /*---------------------------------------------------------------
146  fileOpenCorrectly
147  ---------------------------------------------------------------*/
148 bool CFileInputStream::fileOpenCorrectly() const { return m_if.is_open(); }
149 /*---------------------------------------------------------------
150  readLine
151  ---------------------------------------------------------------*/
152 bool CFileInputStream::readLine(string& str)
153 {
154  str = string(); // clear() is not defined in VC6
155  if (!m_if.is_open()) return false;
156 
157  std::getline(m_if, str);
158  return !m_if.fail() && !m_if.eof();
159 }
160 
161 /*---------------------------------------------------------------
162  checkEOF
163  ---------------------------------------------------------------*/
165 {
166  if (!m_if.is_open()) return true;
167  return m_if.eof();
168 }
169 
171 {
172  if (m_if.is_open()) m_if.clear();
173 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
void close()
Close the stream.
bool checkEOF()
Will be true if EOF has been already reached.
#define MRPT_START
Definition: exceptions.h:241
bool open(const std::string &fileName)
Open a file for reading.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
void clearError()
Resets stream error status bits (e.g.
uint64_t Seek(int64_t off, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
STL namespace.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
std::ifstream m_if
The actual input file stream.
const_iterator end() const
Definition: ts_hash_map.h:246
#define MRPT_END
Definition: exceptions.h:245
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
CFileInputStream()
Default constructor.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
bool fileOpenCorrectly() const
Returns true if the file was open without errors.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020