MRPT  1.9.9
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 }
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
uint64_t getPosition() const override
Method for getting the current cursor position in the compressed, where 0 is the first byte and Total...
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
STL namespace.
std::optional< std::reference_wrapper< T > > optional_ref
Shorter name for std::optional<std::reference_wrapper<T>>
Definition: optional_ref.h:20
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
bool open(const std::string &fileName, mrpt::optional_ref< std::string > error_msg=std::nullopt)
Opens the file for read.
bool checkEOF()
Will be true if EOF has been already reached.
uint64_t getTotalBytesCount() const override
Method for getting the total number of compressed bytes of in the file (the physical size of the comp...
uint64_t Seek(int64_t, CStream::TSeekOrigin=sFromBeginning) override
This method is not implemented in this class.
uint64_t m_file_size
Compressed file size.
This is the global namespace for all Mobile Robot Programming Toolkit (MRPT) libraries.
void close()
Closes the file.
#define MRPT_END
Definition: exceptions.h:245
Transparently opens a compressed "gz" file and reads uncompressed data from it.
pimpl< T > make_impl(Args &&... args)
Definition: pimpl.h:18
CFileGZInputStream()
Constructor without open.
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.14 for MRPT 1.9.9 Git: c7a3bec24 Sun Mar 29 18:33:13 2020 +0200 at dom mar 29 18:50:38 CEST 2020