MRPT  2.0.3
CFileGZOutputStream.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 #include <cerrno>
15 #include <cstring> // strerror
16 
17 #include <zlib.h>
18 
19 using namespace mrpt::io;
20 using namespace std;
21 
23 {
24  gzFile f{nullptr};
25 };
26 
29 {
30 }
31 
34 {
36  std::string err_msg;
37  if (!open(fileName, 1, err_msg))
39  "Error trying to open file: '%s', error: '%s'", fileName.c_str(),
40  err_msg.c_str()));
41  MRPT_END
42 }
43 
45  const string& fileName, int compress_level,
47 {
49 
50  if (m_f->f) gzclose(m_f->f);
51 
52  // Open gz stream:
53  m_f->f = gzopen(fileName.c_str(), format("wb%i", compress_level).c_str());
54  if (m_f->f == nullptr && error_msg)
55  error_msg.value().get() = std::string(strerror(errno));
56  return m_f->f != nullptr;
57 
58  MRPT_END
59 }
60 
63 {
64  if (m_f->f)
65  {
66  gzclose(m_f->f);
67  m_f->f = nullptr;
68  }
69 }
70 
71 size_t CFileGZOutputStream::Read(void*, size_t)
72 {
73  THROW_EXCEPTION("Trying to read from an output file stream.");
74 }
75 
76 size_t CFileGZOutputStream::Write(const void* Buffer, size_t Count)
77 {
78  if (!m_f->f)
79  {
80  THROW_EXCEPTION("File is not open.");
81  }
82  return gzwrite(m_f->f, const_cast<void*>(Buffer), Count);
83 }
84 
86 {
87  if (!m_f->f)
88  {
89  THROW_EXCEPTION("File is not open.");
90  }
91  return gztell(m_f->f);
92 }
93 
95 {
96  return m_f->f != nullptr;
97 }
99 {
100  THROW_EXCEPTION("Method not available in this class.");
101 }
102 
104 {
105  THROW_EXCEPTION("Method not available in this class.");
106 }
mrpt::io::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
mrpt::io::CFileGZOutputStream::m_f
mrpt::pimpl< Impl > m_f
Definition: io/CFileGZOutputStream.h:29
exceptions.h
mrpt::io
Definition: img/CImage.h:24
mrpt::io::CFileGZOutputStream::open
bool open(const std::string &fileName, int compress_level=1, mrpt::optional_ref< std::string > error_msg=std::nullopt)
Open a file for write, choosing the compression level.
Definition: CFileGZOutputStream.cpp:44
CFileGZOutputStream.h
mrpt::io::CFileGZOutputStream::Write
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CFileGZOutputStream.cpp:76
mrpt::io::CFileGZOutputStream::getPosition
uint64_t getPosition() const override
Method for getting the current cursor position, where 0 is the first byte and TotalBytesCount-1 the l...
Definition: CFileGZOutputStream.cpp:85
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
mrpt::io::CFileGZOutputStream::Impl
Definition: CFileGZOutputStream.cpp:22
MRPT_START
#define MRPT_START
Definition: exceptions.h:241
mrpt::io::CFileGZOutputStream
Saves data to a file and transparently compress the data using the given compression level.
Definition: io/CFileGZOutputStream.h:26
mrpt::io::CFileGZOutputStream::CFileGZOutputStream
CFileGZOutputStream()
Constructor, without opening the file.
Definition: CFileGZOutputStream.cpp:27
mrpt::io::CFileGZOutputStream::getTotalBytesCount
uint64_t getTotalBytesCount() const override
This method is not implemented in this class.
Definition: CFileGZOutputStream.cpp:103
mrpt::io::CFileGZOutputStream::fileOpenCorrectly
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
Definition: CFileGZOutputStream.cpp:94
mrpt::io::CFileGZOutputStream::close
void close()
Close the file.
Definition: CFileGZOutputStream.cpp:62
MRPT_END
#define MRPT_END
Definition: exceptions.h:245
mrpt::make_impl
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
mrpt::io::CFileGZOutputStream::Seek
uint64_t Seek(int64_t, CStream::TSeekOrigin=sFromBeginning) override
This method is not implemented in this class.
Definition: CFileGZOutputStream.cpp:98
mrpt::io::CFileGZOutputStream::Read
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CFileGZOutputStream.cpp:71
mrpt::optional_ref
std::optional< std::reference_wrapper< T > > optional_ref
Shorter name for std::optional<std::reference_wrapper<T>>
Definition: optional_ref.h:20
io-precomp.h
mrpt::format
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
mrpt::io::CFileGZOutputStream::~CFileGZOutputStream
~CFileGZOutputStream() override
Destructor.
Definition: CFileGZOutputStream.cpp:61



Page generated by Doxygen 1.8.17 for MRPT 2.0.3 at Fri May 29 13:06:46 UTC 2020