MRPT  1.9.9
CFileGZInputStream.cpp
Go to the documentation of this file.
1 /* +------------------------------------------------------------------------+
2  | Mobile Robot Programming Toolkit (MRPT) |
3  | http://www.mrpt.org/ |
4  | |
5  | Copyright (c) 2005-2018, Individual contributors, see AUTHORS file |
6  | See: http://www.mrpt.org/Authors - All rights reserved. |
7  | Released under BSD License. See details in http://www.mrpt.org/License |
8  +------------------------------------------------------------------------+ */
9 
10 #include "io-precomp.h" // Precompiled headers
11 
13 #include <mrpt/system/filesystem.h>
14 #include <mrpt/core/exceptions.h>
15 
16 #include <zlib.h>
17 
18 using namespace mrpt::io;
19 using namespace std;
20 
21 static_assert(
24  "Copy Check");
25 
26 #define THE_GZFILE reinterpret_cast<gzFile>(m_f)
27 
28 CFileGZInputStream::CFileGZInputStream(const string& fileName) : m_f(nullptr)
29 {
31  open(fileName);
32  MRPT_END
33 }
34 
37 {
39 
40  if (m_f) gzclose(THE_GZFILE);
41 
42  // Get compressed file size:
44  if (m_file_size == uint64_t(-1))
45  THROW_EXCEPTION_FMT("Couldn't access the file '%s'", fileName.c_str());
46 
47  // Open gz stream:
48  m_f = gzopen(fileName.c_str(), "rb");
49  return m_f != nullptr;
50 
51  MRPT_END
52 }
53 
55 {
56  if (m_f)
57  {
58  gzclose(THE_GZFILE);
59  m_f = nullptr;
60  }
61 }
62 
64 size_t CFileGZInputStream::Read(void* Buffer, size_t Count)
65 {
66  if (!m_f)
67  {
68  THROW_EXCEPTION("File is not open.");
69  }
70 
71  return gzread(THE_GZFILE, Buffer, Count);
72 }
73 
74 size_t CFileGZInputStream::Write(const void* Buffer, size_t Count)
75 {
76  MRPT_UNUSED_PARAM(Buffer);
77  MRPT_UNUSED_PARAM(Count);
78  THROW_EXCEPTION("Trying to write to an input file stream.");
79 }
80 
82 {
83  if (!m_f)
84  {
85  THROW_EXCEPTION("File is not open.");
86  }
87  return m_file_size;
88 }
89 
91 {
92  if (!m_f)
93  {
94  THROW_EXCEPTION("File is not open.");
95  }
96  return gztell(THE_GZFILE);
97 }
98 
99 bool CFileGZInputStream::fileOpenCorrectly() const { return m_f != nullptr; }
101 {
102  if (!m_f)
103  return true;
104  else
105  return 0 != gzeof(THE_GZFILE);
106 }
107 
109 {
110  THROW_EXCEPTION("Method not available in this class.");
111 }
#define THE_GZFILE
uint64_t getTotalBytesCount() const override
Method for getting the total number of compressed bytes of in the file (the physical size of the comp...
bool fileOpenCorrectly() const
Returns true if the file was open without errors.
void close()
Closes the file.
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
uint64_t getPosition() const override
Method for getting the current cursor position in the compressed, where 0 is the first byte and Total...
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.
bool checkEOF()
Will be true if EOF has been already reached.
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
bool open(const std::string &fileName)
Opens the file for read.
CFileGZInputStream()
Constructor without open.
TSeekOrigin
Used in CStream::Seek.
Definition: io/CStream.h:33
#define MRPT_UNUSED_PARAM(a)
Determines whether this is an X86 or AMD64 platform.
Definition: common.h:186
#define MRPT_START
Definition: exceptions.h:262
#define MRPT_END
Definition: exceptions.h:266
#define THROW_EXCEPTION(msg)
Definition: exceptions.h:41
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
Definition: exceptions.h:43
GLsizei const GLfloat * value
Definition: glext.h:4117
GLsizei const GLchar ** string
Definition: glext.h:4101
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:350
__int64 int64_t
Definition: rptypes.h:49
unsigned __int64 uint64_t
Definition: rptypes.h:50



Page generated by Doxygen 1.9.1 for MRPT 1.9.9 Git: 814d80880 Fri Aug 24 01:51:28 2018 +0200 at mar 26 may 2026 12:30:59 CEST