MRPT  2.0.4
CFileStream.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/io/CFileStream.h>
13 #include <iomanip>
14 #include <iostream>
15 #include <string> // std::getline()
16 
17 using namespace mrpt::io;
18 using namespace std;
19 
20 static_assert(
21  !std::is_copy_constructible_v<CFileStream> &&
22  !std::is_copy_assignable_v<CFileStream>,
23  "Copy Check");
24 
25 /*---------------------------------------------------------------
26  Constructor
27  ---------------------------------------------------------------*/
29 /*---------------------------------------------------------------
30  Constructor
31  ---------------------------------------------------------------*/
32 CFileStream::CFileStream(const string& fileName, TFileOpenModes mode_) : m_f()
33 {
34  std::ios_base::openmode mode = std::ios_base::in;
35  if (mode_ == fomRead)
36  mode = std::ios_base::in;
37  else if (mode_ == fomWrite)
38  mode = std::ios_base::out | std::ios_base::trunc;
39  else if (mode_ == fomAppend)
40  mode = std::ios_base::app | std::ios_base::out;
41  else if (mode_ == (fomRead | fomWrite))
42  mode = std::ios_base::in | std::ios_base::out | std::ios_base::trunc;
43  else if (mode_ == (fomAppend | fomWrite))
44  mode = std::ios_base::in | std::ios_base::out | std::ios_base::app;
45 
46  // Try to open the file:
47  m_f.open(fileName.c_str(), mode);
48  if (!m_f.is_open())
49  throw std::runtime_error(
50  std::string("CFileStream: Error creating/opening: ") + fileName);
51 }
52 
53 /*---------------------------------------------------------------
54  open
55  ---------------------------------------------------------------*/
56 bool CFileStream::open(const std::string& fileName, TFileOpenModes mode_)
57 {
58  std::ios_base::openmode mode = std::ios_base::in;
59  if (mode_ == fomRead)
60  mode = std::ios_base::in;
61  else if (mode_ == fomWrite)
62  mode = std::ios_base::out | std::ios_base::trunc;
63  else if (mode_ == fomAppend)
64  mode = std::ios_base::app | std::ios_base::out;
65  else if (mode_ == (fomRead | fomWrite))
66  mode = std::ios_base::in | std::ios_base::out | std::ios_base::trunc;
67  else if (mode_ == (fomAppend | fomWrite))
68  mode = std::ios_base::in | std::ios_base::out | std::ios_base::app;
69 
70  if (m_f.is_open()) m_f.close();
71 
72  m_f.open(fileName.c_str(), ios_base::binary | mode);
73  return m_f.is_open();
74 }
75 
76 /*---------------------------------------------------------------
77  close
78  ---------------------------------------------------------------*/
79 void CFileStream::close() { m_f.close(); }
80 /*---------------------------------------------------------------
81  Destructor
82  ---------------------------------------------------------------*/
84 /*---------------------------------------------------------------
85  Read
86  Reads bytes from the stream into Buffer
87  ---------------------------------------------------------------*/
88 size_t CFileStream::Read(void* Buffer, size_t Count)
89 {
90  if (!m_f.is_open()) return 0;
91  m_f.read(static_cast<char*>(Buffer), Count);
92  return m_f.fail() ? 0 : Count;
93 }
94 
95 /*---------------------------------------------------------------
96  Write
97  Writes a block of bytes to the stream.
98  ---------------------------------------------------------------*/
99 size_t CFileStream::Write(const void* Buffer, size_t Count)
100 {
101  if (!m_f.is_open()) return 0;
102 
103  m_f.write(static_cast<const char*>(Buffer), Count);
104  return m_f.fail() ? 0 : Count;
105 }
106 
107 /*---------------------------------------------------------------
108  Seek
109  Method for moving to a specified position in the streamed resource.
110  See documentation of CStream::Seek
111  ---------------------------------------------------------------*/
112 uint64_t CFileStream::Seek(int64_t Offset, CStream::TSeekOrigin Origin)
113 {
114  if (!m_f.is_open()) return 0;
115 
116  fstream::off_type offset = Offset;
117  fstream::seekdir way;
118 
119  switch (Origin)
120  {
121  case sFromBeginning:
122  way = ios_base::beg;
123  break;
124  case sFromCurrent:
125  way = ios_base::cur;
126  break;
127  case sFromEnd:
128  way = ios_base::end;
129  break;
130  default:
131  throw std::runtime_error(
132  "[CFileStream::Seek] Invalid value for 'Origin'");
133  }
134 
135  m_f.seekp(offset, way);
136  m_f.seekg(offset, way);
137 
138  return getPosition();
139 }
140 
142 {
143  if (!fileOpenCorrectly()) return 0;
144 
145  auto& f = const_cast<std::fstream&>(m_f);
146 
147  const uint64_t previousPos = f.tellg();
148  f.seekg(0, ios_base::end);
149  uint64_t fileSize = f.tellg();
150  f.seekg(previousPos, ios_base::beg);
151  return fileSize;
152 }
153 
154 uint64_t CFileStream::getPosition() const
155 {
156  auto& f = const_cast<std::fstream&>(m_f);
157  if (m_f.is_open())
158  return f.tellg();
159  else
160  return 0;
161 }
162 
163 /*---------------------------------------------------------------
164  getPositionI
165  ---------------------------------------------------------------*/
167 {
168  if (m_f.is_open())
169  return m_f.tellg();
170  else
171  return 0;
172 }
173 
174 /*---------------------------------------------------------------
175  getPositionO
176  ---------------------------------------------------------------*/
178 {
179  if (m_f.is_open())
180  return m_f.tellp();
181  else
182  return 0;
183 }
184 
185 /*---------------------------------------------------------------
186  fileOpenCorrectly
187  ---------------------------------------------------------------*/
188 bool CFileStream::fileOpenCorrectly() const { return m_f.is_open(); }
189 /*---------------------------------------------------------------
190  readLine
191  ---------------------------------------------------------------*/
192 bool CFileStream::readLine(string& str)
193 {
194  str = string(); // clear() is not defined in VC6
195  if (!m_f.is_open()) return false;
196 
197  std::getline(m_f, str);
198  return !m_f.fail() && !m_f.eof();
199 }
200 
201 /*---------------------------------------------------------------
202  checkEOF
203  ---------------------------------------------------------------*/
205 {
206  if (!m_f.is_open()) return true;
207  return m_f.eof();
208 }
209 
211 {
212  if (m_f.is_open()) m_f.clear();
213 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
uint64_t Seek(int64_t off, CStream::TSeekOrigin org=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
bool readLine(std::string &str)
Reads one string line from the file (until a new-line character)
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
uint64_t getPositionI()
The current Input cursor position, where 0 is the first byte.
std::fstream m_f
The actual input file stream.
Definition: CFileStream.h:41
void close()
Closes the file.
Definition: CFileStream.cpp:79
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CFileStream.cpp:88
STL namespace.
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
int TFileOpenModes
File open modes are used in CFileStream Posible values are:
Definition: CFileStream.h:22
~CFileStream() override
Destructor.
Definition: CFileStream.cpp:83
uint64_t getPositionO()
The current Input cursor position, where 0 is the first byte.
void clearError()
Resets stream error status bits (e.g.
CFileStream()
Constructor.
Definition: CFileStream.cpp:28
bool open(const std::string &fileName, TFileOpenModes mode=fomRead|fomWrite)
Opens the file, returning true on success.
Definition: CFileStream.cpp:56
const_iterator end() const
Definition: ts_hash_map.h:246
mrpt::vision::TStereoCalibResults out
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CFileStream.cpp:99
bool checkEOF()
Will be true if EOF has been already reached.
uint64_t getTotalBytesCount() const override
Returns the total amount of bytes in the stream.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 5711e29ae Wed May 27 14:29:47 2020 +0200 at miƩ may 27 14:30:10 CEST 2020