MRPT  2.0.4
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 }
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
#define MRPT_START
Definition: exceptions.h:241
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
std::string std::string format(std::string_view fmt, ARGS &&... args)
Definition: format.h:26
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.
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.
std::optional< std::reference_wrapper< T > > optional_ref
Shorter name for std::optional<std::reference_wrapper<T>>
Definition: optional_ref.h:20
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
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.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
#define MRPT_END
Definition: exceptions.h:245
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.
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
CFileGZOutputStream()
Constructor, without opening the file.
Saves data to a file and transparently compress the data using the given compression level...
~CFileGZOutputStream() override
Destructor.



Page generated by Doxygen 1.8.14 for MRPT 2.0.4 Git: 33de1d0ad Sat Jun 20 11:02:42 2020 +0200 at sáb jun 20 17:35:17 CEST 2020