MRPT  2.0.3
CFileGZInputStream.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 <mrpt/system/filesystem.h>
15 #include <cerrno>
16 #include <cstring> // strerror
17 
18 #include <zlib.h>
19 
20 using namespace mrpt::io;
21 using namespace std;
22 
23 static_assert(
24  !std::is_copy_constructible_v<CFileGZInputStream> &&
25  !std::is_copy_assignable_v<CFileGZInputStream>,
26  "Copy Check");
27 
29 {
30  gzFile f{nullptr};
31 };
32 
35 {
36 }
37 
40 {
42  open(fileName);
43  MRPT_END
44 }
45 
47  const std::string& fileName, mrpt::optional_ref<std::string> error_msg)
48 {
50 
51  if (m_f->f) gzclose(m_f->f);
52 
53  // Get compressed file size:
55  if (m_file_size == uint64_t(-1))
56  {
57  if (error_msg)
58  error_msg.value().get() =
59  mrpt::format("Couldn't access the file '%s'", fileName.c_str());
60  return false;
61  }
62 
63  // Open gz stream:
64  m_f->f = gzopen(fileName.c_str(), "rb");
65  if (m_f->f == nullptr && error_msg)
66  error_msg.value().get() = std::string(strerror(errno));
67 
68  return m_f->f != nullptr;
69  MRPT_END
70 }
71 
73 {
74  if (m_f->f)
75  {
76  gzclose(m_f->f);
77  m_f->f = nullptr;
78  }
79 }
80 
82 size_t CFileGZInputStream::Read(void* Buffer, size_t Count)
83 {
84  if (!m_f->f)
85  {
86  THROW_EXCEPTION("File is not open.");
87  }
88 
89  return gzread(m_f->f, Buffer, Count);
90 }
91 
93  [[maybe_unused]] const void* Buffer, [[maybe_unused]] size_t Count)
94 {
95  THROW_EXCEPTION("Trying to write to an input file stream.");
96 }
97 
99 {
100  if (!m_f->f)
101  {
102  THROW_EXCEPTION("File is not open.");
103  }
104  return m_file_size;
105 }
106 
108 {
109  if (!m_f->f)
110  {
111  THROW_EXCEPTION("File is not open.");
112  }
113  return gztell(m_f->f);
114 }
115 
116 bool CFileGZInputStream::fileOpenCorrectly() const { return m_f->f != nullptr; }
118 {
119  if (!m_f->f)
120  return true;
121  else
122  return 0 != gzeof(m_f->f);
123 }
124 
126 {
127  THROW_EXCEPTION("Method not available in this class.");
128 }
filesystem.h
mrpt::io::CStream::TSeekOrigin
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:32
mrpt::io::CFileGZInputStream::CFileGZInputStream
CFileGZInputStream()
Constructor without open.
Definition: CFileGZInputStream.cpp:33
exceptions.h
mrpt::io
Definition: img/CImage.h:24
mrpt::io::CFileGZInputStream::Write
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
Definition: CFileGZInputStream.cpp:92
mrpt
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
Definition: BaseAppDataSource.h:15
mrpt::io::CFileGZInputStream::getTotalBytesCount
uint64_t getTotalBytesCount() const override
Method for getting the total number of compressed bytes of in the file (the physical size of the comp...
Definition: CFileGZInputStream.cpp:98
THROW_EXCEPTION
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:67
mrpt::io::CFileGZInputStream::Read
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
Definition: CFileGZInputStream.cpp:82
mrpt::io::CFileGZInputStream::Seek
uint64_t Seek(int64_t, CStream::TSeekOrigin=sFromBeginning) override
This method is not implemented in this class.
Definition: CFileGZInputStream.cpp:125
mrpt::io::CFileGZInputStream
Transparently opens a compressed "gz" file and reads uncompressed data from it.
Definition: io/CFileGZInputStream.h:26
MRPT_START
#define MRPT_START
Definition: exceptions.h:241
mrpt::io::CFileGZInputStream::close
void close()
Closes the file.
Definition: CFileGZInputStream.cpp:72
mrpt::io::CFileGZInputStream::open
bool open(const std::string &fileName, mrpt::optional_ref< std::string > error_msg=std::nullopt)
Opens the file for read.
Definition: CFileGZInputStream.cpp:46
mrpt::io::CFileGZInputStream::Impl
Definition: CFileGZInputStream.cpp:28
mrpt::io::CFileGZInputStream::m_file_size
uint64_t m_file_size
Compressed file size.
Definition: io/CFileGZInputStream.h:32
mrpt::io::CFileGZInputStream::getPosition
uint64_t getPosition() const override
Method for getting the current cursor position in the compressed, where 0 is the first byte and Total...
Definition: CFileGZInputStream.cpp:107
MRPT_END
#define MRPT_END
Definition: exceptions.h:245
mrpt::io::CFileGZInputStream::~CFileGZInputStream
~CFileGZInputStream() override
Dtor.
Definition: CFileGZInputStream.cpp:81
CFileGZInputStream.h
mrpt::make_impl
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
mrpt::io::CFileGZInputStream::checkEOF
bool checkEOF()
Will be true if EOF has been already reached.
Definition: CFileGZInputStream.cpp:117
mrpt::io::CFileGZInputStream::fileOpenCorrectly
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
Definition: CFileGZInputStream.cpp:116
mrpt::io::CFileGZInputStream::m_f
mrpt::pimpl< Impl > m_f
Definition: io/CFileGZInputStream.h:29
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::system::getFileSize
uint64_t getFileSize(const std::string &fileName)
Return the size of the given file, or size_t(-1) if some error is found accessing that file.
Definition: filesystem.cpp:351



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