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



Page generated by Doxygen 1.8.14 for MRPT 1.5.9 Git: 690a4699f Wed Apr 15 19:29:53 2020 +0200 at miƩ abr 15 19:30:12 CEST 2020