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 
92 size_t CFileGZInputStream::Write(const void* Buffer, size_t Count)
93 {
94  MRPT_UNUSED_PARAM(Buffer);
95  MRPT_UNUSED_PARAM(Count);
96  THROW_EXCEPTION("Trying to write to an input file stream.");
97 }
98 
100 {
101  if (!m_f->f)
102  {
103  THROW_EXCEPTION("File is not open.");
104  }
105  return m_file_size;
106 }
107 
109 {
110  if (!m_f->f)
111  {
112  THROW_EXCEPTION("File is not open.");
113  }
114  return gztell(m_f->f);
115 }
116 
117 bool CFileGZInputStream::fileOpenCorrectly() const { return m_f->f != nullptr; }
119 {
120  if (!m_f->f)
121  return true;
122  else
123  return 0 != gzeof(m_f->f);
124 }
125 
127 {
128  THROW_EXCEPTION("Method not available in this class.");
129 }
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
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: 3a26b90fd Wed Mar 25 20:17:03 2020 +0100 at miƩ mar 25 23:05:41 CET 2020