Main MRPT website > C++ reference for MRPT 1.9.9
CFileOutputStream.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2017, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "base-precomp.h" // Precompiled headers
11 
12 #ifdef _MSC_VER
13 #define _SCL_SECURE_NO_WARNINGS
14 #endif
15 
17 #include <mrpt/system/os.h>
18 #include <mrpt/system/os.h>
19 
20 using namespace mrpt::utils;
21 using namespace std;
22 
23 /*---------------------------------------------------------------
24  Constructor
25  ---------------------------------------------------------------*/
26 CFileOutputStream::CFileOutputStream(const string& fileName, bool append)
27  : m_of()
28 {
30 
31  if (!open(fileName, append))
33  "Error creating/opening for write file: '%s'", fileName.c_str());
34 
35  MRPT_END
36 }
37 
38 /*---------------------------------------------------------------
39  Constructor
40  ---------------------------------------------------------------*/
42 /*---------------------------------------------------------------
43  open
44  ---------------------------------------------------------------*/
45 bool CFileOutputStream::open(const string& fileName, bool append)
46 {
47  close();
48 
49  // Open for write/append:
50  ios_base::openmode openMode = ios_base::binary | ios_base::out;
51  if (append) openMode |= ios_base::app;
52 
53  m_of.open(fileName.c_str(), openMode);
54  return m_of.is_open();
55 }
56 
57 /*---------------------------------------------------------------
58  close
59  ---------------------------------------------------------------*/
61 {
62  if (m_of.is_open()) m_of.close();
63 }
64 
65 /*---------------------------------------------------------------
66  Destructor
67  ---------------------------------------------------------------*/
69 /*---------------------------------------------------------------
70  Read
71  Reads bytes from the stream into Buffer
72  ---------------------------------------------------------------*/
73 
74 size_t CFileOutputStream::Read(void* Buffer, size_t Count)
75 {
76  MRPT_UNUSED_PARAM(Buffer);
77  MRPT_UNUSED_PARAM(Count);
78  THROW_EXCEPTION("Trying to read from a write file stream.");
79 }
80 
81 /*---------------------------------------------------------------
82  Write
83  Writes a block of bytes to the stream.
84  ---------------------------------------------------------------*/
85 size_t CFileOutputStream::Write(const void* Buffer, size_t Count)
86 {
87  if (!m_of.is_open()) return 0;
88 
89  m_of.write(static_cast<const char*>(Buffer), Count);
90  return m_of.fail() ? 0 : Count;
91 }
92 
93 /*---------------------------------------------------------------
94  Seek
95  Method for moving to a specified position in the streamed resource.
96  See documentation of CStream::Seek
97  ---------------------------------------------------------------*/
99 {
100  if (!m_of.is_open()) return 0;
101 
102  ofstream::off_type offset = Offset;
103  ofstream::seekdir way;
104 
105  switch (Origin)
106  {
107  case sFromBeginning:
108  way = ios_base::beg;
109  break;
110  case sFromCurrent:
111  way = ios_base::cur;
112  break;
113  case sFromEnd:
114  way = ios_base::end;
115  break;
116  default:
117  THROW_EXCEPTION("Invalid value for 'Origin'");
118  }
119 
120  m_of.seekp(offset, way);
121  return getPosition();
122 }
123 
124 /*---------------------------------------------------------------
125  getTotalBytesCount
126  ---------------------------------------------------------------*/
128 {
129  if (!fileOpenCorrectly()) return 0;
130 
131  uint64_t previousPos = getPosition();
132  uint64_t fileSize = Seek(0, sFromEnd);
133  Seek(previousPos);
134  return fileSize;
135 }
136 
137 /*---------------------------------------------------------------
138  getPosition
139  ---------------------------------------------------------------*/
141 {
142  if (m_of.is_open())
143  return m_of.tellp();
144  else
145  return 0;
146 }
147 
148 /*---------------------------------------------------------------
149  fileOpenCorrectly
150  ---------------------------------------------------------------*/
151 bool CFileOutputStream::fileOpenCorrectly() { return m_of.is_open(); }
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
bool fileOpenCorrectly()
Returns true if the file was open without errors.
void close()
Close the stream.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
uint64_t Seek(uint64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) override
Introduces a pure virtual method for moving to a specified position in the streamed resource...
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:45
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLintptr offset
Definition: glext.h:3925
bool open(const std::string &fileName, bool append=false)
Open the given file for write.
std::ofstream m_of
The actual output file stream.
STL namespace.
uint64_t getPosition() override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
GLuint GLuint end
Definition: glext.h:3528
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:50
CFileOutputStream()
Default constructor.
virtual ~CFileOutputStream()
Destructor.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
uint64_t getTotalBytesCount() override
Method for getting the total number of bytes writen to buffer.



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019