EMODnet Quantized Mesh Generator for Cesium
gzip_file_reader.h
1 // Copyright (c) 2018 Coronis Computing S.L. (Spain)
2 // All rights reserved.
3 //
4 // This file is part of EMODnet Quantized Mesh Generator for Cesium.
5 //
6 // This program is free software: you can redistribute it and/or modify
7 // it under the terms of the GNU General Public License as published by
8 // the Free Software Foundation, either version 3 of the License, or
9 // (at your option) any later version.
10 //
11 // This program is distributed in the hope that it will be useful,
12 // but WITHOUT ANY WARRANTY; without even the implied warranty of
13 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 // GNU General Public License for more details.
15 //
16 // You should have received a copy of the GNU General Public License
17 // along with this program. If not, see <https://www.gnu.org/licenses/>.
18 //
19 // Author: Ricard Campos (ricardcd@gmail.com)
20 
21 #ifndef EMODNET_QMGC_GZIP_FILE_READER_H
22 #define EMODNET_QMGC_GZIP_FILE_READER_H
23 
24 #include <zlib.h>
25 #include <string>
26 #include <cstring>
27 
33 
34 public:
35  // Typedefs
36  typedef unsigned char Byte;
37 
39  GZipFileReader( const std::string &filePath );
40 
42  bool isFileOpen() { return m_file != NULL; }
43 
45  Byte readByte();
46 
48  void skipBytes(int numBytes);
49 
51  double readDouble();
52 
54  float readFloat();
55 
57  int readInt();
58 
60  unsigned int readUInt();
61 
63  short readShort();
64 
66  unsigned short readUShort();
67 
69  char readChar();
70 
72  unsigned char readUChar();
73 
75  template <typename T>
76  T read() {
77  // Size of the type used
78  int sz = sizeof(T);
79 
80  // Read from the GZip file
81  Byte buffer[sz];
82  int bytesRead = gzread(m_file, buffer, sz);
83  T ret;
84  memcpy(&ret, &buffer[0], sz);
85 
86  // Update the position of the pointer in the file
87  m_pos += sz;
88 
89  return ret;
90  }
91 
93  int getPos() { return m_pos; }
94 
96  bool close() { return gzclose(m_file); }
97 
99  bool eof() { return gzeof(m_file); }
100 
101 private:
102  gzFile m_file;
103  int m_pos;
104 };
105 
106 
107 #endif //EMODNET_QMGC_GZIP_FILE_READER_H
Helper class to read a GZip file.
Definition: gzip_file_reader.h:32
unsigned short readUShort()
Reads an unsigned short.
Definition: gzip_file_reader.cpp:81
short readShort()
Reads a short.
Definition: gzip_file_reader.cpp:74
GZipFileReader(const std::string &filePath)
Constructor.
Definition: gzip_file_reader.cpp:23
void skipBytes(int numBytes)
Skips a given number of bytes.
Definition: gzip_file_reader.cpp:38
bool eof()
Indicates the end of file.
Definition: gzip_file_reader.h:99
bool isFileOpen()
Check if the file is open.
Definition: gzip_file_reader.h:42
int readInt()
Reads a int.
Definition: gzip_file_reader.cpp:60
unsigned int readUInt()
Reads an unsigned int.
Definition: gzip_file_reader.cpp:67
T read()
Generic templated read function.
Definition: gzip_file_reader.h:76
unsigned char readUChar()
Reads an unsigned char.
Definition: gzip_file_reader.cpp:95
bool close()
Closes the file.
Definition: gzip_file_reader.h:96
Byte readByte()
Reads a byte.
Definition: gzip_file_reader.cpp:31
int getPos()
Returns the position on the file (i.e., read byte counter)
Definition: gzip_file_reader.h:93
double readDouble()
Reads a double.
Definition: gzip_file_reader.cpp:46
char readChar()
Reads a char.
Definition: gzip_file_reader.cpp:88
float readFloat()
Reads a float.
Definition: gzip_file_reader.cpp:53