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-2019, 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  THROW_EXCEPTION_FMT("Couldn't access the file '%s'", fileName.c_str());
57 
58  // Open gz stream:
59  m_f->f = gzopen(fileName.c_str(), "rb");
60  if (m_f->f == nullptr && error_msg)
61  error_msg.value().get() = std::string(strerror(errno));
62 
63  return m_f->f != nullptr;
64  MRPT_END
65 }
66 
68 {
69  if (m_f->f)
70  {
71  gzclose(m_f->f);
72  m_f->f = nullptr;
73  }
74 }
75 
77 size_t CFileGZInputStream::Read(void* Buffer, size_t Count)
78 {
79  if (!m_f->f)
80  {
81  THROW_EXCEPTION("File is not open.");
82  }
83 
84  return gzread(m_f->f, Buffer, Count);
85 }
86 
87 size_t CFileGZInputStream::Write(const void* Buffer, size_t Count)
88 {
89  MRPT_UNUSED_PARAM(Buffer);
90  MRPT_UNUSED_PARAM(Count);
91  THROW_EXCEPTION("Trying to write to an input file stream.");
92 }
93 
95 {
96  if (!m_f->f)
97  {
98  THROW_EXCEPTION("File is not open.");
99  }
100  return m_file_size;
101 }
102 
104 {
105  if (!m_f->f)
106  {
107  THROW_EXCEPTION("File is not open.");
108  }
109  return gztell(m_f->f);
110 }
111 
112 bool CFileGZInputStream::fileOpenCorrectly() const { return m_f->f != nullptr; }
114 {
115  if (!m_f->f)
116  return true;
117  else
118  return 0 != gzeof(m_f->f);
119 }
120 
122 {
123  THROW_EXCEPTION("Method not available in this class.");
124 }
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
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
voidp gzFile
Definition: zlib.h:1047
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.
__int64 int64_t
Definition: rptypes.h:52
uint64_t getTotalBytesCount() const override
Method for getting the total number of compressed bytes of in the file (the physical size of the comp...
GLsizei const GLchar ** string
Definition: glext.h:4116
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.
unsigned __int64 uint64_t
Definition: rptypes.h:53
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 THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:69
#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: 8fe78517f Sun Jul 14 19:43:28 2019 +0200 at lun oct 28 02:10:00 CET 2019