Main MRPT website > C++ reference for MRPT 1.5.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 
13 #ifdef _MSC_VER
14 # define _SCL_SECURE_NO_WARNINGS
15 #endif
16 
18 #include <mrpt/system/os.h>
19 #include <mrpt/system/os.h>
20 
21 using namespace mrpt::utils;
22 using namespace std;
23 
24 /*---------------------------------------------------------------
25  Constructor
26  ---------------------------------------------------------------*/
28  const string &fileName,
29  bool append ) : m_of()
30 {
32 
33  if (!open(fileName,append))
34  THROW_EXCEPTION_FMT( "Error creating/opening for write file: '%s'",fileName.c_str() );
35 
36  MRPT_END
37 }
38 
39 /*---------------------------------------------------------------
40  Constructor
41  ---------------------------------------------------------------*/
43 {
44 }
45 
46 /*---------------------------------------------------------------
47  open
48  ---------------------------------------------------------------*/
50  const string &fileName,
51  bool append )
52 {
53  close();
54 
55  // Open for write/append:
56  ios_base::openmode openMode = ios_base::binary | ios_base::out;
57  if ( append )
58  openMode |= ios_base::app;
59 
60  m_of.open(fileName.c_str(), openMode );
61  return m_of.is_open();
62 }
63 
64 /*---------------------------------------------------------------
65  close
66  ---------------------------------------------------------------*/
68 {
69  if (m_of.is_open()) m_of.close();
70 }
71 
72 /*---------------------------------------------------------------
73  Destructor
74  ---------------------------------------------------------------*/
76 {
77  close();
78 }
79 
80 
81 /*---------------------------------------------------------------
82  Read
83  Reads bytes from the stream into Buffer
84  ---------------------------------------------------------------*/
85 
86 size_t CFileOutputStream::Read(void *Buffer, size_t Count)
87 {
88  MRPT_UNUSED_PARAM(Buffer); MRPT_UNUSED_PARAM(Count);
89  THROW_EXCEPTION("Trying to read from a write file stream.");
90 }
91 
92 /*---------------------------------------------------------------
93  Write
94  Writes a block of bytes to the stream.
95  ---------------------------------------------------------------*/
96 size_t CFileOutputStream::Write(const void *Buffer, size_t Count)
97 {
98  if (!m_of.is_open()) return 0;
99 
100  m_of.write( static_cast<const char*>(Buffer),Count);
101  return m_of.fail() ? 0:Count;
102 }
103 
104 /*---------------------------------------------------------------
105  Seek
106  Method for moving to a specified position in the streamed resource.
107  See documentation of CStream::Seek
108  ---------------------------------------------------------------*/
110 {
111  if (!m_of.is_open()) return 0;
112 
113  ofstream::off_type offset = Offset;
114  ofstream::seekdir way;
115 
116  switch(Origin)
117  {
118  case sFromBeginning: way = ios_base::beg; break;
119  case sFromCurrent: way = ios_base::cur; break;
120  case sFromEnd: way = ios_base::end; break;
121  default: THROW_EXCEPTION("Invalid value for 'Origin'");
122  }
123 
124  m_of.seekp(offset, way);
125  return getPosition();
126 }
127 
128 /*---------------------------------------------------------------
129  getTotalBytesCount
130  ---------------------------------------------------------------*/
132 {
133  if (!fileOpenCorrectly()) return 0;
134 
135  uint64_t previousPos = getPosition();
136  uint64_t fileSize = Seek(0,sFromEnd);
137  Seek(previousPos);
138  return fileSize;
139 }
140 
141 /*---------------------------------------------------------------
142  getPosition
143  ---------------------------------------------------------------*/
145 {
146  if (m_of.is_open())
147  return m_of.tellp();
148  else return 0;
149 }
150 
151 /*---------------------------------------------------------------
152  fileOpenCorrectly
153  ---------------------------------------------------------------*/
155 {
156  return m_of.is_open();
157 }
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.
Definition: zip.h:16
TSeekOrigin
Used in CStream::Seek.
Definition: CStream.h:42
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
GLintptr offset
Definition: glext.h:3780
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 getTotalBytesCount() MRPT_OVERRIDE
Method for getting the total number of bytes writen to buffer.
size_t Read(void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for reading from the stream.
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
__int64 int64_t
Definition: rptypes.h:51
GLuint GLuint end
Definition: glext.h:3512
uint64_t getPosition() MRPT_OVERRIDE
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
size_t Write(const void *Buffer, size_t Count) MRPT_OVERRIDE
Introduces a pure virtual method responsible for writing to the stream.
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:52
CFileOutputStream()
Default constructor.
uint64_t Seek(int64_t Offset, CStream::TSeekOrigin Origin=sFromBeginning) MRPT_OVERRIDE
Introduces a pure virtual method for moving to a specified position in the streamed resource...
virtual ~CFileOutputStream()
Destructor.



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020