MRPT  2.0.4
CFileOutputStream.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 CFileOutputStream::CFileOutputStream(const string& fileName, bool append)
19  : m_of()
20 {
22 
23  if (!open(fileName, append))
25  "Error creating/opening for write file: '%s'", fileName.c_str());
26 
27  MRPT_END
28 }
29 
31 bool CFileOutputStream::open(const string& fileName, bool append)
32 {
33  close();
34 
35  // Open for write/append:
36  ios_base::openmode openMode = ios_base::binary | ios_base::out;
37  if (append) openMode |= ios_base::app;
38 
39  m_of.open(fileName.c_str(), openMode);
40  return m_of.is_open();
41 }
42 
44 {
45  if (m_of.is_open()) m_of.close();
46 }
47 
50  [[maybe_unused]] void* Buffer, [[maybe_unused]] size_t Count)
51 {
52  THROW_EXCEPTION("Trying to read from a write file stream.");
53 }
54 
55 size_t CFileOutputStream::Write(const void* Buffer, size_t Count)
56 {
57  if (!m_of.is_open()) return 0;
58 
59  m_of.write(static_cast<const char*>(Buffer), Count);
60  return m_of.fail() ? 0 : Count;
61 }
62 
63 uint64_t CFileOutputStream::Seek(int64_t Offset, CStream::TSeekOrigin Origin)
64 {
65  if (!m_of.is_open()) return 0;
66 
67  ofstream::off_type offset = Offset;
68  ofstream::seekdir way;
69 
70  switch (Origin)
71  {
72  case sFromBeginning:
73  way = ios_base::beg;
74  break;
75  case sFromCurrent:
76  way = ios_base::cur;
77  break;
78  case sFromEnd:
79  way = ios_base::end;
80  break;
81  default:
82  THROW_EXCEPTION("Invalid value for 'Origin'");
83  }
84 
85  m_of.seekp(offset, way);
86  return getPosition();
87 }
88 
90 {
91  if (!fileOpenCorrectly()) return 0;
92 
93  auto& f = const_cast<std::ofstream&>(m_of);
94 
95  const uint64_t previousPos = f.tellp();
96  f.seekp(0, ios_base::end);
97  uint64_t fileSize = f.tellp();
98  f.seekp(previousPos, ios_base::beg);
99  return fileSize;
100 }
101 
103 {
104  auto& f = const_cast<std::ofstream&>(m_of);
105  if (m_of.is_open())
106  return f.tellp();
107  else
108  return 0;
109 }
110 
111 bool CFileOutputStream::fileOpenCorrectly() const { return m_of.is_open(); }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
#define MRPT_START
Definition: exceptions.h:241
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
CFileOutputStream()
Default constructor.
STL namespace.
std::ofstream m_of
The actual output file stream.
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
bool open(const std::string &fileName, bool append=false)
Open the given file for write.
const_iterator end() const
Definition: ts_hash_map.h:246
~CFileOutputStream() override
Destructor.
uint64_t getTotalBytesCount() const override
Method for getting the total number of bytes writen to buffer.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
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.
#define MRPT_END
Definition: exceptions.h:245
void close()
Close the stream.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69



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