Main MRPT website > C++ reference for MRPT 1.9.9
CFileGZOutputStream.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-2018, 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 "io-precomp.h" // Precompiled headers
11 
13 #include <mrpt/core/exceptions.h>
14 
15 #include <zlib.h>
16 
17 #define THE_GZFILE reinterpret_cast<gzFile>(m_f)
18 
19 using namespace mrpt::io;
20 using namespace std;
21 
22 CFileGZOutputStream::CFileGZOutputStream(const string& fileName) : m_f(nullptr)
23 {
25  if (!open(fileName))
27  "Error trying to open file: '%s'", fileName.c_str());
28  MRPT_END
29 }
30 
32 bool CFileGZOutputStream::open(const string& fileName, int compress_level)
33 {
35 
36  if (m_f) gzclose(THE_GZFILE);
37 
38  // Open gz stream:
39  m_f = gzopen(fileName.c_str(), format("wb%i", compress_level).c_str());
40  return m_f != nullptr;
41 
42  MRPT_END
43 }
44 
47 {
48  if (m_f)
49  {
50  gzclose(THE_GZFILE);
51  m_f = nullptr;
52  }
53 }
54 
55 size_t CFileGZOutputStream::Read(void*, size_t)
56 {
57  THROW_EXCEPTION("Trying to read from an output file stream.");
58 }
59 
60 size_t CFileGZOutputStream::Write(const void* Buffer, size_t Count)
61 {
62  if (!m_f)
63  {
64  THROW_EXCEPTION("File is not open.");
65  }
66  return gzwrite(THE_GZFILE, const_cast<void*>(Buffer), Count);
67 }
68 
70 {
71  if (!m_f)
72  {
73  THROW_EXCEPTION("File is not open.");
74  }
75  return gztell(THE_GZFILE);
76 }
77 
78 bool CFileGZOutputStream::fileOpenCorrectly() const { return m_f != nullptr; }
80 {
81  THROW_EXCEPTION("Method not available in this class.");
82 }
83 
85 {
86  THROW_EXCEPTION("Method not available in this class.");
87 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:34
#define MRPT_START
Definition: exceptions.h:262
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
virtual ~CFileGZOutputStream()
Destructor.
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
STL namespace.
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
#define THE_GZFILE
__int64 int64_t
Definition: rptypes.h:49
std::string format(const char *fmt,...) MRPT_printf_format_check(1
A std::string version of C sprintf.
Definition: format.cpp:16
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
uint64_t getTotalBytesCount() const override
This method is not implemented in this class.
unsigned __int64 uint64_t
Definition: rptypes.h:50
#define MRPT_END
Definition: exceptions.h:266
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
uint64_t Seek(int64_t, CStream::TSeekOrigin=sFromBeginning) override
This method is not implemented in this class.
CFileGZOutputStream()
Constructor, without opening the file.
bool open(const std::string &fileName, int compress_level=1)
Open a file for write, choosing the compression level.
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ad3a9d8ae Tue May 1 23:10:22 2018 -0700 at lun oct 28 00:14:14 CET 2019