MRPT  1.9.9
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-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>
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 
49 size_t CFileOutputStream::Read(void* Buffer, size_t Count)
50 {
51  MRPT_UNUSED_PARAM(Buffer);
52  MRPT_UNUSED_PARAM(Count);
53  THROW_EXCEPTION("Trying to read from a write file stream.");
54 }
55 
56 size_t CFileOutputStream::Write(const void* Buffer, size_t Count)
57 {
58  if (!m_of.is_open()) return 0;
59 
60  m_of.write(static_cast<const char*>(Buffer), Count);
61  return m_of.fail() ? 0 : Count;
62 }
63 
65 {
66  if (!m_of.is_open()) return 0;
67 
68  ofstream::off_type offset = Offset;
69  ofstream::seekdir way;
70 
71  switch (Origin)
72  {
73  case sFromBeginning:
74  way = ios_base::beg;
75  break;
76  case sFromCurrent:
77  way = ios_base::cur;
78  break;
79  case sFromEnd:
80  way = ios_base::end;
81  break;
82  default:
83  THROW_EXCEPTION("Invalid value for 'Origin'");
84  }
85 
86  m_of.seekp(offset, way);
87  return getPosition();
88 }
89 
91 {
92  if (!fileOpenCorrectly()) return 0;
93 
94  auto& f = const_cast<std::ofstream&>(m_of);
95 
96  const uint64_t previousPos = f.tellp();
97  f.seekp(0, ios_base::end);
98  uint64_t fileSize = f.tellp();
99  f.seekp(previousPos, ios_base::beg);
100  return fileSize;
101 }
102 
104 {
105  auto& f = const_cast<std::ofstream&>(m_of);
106  if (m_of.is_open())
107  return f.tellp();
108  else
109  return 0;
110 }
111 
112 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.
GLintptr offset
Definition: glext.h:3936
STL namespace.
std::ofstream m_of
The actual output file stream.
__int64 int64_t
Definition: rptypes.h:52
GLuint GLuint end
Definition: glext.h:3532
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.
unsigned __int64 uint64_t
Definition: rptypes.h:53
~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.
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
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019