Main MRPT website > C++ reference for 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-2017, 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 "base-precomp.h" // Precompiled headers
11 
12 #ifdef _MSC_VER
13 #define _SCL_SECURE_NO_WARNINGS
14 #endif
15 
17 #include <mrpt/system/os.h>
18 #include <mrpt/system/filesystem.h>
19 
20 #include <zlib.h>
21 
22 using namespace mrpt::utils;
23 using namespace std;
24 
25 #define THE_GZFILE reinterpret_cast<gzFile>(m_f)
26 
27 /*---------------------------------------------------------------
28  Constructor
29  ---------------------------------------------------------------*/
30 CFileGZInputStream::CFileGZInputStream(const string& fileName) : m_f(nullptr)
31 {
33  open(fileName);
34  MRPT_END
35 }
36 /*---------------------------------------------------------------
37  Constructor
38  ---------------------------------------------------------------*/
40 /*---------------------------------------------------------------
41  open
42  ---------------------------------------------------------------*/
44 {
46 
47  if (m_f) gzclose(THE_GZFILE);
48 
49  // Get compressed file size:
51  if (m_file_size == uint64_t(-1))
52  THROW_EXCEPTION_FMT("Couldn't access the file '%s'", fileName.c_str());
53 
54  // Open gz stream:
55  m_f = gzopen(fileName.c_str(), "rb");
56  return m_f != nullptr;
57 
58  MRPT_END
59 }
60 
61 /*---------------------------------------------------------------
62  close
63  ---------------------------------------------------------------*/
65 {
66  if (m_f)
67  {
68  gzclose(THE_GZFILE);
69  m_f = nullptr;
70  }
71 }
72 
73 /*---------------------------------------------------------------
74  Destructor
75  ---------------------------------------------------------------*/
77 /*---------------------------------------------------------------
78  Read
79  Reads bytes from the stream into Buffer
80  ---------------------------------------------------------------*/
81 size_t CFileGZInputStream::Read(void* Buffer, size_t Count)
82 {
83  if (!m_f)
84  {
85  THROW_EXCEPTION("File is not open.");
86  }
87 
88  return gzread(THE_GZFILE, Buffer, Count);
89 }
90 
91 /*---------------------------------------------------------------
92  Write
93  Writes a block of bytes to the stream.
94  ---------------------------------------------------------------*/
95 size_t CFileGZInputStream::Write(const void* Buffer, size_t Count)
96 {
97  MRPT_UNUSED_PARAM(Buffer);
98  MRPT_UNUSED_PARAM(Count);
99  THROW_EXCEPTION("Trying to write to an input file stream.");
100 }
101 
102 /*---------------------------------------------------------------
103  getTotalBytesCount
104  ---------------------------------------------------------------*/
106 {
107  if (!m_f)
108  {
109  THROW_EXCEPTION("File is not open.");
110  }
111  return m_file_size;
112 }
113 
114 /*---------------------------------------------------------------
115  getPosition
116  ---------------------------------------------------------------*/
118 {
119  if (!m_f)
120  {
121  THROW_EXCEPTION("File is not open.");
122  }
123  return gztell(THE_GZFILE);
124 }
125 
126 /*---------------------------------------------------------------
127  fileOpenCorrectly
128  ---------------------------------------------------------------*/
129 bool CFileGZInputStream::fileOpenCorrectly() { return m_f != nullptr; }
130 /*---------------------------------------------------------------
131  checkEOF
132  ---------------------------------------------------------------*/
134 {
135  if (!m_f)
136  return true;
137  else
138  return 0 != gzeof(THE_GZFILE);
139 }
uint64_t m_file_size
Compressed file size.
Classes for serialization, sockets, ini-file manipulation, streams, list of properties-values, timewatch, extensions to STL.
bool open(const std::string &fileName)
Opens the file for read.
#define THROW_EXCEPTION(msg)
#define THROW_EXCEPTION_FMT(_FORMAT_STRING,...)
size_t Write(const void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for writing to the stream.
STL namespace.
uint64_t getTotalBytesCount() override
Method for getting the total number of compressed bytes of in the file (the physical size of the comp...
size_t Read(void *Buffer, size_t Count) override
Introduces a pure virtual method responsible for reading from the stream.
#define MRPT_END
#define MRPT_UNUSED_PARAM(a)
Can be used to avoid "not used parameters" warnings from the compiler.
#define THE_GZFILE
GLsizei const GLchar ** string
Definition: glext.h:4101
#define MRPT_START
unsigned __int64 uint64_t
Definition: rptypes.h:50
bool fileOpenCorrectly()
Returns true if the file was open without errors.
bool checkEOF()
Will be true if EOF has been already reached.
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:349
uint64_t getPosition() override
Method for getting the current cursor position in the compressed, where 0 is the first byte and Total...



Page generated by Doxygen 1.8.14 for MRPT 1.9.9 Git: ae4571287 Thu Nov 23 00:06:53 2017 +0100 at dom oct 27 23:51:55 CET 2019